forked from nus-cs2103-AY1718S1/addressbook-level3-old
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[nus-cs2103-AY1718S1#47] Add unit tests to the StorageFile class nus-…
- Loading branch information
Showing
4 changed files
with
154 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
<?xml version="1.0" encoding="UTF-8" standalone="yes"?> | ||
<AddressBook> | ||
<persons> | ||
<invalid>data</invalid> | ||
</persons> | ||
</AddressBook> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<?xml version="1.0" encoding="UTF-8" standalone="yes"?> | ||
<AddressBook> | ||
<persons> | ||
<name>John Doe</name> | ||
<phone isPrivate="false">98765432</phone> | ||
<email isPrivate="false">[email protected]</email> | ||
<address isPrivate="false">John street, block 123, #01-01</address> | ||
</persons> | ||
<persons> | ||
<name>Betsy Crowe</name> | ||
<phone isPrivate="true">1234567</phone> | ||
<email isPrivate="false">[email protected]</email> | ||
<address isPrivate="true">Newgate Prison</address> | ||
<tagged>friend</tagged> | ||
<tagged>criminal</tagged> | ||
</persons> | ||
<tags>friend</tags> | ||
<tags>criminal</tags> | ||
</AddressBook> |
109 changes: 109 additions & 0 deletions
109
test/java/seedu/addressbook/storage/StorageFileTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
package seedu.addressbook.storage; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
import java.nio.file.Paths; | ||
import java.util.Collections; | ||
import org.junit.Rule; | ||
import org.junit.Test; | ||
import org.junit.rules.ExpectedException; | ||
import org.junit.rules.TemporaryFolder; | ||
|
||
import seedu.addressbook.data.AddressBook; | ||
import seedu.addressbook.data.exception.IllegalValueException; | ||
import seedu.addressbook.data.person.Address; | ||
import seedu.addressbook.data.person.Email; | ||
import seedu.addressbook.data.person.Name; | ||
import seedu.addressbook.data.person.Person; | ||
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 static seedu.addressbook.util.TestUtil.assertTextFilesEqual; | ||
|
||
public class StorageFileTest { | ||
private static final String TEST_DATA_FOLDER = "test/data/StorageFileTest"; | ||
|
||
@Rule | ||
public ExpectedException thrown = ExpectedException.none(); | ||
|
||
@Rule | ||
public TemporaryFolder testFolder = new TemporaryFolder(); | ||
|
||
@Test | ||
public void constructor_nullFilePath_exceptionThrown() throws Exception { | ||
thrown.expect(NullPointerException.class); | ||
new StorageFile(null); | ||
} | ||
|
||
@Test | ||
public void constructor_noTxtExtension_exceptionThrown() throws Exception { | ||
thrown.expect(IllegalValueException.class); | ||
new StorageFile(TEST_DATA_FOLDER + "/" + "InvalidfileName"); | ||
} | ||
|
||
@Test | ||
public void load_invalidFormat_exceptionThrown() throws Exception { | ||
// The file contains valid xml data, but does not match the AddressBook class | ||
StorageFile storage = getStorage("InvalidData.txt"); | ||
thrown.expect(StorageOperationException.class); | ||
storage.load(); | ||
} | ||
|
||
@Test | ||
public void load_validFormat() throws Exception { | ||
AddressBook actualAB = getStorage("ValidData.txt").load(); | ||
AddressBook expectedAB = getTestAddressBook(); | ||
|
||
// ensure loaded AddressBook is properly constructed with test data | ||
// TODO: overwrite equals method in AddressBook class and replace with equals method below | ||
assertEquals(actualAB.getAllPersons(), expectedAB.getAllPersons()); | ||
} | ||
|
||
@Test | ||
public void save_nullAddressBook_exceptionThrown() throws Exception { | ||
StorageFile storage = getTempStorage(); | ||
thrown.expect(NullPointerException.class); | ||
storage.save(null); | ||
} | ||
|
||
@Test | ||
public void save_validAddressBook() throws Exception { | ||
AddressBook ab = getTestAddressBook(); | ||
StorageFile storage = getTempStorage(); | ||
storage.save(ab); | ||
|
||
assertStorageFilesEqual(storage, getStorage("ValidData.txt")); | ||
} | ||
|
||
// getPath() method in StorageFile class is trivial so it is not tested | ||
|
||
/** | ||
* Asserts that the contents of two storage files are the same. | ||
*/ | ||
private void assertStorageFilesEqual(StorageFile sf1, StorageFile sf2) throws Exception { | ||
assertTextFilesEqual(Paths.get(sf1.getPath()), Paths.get(sf2.getPath())); | ||
} | ||
|
||
private StorageFile getStorage(String fileName) throws Exception { | ||
return new StorageFile(TEST_DATA_FOLDER + "/" + fileName); | ||
} | ||
|
||
private StorageFile getTempStorage() throws Exception { | ||
return new StorageFile(testFolder.getRoot().getPath() + "/" + "temp.txt"); | ||
} | ||
|
||
private AddressBook getTestAddressBook() throws Exception { | ||
AddressBook ab = new AddressBook(); | ||
ab.addPerson(new Person(new Name("John Doe"), | ||
new Phone("98765432", false), | ||
new Email("[email protected]", false), | ||
new Address("John street, block 123, #01-01", false), | ||
new UniqueTagList(Collections.emptySet()))); | ||
ab.addPerson(new Person(new Name("Betsy Crowe"), | ||
new Phone("1234567", true), | ||
new Email("[email protected]", false), | ||
new Address("Newgate Prison", true), | ||
new UniqueTagList(new Tag("friend"), new Tag("criminal")))); | ||
return ab; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package seedu.addressbook.util; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
import java.io.IOException; | ||
import java.nio.charset.Charset; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.util.List; | ||
|
||
public class TestUtil { | ||
/** | ||
* Asserts whether the text in the two given files are the same. Ignores any | ||
* differences in line endings | ||
*/ | ||
public static void assertTextFilesEqual(Path path1, Path path2) throws IOException { | ||
List<String> list1 = Files.readAllLines(path1, Charset.defaultCharset()); | ||
List<String> list2 = Files.readAllLines(path2, Charset.defaultCharset()); | ||
assertEquals(String.join("\n", list1), String.join("\n", list2)); | ||
} | ||
} |