Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[T6A4][T2]Mattheus Lee #25

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 20 additions & 9 deletions src/seedu/addressbook/logic/Logic.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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. */
Expand All @@ -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;
}

Expand All @@ -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;
}

/**
Expand Down
50 changes: 50 additions & 0 deletions src/seedu/addressbook/storage/Storage.java
Original file line number Diff line number Diff line change
@@ -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;
}
18 changes: 2 additions & 16 deletions src/seedu/addressbook/storage/StorageFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
22 changes: 7 additions & 15 deletions test/java/seedu/addressbook/logic/LogicTest.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
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;
import seedu.addressbook.data.AddressBook;
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.*;

Expand All @@ -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
Expand Down Expand Up @@ -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());
}


Expand Down
2 changes: 1 addition & 1 deletion test/java/seedu/addressbook/storage/StorageFileTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
22 changes: 22 additions & 0 deletions test/java/seedu/addressbook/storage/StorageStub.java
Original file line number Diff line number Diff line change
@@ -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();
}
}