Skip to content

Commit 7f2ace0

Browse files
authored
FilesTest fix (eugenp#3146)
* FilesTest fix * StopThreadTest fix
1 parent bac574a commit 7f2ace0

6 files changed

Lines changed: 72 additions & 171 deletions

File tree

‎core-java-concurrency/src/test/java/com/baeldung/concurrent/stopping/StopThreadTest.java‎

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
package com.baeldung.concurrent.stopping;
22

3+
import com.jayway.awaitility.Awaitility;
34
import org.junit.Test;
45

6+
import java.util.concurrent.TimeUnit;
7+
8+
import static com.jayway.awaitility.Awaitility.await;
59
import static org.junit.Assert.assertFalse;
610
import static org.junit.Assert.assertTrue;
711

@@ -22,11 +26,10 @@ public void whenStoppedThreadIsStopped() throws InterruptedException {
2226

2327
// Stop it and make sure the flags have been reversed
2428
controlSubThread.stop();
25-
Thread.sleep(interval);
26-
assertTrue(controlSubThread.isStopped());
29+
await()
30+
.until(() -> assertTrue(controlSubThread.isStopped()));
2731
}
2832

29-
3033
@Test
3134
public void whenInterruptedThreadIsStopped() throws InterruptedException {
3235

@@ -44,7 +47,8 @@ public void whenInterruptedThreadIsStopped() throws InterruptedException {
4447
controlSubThread.interrupt();
4548

4649
// Wait less than the time we would normally sleep, and make sure we exited.
47-
Thread.sleep(interval/10);
48-
assertTrue(controlSubThread.isStopped());
50+
Awaitility.await()
51+
.atMost(interval/ 10, TimeUnit.MILLISECONDS)
52+
.until(controlSubThread::isStopped);
4953
}
5054
}

‎core-java/src/test/java/com/baeldung/file/FileOutputStreamTest.java‎

Lines changed: 0 additions & 36 deletions
This file was deleted.

‎core-java/src/test/java/com/baeldung/file/FileUtilsTest.java‎

Lines changed: 0 additions & 37 deletions
This file was deleted.

‎core-java/src/test/java/com/baeldung/file/FileWriterTest.java‎

Lines changed: 0 additions & 40 deletions
This file was deleted.

‎core-java/src/test/java/com/baeldung/file/FilesTest.java‎

Lines changed: 63 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,24 @@
22

33
import static org.assertj.core.api.Assertions.assertThat;
44

5+
import java.io.BufferedWriter;
6+
import java.io.File;
57
import java.io.FileInputStream;
8+
import java.io.FileOutputStream;
9+
import java.io.FileWriter;
610
import java.io.IOException;
711
import java.io.PrintWriter;
12+
import java.nio.charset.StandardCharsets;
813
import java.nio.file.Files;
914
import java.nio.file.Paths;
1015
import java.nio.file.StandardOpenOption;
1116

17+
import com.google.common.base.Charsets;
18+
import com.google.common.io.CharSink;
19+
import com.google.common.io.FileWriteMode;
20+
import org.apache.commons.io.FileUtils;
1221
import org.junit.After;
22+
import org.junit.Before;
1323
import org.junit.Test;
1424

1525
import com.baeldung.util.StreamUtils;
@@ -18,6 +28,26 @@ public class FilesTest {
1828

1929
public static final String fileName = "src/main/resources/countries.properties";
2030

31+
@Before
32+
@After
33+
public void setup() throws Exception {
34+
PrintWriter writer = new PrintWriter(fileName);
35+
writer.print("UK\r\n" + "US\r\n" + "Germany\r\n");
36+
writer.close();
37+
}
38+
39+
@Test
40+
public void whenAppendToFileUsingGuava_thenCorrect() throws IOException {
41+
File file = new File(fileName);
42+
CharSink chs = com.google.common.io.Files.asCharSink(file, Charsets.UTF_8, FileWriteMode.APPEND);
43+
chs.write("Spain\r\n");
44+
45+
assertThat(StreamUtils.getStringFromInputStream(
46+
new FileInputStream(fileName)))
47+
.isEqualTo("UK\r\n" + "US\r\n" + "Germany\r\n" + "Spain\r\n");
48+
}
49+
50+
2151
@Test
2252
public void whenAppendToFileUsingFiles_thenCorrect() throws IOException {
2353
Files.write(Paths.get(fileName), "Spain\r\n".getBytes(), StandardOpenOption.APPEND);
@@ -27,10 +57,38 @@ public void whenAppendToFileUsingFiles_thenCorrect() throws IOException {
2757
.isEqualTo("UK\r\n" + "US\r\n" + "Germany\r\n" + "Spain\r\n");
2858
}
2959

30-
@After
31-
public void revertFile() throws IOException {
32-
PrintWriter writer = new PrintWriter(fileName);
33-
writer.print("UK\r\n" + "US\r\n" + "Germany\r\n");
34-
writer.close();
60+
@Test
61+
public void whenAppendToFileUsingFileUtils_thenCorrect() throws IOException {
62+
File file = new File(fileName);
63+
FileUtils.writeStringToFile(file, "Spain\r\n", StandardCharsets.UTF_8, true);
64+
65+
assertThat(StreamUtils.getStringFromInputStream(
66+
new FileInputStream(fileName)))
67+
.isEqualTo("UK\r\n" + "US\r\n" + "Germany\r\n" + "Spain\r\n");
68+
}
69+
70+
@Test
71+
public void whenAppendToFileUsingFileOutputStream_thenCorrect() throws Exception {
72+
FileOutputStream fos = new FileOutputStream(fileName, true);
73+
fos.write("Spain\r\n".getBytes());
74+
fos.close();
75+
76+
assertThat(StreamUtils.getStringFromInputStream(
77+
new FileInputStream(fileName)))
78+
.isEqualTo("UK\r\n" + "US\r\n" + "Germany\r\n" + "Spain\r\n");
79+
}
80+
81+
@Test
82+
public void whenAppendToFileUsingFileWriter_thenCorrect() throws IOException {
83+
FileWriter fw = new FileWriter(fileName, true);
84+
BufferedWriter bw = new BufferedWriter(fw);
85+
bw.write("Spain");
86+
bw.newLine();
87+
bw.close();
88+
89+
assertThat(
90+
StreamUtils.getStringFromInputStream(
91+
new FileInputStream(fileName)))
92+
.isEqualTo("UK\r\n" + "US\r\n" + "Germany\r\n" + "Spain\n");
3593
}
3694
}

‎core-java/src/test/java/com/baeldung/file/GuavaTest.java‎

Lines changed: 0 additions & 48 deletions
This file was deleted.

0 commit comments

Comments
 (0)