From 17ad9e1886253b802e2d007bb9787fb731e75060 Mon Sep 17 00:00:00 2001 From: Krzysztof Krason Date: Fri, 20 Apr 2018 23:02:20 +0200 Subject: [PATCH] #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(