ソースを参照

FileWriter testing

Andrew Grant 4 ヶ月 前
コミット
f418d4e374

+ 2 - 0
src/main/java/scot/carricksoftware/grantswriter/writer/FileWriterImpl.java

@@ -19,6 +19,7 @@ public class FileWriterImpl implements FileWriter {
 
     private PrintWriter printWriter = null;
 
+
     @Override
     public void init(String fileName) throws IOException {
         logger.debug("FileWriterImpl::init");
@@ -28,6 +29,7 @@ public class FileWriterImpl implements FileWriter {
     @Override
     public void close() {
         logger.debug("FileWriterImpl::close");
+        printWriter.close();
         printWriter = null;
     }
 

+ 34 - 0
src/test/java/scot/carricksoftware/grantswriter/writer/FileWriterTest.java

@@ -8,6 +8,12 @@ package scot.carricksoftware.grantswriter.writer;
 import org.junit.jupiter.api.BeforeEach;
 import org.junit.jupiter.api.Test;
 
+import java.io.File;
+import java.nio.file.Files;
+import java.nio.file.Paths;
+import java.util.List;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
 import static org.junit.jupiter.api.Assertions.assertNotNull;
 import static org.junit.jupiter.api.Assertions.assertNull;
 import static scot.carricksoftware.grantswriter.GenerateRandomNumberValues.GetRandomString;
@@ -39,6 +45,34 @@ class FileWriterTest {
         assertNull(writer.getPrintWriter());
     }
 
+    @Test
+    public void writeLineTest() throws Exception {
+       String tempFileName = getTemporaryFileName();
+       deleteTempFile(tempFileName);
+       String line = GetRandomString();
+
+       writer.init(tempFileName);
+       writer.writeLine(line);
+       writer.close();
+
+       List<String> contents = getTempFileContents(tempFileName);
+        assertEquals(line, contents.get(0));
+    }
+
+    private List<String> getTempFileContents(String tempFileName) throws Exception {
+        return Files.readAllLines(Paths.get(tempFileName));
+    }
+
+    private void deleteTempFile(String tempFileName) {
+        File file = new File(tempFileName);
+        //noinspection ResultOfMethodCallIgnored
+        file.delete();
+    }
+
+    private String getTemporaryFileName() throws Exception {
+        File file = File.createTempFile("/tmp", "tmp");
+        return file.getAbsolutePath();
+    }
 
 
 }