Browse Source

implement writeFile storage method

feature-add-storage-class
binsky 3 years ago
parent
commit
6e8aefc25f
  1. 13
      src/main/java/Storage.java
  2. 16
      src/test/java/StorageTest.java

13
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 { public class Storage implements StorageInterface {
@Override @Override
public void export() { public void export() {
@ -11,6 +15,15 @@ public class Storage implements StorageInterface {
public boolean writeFile(String path, String content) { public boolean writeFile(String path, String content) {
if (!path.isEmpty() && !content.isEmpty()) { 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 true;
} }
return false; return false;

16
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.BeforeAll;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import java.io.File;
import static org.junit.jupiter.api.Assertions.*; import static org.junit.jupiter.api.Assertions.*;
class StorageTest { class StorageTest {
static Storage storage; static Storage storage;
static String testFilePath = "/tmp/test.txt";
@BeforeAll @BeforeAll
static void init() { static void init() {
storage = new Storage(); storage = new Storage();
} }
@AfterAll
static void clear() {
File myObj = new File(testFilePath);
myObj.delete();
}
@Test @Test
void constructor() { void constructor() {
assertInstanceOf(Storage.class, storage); assertInstanceOf(Storage.class, storage);
@ -27,6 +37,10 @@ class StorageTest {
@Test @Test
void writeFile() { 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());
} }
} }
Loading…
Cancel
Save