diff --git a/src/seedu/addressbook/logic/Logic.java b/src/seedu/addressbook/logic/Logic.java index 17afd61a0..813ed05a1 100644 --- a/src/seedu/addressbook/logic/Logic.java +++ b/src/seedu/addressbook/logic/Logic.java @@ -5,6 +5,7 @@ import seedu.addressbook.data.AddressBook; import seedu.addressbook.data.person.ReadOnlyPerson; import seedu.addressbook.parser.Parser; +import seedu.addressbook.storage.Storage; import seedu.addressbook.storage.StorageFile; import java.util.Collections; @@ -16,8 +17,7 @@ */ public class Logic { - - private StorageFile storage; + private Storage storage; private AddressBook addressBook; /** The list of person shown to the user most recently. */ @@ -28,12 +28,12 @@ public Logic() throws Exception{ setAddressBook(storage.load()); } - Logic(StorageFile storageFile, AddressBook addressBook){ + Logic(Storage storageFile, AddressBook addressBook){ setStorage(storageFile); setAddressBook(addressBook); } - void setStorage(StorageFile storage){ + void setStorage(Storage storage){ this.storage = storage; } @@ -42,15 +42,26 @@ void setAddressBook(AddressBook addressBook){ } /** - * Creates the StorageFile object based on the user specified path (if any) or the default storage path. - * @throws StorageFile.InvalidStorageFilePathException if the target file path is incorrect. + * Creates the Storage object based on the user specified (if any) location or the default location. + * @throws Storage.InvalidStorageLocationException if the target file location is incorrect. */ - private StorageFile initializeStorage() throws StorageFile.InvalidStorageFilePathException { - return new StorageFile(); + private Storage initializeStorage() throws Storage.InvalidStorageLocationException { + // Uses StorageFile for now + try { + Storage temp = new StorageFile(); + return temp; + } catch (Exception e) { + + } + return null; } public String getStorageFilePath() { - return storage.getPath(); + // Path is only valid for StorageFile + if (storage instanceof StorageFile) { + return ((StorageFile)storage).getPath(); + } + return null; } /** diff --git a/src/seedu/addressbook/storage/Storage.java b/src/seedu/addressbook/storage/Storage.java new file mode 100644 index 000000000..9460875e6 --- /dev/null +++ b/src/seedu/addressbook/storage/Storage.java @@ -0,0 +1,50 @@ +package seedu.addressbook.storage; + +import seedu.addressbook.data.AddressBook; +import seedu.addressbook.data.exception.IllegalValueException; + +/** + * Abstract class to represent a form of storage. + */ +public abstract class Storage { + + /** + * Signals that an error has occured in trying to setup the storage location. + */ + public static class InvalidStorageLocationException extends IllegalValueException{ + public InvalidStorageLocationException(String message) { + super(message); + } + } + + /** + * Signals that some error has occured while trying to convert and read/write data between the application + * and the storage. + */ + public static class StorageOperationException extends Exception { + public StorageOperationException(String message) { + super(message); + } + } + + /** + * Default constructor, uses defaults. + * @throws InvalidStorageLocationException if the default storage location is invalid. + */ + public Storage() throws InvalidStorageLocationException { + }; + + /** + * Saves all data to storage. + * + * @throws StorageOperationException if there were errors converting and/or storing data to storage. + */ + public abstract void save(AddressBook addressBook) throws StorageOperationException; + + /** + * Loads data from storage. + * + * @throws StorageOperationException if there were errors reading and/or converting data from storage. + */ + public abstract AddressBook load() throws StorageOperationException; +} \ No newline at end of file diff --git a/src/seedu/addressbook/storage/StorageFile.java b/src/seedu/addressbook/storage/StorageFile.java index 693097a86..a7c2c6ba0 100644 --- a/src/seedu/addressbook/storage/StorageFile.java +++ b/src/seedu/addressbook/storage/StorageFile.java @@ -15,34 +15,20 @@ /** * Represents the file used to store address book data. */ -public class StorageFile { +public class StorageFile extends Storage { /** Default file path used if the user doesn't provide the file name. */ public static final String DEFAULT_STORAGE_FILEPATH = "addressbook.txt"; - /* Note: Note the use of nested classes below. - * More info https://docs.oracle.com/javase/tutorial/java/javaOO/nested.html - */ - /** * Signals that the given file path does not fulfill the storage filepath constraints. */ - public static class InvalidStorageFilePathException extends IllegalValueException { + public static class InvalidStorageFilePathException extends InvalidStorageLocationException { public InvalidStorageFilePathException(String message) { super(message); } } - /** - * Signals that some error has occured while trying to convert and read/write data between the application - * and the storage file. - */ - public static class StorageOperationException extends Exception { - public StorageOperationException(String message) { - super(message); - } - } - private final JAXBContext jaxbContext; public final Path path; diff --git a/test/java/seedu/addressbook/logic/LogicTest.java b/test/java/seedu/addressbook/logic/LogicTest.java index 7efa921ca..b94fdcf0a 100644 --- a/test/java/seedu/addressbook/logic/LogicTest.java +++ b/test/java/seedu/addressbook/logic/LogicTest.java @@ -1,10 +1,7 @@ package seedu.addressbook.logic; - import org.junit.Before; -import org.junit.Rule; import org.junit.Test; -import org.junit.rules.TemporaryFolder; import seedu.addressbook.commands.CommandResult; import seedu.addressbook.commands.*; import seedu.addressbook.common.Messages; @@ -12,7 +9,8 @@ import seedu.addressbook.data.person.*; import seedu.addressbook.data.tag.Tag; import seedu.addressbook.data.tag.UniqueTagList; -import seedu.addressbook.storage.StorageFile; +import seedu.addressbook.storage.Storage; +import seedu.addressbook.storage.StorageStub; import java.util.*; @@ -22,22 +20,16 @@ public class LogicTest { - /** - * See https://github.com/junit-team/junit4/wiki/rules#temporaryfolder-rule - */ - @Rule - public TemporaryFolder saveFolder = new TemporaryFolder(); - - private StorageFile saveFile; + private Storage storage; private AddressBook addressBook; private Logic logic; @Before public void setup() throws Exception { - saveFile = new StorageFile(saveFolder.newFile("testSaveFile.txt").getPath()); + storage = new StorageStub(); addressBook = new AddressBook(); - saveFile.save(addressBook); - logic = new Logic(saveFile, addressBook); + storage.save(addressBook); + logic = new Logic(storage, addressBook); } @Test @@ -90,7 +82,7 @@ private void assertCommandBehavior(String inputCommand, //Confirm the state of data is as expected assertEquals(expectedAddressBook, addressBook); assertEquals(lastShownList, logic.getLastShownList()); - assertEquals(addressBook, saveFile.load()); + assertEquals(addressBook, storage.load()); } diff --git a/test/java/seedu/addressbook/storage/StorageFileTest.java b/test/java/seedu/addressbook/storage/StorageFileTest.java index 1bd6fcab3..a321670c5 100644 --- a/test/java/seedu/addressbook/storage/StorageFileTest.java +++ b/test/java/seedu/addressbook/storage/StorageFileTest.java @@ -17,7 +17,7 @@ import seedu.addressbook.data.person.Phone; import seedu.addressbook.data.tag.Tag; import seedu.addressbook.data.tag.UniqueTagList; -import seedu.addressbook.storage.StorageFile.StorageOperationException; +import seedu.addressbook.storage.Storage.StorageOperationException; import static seedu.addressbook.util.TestUtil.assertTextFilesEqual; public class StorageFileTest { diff --git a/test/java/seedu/addressbook/storage/StorageStub.java b/test/java/seedu/addressbook/storage/StorageStub.java new file mode 100644 index 000000000..37ec900d6 --- /dev/null +++ b/test/java/seedu/addressbook/storage/StorageStub.java @@ -0,0 +1,22 @@ +package seedu.addressbook.storage; + +import seedu.addressbook.data.AddressBook; + +/** + * Storage stub class for dependency injection for a Storage object. + * Does not perform any reading/writing of data to storage. + */ +public class StorageStub extends Storage { + + public StorageStub() throws InvalidStorageLocationException { + // Do nothing + } + + public void save(AddressBook addressBook) { + // Do nothing + } + + public AddressBook load() { + return new AddressBook(); + } +} \ No newline at end of file