diff --git a/src/seedu/addressbook/data/person/Address.java b/src/seedu/addressbook/data/person/Address.java index 8ac726444..5672c9610 100644 --- a/src/seedu/addressbook/data/person/Address.java +++ b/src/seedu/addressbook/data/person/Address.java @@ -21,11 +21,12 @@ public class Address { * @throws IllegalValueException if given address string is invalid. */ public Address(String address, boolean isPrivate) throws IllegalValueException { + String trimmedAddress = address.trim(); this.isPrivate = isPrivate; - if (!isValidAddress(address)) { + if (!isValidAddress(trimmedAddress)) { throw new IllegalValueException(MESSAGE_ADDRESS_CONSTRAINTS); } - this.value = address; + this.value = trimmedAddress; } /** diff --git a/test/java/seedu/addressbook/commands/AddCommandTest.java b/test/java/seedu/addressbook/commands/AddCommandTest.java new file mode 100644 index 000000000..fd870a62e --- /dev/null +++ b/test/java/seedu/addressbook/commands/AddCommandTest.java @@ -0,0 +1,147 @@ +package seedu.addressbook.commands; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + +import java.util.Arrays; +import java.util.Collections; +import java.util.HashSet; +import java.util.List; +import java.util.Set; + +import org.junit.Test; + +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.person.ReadOnlyPerson; +import seedu.addressbook.data.person.UniquePersonList; +import seedu.addressbook.util.TestUtil; + +public class AddCommandTest { + private static final List EMPTY_PERSON_LIST = Collections.emptyList(); + private static final Set EMPTY_STRING_LIST = Collections.emptySet(); + + @Test + public void addCommand_invalidName_throwsException() { + final String[] invalidNames = { "", " ", "[]\\[;]" }; + for (String name : invalidNames) { + assertConstructingInvalidAddCmdThrowsException(name, Phone.EXAMPLE, true, Email.EXAMPLE, false, + Address.EXAMPLE, true, EMPTY_STRING_LIST); + } + } + + @Test + public void addCommand_invalidPhone_throwsException() { + final String[] invalidNumbers = { "", " ", "1234-5678", "[]\\[;]", "abc", "a123", "+651234" }; + for (String number : invalidNumbers) { + assertConstructingInvalidAddCmdThrowsException(Name.EXAMPLE, number, false, Email.EXAMPLE, true, + Address.EXAMPLE, false, EMPTY_STRING_LIST); + } + } + + @Test + public void addCommand_invalidEmail_throwsException() { + final String[] invalidEmails = { "", " ", "def.com", "@", "@def", "@def.com", "abc@", + "@invalid@email", "invalid@email!", "!invalid@email" }; + for (String email : invalidEmails) { + assertConstructingInvalidAddCmdThrowsException(Name.EXAMPLE, Phone.EXAMPLE, false, email, false, + Address.EXAMPLE, false, EMPTY_STRING_LIST); + } + } + + @Test + public void addCommand_invalidAddress_throwsException() { + final String[] invalidAddresses = { "", " " }; + for (String address : invalidAddresses) { + assertConstructingInvalidAddCmdThrowsException(Name.EXAMPLE, Phone.EXAMPLE, true, Email.EXAMPLE, + true, address, true, EMPTY_STRING_LIST); + } + } + + @Test + public void addCommand_invalidTags_throwsException() { + final String[][] invalidTags = { { "" }, { " " }, { "'" }, { "[]\\[;]" }, { "validTag", "" }, + { "", " " } }; + for (String[] tags : invalidTags) { + Set tagsToAdd = new HashSet<>(Arrays.asList(tags)); + assertConstructingInvalidAddCmdThrowsException(Name.EXAMPLE, Phone.EXAMPLE, true, Email.EXAMPLE, + true, Address.EXAMPLE, false, tagsToAdd); + } + } + + /** + * Asserts that attempting to construct an add command with the supplied + * invalid data throws an IllegalValueException + */ + private void assertConstructingInvalidAddCmdThrowsException(String name, String phone, + boolean isPhonePrivate, String email, boolean isEmailPrivate, String address, + boolean isAddressPrivate, Set tags) { + try { + new AddCommand(name, phone, isPhonePrivate, email, isEmailPrivate, address, isAddressPrivate, + tags); + } catch (IllegalValueException e) { + return; + } + String error = String.format( + "An add command was successfully constructed with invalid input: %s %s %s %s %s %s %s %s", + name, phone, isPhonePrivate, email, isEmailPrivate, address, isAddressPrivate, tags); + fail(error); + } + + @Test + public void addCommand_validData_correctlyConstructed() throws Exception { + AddCommand command = new AddCommand(Name.EXAMPLE, Phone.EXAMPLE, true, Email.EXAMPLE, false, + Address.EXAMPLE, true, EMPTY_STRING_LIST); + ReadOnlyPerson p = command.getPerson(); + + // TODO: add comparison of tags to person.equals and equality methods to + // individual fields that compare privacy to simplify this + assertEquals(Name.EXAMPLE, p.getName().fullName); + assertEquals(Phone.EXAMPLE, p.getPhone().value); + assertTrue(p.getPhone().isPrivate()); + assertEquals(Email.EXAMPLE, p.getEmail().value); + assertFalse(p.getEmail().isPrivate()); + assertEquals(Address.EXAMPLE, p.getAddress().value); + assertTrue(p.getAddress().isPrivate()); + boolean isTagListEmpty = !p.getTags().iterator().hasNext(); + assertTrue(isTagListEmpty); + } + + @Test + public void addCommand_emptyAddressBook_addressBookContainsPerson() { + Person p = TestUtil.generateTestPerson(); + AddCommand command = new AddCommand(p); + AddressBook book = new AddressBook(); + command.setData(book, EMPTY_PERSON_LIST); + CommandResult result = command.execute(); + UniquePersonList people = book.getAllPersons(); + + assertTrue(people.contains(p)); + assertEquals(1, people.immutableListView().size()); + assertFalse(result.getRelevantPersons().isPresent()); + assertEquals(String.format(AddCommand.MESSAGE_SUCCESS, p), result.feedbackToUser); + } + + @Test + public void addCommand_addressBookAlreadyContainsPerson_addressBookUnmodified() throws Exception { + Person p = TestUtil.generateTestPerson(); + AddressBook book = new AddressBook(); + book.addPerson(p); + AddCommand command = new AddCommand(p); + command.setData(book, EMPTY_PERSON_LIST); + CommandResult result = command.execute(); + + assertFalse(result.getRelevantPersons().isPresent()); + assertEquals(AddCommand.MESSAGE_DUPLICATE_PERSON, result.feedbackToUser); + UniquePersonList people = book.getAllPersons(); + assertTrue(people.contains(p)); + assertEquals(1, people.immutableListView().size()); + } +} diff --git a/test/java/seedu/addressbook/commands/DeleteCommandTest.java b/test/java/seedu/addressbook/commands/DeleteCommandTest.java new file mode 100644 index 000000000..cdda97f9e --- /dev/null +++ b/test/java/seedu/addressbook/commands/DeleteCommandTest.java @@ -0,0 +1,162 @@ +package seedu.addressbook.commands; + +import static org.junit.Assert.assertEquals; + +import java.util.List; + +import org.junit.Before; +import org.junit.Test; + +import seedu.addressbook.common.Messages; +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.person.ReadOnlyPerson; +import seedu.addressbook.data.person.UniquePersonList.PersonNotFoundException; +import seedu.addressbook.data.tag.UniqueTagList; +import seedu.addressbook.ui.Gui; +import seedu.addressbook.util.TestUtil; + +public class DeleteCommandTest { + + private AddressBook emptyAddressBook; + private AddressBook addressBook; + + private List emptyDisplayList; + private List listWithEveryone; + private List listWithSurnameDoe; + + @Before + public void setUp() throws Exception { + Person johnDoe = new Person(new Name("John Doe"), new Phone("61234567", false), + new Email("john@doe.com", false), new Address("395C Ben Road", false), new UniqueTagList()); + Person janeDoe = new Person(new Name("Jane Doe"), new Phone("91234567", false), + new Email("jane@doe.com", false), new Address("33G Ohm Road", false), new UniqueTagList()); + Person samDoe = new Person(new Name("Sam Doe"), new Phone("63345566", false), + new Email("sam@doe.com", false), new Address("55G Abc Road", false), new UniqueTagList()); + Person davidGrant = new Person(new Name("David Grant"), new Phone("61121122", false), + new Email("david@grant.com", false), new Address("44H Define Road", false), + new UniqueTagList()); + + emptyAddressBook = TestUtil.createAddressBook(); + addressBook = TestUtil.createAddressBook(johnDoe, janeDoe, davidGrant, samDoe); + + emptyDisplayList = TestUtil.createList(); + + listWithEveryone = TestUtil.createList(johnDoe, janeDoe, davidGrant, samDoe); + listWithSurnameDoe = TestUtil.createList(johnDoe, janeDoe, samDoe); + } + + @Test + public void execute_emptyAddressBook_returnsPersonNotFoundMessage() { + assertDeletionFailsDueToNoSuchPerson(1, emptyAddressBook, listWithEveryone); + } + + @Test + public void execute_noPersonDisplayed_returnsInvalidIndexMessage() { + assertDeletionFailsDueToInvalidIndex(1, addressBook, emptyDisplayList); + } + + @Test + public void execute_targetPersonNotInAddressBook_returnsPersonNotFoundMessage() + throws IllegalValueException { + Person notInAddressBookPerson = new Person(new Name("Not In Book"), new Phone("63331444", false), + new Email("notin@book.com", false), new Address("156D Grant Road", false), new UniqueTagList()); + List listWithPersonNotInAddressBook = TestUtil.createList(notInAddressBookPerson); + + assertDeletionFailsDueToNoSuchPerson(1, addressBook, listWithPersonNotInAddressBook); + } + + @Test + public void execute_invalidIndex_returnsInvalidIndexMessage() { + assertDeletionFailsDueToInvalidIndex(0, addressBook, listWithEveryone); + assertDeletionFailsDueToInvalidIndex(-1, addressBook, listWithEveryone); + assertDeletionFailsDueToInvalidIndex(listWithEveryone.size() + 1, addressBook, listWithEveryone); + } + + @Test + public void execute_validIndex_personIsDeleted() throws PersonNotFoundException { + assertDeletionSuccessful(1, addressBook, listWithSurnameDoe); + assertDeletionSuccessful(listWithSurnameDoe.size(), addressBook, listWithSurnameDoe); + + int middleIndex = (listWithSurnameDoe.size() / 2) + 1; + assertDeletionSuccessful(middleIndex, addressBook, listWithSurnameDoe); + } + + /** + * Creates a new delete command. + * + * @param targetVisibleIndex of the person that we want to delete + */ + private DeleteCommand createDeleteCommand(int targetVisibleIndex, AddressBook addressBook, + List displayList) { + + DeleteCommand command = new DeleteCommand(targetVisibleIndex); + command.setData(addressBook, displayList); + + return command; + } + + /** + * Executes the command, and checks that the execution was what we had expected. + */ + private void assertCommandBehaviour(DeleteCommand deleteCommand, String expectedMessage, + AddressBook expectedAddressBook, AddressBook actualAddressBook) { + + CommandResult result = deleteCommand.execute(); + + assertEquals(expectedMessage, result.feedbackToUser); + assertEquals(expectedAddressBook.getAllPersons(), actualAddressBook.getAllPersons()); + } + + /** + * Asserts that the index is not valid for the given display list. + */ + private void assertDeletionFailsDueToInvalidIndex(int invalidVisibleIndex, AddressBook addressBook, + List displayList) { + + String expectedMessage = Messages.MESSAGE_INVALID_PERSON_DISPLAYED_INDEX; + + DeleteCommand command = createDeleteCommand(invalidVisibleIndex, addressBook, displayList); + assertCommandBehaviour(command, expectedMessage, addressBook, addressBook); + } + + /** + * Asserts that the person at the specified index cannot be deleted, because that person + * is not in the address book. + */ + private void assertDeletionFailsDueToNoSuchPerson(int visibleIndex, AddressBook addressBook, + List displayList) { + + String expectedMessage = Messages.MESSAGE_PERSON_NOT_IN_ADDRESSBOOK; + + DeleteCommand command = createDeleteCommand(visibleIndex, addressBook, displayList); + assertCommandBehaviour(command, expectedMessage, addressBook, addressBook); + } + + /** + * Asserts that the person at the specified index can be successfully deleted. + * + * The addressBook passed in will not be modified (no side effects). + * + * @throws PersonNotFoundException if the selected person is not in the address book + */ + private void assertDeletionSuccessful(int targetVisibleIndex, AddressBook addressBook, + List displayList) throws PersonNotFoundException { + + ReadOnlyPerson targetPerson = displayList.get(targetVisibleIndex - Gui.DISPLAYED_INDEX_OFFSET); + + AddressBook expectedAddressBook = TestUtil.clone(addressBook); + expectedAddressBook.removePerson(targetPerson); + String expectedMessage = String.format(DeleteCommand.MESSAGE_DELETE_PERSON_SUCCESS, targetPerson); + + AddressBook actualAddressBook = TestUtil.clone(addressBook); + + DeleteCommand command = createDeleteCommand(targetVisibleIndex, actualAddressBook, displayList); + assertCommandBehaviour(command, expectedMessage, expectedAddressBook, actualAddressBook); + } +} diff --git a/test/java/seedu/addressbook/commands/FindCommandTest.java b/test/java/seedu/addressbook/commands/FindCommandTest.java new file mode 100644 index 000000000..f21b1e8e7 --- /dev/null +++ b/test/java/seedu/addressbook/commands/FindCommandTest.java @@ -0,0 +1,63 @@ +package seedu.addressbook.commands; + +import static org.junit.Assert.assertEquals; + +import java.util.Arrays; +import java.util.Collections; +import java.util.HashSet; +import java.util.List; +import java.util.Set; + +import org.junit.Test; + +import seedu.addressbook.data.AddressBook; +import seedu.addressbook.data.exception.IllegalValueException; +import seedu.addressbook.data.person.ReadOnlyPerson; +import seedu.addressbook.util.TypicalPersons; + +public class FindCommandTest { + + private final AddressBook addressBook = new TypicalPersons().getTypicalAddressBook(); + private final TypicalPersons td = new TypicalPersons(); + + @Test + public void execute() throws IllegalValueException { + //same word, same case: matched + assertFindCommandBehavior(new String[]{"Amy"}, Arrays.asList(td.amy)); + + //same word, different case: not matched + assertFindCommandBehavior(new String[]{"aMy"}, Collections.emptyList()); + + //partial word: not matched + assertFindCommandBehavior(new String[]{"my"}, Collections.emptyList()); + + //multiple words: matched + assertFindCommandBehavior(new String[]{"Amy", "Bill", "Candy", "Destiny"}, + Arrays.asList(td.amy, td.bill, td.candy)); + + //repeated keywords: matched + assertFindCommandBehavior(new String[]{"Amy", "Amy"}, Arrays.asList(td.amy)); + + //Keyword matching a word in address: not matched + assertFindCommandBehavior(new String[]{"Clementi"}, Collections.emptyList()); + } + + /** + * Executes the find command for the given keywords and verifies + * the result matches the persons in the expectedPersonList exactly. + */ + private void assertFindCommandBehavior(String[] keywords, List expectedPersonList) { + FindCommand command = createFindCommand(keywords); + CommandResult result = command.execute(); + + assertEquals(Command.getMessageForPersonListShownSummary(expectedPersonList), result.feedbackToUser); + } + + private FindCommand createFindCommand(String[] keywords) { + final Set keywordSet = new HashSet<>(Arrays.asList(keywords)); + FindCommand command = new FindCommand(keywordSet); + command.setData(addressBook, Collections.emptyList()); + return command; + } + +} diff --git a/test/java/seedu/addressbook/commands/ViewAllCommandTest.java b/test/java/seedu/addressbook/commands/ViewAllCommandTest.java new file mode 100644 index 000000000..fc1ee66fa --- /dev/null +++ b/test/java/seedu/addressbook/commands/ViewAllCommandTest.java @@ -0,0 +1,7 @@ +package seedu.addressbook.commands; + +public class ViewAllCommandTest { + // ViewAllCommand is tested together with ViewCommand in ViewCommandTest. + // This is because they function similarly but ViewCommand hides private information. + // They are tested with same test data input. +} diff --git a/test/java/seedu/addressbook/commands/ViewCommandTest.java b/test/java/seedu/addressbook/commands/ViewCommandTest.java new file mode 100644 index 000000000..3408463c1 --- /dev/null +++ b/test/java/seedu/addressbook/commands/ViewCommandTest.java @@ -0,0 +1,154 @@ +package seedu.addressbook.commands; + +import static org.junit.Assert.assertEquals; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.List; +import java.util.Optional; + +import org.junit.Test; + +import seedu.addressbook.common.Messages; +import seedu.addressbook.data.AddressBook; +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.person.ReadOnlyPerson; +import seedu.addressbook.data.tag.UniqueTagList; +import seedu.addressbook.util.TestUtil; +import seedu.addressbook.util.TypicalPersons; + +public class ViewCommandTest { + private TypicalPersons td = new TypicalPersons(); + + private AddressBook typicalAddressBook = td.getTypicalAddressBook(); + private AddressBook emptyAddressBook = TestUtil.createAddressBook(); + private List emptyPersonList = Collections.emptyList(); + private List listWithAllTypicalPersons = Arrays.asList(td.getTypicalPersons()); + private List listWithSomeTypicalPersons = Arrays.asList(td.amy, td.candy, td.dan); + + @Test + public void execute_invalidIndex_returnsInvalidIndexMessage() { + // empty addressbook + assertViewErrorInvalidIndex(emptyAddressBook, emptyPersonList, 1); + + // non-empty addressbook + assertViewErrorInvalidIndex(typicalAddressBook, listWithAllTypicalPersons, -1); + assertViewErrorInvalidIndex(typicalAddressBook, listWithAllTypicalPersons, 0); + assertViewErrorInvalidIndex(typicalAddressBook, listWithAllTypicalPersons, + listWithAllTypicalPersons.size() + 1); + } + + @Test + public void execute_personNotInAddressBook_returnsPersonNotInAddressBookMessage() throws Exception { + // generate list with person not in addressbook, add to list + ReadOnlyPerson stranger = new Person(new Name("me"), + new Phone("123", true), + new Email("some@hey.go", true), + new Address("nus", false), + new UniqueTagList(Collections.emptySet())); + List listWithExtraPerson + = new ArrayList(listWithAllTypicalPersons); + listWithExtraPerson.add(stranger); + + // empty addressbook + assertViewErrorPersonNotInAddressBook(emptyAddressBook, listWithExtraPerson, 1); + + // non-empty addressbook + assertViewErrorPersonNotInAddressBook(typicalAddressBook, listWithExtraPerson, + listWithExtraPerson.size()); + } + + @Test + public void execute_validIndex_returnsPersonDetails() { + // person with no private information + assertViewSuccess(typicalAddressBook, listWithAllTypicalPersons, 1); + + // person with some private information + assertViewSuccess(typicalAddressBook, listWithAllTypicalPersons, 2); + + // person with all private information + assertViewSuccess(typicalAddressBook, listWithAllTypicalPersons, 4); + + // Addressbook has more people than the list. + // This can happen when a command causes the list to show only a sub-set of persons(e.g. FindCommand). + assertViewSuccess(typicalAddressBook, listWithSomeTypicalPersons, 1); + } + + /** + * Asserts that the details of person at specific index cannot be retrieved due to + * invalid index. + */ + private void assertViewErrorInvalidIndex(AddressBook addressBook, List relevantPersons, + int targetVisibleIndex) { + assertViewError(addressBook, relevantPersons, targetVisibleIndex, + Messages.MESSAGE_INVALID_PERSON_DISPLAYED_INDEX); + } + + /** + * Asserts that the details of person at specific index cannot be retrieved due to + * person not existing in the addressbook. + */ + private void assertViewErrorPersonNotInAddressBook(AddressBook addressBook, List relevantPersons, + int targetVisibleIndex) { + assertViewError(addressBook, relevantPersons, targetVisibleIndex, + Messages.MESSAGE_PERSON_NOT_IN_ADDRESSBOOK); + } + + /** + * Asserts that both a ViewCommand and a ViewAllCommand can retrieve from + * the {@code addressBook} details of the person at the given {@code targetVisibleIndex} + * in the given {@code relevantPersons} list. + * + * @param targetVisibleIndex one-indexed position of the target person in the list + */ + private void assertViewSuccess(AddressBook addressBook, List relevantPersons, + int targetVisibleIndex) { + // get person to be viewed (targetVisibleIndex - 1 because index is one-indexed) + ReadOnlyPerson personToBeViewed = relevantPersons.get(targetVisibleIndex - 1); + + String expectedMessage = String.format(ViewCommand.MESSAGE_VIEW_PERSON_DETAILS, + personToBeViewed.getAsTextHidePrivate()); + assertViewBehavior(new ViewCommand(targetVisibleIndex), addressBook, relevantPersons, expectedMessage); + + expectedMessage = String.format(ViewAllCommand.MESSAGE_VIEW_PERSON_DETAILS, + personToBeViewed.getAsTextShowAll()); + assertViewBehavior(new ViewAllCommand(targetVisibleIndex), addressBook, relevantPersons, expectedMessage); + } + + /** + * Asserts that the Viewcommand and ViewAllcommand reports the given error for the given input. + */ + private static void assertViewError(AddressBook addressBook, List relevantPersons, + int targetVisibleIndex, String expectedMessage) { + assertViewBehavior(new ViewCommand(targetVisibleIndex), addressBook, relevantPersons, expectedMessage); + assertViewBehavior(new ViewAllCommand(targetVisibleIndex), addressBook, relevantPersons, expectedMessage); + } + + /** + * Executes the test command for the given addressbook data. + * Checks that ViewCommand and ViewAllCommand exhibits the correct command behavior, namely: + * 1. The feedback message of the CommandResult it returns matches expectedMessage. + * 2. The CommandResult it returns has no relevant persons. + * 3. The original addressbook data is not modified after executing ViewCommand and ViewAllCommand. + */ + private static void assertViewBehavior(Command viewCommand, AddressBook addressBook, + List relevantPersons, String expectedMessage) { + AddressBook expectedAddressBook = TestUtil.clone(addressBook); + + viewCommand.setData(addressBook, relevantPersons); + CommandResult result = viewCommand.execute(); + + // feedback message is as expected and there are no relevant persons returned. + assertEquals(expectedMessage, result.feedbackToUser); + assertEquals(Optional.empty(), result.getRelevantPersons()); + + // addressbook was not modified. + assertEquals(expectedAddressBook.getAllPersons(), addressBook.getAllPersons()); + } + +} diff --git a/test/java/seedu/addressbook/util/TestUtil.java b/test/java/seedu/addressbook/util/TestUtil.java index 18fbfba7a..a866e4cee 100644 --- a/test/java/seedu/addressbook/util/TestUtil.java +++ b/test/java/seedu/addressbook/util/TestUtil.java @@ -1,13 +1,65 @@ package seedu.addressbook.util; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.fail; + import java.io.IOException; import java.nio.charset.Charset; import java.nio.file.Files; import java.nio.file.Path; +import java.util.ArrayList; import java.util.List; +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.person.ReadOnlyPerson; +import seedu.addressbook.data.person.UniquePersonList.DuplicatePersonException; +import seedu.addressbook.data.tag.UniqueTagList; + public class TestUtil { + /** + * Creates an address book containing the given persons. + */ + public static AddressBook createAddressBook(Person... persons) { + AddressBook addressBook = new AddressBook(); + + for (Person person : persons) { + try { + addressBook.addPerson(person); + } catch (DuplicatePersonException e) { + throw new AssertionError(e); + } + } + + return addressBook; + } + + /** + * Creates a list of persons. + */ + public static List createList(Person...persons) { + List list = new ArrayList(); + + for (Person person : persons) { + list.add(person); + } + + return list; + } + + /** + * Creates a copy of the original address book with the same entries + * of Persons and Tags. The Persons and Tags are not cloned. + */ + public static AddressBook clone(AddressBook addressBook) { + return new AddressBook(addressBook.getAllPersons(), addressBook.getAllTags()); + } + /** * Asserts whether the text in the two given files are the same. Ignores any * differences in line endings @@ -17,4 +69,14 @@ public static void assertTextFilesEqual(Path path1, Path path2) throws IOExcepti List list2 = Files.readAllLines(path2, Charset.defaultCharset()); assertEquals(String.join("\n", list1), String.join("\n", list2)); } + + public static Person generateTestPerson() { + try { + return new Person(new Name(Name.EXAMPLE), new Phone(Phone.EXAMPLE, false), + new Email(Email.EXAMPLE, true), new Address(Address.EXAMPLE, false), new UniqueTagList()); + } catch (IllegalValueException e) { + fail("test person data should be valid by definition"); + return null; + } + } } diff --git a/test/java/seedu/addressbook/util/TypicalPersons.java b/test/java/seedu/addressbook/util/TypicalPersons.java new file mode 100644 index 000000000..7c03f6fa5 --- /dev/null +++ b/test/java/seedu/addressbook/util/TypicalPersons.java @@ -0,0 +1,56 @@ +package seedu.addressbook.util; + +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; + +/** + * Class to generate typical test persons + */ +public class TypicalPersons { + + public Person amy, bill, candy, dan; + + public TypicalPersons() { + try { + amy = new Person(new Name("Amy Buck"), new Phone("91119111", false), new Email("ab@gmail.com", false), + new Address("1 Clementi Road", false), new UniqueTagList()); + bill = new Person(new Name("Bill Clint"), new Phone("92229222", false), new Email("bc@gmail.com", false), + new Address("2 Clementi Road", true), new UniqueTagList()); + candy = new Person(new Name("Candy Destiny"), new Phone("93339333", true), + new Email("cd@gmail.com", false), new Address("3 Clementi Road", true), new UniqueTagList()); + dan = new Person(new Name("Dan Smith"), new Phone("1234556", true), new Email("ss@tt.com", true), + new Address("NUS", true), new UniqueTagList(new Tag("Test"))); + } catch (IllegalValueException e) { + e.printStackTrace(); + assert false : "not possible"; + } + } + + private void loadAddressBookWithSampleData(AddressBook ab) { + try { + for (Person p : this.getTypicalPersons()) { + ab.addPerson(new Person(p)); + } + } catch (IllegalValueException e) { + assert false : "not possible"; + } + } + + public Person[] getTypicalPersons() { + return new Person[]{amy, bill, candy, dan}; + } + + public AddressBook getTypicalAddressBook() { + AddressBook ab = new AddressBook(); + loadAddressBookWithSampleData(ab); + return ab; + } + +}