Skip to content

Commit

Permalink
FilesTest fix (eugenp#3146)
Browse files Browse the repository at this point in the history
* FilesTest fix

* StopThreadTest fix
  • Loading branch information
pivovarit authored Nov 27, 2017
1 parent bac574a commit 7f2ace0
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 171 deletions.
Original file line number Diff line number Diff line change
@@ -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;

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

// 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 {

Expand All @@ -44,7 +47,8 @@ public void whenInterruptedThreadIsStopped() throws InterruptedException {
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);
}
}

This file was deleted.

37 changes: 0 additions & 37 deletions core-java/src/test/java/com/baeldung/file/FileUtilsTest.java

This file was deleted.

40 changes: 0 additions & 40 deletions core-java/src/test/java/com/baeldung/file/FileWriterTest.java

This file was deleted.

68 changes: 63 additions & 5 deletions core-java/src/test/java/com/baeldung/file/FilesTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,24 @@

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;
Expand All @@ -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);
Expand All @@ -27,10 +57,38 @@ public void whenAppendToFileUsingFiles_thenCorrect() throws IOException {
.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");
}
}
48 changes: 0 additions & 48 deletions core-java/src/test/java/com/baeldung/file/GuavaTest.java

This file was deleted.

0 comments on commit 7f2ace0

Please sign in to comment.