Skip to content

Commit

Permalink
[se-edu#105] Add unit tests for AddressBook class (se-edu#121)
Browse files Browse the repository at this point in the history
  • Loading branch information
thenaesh authored and damithc committed Jan 8, 2017
1 parent 70f60ba commit 03e0b27
Show file tree
Hide file tree
Showing 3 changed files with 339 additions and 1 deletion.
16 changes: 15 additions & 1 deletion src/seedu/addressbook/data/AddressBook.java
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,8 @@ private void syncTagsWithMasterList(Person person) {
* @throws DuplicatePersonException if an equivalent person already exists.
*/
public void addPerson(Person toAdd) throws DuplicatePersonException {
syncTagsWithMasterList(toAdd);
allPersons.add(toAdd);
syncTagsWithMasterList(toAdd);
}

/**
Expand Down Expand Up @@ -115,12 +115,26 @@ public void removePerson(ReadOnlyPerson toRemove) throws PersonNotFoundException

/**
* Removes the equivalent Tag from the address book.
* The Tag will also be removed from any Person in the address book who has that tag.
*
* @throws TagNotFoundException if no such Tag could be found.
*/
public void removeTag(Tag toRemove) throws TagNotFoundException {
removeTagFromAllPersons(toRemove);
allTags.remove(toRemove);
}

private void removeTagFromAllPersons(Tag toRemove) throws TagNotFoundException {
for (Person person : allPersons) {
UniqueTagList personTagList = person.getTags();

if (personTagList.contains(toRemove)) {
personTagList.remove(toRemove);
}

person.setTags(personTagList);
}
}

/**
* Clears all persons and tags from the address book.
Expand Down
265 changes: 265 additions & 0 deletions test/java/seedu/addressbook/data/AddressBookTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,265 @@
package seedu.addressbook.data;

import static org.junit.Assert.*;

import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;

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.UniquePersonList;
import seedu.addressbook.data.person.UniquePersonList.DuplicatePersonException;
import seedu.addressbook.data.person.UniquePersonList.PersonNotFoundException;
import seedu.addressbook.data.tag.Tag;
import seedu.addressbook.data.tag.UniqueTagList;
import seedu.addressbook.data.tag.UniqueTagList.DuplicateTagException;
import seedu.addressbook.data.tag.UniqueTagList.TagNotFoundException;

import static seedu.addressbook.util.TestUtil.isEmpty;
import static seedu.addressbook.util.TestUtil.isIdentical;
import static seedu.addressbook.util.TestUtil.getSize;

public class AddressBookTest {
private Tag tagPrizeWinner;
private Tag tagScientist;
private Tag tagMathematician;
private Tag tagEconomist;

private Person aliceBetsy;
private Person bobChaplin;
private Person charlieDouglas;
private Person davidElliot;

private AddressBook defaultAddressBook;
private AddressBook emptyAddressBook;


@Before
public void setUp() throws Exception {
tagPrizeWinner = new Tag("prizewinner");
tagScientist = new Tag("scientist");
tagMathematician = new Tag("mathematician");
tagEconomist = new Tag("economist");

aliceBetsy = new Person(new Name("Alice Betsy"),
new Phone("91235468", false),
new Email("alice@nushackers.org", false),
new Address("8 Computing Drive, Singapore", false),
new UniqueTagList(tagMathematician));

bobChaplin = new Person(new Name("Bob Chaplin"),
new Phone("94321500", false),
new Email("bob@nusgreyhats.org", false),
new Address("9 Computing Drive", false),
new UniqueTagList(tagMathematician));

charlieDouglas = new Person(new Name("Charlie Douglas"),
new Phone("98751365", false),
new Email("charlie@nusgdg.org", false),
new Address("10 Science Drive", false),
new UniqueTagList(tagScientist));

davidElliot = new Person(new Name("David Elliot"),
new Phone("84512575", false),
new Email("douglas@nuscomputing.com", false),
new Address("11 Arts Link", false),
new UniqueTagList(tagEconomist, tagPrizeWinner));

emptyAddressBook = new AddressBook();
defaultAddressBook = new AddressBook(new UniquePersonList(aliceBetsy, bobChaplin),
new UniqueTagList(tagMathematician, tagScientist));
}

@Rule
public ExpectedException thrown = ExpectedException.none();

@Test
public void addPerson_emptyAddressBook() throws Exception {
emptyAddressBook.addPerson(bobChaplin);
emptyAddressBook.addPerson(charlieDouglas);

UniqueTagList expectedTagList = new UniqueTagList(tagMathematician, tagScientist);
assertTrue(isIdentical(expectedTagList, emptyAddressBook.getAllTags()));

assertTrue(isTagObjectInAddressBookList(tagMathematician, emptyAddressBook));
assertTrue(isTagObjectInAddressBookList(tagScientist, emptyAddressBook));

}

@Test
public void addPerson_someTagsNotInTagList() throws Exception {
assertFalse(defaultAddressBook.containsTag(tagEconomist));
assertFalse(defaultAddressBook.containsTag(tagPrizeWinner));
defaultAddressBook.addPerson(davidElliot);
assertTrue(defaultAddressBook.containsTag(tagEconomist));
assertTrue(defaultAddressBook.containsTag(tagPrizeWinner));

assertTrue(isTagObjectInAddressBookList(tagEconomist, defaultAddressBook));
assertTrue(isTagObjectInAddressBookList(tagPrizeWinner, defaultAddressBook));
}

@Test
public void addPerson_personAlreadyInList_throwsDuplicatePersonException() throws Exception {
thrown.expect(DuplicatePersonException.class);
defaultAddressBook.addPerson(aliceBetsy);
}

@Test
public void addPerson_personAlreadyInListButHasTagNotInList_tagNotAdded() throws Exception {
aliceBetsy.setTags(new UniqueTagList(tagMathematician, tagPrizeWinner));

try {
defaultAddressBook.addPerson(aliceBetsy);
} catch (DuplicatePersonException e) {
// ignore expected exception
}

assertFalse(defaultAddressBook.containsTag(tagPrizeWinner));
}

@Test
public void addTag_tagNotInList_addsNormally() throws Exception {
assertFalse(defaultAddressBook.containsTag(tagEconomist));
defaultAddressBook.addTag(tagEconomist);

UniqueTagList expectedTagsAfterAddition = new UniqueTagList(tagMathematician, tagScientist, tagEconomist);
assertTrue(isIdentical(expectedTagsAfterAddition, defaultAddressBook.getAllTags()));
}

@Test
public void addTag_tagAlreadyInList_throwsDuplicateTagException() throws Exception {
thrown.expect(DuplicateTagException.class);
defaultAddressBook.addTag(tagMathematician);
}

@Test
public void containsPerson() throws Exception {
UniquePersonList personsWhoShouldBeIn = new UniquePersonList(aliceBetsy, bobChaplin);
UniquePersonList personsWhoShouldNotBeIn = new UniquePersonList(charlieDouglas, davidElliot);

for (Person personWhoShouldBeIn : personsWhoShouldBeIn) {
assertTrue(defaultAddressBook.containsPerson(personWhoShouldBeIn));
}
for (Person personWhoShouldNotBeIn : personsWhoShouldNotBeIn) {
assertFalse(defaultAddressBook.containsPerson(personWhoShouldNotBeIn));
}

UniquePersonList allPersons = new UniquePersonList(aliceBetsy, bobChaplin, charlieDouglas, davidElliot);

for (Person person : allPersons) {
assertFalse(emptyAddressBook.containsPerson(person));
}
}

@Test
public void containsTag() throws Exception {
UniqueTagList tagsWhichShouldBeIn = new UniqueTagList(tagMathematician, tagScientist);
UniqueTagList tagsWHichShouldNotBeIn = new UniqueTagList(tagEconomist, tagPrizeWinner);

for (Tag tagWhichShouldBeIn : tagsWhichShouldBeIn) {
assertTrue(defaultAddressBook.containsTag(tagWhichShouldBeIn));
}
for (Tag tagWhichShouldNotBeIn : tagsWHichShouldNotBeIn) {
assertFalse(defaultAddressBook.containsTag(tagWhichShouldNotBeIn));
}

UniqueTagList allTags = new UniqueTagList(tagPrizeWinner, tagScientist, tagMathematician, tagEconomist);

for (Tag tag : allTags) {
assertFalse(emptyAddressBook.containsTag(tag));
}
}

@Test
public void removePerson_personExists_removesNormally() throws Exception {
int numberOfPersonsBeforeRemoval = getSize(defaultAddressBook.getAllPersons());
defaultAddressBook.removePerson(aliceBetsy);

assertFalse(defaultAddressBook.containsPerson(aliceBetsy));

int numberOfPersonsAfterRemoval = getSize(defaultAddressBook.getAllPersons());
assertTrue(numberOfPersonsAfterRemoval == numberOfPersonsBeforeRemoval - 1);

}

@Test
public void removePerson_personNotExists_throwsPersonNotFoundException() throws Exception {
thrown.expect(PersonNotFoundException.class);
defaultAddressBook.removePerson(charlieDouglas);
}

@Test
public void removeTag_tagExistsAndUnused_removesNormally() throws Exception {
int numberOfTagsBeforeRemoval = getSize(defaultAddressBook.getAllTags());
defaultAddressBook.removeTag(tagScientist);

assertFalse(defaultAddressBook.containsTag(tagScientist));

int numberOfTagsAfterRemoval = getSize(defaultAddressBook.getAllTags());
assertTrue(numberOfTagsAfterRemoval == numberOfTagsBeforeRemoval - 1);

}

@Test
public void removeTag_tagExistsAndUsed_removesUsedTagFromPersons() throws Exception {
aliceBetsy.setTags(new UniqueTagList(tagMathematician, tagPrizeWinner));
bobChaplin.setTags(new UniqueTagList(tagEconomist));

int numberOfTagsBeforeRemoval = getSize(defaultAddressBook.getAllTags());
defaultAddressBook.removeTag(tagMathematician);

assertTrue(isIdentical(aliceBetsy.getTags(), new UniqueTagList(tagPrizeWinner)));
assertTrue(isIdentical(bobChaplin.getTags(), new UniqueTagList(tagEconomist)));
assertFalse(defaultAddressBook.containsTag(tagMathematician));

int numberOfTagsAfterRemoval = getSize(defaultAddressBook.getAllTags());
assertTrue(numberOfTagsAfterRemoval == numberOfTagsBeforeRemoval - 1);
}

@Test
public void removeTag_tagNotExists_throwsTagNotFoundException() throws Exception {
thrown.expect(TagNotFoundException.class);
defaultAddressBook.removeTag(tagEconomist);
}

@Test
public void clear() throws Exception {
defaultAddressBook.clear();

assertTrue(isEmpty(defaultAddressBook.getAllPersons()));
assertTrue(isEmpty(defaultAddressBook.getAllTags()));
}

@Test
public void getAllPersons() throws Exception {
UniquePersonList allPersons = defaultAddressBook.getAllPersons();
UniquePersonList personsToCheck = new UniquePersonList(aliceBetsy, bobChaplin);

assertTrue(isIdentical(allPersons, personsToCheck));
}

@Test
public void getAllTags() throws Exception {
UniqueTagList allTags = defaultAddressBook.getAllTags();
UniqueTagList tagsToCheck = new UniqueTagList(tagMathematician, tagScientist);

assertTrue(isIdentical(allTags, tagsToCheck));
}

/**
* Returns true if the given Tag object is found in the tag list of the given AddressBook.
*/
private boolean isTagObjectInAddressBookList(Tag tagToCheck, AddressBook addressBook) {
for (Tag tag : addressBook.getAllTags()) {
if (tag == tagToCheck) {
return true;
}
}
return false;
}
}
59 changes: 59 additions & 0 deletions test/java/seedu/addressbook/util/TestUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import seedu.addressbook.data.AddressBook;
import seedu.addressbook.data.exception.IllegalValueException;
Expand All @@ -18,7 +21,9 @@
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.data.person.UniquePersonList.DuplicatePersonException;
import seedu.addressbook.data.tag.Tag;
import seedu.addressbook.data.tag.UniqueTagList;

public class TestUtil {
Expand Down Expand Up @@ -56,6 +61,48 @@ public static List<ReadOnlyPerson> createList(Person...persons) {
public static AddressBook clone(AddressBook addressBook) {
return new AddressBook(addressBook.getAllPersons(), addressBook.getAllTags());
}

/**
* Returns true if every pair of corresponding elements two iterables are (deeply) identical.
* In other words, the two containers must have the same elements, in the same order.
*/
public static <T> boolean isIdentical(Iterable<T> firstIterable, Iterable<T> secondIterable) {
Iterator<T> currentPtr0 = firstIterable.iterator();
Iterator<T> currentPtr1 = secondIterable.iterator();

while (currentPtr0.hasNext() && currentPtr1.hasNext()) {
T val0 = currentPtr0.next();
T val1 = currentPtr1.next();

if (!val0.equals(val1)) {
return false;
}
}

// If any of the two iterables still have elements, then they have different sizes.
return !(currentPtr0.hasNext() || currentPtr1.hasNext());
}

/**
* Returns true if the underlying container behind an iterable is empty.
*/
public static <T> boolean isEmpty(Iterable<T> it) {
return !it.iterator().hasNext();
}

/**
* Returns the number of elements in the container behind an iterable.
*/
public static <T> int getSize(Iterable<T> it) {
int numberOfElementsSeen = 0;

for (T elem : it) {
numberOfElementsSeen++;
}

return numberOfElementsSeen;
}

public static Person generateTestPerson() {
try {
return new Person(new Name(Name.EXAMPLE), new Phone(Phone.EXAMPLE, false),
Expand All @@ -65,6 +112,18 @@ public static Person generateTestPerson() {
return null;
}
}

public static UniqueTagList getAllTags(UniquePersonList persons) {
Set<Tag> combinedTagList = new HashSet<Tag>();

for (Person person : persons) {
for (Tag tag : person.getTags()) {
combinedTagList.add(tag);
}
}

return new UniqueTagList(combinedTagList);
}

/**
* Asserts whether the text in the two given files are the same. Ignores any
Expand Down

0 comments on commit 03e0b27

Please sign in to comment.