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 { public class FileWriterExampleUnitTest {
private static final String FILE_NAME = "src/test/resources/FileWriterTest.txt";
@After @After
public void tearDown() throws IOException { public void tearDown() throws IOException {
Files.delete(Path.of(FILE_NAME)); Files.delete(Path.of("src/test/resources/FileWriterTest.txt"));
} }
@Test @Test
public void testWriteString() throws IOException { 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!"); 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 @Test
public void testAppendString() throws IOException { 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!"); fileWriter.write("Hello Folks!");
} }
// using another try with resources to reopen the file in append mode // 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!"); 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 @Test
public void testWriteCharArray() throws IOException { 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()); 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"))));
} }
} }