From 6e8aefc25f40033e6eedc444feec3931ed5ae4f9 Mon Sep 17 00:00:00 2001 From: binsky Date: Wed, 26 Jan 2022 10:06:32 +0100 Subject: [PATCH] implement writeFile storage method --- src/main/java/Storage.java | 13 +++++++++++++ src/test/java/StorageTest.java | 16 +++++++++++++++- 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/src/main/java/Storage.java b/src/main/java/Storage.java index e5fc3de..fdbaf25 100644 --- a/src/main/java/Storage.java +++ b/src/main/java/Storage.java @@ -1,3 +1,7 @@ +import java.io.BufferedWriter; +import java.io.FileWriter; +import java.io.IOException; + public class Storage implements StorageInterface { @Override public void export() { @@ -11,6 +15,15 @@ public class Storage implements StorageInterface { public boolean writeFile(String path, String content) { if (!path.isEmpty() && !content.isEmpty()) { + try { + BufferedWriter writer = new BufferedWriter(new FileWriter(path, false)); + writer.append(content); + writer.close(); + } catch (IOException e) { + e.printStackTrace(); + return false; + } + return true; } return false; diff --git a/src/test/java/StorageTest.java b/src/test/java/StorageTest.java index b1d6098..5ef0e42 100644 --- a/src/test/java/StorageTest.java +++ b/src/test/java/StorageTest.java @@ -1,17 +1,27 @@ +import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; +import java.io.File; + import static org.junit.jupiter.api.Assertions.*; class StorageTest { static Storage storage; + static String testFilePath = "/tmp/test.txt"; @BeforeAll static void init() { storage = new Storage(); } + @AfterAll + static void clear() { + File myObj = new File(testFilePath); + myObj.delete(); + } + @Test void constructor() { assertInstanceOf(Storage.class, storage); @@ -27,6 +37,10 @@ class StorageTest { @Test void writeFile() { - assertTrue(storage.writeFile("/tmp/test.txt", "test")); + String content = "test"; + assertTrue(storage.writeFile(testFilePath, content)); + + File f = new File(testFilePath); + assertTrue(f.isFile()); } } \ No newline at end of file