From 560a61aa880f9fcfb497e4095acb320fd16548b0 Mon Sep 17 00:00:00 2001 From: Paul Tan Date: Wed, 8 Aug 2018 16:19:10 +0800 Subject: [PATCH] UniquePersonListTest: add some more tests More test coverage is always nice. In particular, we want to check that UniquePersonList's methods throw DuplicatePersonException and PersonNotFoundException where relevant. These cases were not tested by existing test code. --- .../model/person/UniquePersonListTest.java | 169 +++++++++++++++++- 1 file changed, 168 insertions(+), 1 deletion(-) diff --git a/src/test/java/seedu/address/model/person/UniquePersonListTest.java b/src/test/java/seedu/address/model/person/UniquePersonListTest.java index 71885bd430d2..84c670c736a1 100644 --- a/src/test/java/seedu/address/model/person/UniquePersonListTest.java +++ b/src/test/java/seedu/address/model/person/UniquePersonListTest.java @@ -1,16 +1,183 @@ package seedu.address.model.person; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; +import static seedu.address.logic.commands.CommandTestUtil.VALID_ADDRESS_BOB; +import static seedu.address.logic.commands.CommandTestUtil.VALID_TAG_HUSBAND; +import static seedu.address.testutil.TypicalPersons.ALICE; +import static seedu.address.testutil.TypicalPersons.BOB; + +import java.util.Arrays; +import java.util.List; + import org.junit.Rule; import org.junit.Test; import org.junit.rules.ExpectedException; +import seedu.address.model.person.exceptions.DuplicatePersonException; +import seedu.address.model.person.exceptions.PersonNotFoundException; +import seedu.address.testutil.PersonBuilder; + public class UniquePersonListTest { @Rule public ExpectedException thrown = ExpectedException.none(); + private final UniquePersonList uniquePersonList = new UniquePersonList(); + + @Test + public void contains_nullPerson_throwsNullPointerException() { + thrown.expect(NullPointerException.class); + uniquePersonList.contains(null); + } + + @Test + public void contains_personNotInList_returnsFalse() { + assertFalse(uniquePersonList.contains(ALICE)); + } + + @Test + public void contains_personInList_returnsTrue() { + uniquePersonList.add(ALICE); + assertTrue(uniquePersonList.contains(ALICE)); + } + + @Test + public void contains_personWithSameIdentityFieldsInList_returnsTrue() { + uniquePersonList.add(ALICE); + Person editedAlice = new PersonBuilder(ALICE).withAddress(VALID_ADDRESS_BOB).withTags(VALID_TAG_HUSBAND) + .build(); + assertTrue(uniquePersonList.contains(editedAlice)); + } + + @Test + public void add_nullPerson_throwsNullPointerException() { + thrown.expect(NullPointerException.class); + uniquePersonList.add(null); + } + + @Test + public void add_duplicatePerson_throwsDuplicatePersonException() { + uniquePersonList.add(ALICE); + thrown.expect(DuplicatePersonException.class); + uniquePersonList.add(ALICE); + } + + @Test + public void setPerson_nullTargetPerson_throwsNullPointerException() { + thrown.expect(NullPointerException.class); + uniquePersonList.setPerson(null, ALICE); + } + + @Test + public void setPerson_nullEditedPerson_throwsNullPointerException() { + thrown.expect(NullPointerException.class); + uniquePersonList.setPerson(ALICE, null); + } + + @Test + public void setPerson_targetPersonNotInList_throwsPersonNotFoundException() { + thrown.expect(PersonNotFoundException.class); + uniquePersonList.setPerson(ALICE, ALICE); + } + + @Test + public void setPerson_editedPersonIsSamePerson_success() { + uniquePersonList.add(ALICE); + uniquePersonList.setPerson(ALICE, ALICE); + UniquePersonList expectedUniquePersonList = new UniquePersonList(); + expectedUniquePersonList.add(ALICE); + assertEquals(expectedUniquePersonList, uniquePersonList); + } + + @Test + public void setPerson_editedPersonHasSameIdentity_success() { + uniquePersonList.add(ALICE); + Person editedAlice = new PersonBuilder(ALICE).withAddress(VALID_ADDRESS_BOB).withTags(VALID_TAG_HUSBAND) + .build(); + uniquePersonList.setPerson(ALICE, editedAlice); + UniquePersonList expectedUniquePersonList = new UniquePersonList(); + expectedUniquePersonList.add(editedAlice); + assertEquals(expectedUniquePersonList, uniquePersonList); + } + + @Test + public void setPerson_editedPersonHasDifferentIdentity_success() { + uniquePersonList.add(ALICE); + uniquePersonList.setPerson(ALICE, BOB); + UniquePersonList expectedUniquePersonList = new UniquePersonList(); + expectedUniquePersonList.add(BOB); + assertEquals(expectedUniquePersonList, uniquePersonList); + } + + @Test + public void setPerson_editedPersonHasNonUniqueIdentity_throwsDuplicatePersonException() { + uniquePersonList.add(ALICE); + uniquePersonList.add(BOB); + thrown.expect(DuplicatePersonException.class); + uniquePersonList.setPerson(ALICE, BOB); + } + + @Test + public void remove_nullPerson_throwsNullPointerException() { + thrown.expect(NullPointerException.class); + uniquePersonList.remove(null); + } + + @Test + public void remove_personDoesNotExist_throwsPersonNotFoundException() { + thrown.expect(PersonNotFoundException.class); + uniquePersonList.remove(ALICE); + } + + @Test + public void remove_existingPerson_removesPerson() { + uniquePersonList.add(ALICE); + uniquePersonList.remove(ALICE); + UniquePersonList expectedUniquePersonList = new UniquePersonList(); + assertEquals(expectedUniquePersonList, uniquePersonList); + } + + @Test + public void setPersons_nullUniquePersonList_throwsNullPointerException() { + thrown.expect(NullPointerException.class); + uniquePersonList.setPersons((UniquePersonList) null); + } + + @Test + public void setPersons_uniquePersonList_replacesOwnListWithProvidedUniquePersonList() { + uniquePersonList.add(ALICE); + UniquePersonList expectedUniquePersonList = new UniquePersonList(); + expectedUniquePersonList.add(BOB); + uniquePersonList.setPersons(expectedUniquePersonList); + assertEquals(expectedUniquePersonList, uniquePersonList); + } + + @Test + public void setPersons_nullList_throwsNullPointerException() { + thrown.expect(NullPointerException.class); + uniquePersonList.setPersons((List) null); + } + + @Test + public void setPersons_list_replacesOwnListWithProvidedList() { + uniquePersonList.add(ALICE); + List personList = Arrays.asList(BOB); + uniquePersonList.setPersons(personList); + UniquePersonList expectedUniquePersonList = new UniquePersonList(); + expectedUniquePersonList.add(BOB); + assertEquals(expectedUniquePersonList, uniquePersonList); + } + + @Test + public void setPersons_listWithDuplicatePersons_throwsDuplicatePersonException() { + List listWithDuplicatePersons = Arrays.asList(ALICE, ALICE); + thrown.expect(DuplicatePersonException.class); + uniquePersonList.setPersons(listWithDuplicatePersons); + } + @Test public void asUnmodifiableObservableList_modifyList_throwsUnsupportedOperationException() { - UniquePersonList uniquePersonList = new UniquePersonList(); thrown.expect(UnsupportedOperationException.class); uniquePersonList.asUnmodifiableObservableList().remove(0); }