From 7f2ace0da913567513f9c4d6a2da15639054be86 Mon Sep 17 00:00:00 2001 From: Grzegorz Piwowarek Date: Mon, 27 Nov 2017 21:02:42 +0100 Subject: [PATCH] FilesTest fix (#3146) * FilesTest fix * StopThreadTest fix --- .../concurrent/stopping/StopThreadTest.java | 14 ++-- .../baeldung/file/FileOutputStreamTest.java | 36 ---------- .../java/com/baeldung/file/FileUtilsTest.java | 37 ---------- .../com/baeldung/file/FileWriterTest.java | 40 ----------- .../java/com/baeldung/file/FilesTest.java | 68 +++++++++++++++++-- .../java/com/baeldung/file/GuavaTest.java | 48 ------------- 6 files changed, 72 insertions(+), 171 deletions(-) delete mode 100644 core-java/src/test/java/com/baeldung/file/FileOutputStreamTest.java delete mode 100644 core-java/src/test/java/com/baeldung/file/FileUtilsTest.java delete mode 100644 core-java/src/test/java/com/baeldung/file/FileWriterTest.java delete mode 100644 core-java/src/test/java/com/baeldung/file/GuavaTest.java diff --git a/core-java-concurrency/src/test/java/com/baeldung/concurrent/stopping/StopThreadTest.java b/core-java-concurrency/src/test/java/com/baeldung/concurrent/stopping/StopThreadTest.java index 8c1bdbf787..70854f013f 100644 --- a/core-java-concurrency/src/test/java/com/baeldung/concurrent/stopping/StopThreadTest.java +++ b/core-java-concurrency/src/test/java/com/baeldung/concurrent/stopping/StopThreadTest.java @@ -1,7 +1,11 @@ package com.baeldung.concurrent.stopping; +import com.jayway.awaitility.Awaitility; import org.junit.Test; +import java.util.concurrent.TimeUnit; + +import static com.jayway.awaitility.Awaitility.await; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; @@ -22,11 +26,10 @@ public class StopThreadTest { // Stop it and make sure the flags have been reversed controlSubThread.stop(); - Thread.sleep(interval); - assertTrue(controlSubThread.isStopped()); + await() + .until(() -> assertTrue(controlSubThread.isStopped())); } - @Test public void whenInterruptedThreadIsStopped() throws InterruptedException { @@ -44,7 +47,8 @@ public class StopThreadTest { controlSubThread.interrupt(); // Wait less than the time we would normally sleep, and make sure we exited. - Thread.sleep(interval/10); - assertTrue(controlSubThread.isStopped()); + Awaitility.await() + .atMost(interval/ 10, TimeUnit.MILLISECONDS) + .until(controlSubThread::isStopped); } } diff --git a/core-java/src/test/java/com/baeldung/file/FileOutputStreamTest.java b/core-java/src/test/java/com/baeldung/file/FileOutputStreamTest.java deleted file mode 100644 index 451c1b4c4d..0000000000 --- a/core-java/src/test/java/com/baeldung/file/FileOutputStreamTest.java +++ /dev/null @@ -1,36 +0,0 @@ -package com.baeldung.file; - -import static org.assertj.core.api.Assertions.assertThat; - -import java.io.FileInputStream; -import java.io.FileOutputStream; -import java.io.IOException; -import java.io.PrintWriter; - -import org.junit.After; -import org.junit.Test; - -import com.baeldung.util.StreamUtils; - -public class FileOutputStreamTest { - - public static final String fileName = "src/main/resources/countries.properties"; - - @Test - public void whenAppendToFileUsingFileOutputStream_thenCorrect() throws Exception { - FileOutputStream fos = new FileOutputStream(fileName, true); - fos.write("Spain\r\n".getBytes()); - fos.close(); - - assertThat(StreamUtils.getStringFromInputStream( - new FileInputStream(fileName))) - .isEqualTo("UK\r\n" + "US\r\n" + "Germany\r\n" + "Spain\r\n"); - } - - @After - public void revertFile() throws IOException { - PrintWriter writer = new PrintWriter(fileName); - writer.print("UK\r\n" + "US\r\n" + "Germany\r\n"); - writer.close(); - } -} diff --git a/core-java/src/test/java/com/baeldung/file/FileUtilsTest.java b/core-java/src/test/java/com/baeldung/file/FileUtilsTest.java deleted file mode 100644 index 9ee8726575..0000000000 --- a/core-java/src/test/java/com/baeldung/file/FileUtilsTest.java +++ /dev/null @@ -1,37 +0,0 @@ -package com.baeldung.file; - -import static org.assertj.core.api.Assertions.assertThat; - -import java.io.File; -import java.io.FileInputStream; -import java.io.IOException; -import java.io.PrintWriter; -import java.nio.charset.StandardCharsets; - -import org.apache.commons.io.FileUtils; -import org.junit.After; -import org.junit.Test; - -import com.baeldung.util.StreamUtils; - -public class FileUtilsTest { - - public static final String fileName = "src/main/resources/countries.properties"; - - @Test - public void whenAppendToFileUsingFiles_thenCorrect() throws IOException { - File file = new File(fileName); - FileUtils.writeStringToFile(file, "Spain\r\n", StandardCharsets.UTF_8, true); - - assertThat(StreamUtils.getStringFromInputStream( - new FileInputStream(fileName))) - .isEqualTo("UK\r\n" + "US\r\n" + "Germany\r\n" + "Spain\r\n"); - } - - @After - public void revertFile() throws IOException { - PrintWriter writer = new PrintWriter(fileName); - writer.print("UK\r\n" + "US\r\n" + "Germany\r\n"); - writer.close(); - } -} diff --git a/core-java/src/test/java/com/baeldung/file/FileWriterTest.java b/core-java/src/test/java/com/baeldung/file/FileWriterTest.java deleted file mode 100644 index 8d2ce4310e..0000000000 --- a/core-java/src/test/java/com/baeldung/file/FileWriterTest.java +++ /dev/null @@ -1,40 +0,0 @@ -package com.baeldung.file; - -import static org.assertj.core.api.Assertions.assertThat; - -import java.io.BufferedWriter; -import java.io.FileInputStream; -import java.io.FileWriter; -import java.io.IOException; -import java.io.PrintWriter; - -import org.junit.After; -import org.junit.Test; - -import com.baeldung.util.StreamUtils; - -public class FileWriterTest { - - public static final String fileName = "src/main/resources/countries.properties"; - - @Test - public void whenAppendToFileUsingFileWriter_thenCorrect() throws IOException { - FileWriter fw = new FileWriter(fileName, true); - BufferedWriter bw = new BufferedWriter(fw); - bw.write("Spain"); - bw.newLine(); - bw.close(); - - assertThat( - StreamUtils.getStringFromInputStream( - new FileInputStream(fileName))) - .isEqualTo("UK\r\n" + "US\r\n" + "Germany\r\n" + "Spain\n"); - } - - @After - public void revertFile() throws IOException { - PrintWriter writer = new PrintWriter(fileName); - writer.print("UK\r\n" + "US\r\n" + "Germany\r\n"); - writer.close(); - } -} diff --git a/core-java/src/test/java/com/baeldung/file/FilesTest.java b/core-java/src/test/java/com/baeldung/file/FilesTest.java index bd39d004d3..e17e8580aa 100644 --- a/core-java/src/test/java/com/baeldung/file/FilesTest.java +++ b/core-java/src/test/java/com/baeldung/file/FilesTest.java @@ -2,14 +2,24 @@ package com.baeldung.file; import static org.assertj.core.api.Assertions.assertThat; +import java.io.BufferedWriter; +import java.io.File; import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.FileWriter; import java.io.IOException; import java.io.PrintWriter; +import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.Paths; import java.nio.file.StandardOpenOption; +import com.google.common.base.Charsets; +import com.google.common.io.CharSink; +import com.google.common.io.FileWriteMode; +import org.apache.commons.io.FileUtils; import org.junit.After; +import org.junit.Before; import org.junit.Test; import com.baeldung.util.StreamUtils; @@ -18,6 +28,26 @@ public class FilesTest { public static final String fileName = "src/main/resources/countries.properties"; + @Before + @After + public void setup() throws Exception { + PrintWriter writer = new PrintWriter(fileName); + writer.print("UK\r\n" + "US\r\n" + "Germany\r\n"); + writer.close(); + } + + @Test + public void whenAppendToFileUsingGuava_thenCorrect() throws IOException { + File file = new File(fileName); + CharSink chs = com.google.common.io.Files.asCharSink(file, Charsets.UTF_8, FileWriteMode.APPEND); + chs.write("Spain\r\n"); + + assertThat(StreamUtils.getStringFromInputStream( + new FileInputStream(fileName))) + .isEqualTo("UK\r\n" + "US\r\n" + "Germany\r\n" + "Spain\r\n"); + } + + @Test public void whenAppendToFileUsingFiles_thenCorrect() throws IOException { Files.write(Paths.get(fileName), "Spain\r\n".getBytes(), StandardOpenOption.APPEND); @@ -27,10 +57,38 @@ public class FilesTest { .isEqualTo("UK\r\n" + "US\r\n" + "Germany\r\n" + "Spain\r\n"); } - @After - public void revertFile() throws IOException { - PrintWriter writer = new PrintWriter(fileName); - writer.print("UK\r\n" + "US\r\n" + "Germany\r\n"); - writer.close(); + @Test + public void whenAppendToFileUsingFileUtils_thenCorrect() throws IOException { + File file = new File(fileName); + FileUtils.writeStringToFile(file, "Spain\r\n", StandardCharsets.UTF_8, true); + + assertThat(StreamUtils.getStringFromInputStream( + new FileInputStream(fileName))) + .isEqualTo("UK\r\n" + "US\r\n" + "Germany\r\n" + "Spain\r\n"); + } + + @Test + public void whenAppendToFileUsingFileOutputStream_thenCorrect() throws Exception { + FileOutputStream fos = new FileOutputStream(fileName, true); + fos.write("Spain\r\n".getBytes()); + fos.close(); + + assertThat(StreamUtils.getStringFromInputStream( + new FileInputStream(fileName))) + .isEqualTo("UK\r\n" + "US\r\n" + "Germany\r\n" + "Spain\r\n"); + } + + @Test + public void whenAppendToFileUsingFileWriter_thenCorrect() throws IOException { + FileWriter fw = new FileWriter(fileName, true); + BufferedWriter bw = new BufferedWriter(fw); + bw.write("Spain"); + bw.newLine(); + bw.close(); + + assertThat( + StreamUtils.getStringFromInputStream( + new FileInputStream(fileName))) + .isEqualTo("UK\r\n" + "US\r\n" + "Germany\r\n" + "Spain\n"); } } diff --git a/core-java/src/test/java/com/baeldung/file/GuavaTest.java b/core-java/src/test/java/com/baeldung/file/GuavaTest.java deleted file mode 100644 index 5a7ec6c4a8..0000000000 --- a/core-java/src/test/java/com/baeldung/file/GuavaTest.java +++ /dev/null @@ -1,48 +0,0 @@ -package com.baeldung.file; - -import static org.assertj.core.api.Assertions.assertThat; - -import java.io.File; -import java.io.FileInputStream; -import java.io.IOException; -import java.io.PrintWriter; - -import org.junit.After; -import org.junit.Before; -import org.junit.Test; - -import com.baeldung.util.StreamUtils; -import com.google.common.base.Charsets; -import com.google.common.io.CharSink; -import com.google.common.io.FileWriteMode; -import com.google.common.io.Files; - -public class GuavaTest { - - public static final String fileName = "src/main/resources/countries.properties"; - - @Before - public void setup() throws Exception { - PrintWriter writer = new PrintWriter(fileName); - writer.print("UK\r\n" + "US\r\n" + "Germany\r\n"); - writer.close(); - } - - @Test - public void whenAppendToFileUsingGuava_thenCorrect() throws IOException { - File file = new File(fileName); - CharSink chs = Files.asCharSink(file, Charsets.UTF_8, FileWriteMode.APPEND); - chs.write("Spain\r\n"); - - assertThat(StreamUtils.getStringFromInputStream( - new FileInputStream(fileName))) - .isEqualTo("UK\r\n" + "US\r\n" + "Germany\r\n" + "Spain\r\n"); - } - - @After - public void revertFile() throws IOException { - PrintWriter writer = new PrintWriter(fileName); - writer.print("UK\r\n" + "US\r\n" + "Germany\r\n"); - writer.close(); - } -}