BAEL-3214 | Code review related changes - 2
This commit is contained in:
parent
4e063a1120
commit
52934fdb20
|
@ -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"))));
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue