FilesTest fix (#3146)

* FilesTest fix

* StopThreadTest fix
This commit is contained in:
Grzegorz Piwowarek 2017-11-27 21:02:42 +01:00 committed by GitHub
parent bac574a7c2
commit 7f2ace0da9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 72 additions and 171 deletions

View File

@ -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);
}
}

View File

@ -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();
}
}

View File

@ -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();
}
}

View File

@ -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();
}
}

View File

@ -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");
}
}

View File

@ -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();
}
}