-
Notifications
You must be signed in to change notification settings - Fork 163
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
[T6A1][F11-B3]Hsieh Hsin Han #513
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package seedu.addressbook.storage; | ||
|
||
import seedu.addressbook.data.AddressBook; | ||
import seedu.addressbook.data.exception.IllegalValueException; | ||
|
||
public abstract class Storage { | ||
|
||
public abstract void save(AddressBook addressBook) throws StorageOperationException ; | ||
|
||
public abstract AddressBook load() throws StorageOperationException; | ||
|
||
public abstract String getPath(); | ||
|
||
/** | ||
* Signals that some error has occured while trying to convert and read/write data between the application | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Proper Javadoc should like /**
* Signals that...
* ....
*/ |
||
* and the storage file. | ||
*/ | ||
public static class StorageOperationException extends Exception { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Indentation problem here?. Our coding standard requires you to use 4 spaces instead of tabs. You can configure Eclipse to convert tabs to spaces automatically. |
||
public StorageOperationException(String message) { | ||
super(message); | ||
} | ||
} | ||
|
||
public static class InvalidStorageFilePathException extends IllegalValueException { | ||
public InvalidStorageFilePathException(String message) { | ||
super(message); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
package seedu.addressbook.storage; | ||
|
||
import java.io.BufferedReader; | ||
import java.io.BufferedWriter; | ||
import java.io.FileNotFoundException; | ||
import java.io.FileReader; | ||
import java.io.FileWriter; | ||
import java.io.IOException; | ||
import java.io.Reader; | ||
import java.io.Writer; | ||
import java.nio.file.Path; | ||
import java.nio.file.Paths; | ||
|
||
import javax.xml.bind.JAXBContext; | ||
import javax.xml.bind.JAXBException; | ||
import javax.xml.bind.Marshaller; | ||
import javax.xml.bind.Unmarshaller; | ||
|
||
import seedu.addressbook.data.AddressBook; | ||
import seedu.addressbook.data.exception.IllegalValueException; | ||
import seedu.addressbook.storage.Storage.InvalidStorageFilePathException; | ||
import seedu.addressbook.storage.jaxb.AdaptedAddressBook; | ||
|
||
public class StorageStub 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 | ||
*/ | ||
|
||
private final JAXBContext jaxbContext; | ||
|
||
public final Path path; | ||
|
||
/** | ||
* @throws InvalidStorageFilePathException if the default path is invalid | ||
*/ | ||
public StorageStub() throws InvalidStorageFilePathException { | ||
this(DEFAULT_STORAGE_FILEPATH); | ||
} | ||
|
||
/** | ||
* @throws InvalidStorageFilePathException if the given file path is invalid | ||
*/ | ||
public StorageStub(String filePath) throws InvalidStorageFilePathException { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why do you need this code here? A |
||
try { | ||
jaxbContext = JAXBContext.newInstance(AdaptedAddressBook.class); | ||
} catch (JAXBException jaxbe) { | ||
throw new RuntimeException("jaxb initialisation error"); | ||
} | ||
|
||
path = Paths.get(filePath); | ||
if (!isValidPath(path)) { | ||
throw new InvalidStorageFilePathException("Storage file should end with '.txt'"); | ||
} | ||
} | ||
|
||
/** | ||
* Returns true if the given path is acceptable as a storage file. | ||
* The file path is considered acceptable if it ends with '.txt' | ||
*/ | ||
private static boolean isValidPath(Path filePath) { | ||
return filePath.toString().endsWith(".txt"); | ||
} | ||
|
||
/** | ||
* Saves all data to this storage file. | ||
* | ||
* @throws StorageOperationException if there were errors converting and/or storing data to file. | ||
*/ | ||
@Override | ||
public void save(AddressBook addressBook) throws StorageOperationException { | ||
|
||
} | ||
|
||
/** | ||
* Loads data from this storage file. | ||
* | ||
* @throws StorageOperationException if there were errors reading and/or converting data from file. | ||
*/ | ||
@Override | ||
public AddressBook load() throws StorageOperationException { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here. |
||
try (final Reader fileReader = | ||
new BufferedReader(new FileReader(path.toFile()))) { | ||
|
||
final Unmarshaller unmarshaller = jaxbContext.createUnmarshaller(); | ||
final AdaptedAddressBook loaded = (AdaptedAddressBook) unmarshaller.unmarshal(fileReader); | ||
// manual check for missing elements | ||
if (loaded.isAnyRequiredFieldMissing()) { | ||
throw new StorageOperationException("File data missing some elements"); | ||
} | ||
return loaded.toModelType(); | ||
|
||
/* Note: Here, we are using an exception to create the file if it is missing. However, we should minimize | ||
* using exceptions to facilitate normal paths of execution. If we consider the missing file as a 'normal' | ||
* situation (i.e. not truly exceptional) we should not use an exception to handle it. | ||
*/ | ||
|
||
// create empty file if not found | ||
} catch (FileNotFoundException fnfe) { | ||
final AddressBook empty = new AddressBook(); | ||
save(empty); | ||
return empty; | ||
|
||
// other errors | ||
} catch (IOException ioe) { | ||
throw new StorageOperationException("Error writing to file: " + path); | ||
} catch (JAXBException jaxbe) { | ||
throw new StorageOperationException("Error parsing file data format"); | ||
} catch (IllegalValueException ive) { | ||
throw new StorageOperationException("File contains illegal data values; data type constraints not met"); | ||
} | ||
} | ||
|
||
@Override | ||
public String getPath() { | ||
return path.toString(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -31,7 +31,7 @@ public Gui(Logic logic, String version) { | |
|
||
public void start(Stage stage, Stoppable mainApp) throws IOException { | ||
mainWindow = createMainWindow(stage, mainApp); | ||
mainWindow.displayWelcomeMessage(version, logic.getStorageFilePath()); | ||
mainWindow.displayWelcomeMessage(version, logic.getStoragePath()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Great! You also remember to change this one. |
||
} | ||
|
||
private MainWindow createMainWindow(Stage stage, Stoppable mainApp) throws IOException{ | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,6 +13,7 @@ | |
import seedu.addressbook.data.tag.Tag; | ||
import seedu.addressbook.data.tag.UniqueTagList; | ||
import seedu.addressbook.storage.StorageFile; | ||
import seedu.addressbook.storage.StorageStub; | ||
|
||
import java.util.*; | ||
|
||
|
@@ -28,16 +29,16 @@ public class LogicTest { | |
@Rule | ||
public TemporaryFolder saveFolder = new TemporaryFolder(); | ||
|
||
private StorageFile saveFile; | ||
private StorageStub saveStub; | ||
private AddressBook addressBook; | ||
private Logic logic; | ||
|
||
@Before | ||
public void setup() throws Exception { | ||
saveFile = new StorageFile(saveFolder.newFile("testSaveFile.txt").getPath()); | ||
saveStub = new StorageStub(saveFolder.newFile("testSaveFile.txt").getPath()); | ||
addressBook = new AddressBook(); | ||
saveFile.save(addressBook); | ||
logic = new Logic(saveFile, addressBook); | ||
saveStub.save(addressBook); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why do you need this code here since |
||
logic = new Logic(saveStub, addressBook); | ||
} | ||
|
||
@Test | ||
|
@@ -90,7 +91,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, saveStub.load()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since you are doing DI. You no longer need to test the storage when you test the logic. |
||
} | ||
|
||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This class and its methods should have a header comments. If not, developers who needs to inherit from this class will not know how exactly they should override the methods and other developers who write client code for this class will not know how to use it either.