From 17ad9e1886253b802e2d007bb9787fb731e75060 Mon Sep 17 00:00:00 2001 From: Krzysztof Krason Date: Fri, 20 Apr 2018 23:02:20 +0200 Subject: [PATCH 1/3] #702 removing temporary files in tests --- src/test/java/org/cactoos/io/InputOfTest.java | 10 +++++++++- src/test/java/org/cactoos/io/InputStreamOfTest.java | 10 +++++++++- src/test/java/org/cactoos/io/LoggingOutputTest.java | 12 +++++++++--- src/test/java/org/cactoos/io/OutputStreamToTest.java | 11 +++++++++-- .../org/cactoos/io/WriterAsOutputStreamTest.java | 10 +++++++++- src/test/java/org/cactoos/io/WriterAsOutputTest.java | 11 +++++++++-- src/test/java/org/cactoos/io/WriterToTest.java | 11 +++++++++-- 7 files changed, 63 insertions(+), 12 deletions(-) diff --git a/src/test/java/org/cactoos/io/InputOfTest.java b/src/test/java/org/cactoos/io/InputOfTest.java index ae68a5790b..e983f1c1cb 100644 --- a/src/test/java/org/cactoos/io/InputOfTest.java +++ b/src/test/java/org/cactoos/io/InputOfTest.java @@ -39,7 +39,9 @@ import org.cactoos.text.TextOf; import org.hamcrest.MatcherAssert; import org.hamcrest.Matchers; +import org.junit.Rule; import org.junit.Test; +import org.junit.rules.TemporaryFolder; import org.takes.http.FtRemote; import org.takes.tk.TkHtml; @@ -56,6 +58,11 @@ */ @SuppressWarnings("PMD.TooManyMethods") public final class InputOfTest { + /** + * Temporary files and folders generator. + */ + @Rule + public final TemporaryFolder folder = new TemporaryFolder(); @Test public void readsAlternativeInputForFileCase() throws IOException { @@ -75,7 +82,8 @@ public void readsAlternativeInputForFileCase() throws IOException { @Test public void readsSimpleFileContent() throws IOException { - final Path temp = Files.createTempFile("cactoos-1", "txt-1"); + final Path temp = this.folder.newFile("cactoos-1.txt-1") + .toPath(); final String content = "Hello, товарищ!"; Files.write(temp, content.getBytes(StandardCharsets.UTF_8)); MatcherAssert.assertThat( diff --git a/src/test/java/org/cactoos/io/InputStreamOfTest.java b/src/test/java/org/cactoos/io/InputStreamOfTest.java index 49a73f5bb8..b45ecd4fb2 100644 --- a/src/test/java/org/cactoos/io/InputStreamOfTest.java +++ b/src/test/java/org/cactoos/io/InputStreamOfTest.java @@ -33,7 +33,9 @@ import org.cactoos.text.TextOf; import org.hamcrest.MatcherAssert; import org.hamcrest.Matchers; +import org.junit.Rule; import org.junit.Test; +import org.junit.rules.TemporaryFolder; /** * Test case for {@link InputStreamOf}. @@ -46,10 +48,16 @@ */ @SuppressWarnings("PMD.TooManyMethods") public final class InputStreamOfTest { + /** + * Temporary files and folders generator. + */ + @Rule + public final TemporaryFolder folder = new TemporaryFolder(); @Test public void readsSimpleFileContent() throws IOException { - final Path temp = Files.createTempFile("cactoos-1", "txt-1"); + final Path temp = this.folder.newFile("cactoos-1.txt-1") + .toPath(); final String content = "Hello, товарищ!"; Files.write(temp, content.getBytes(StandardCharsets.UTF_8)); MatcherAssert.assertThat( diff --git a/src/test/java/org/cactoos/io/LoggingOutputTest.java b/src/test/java/org/cactoos/io/LoggingOutputTest.java index 968638b4a4..b293c1d01c 100644 --- a/src/test/java/org/cactoos/io/LoggingOutputTest.java +++ b/src/test/java/org/cactoos/io/LoggingOutputTest.java @@ -26,13 +26,14 @@ import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.OutputStream; -import java.nio.file.Files; import java.nio.file.Path; import java.util.logging.Level; import java.util.logging.Logger; import org.hamcrest.MatcherAssert; import org.hamcrest.Matchers; +import org.junit.Rule; import org.junit.Test; +import org.junit.rules.TemporaryFolder; /** * Test case for {@link LoggingOutput}. @@ -50,6 +51,11 @@ } ) public final class LoggingOutputTest { + /** + * Temporary files and folders generator. + */ + @Rule + public final TemporaryFolder folder = new TemporaryFolder(); @Test public void logWriteZero() throws IOException { @@ -112,7 +118,7 @@ public void logWriteText() throws IOException { @Test public void logWriteToLargeTextFile() throws Exception { final Logger logger = new FakeLogger(); - final Path temp = Files.createTempDirectory("ccts-1"); + final Path temp = this.folder.newFolder("ccts-1").toPath(); final Path path = temp.resolve("x/y/z/file.txt"); new LengthOf( new TeeInput( @@ -142,7 +148,7 @@ public void logWriteToLargeTextFile() throws Exception { @Test public void logAllWriteToLargeTextFile() throws Exception { final Logger logger = new FakeLogger(Level.WARNING); - final Path temp = Files.createTempDirectory("ccts-2"); + final Path temp = this.folder.newFolder("ccts-2").toPath(); final Path path = temp.resolve("a/b/c/file.txt"); new LengthOf( new TeeInput( diff --git a/src/test/java/org/cactoos/io/OutputStreamToTest.java b/src/test/java/org/cactoos/io/OutputStreamToTest.java index 8b382ae292..b022745a50 100644 --- a/src/test/java/org/cactoos/io/OutputStreamToTest.java +++ b/src/test/java/org/cactoos/io/OutputStreamToTest.java @@ -24,13 +24,14 @@ package org.cactoos.io; import java.io.IOException; -import java.nio.file.Files; import java.nio.file.Path; import org.cactoos.matchers.MatcherOf; import org.cactoos.matchers.TextHasString; import org.cactoos.text.TextOf; import org.hamcrest.MatcherAssert; +import org.junit.Rule; import org.junit.Test; +import org.junit.rules.TemporaryFolder; /** * Test case for {@link OutputStreamTo}. @@ -42,10 +43,16 @@ * @checkstyle ClassDataAbstractionCouplingCheck (500 lines) */ public final class OutputStreamToTest { + /** + * Temporary files and folders generator. + */ + @Rule + public final TemporaryFolder folder = new TemporaryFolder(); @Test public void writesLargeContentToFile() throws IOException { - final Path temp = Files.createTempFile("cactoos-1", "txt-1"); + final Path temp = this.folder.newFile("cactoos-1.txt-1") + .toPath(); MatcherAssert.assertThat( "Can't copy Input to Output and return Input", new TextOf( diff --git a/src/test/java/org/cactoos/io/WriterAsOutputStreamTest.java b/src/test/java/org/cactoos/io/WriterAsOutputStreamTest.java index b7415e1d84..3f8e1f8cc5 100644 --- a/src/test/java/org/cactoos/io/WriterAsOutputStreamTest.java +++ b/src/test/java/org/cactoos/io/WriterAsOutputStreamTest.java @@ -35,7 +35,9 @@ import org.cactoos.matchers.TextHasString; import org.cactoos.text.TextOf; import org.hamcrest.MatcherAssert; +import org.junit.Rule; import org.junit.Test; +import org.junit.rules.TemporaryFolder; /** * Test case for {@link WriterAsOutputStream}. @@ -48,6 +50,11 @@ */ @SuppressWarnings("PMD.AvoidDuplicateLiterals") public final class WriterAsOutputStreamTest { + /** + * Temporary files and folders generator. + */ + @Rule + public final TemporaryFolder folder = new TemporaryFolder(); @Test public void writesToByteArray() { @@ -84,7 +91,8 @@ public void writesToByteArray() { @Test public void writesLargeContentToFile() throws IOException { - final Path temp = Files.createTempFile("cactoos-1", "txt-1"); + final Path temp = this.folder.newFile("cactoos-1.txt-1") + .toPath(); MatcherAssert.assertThat( "Can't copy Input to Output and return Input", new TextOf( diff --git a/src/test/java/org/cactoos/io/WriterAsOutputTest.java b/src/test/java/org/cactoos/io/WriterAsOutputTest.java index 79be7495f2..8470b02b53 100644 --- a/src/test/java/org/cactoos/io/WriterAsOutputTest.java +++ b/src/test/java/org/cactoos/io/WriterAsOutputTest.java @@ -27,13 +27,14 @@ import java.io.IOException; import java.io.OutputStreamWriter; import java.nio.charset.StandardCharsets; -import java.nio.file.Files; import java.nio.file.Path; import org.cactoos.matchers.MatcherOf; import org.cactoos.matchers.TextHasString; import org.cactoos.text.TextOf; import org.hamcrest.MatcherAssert; +import org.junit.Rule; import org.junit.Test; +import org.junit.rules.TemporaryFolder; /** * Test case for {@link WriterAsOutput}. @@ -45,10 +46,16 @@ * @checkstyle ClassDataAbstractionCouplingCheck (500 lines) */ public final class WriterAsOutputTest { + /** + * Temporary files and folders generator. + */ + @Rule + public final TemporaryFolder folder = new TemporaryFolder(); @Test public void writesLargeContentToFile() throws IOException { - final Path temp = Files.createTempFile("cactoos-1", "txt-1"); + final Path temp = this.folder.newFile("cactoos-1.txt-1") + .toPath(); MatcherAssert.assertThat( "Can't copy Input to Output and return Input", new TextOf( diff --git a/src/test/java/org/cactoos/io/WriterToTest.java b/src/test/java/org/cactoos/io/WriterToTest.java index 836bd5a08e..3441a3f38e 100644 --- a/src/test/java/org/cactoos/io/WriterToTest.java +++ b/src/test/java/org/cactoos/io/WriterToTest.java @@ -24,13 +24,14 @@ package org.cactoos.io; import java.io.IOException; -import java.nio.file.Files; import java.nio.file.Path; import org.cactoos.matchers.MatcherOf; import org.cactoos.matchers.TextHasString; import org.cactoos.text.TextOf; import org.hamcrest.MatcherAssert; +import org.junit.Rule; import org.junit.Test; +import org.junit.rules.TemporaryFolder; /** * Test case for {@link WriterTo}. @@ -42,10 +43,16 @@ * @checkstyle ClassDataAbstractionCouplingCheck (500 lines) */ public final class WriterToTest { + /** + * Temporary files and folders generator. + */ + @Rule + public final TemporaryFolder folder = new TemporaryFolder(); @Test public void writesLargeContentToFile() throws IOException { - final Path temp = Files.createTempFile("cactoos-1", "txt-1"); + final Path temp = this.folder.newFile("cactoos-1.txt-1") + .toPath(); MatcherAssert.assertThat( "Can't copy Input to Output and return Input", new TextOf( From 9a885b3e1315113f8972ea8fc3089d4744332a9f Mon Sep 17 00:00:00 2001 From: Krzysztof Krason Date: Thu, 26 Apr 2018 09:05:36 +0200 Subject: [PATCH 2/3] #702 CR --- src/test/java/org/cactoos/io/InputStreamOfTest.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/test/java/org/cactoos/io/InputStreamOfTest.java b/src/test/java/org/cactoos/io/InputStreamOfTest.java index b45ecd4fb2..955cf8775b 100644 --- a/src/test/java/org/cactoos/io/InputStreamOfTest.java +++ b/src/test/java/org/cactoos/io/InputStreamOfTest.java @@ -99,7 +99,7 @@ public void makesDataAvailable() throws IOException { @Test public void readsFileContent() throws IOException { - final File file = File.createTempFile("readFileContent", "txt-2"); + final File file = this.folder.newFile("readFileContent.txt-2"); final String content = "Content in a file"; new LengthOf( new TeeInput(content, file) @@ -145,7 +145,7 @@ public void readsText() throws IOException { @Test public void readsFromUri() throws IOException { final String content = "Content for reading through URI"; - final File file = File.createTempFile("readFromUri", "txt-3"); + final File file = this.folder.newFile("readFromUri.txt-3"); new LengthOf( new TeeInput(content, file) ).intValue(); @@ -159,7 +159,7 @@ public void readsFromUri() throws IOException { @Test public void readsFromUrl() throws IOException { final String content = "Content for reading through URL"; - final File file = File.createTempFile("readFromUrl", "txt-4"); + final File file = this.folder.newFile("readFromUrl.txt-4"); new LengthOf( new TeeInput(content, file) ).intValue(); @@ -220,7 +220,7 @@ public void readsFromReaderWithCharset() throws IOException { @Test public void readsFromTextWithCharset() throws IOException { - final File file = File.createTempFile("readTextWithCharset", "txt-5"); + final File file = this.folder.newFile("readTextWithCharset.txt-5"); final String content = "Content for reading text with charset"; new LengthOf( new TeeInput(content, file) From 9cf876da8ac8bdeed5aeeb27058eea1f4f2ced92 Mon Sep 17 00:00:00 2001 From: Krzysztof Krason Date: Thu, 26 Apr 2018 09:29:38 +0200 Subject: [PATCH 3/3] #702 CR --- pom.xml | 5 +++++ .../java/org/cactoos/io/GzipOutputTest.java | 9 ++++++++- .../org/cactoos/io/ReaderAsBytesTest.java | 9 ++++++++- .../java/org/cactoos/io/TeeOutputTest.java | 20 +++++++++++++------ .../cactoos/io/WriterAsOutputStreamTest.java | 2 +- 5 files changed, 36 insertions(+), 9 deletions(-) diff --git a/pom.xml b/pom.xml index 212a39bdb2..5f80fed4f2 100644 --- a/pom.xml +++ b/pom.xml @@ -82,6 +82,11 @@ The MIT License (MIT) + org.hamcrest hamcrest-core diff --git a/src/test/java/org/cactoos/io/GzipOutputTest.java b/src/test/java/org/cactoos/io/GzipOutputTest.java index 1ee0c07710..5c8a5d74e1 100644 --- a/src/test/java/org/cactoos/io/GzipOutputTest.java +++ b/src/test/java/org/cactoos/io/GzipOutputTest.java @@ -31,7 +31,9 @@ import java.util.zip.GZIPInputStream; import org.hamcrest.MatcherAssert; import org.hamcrest.Matchers; +import org.junit.Rule; import org.junit.Test; +import org.junit.rules.TemporaryFolder; /** * Test case for {@link org.cactoos.io.GzipOutput}. @@ -42,6 +44,11 @@ */ @SuppressWarnings("PMD.AvoidDuplicateLiterals") public final class GzipOutputTest { + /** + * Temporary files and folders generator. + */ + @Rule + public final TemporaryFolder folder = new TemporaryFolder(); @Test public void writeToGzipOutput() throws Exception { @@ -72,7 +79,7 @@ public void writeToGzipOutput() throws Exception { @Test(expected = IOException.class) public void writeToClosedGzipOutput() throws Exception { final OutputStream stream = new FileOutputStream( - new TempFile("cactoos", "txt").value().toFile() + this.folder.newFile("cactoos.txt") ); stream.close(); new LengthOf( diff --git a/src/test/java/org/cactoos/io/ReaderAsBytesTest.java b/src/test/java/org/cactoos/io/ReaderAsBytesTest.java index 6bc96d8183..011be15747 100644 --- a/src/test/java/org/cactoos/io/ReaderAsBytesTest.java +++ b/src/test/java/org/cactoos/io/ReaderAsBytesTest.java @@ -30,7 +30,9 @@ import org.cactoos.text.TextOf; import org.hamcrest.MatcherAssert; import org.hamcrest.Matchers; +import org.junit.Rule; import org.junit.Test; +import org.junit.rules.TemporaryFolder; /** * Test case for {@link ReaderAsBytes}. @@ -41,6 +43,11 @@ * @checkstyle JavadocMethodCheck (500 lines) */ public final class ReaderAsBytesTest { + /** + * Temporary files and folders generator. + */ + @Rule + public final TemporaryFolder folder = new TemporaryFolder(); @Test public void readsString() throws IOException { @@ -58,7 +65,7 @@ public void readsString() throws IOException { @Test public void readsAsBytesAndDeletesTempFile() throws Exception { - final Path file = new TempFile().value(); + final Path file = this.folder.newFile().toPath(); new ReaderAsBytes( new ReaderOf(file) ).asBytes(); diff --git a/src/test/java/org/cactoos/io/TeeOutputTest.java b/src/test/java/org/cactoos/io/TeeOutputTest.java index bbd8cef91a..ce807c4daa 100644 --- a/src/test/java/org/cactoos/io/TeeOutputTest.java +++ b/src/test/java/org/cactoos/io/TeeOutputTest.java @@ -24,12 +24,15 @@ package org.cactoos.io; import java.io.ByteArrayOutputStream; +import java.io.File; import java.nio.charset.StandardCharsets; import org.cactoos.matchers.MatcherOf; import org.cactoos.matchers.TextHasString; import org.cactoos.text.TextOf; import org.hamcrest.MatcherAssert; +import org.junit.Rule; import org.junit.Test; +import org.junit.rules.TemporaryFolder; /** * Test case for {@link TeeOutput}. @@ -40,6 +43,11 @@ * @checkstyle ClassDataAbstractionCouplingCheck (500 lines) */ public final class TeeOutputTest { + /** + * Temporary files generator. + */ + @Rule + public TemporaryFolder folder = new TemporaryFolder(); @Test public void copiesContent() { @@ -126,7 +134,7 @@ public void copiesWithWriterAndCharset() { @Test public void copiesWithPath() throws Exception { final ByteArrayOutputStream baos = new ByteArrayOutputStream(); - final TempFile file = new TempFile(); + final File file = this.folder.newFile(); MatcherAssert.assertThat( "Can't copy Output with path", new TextOf( @@ -134,7 +142,7 @@ public void copiesWithPath() throws Exception { new InputOf("Hello, товарищ! with path"), new TeeOutput( new OutputTo(baos), - file.value() + file.toPath() ) ) ), @@ -142,7 +150,7 @@ public void copiesWithPath() throws Exception { new MatcherOf<>( str -> { return new String( - new TextOf(file.value()).asString() + new TextOf(file.toPath()).asString() ).equals(str); } ) @@ -153,7 +161,7 @@ public void copiesWithPath() throws Exception { @Test public void copiesWithFile() throws Exception { final ByteArrayOutputStream baos = new ByteArrayOutputStream(); - final TempFile file = new TempFile(); + final File file = this.folder.newFile(); MatcherAssert.assertThat( "Can't copy Output with file", new TextOf( @@ -161,7 +169,7 @@ public void copiesWithFile() throws Exception { new InputOf("Hello, товарищ! with file"), new TeeOutput( new OutputTo(baos), - file.value().toFile() + file ) ) ), @@ -169,7 +177,7 @@ public void copiesWithFile() throws Exception { new MatcherOf<>( str -> { return new String( - new TextOf(file.value()).asString() + new TextOf(file.toPath()).asString() ).equals(str); } ) diff --git a/src/test/java/org/cactoos/io/WriterAsOutputStreamTest.java b/src/test/java/org/cactoos/io/WriterAsOutputStreamTest.java index 3f8e1f8cc5..376868e647 100644 --- a/src/test/java/org/cactoos/io/WriterAsOutputStreamTest.java +++ b/src/test/java/org/cactoos/io/WriterAsOutputStreamTest.java @@ -123,7 +123,7 @@ public void writesLargeContentToFile() throws IOException { @Test public void writesToFileAndRemovesIt() throws Exception { - final Path temp = new TempFile().value(); + final Path temp = this.folder.newFile().toPath(); final String content = "Hello, товарищ! How are you?"; new LengthOf( new TeeInput(