BAEL-3214 | Code review related changes - 2

This commit is contained in:
Nikunj Gandhi 2019-09-19 15:31:48 -04:00
parent 4e063a1120
commit 52934fdb20
1 changed files with 8 additions and 10 deletions

View File

@ -11,39 +11,37 @@ import org.junit.Test;
public class FileWriterExampleUnitTest {
private static final String FILE_NAME = "src/test/resources/FileWriterTest.txt";
@After
public void tearDown() throws IOException {
Files.delete(Path.of(FILE_NAME));
Files.delete(Path.of("src/test/resources/FileWriterTest.txt"));
}
@Test
public void testWriteString() throws IOException {
try (FileWriter fileWriter = new FileWriter(FILE_NAME)) {
try (FileWriter fileWriter = new FileWriter("src/test/resources/FileWriterTest.txt")) {
fileWriter.write("Hello Folks!");
}
Assert.assertEquals("Hello Folks!", new String(Files.readAllBytes(Path.of(FILE_NAME))));
Assert.assertEquals("Hello Folks!", new String(Files.readAllBytes(Path.of("src/test/resources/FileWriterTest.txt"))));
}
@Test
public void testAppendString() throws IOException {
try (FileWriter fileWriter = new FileWriter(FILE_NAME)) {
try (FileWriter fileWriter = new FileWriter("src/test/resources/FileWriterTest.txt")) {
fileWriter.write("Hello Folks!");
}
// using another try with resources to reopen the file in append mode
try (FileWriter fileWriter = new FileWriter(FILE_NAME, true)) {
try (FileWriter fileWriter = new FileWriter("src/test/resources/FileWriterTest.txt", true)) {
fileWriter.write("Hello Folks Again!");
}
Assert.assertEquals("Hello Folks!" + "Hello Folks Again!", new String(Files.readAllBytes(Path.of(FILE_NAME))));
Assert.assertEquals("Hello Folks!" + "Hello Folks Again!", new String(Files.readAllBytes(Path.of("src/test/resources/FileWriterTest.txt"))));
}
@Test
public void testWriteCharArray() throws IOException {
try (FileWriter fileWriter = new FileWriter(FILE_NAME)) {
try (FileWriter fileWriter = new FileWriter("src/test/resources/FileWriterTest.txt")) {
fileWriter.write("Hello Folks!".toCharArray());
}
Assert.assertEquals("Hello Folks!", new String(Files.readAllBytes(Path.of(FILE_NAME))));
Assert.assertEquals("Hello Folks!", new String(Files.readAllBytes(Path.of("src/test/resources/FileWriterTest.txt"))));
}
}