This commit is contained in:
Ahmed Tawila 2017-11-23 23:24:59 +02:00
commit 79a4d133c3
6 changed files with 18 additions and 13 deletions

View File

@ -0,0 +1,3 @@
UK
US
Germany

View File

@ -14,7 +14,7 @@ import com.baeldung.util.StreamUtils;
public class FileOutputStreamTest { public class FileOutputStreamTest {
public static final String fileName = "src/main/resources/countries.txt"; public static final String fileName = "src/main/resources/countries.properties";
@Test @Test
public void whenAppendToFileUsingFileOutputStream_thenCorrect() throws Exception { public void whenAppendToFileUsingFileOutputStream_thenCorrect() throws Exception {
@ -30,7 +30,6 @@ public class FileOutputStreamTest {
@After @After
public void revertFile() throws IOException { public void revertFile() throws IOException {
PrintWriter writer = new PrintWriter(fileName); PrintWriter writer = new PrintWriter(fileName);
writer.print("");
writer.print("UK\r\n" + "US\r\n" + "Germany\r\n"); writer.print("UK\r\n" + "US\r\n" + "Germany\r\n");
writer.close(); writer.close();
} }

View File

@ -16,7 +16,7 @@ import com.baeldung.util.StreamUtils;
public class FileUtilsTest { public class FileUtilsTest {
public static final String fileName = "src/main/resources/countries.txt"; public static final String fileName = "src/main/resources/countries.properties";
@Test @Test
public void whenAppendToFileUsingFiles_thenCorrect() throws IOException { public void whenAppendToFileUsingFiles_thenCorrect() throws IOException {
@ -31,7 +31,6 @@ public class FileUtilsTest {
@After @After
public void revertFile() throws IOException { public void revertFile() throws IOException {
PrintWriter writer = new PrintWriter(fileName); PrintWriter writer = new PrintWriter(fileName);
writer.print("");
writer.print("UK\r\n" + "US\r\n" + "Germany\r\n"); writer.print("UK\r\n" + "US\r\n" + "Germany\r\n");
writer.close(); writer.close();
} }

View File

@ -15,11 +15,10 @@ import com.baeldung.util.StreamUtils;
public class FileWriterTest { public class FileWriterTest {
public static final String fileName = "src/main/resources/countries.txt"; public static final String fileName = "src/main/resources/countries.properties";
@Test @Test
public void whenAppendToFileUsingFileWriter_thenCorrect() throws IOException { public void whenAppendToFileUsingFileWriter_thenCorrect() throws IOException {
String fileName = "src/main/resources/countries.txt";
FileWriter fw = new FileWriter(fileName, true); FileWriter fw = new FileWriter(fileName, true);
BufferedWriter bw = new BufferedWriter(fw); BufferedWriter bw = new BufferedWriter(fw);
bw.write("Spain"); bw.write("Spain");
@ -29,13 +28,12 @@ public class FileWriterTest {
assertThat( assertThat(
StreamUtils.getStringFromInputStream( StreamUtils.getStringFromInputStream(
new FileInputStream(fileName))) new FileInputStream(fileName)))
.isEqualTo("UK\r\n" + "US\r\n" + "Germany\r\n" + "Spain\r\n"); .isEqualTo("UK\r\n" + "US\r\n" + "Germany\r\n" + "Spain\n");
} }
@After @After
public void revertFile() throws IOException { public void revertFile() throws IOException {
PrintWriter writer = new PrintWriter(fileName); PrintWriter writer = new PrintWriter(fileName);
writer.print("");
writer.print("UK\r\n" + "US\r\n" + "Germany\r\n"); writer.print("UK\r\n" + "US\r\n" + "Germany\r\n");
writer.close(); writer.close();
} }

View File

@ -16,7 +16,7 @@ import com.baeldung.util.StreamUtils;
public class FilesTest { public class FilesTest {
public static final String fileName = "src/main/resources/countries.txt"; public static final String fileName = "src/main/resources/countries.properties";
@Test @Test
public void whenAppendToFileUsingFiles_thenCorrect() throws IOException { public void whenAppendToFileUsingFiles_thenCorrect() throws IOException {
@ -30,7 +30,6 @@ public class FilesTest {
@After @After
public void revertFile() throws IOException { public void revertFile() throws IOException {
PrintWriter writer = new PrintWriter(fileName); PrintWriter writer = new PrintWriter(fileName);
writer.print("");
writer.print("UK\r\n" + "US\r\n" + "Germany\r\n"); writer.print("UK\r\n" + "US\r\n" + "Germany\r\n");
writer.close(); writer.close();
} }

View File

@ -8,6 +8,7 @@ import java.io.IOException;
import java.io.PrintWriter; import java.io.PrintWriter;
import org.junit.After; import org.junit.After;
import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import com.baeldung.util.StreamUtils; import com.baeldung.util.StreamUtils;
@ -18,10 +19,17 @@ import com.google.common.io.Files;
public class GuavaTest { public class GuavaTest {
public static final String fileName = "src/main/resources/countries.txt"; 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 @Test
public void whenAppendToFileUsingFileWriter_thenCorrect() throws IOException { public void whenAppendToFileUsingGuava_thenCorrect() throws IOException {
File file = new File(fileName); File file = new File(fileName);
CharSink chs = Files.asCharSink(file, Charsets.UTF_8, FileWriteMode.APPEND); CharSink chs = Files.asCharSink(file, Charsets.UTF_8, FileWriteMode.APPEND);
chs.write("Spain\r\n"); chs.write("Spain\r\n");
@ -34,7 +42,6 @@ public class GuavaTest {
@After @After
public void revertFile() throws IOException { public void revertFile() throws IOException {
PrintWriter writer = new PrintWriter(fileName); PrintWriter writer = new PrintWriter(fileName);
writer.print("");
writer.print("UK\r\n" + "US\r\n" + "Germany\r\n"); writer.print("UK\r\n" + "US\r\n" + "Germany\r\n");
writer.close(); writer.close();
} }