Skip to content

Commit

Permalink
[se-edu#569] TypicalPersons: Make all methods and fields static (se-e…
Browse files Browse the repository at this point in the history
…du#574)

TypicalPersons is a utility class that creates preset Persons for tests
to use. Some methods and fields are non-static.

This makes it troublesome for test classes to access these preset
Persons as they have to create a TypicalPersons object prior to
accessing the Person objects.

Let's make all methods and fields in TypicalPersons to be static.
  • Loading branch information
yamgent authored Jul 20, 2017
2 parents 48d5acc + d975164 commit e3bdbae
Show file tree
Hide file tree
Showing 25 changed files with 109 additions and 101 deletions.
21 changes: 13 additions & 8 deletions src/test/java/guitests/AddCommandTest.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package guitests;

import static org.junit.Assert.assertTrue;
import static seedu.address.testutil.TypicalPersons.ALICE;
import static seedu.address.testutil.TypicalPersons.HOON;
import static seedu.address.testutil.TypicalPersons.IDA;
import static seedu.address.testutil.TypicalPersons.TYPICAL_PERSONS;

import java.util.ArrayList;
import java.util.Arrays;
Expand All @@ -12,41 +16,42 @@
import seedu.address.logic.commands.AddCommand;
import seedu.address.logic.commands.ClearCommand;
import seedu.address.model.person.Person;
import seedu.address.model.person.ReadOnlyPerson;
import seedu.address.testutil.PersonUtil;

public class AddCommandTest extends AddressBookGuiTest {

@Test
public void add() throws Exception {
//add one person
ArrayList<Person> expectedList = new ArrayList<>(Arrays.asList(td.getTypicalPersons()));
Person personToAdd = td.hoon;
ArrayList<ReadOnlyPerson> expectedList = new ArrayList<>(Arrays.asList(TYPICAL_PERSONS));
ReadOnlyPerson personToAdd = HOON;
expectedList.add(personToAdd);
assertAddSuccess(personToAdd, expectedList);

//add another person
personToAdd = td.ida;
personToAdd = IDA;
expectedList.add(personToAdd);
assertAddSuccess(personToAdd, expectedList);

//add duplicate person
runCommand(PersonUtil.getAddCommand(td.hoon));
runCommand(PersonUtil.getAddCommand(HOON));
assertResultMessage(AddCommand.MESSAGE_DUPLICATE_PERSON);
assertTrue(getPersonListPanel().isListMatching(expectedList.toArray(new Person[expectedList.size()])));

//add to empty list
runCommand(ClearCommand.COMMAND_WORD);
expectedList.clear();
personToAdd = td.alice;
personToAdd = ALICE;
expectedList.add(personToAdd);
assertAddSuccess(td.alice, expectedList);
assertAddSuccess(ALICE, expectedList);

//invalid command
runCommand("adds Johnny");
assertResultMessage(Messages.MESSAGE_UNKNOWN_COMMAND);
}

private void assertAddSuccess(Person personToAdd, ArrayList<Person> expectedList) throws Exception {
private void assertAddSuccess(ReadOnlyPerson personToAdd, ArrayList<ReadOnlyPerson> expectedList) throws Exception {
runCommand(PersonUtil.getAddCommand(personToAdd));

//confirm the new card contains the right data
Expand All @@ -55,7 +60,7 @@ private void assertAddSuccess(Person personToAdd, ArrayList<Person> expectedList
assertCardMatchesPerson(addedCard, personToAdd);

//confirm the list now contains all previous persons plus the new person
assertTrue(getPersonListPanel().isListMatching(expectedList.toArray(new Person[expectedList.size()])));
assertTrue(getPersonListPanel().isListMatching(expectedList.toArray(new ReadOnlyPerson[expectedList.size()])));
}

}
1 change: 0 additions & 1 deletion src/test/java/guitests/AddressBookGuiTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ public abstract class AddressBookGuiTest {
@Rule
public TestName name = new TestName();

protected TypicalPersons td = new TypicalPersons();
protected GuiRobot guiRobot = new GuiRobot();

protected Stage stage;
Expand Down
8 changes: 5 additions & 3 deletions src/test/java/guitests/ClearCommandTest.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package guitests;

import static org.junit.Assert.assertTrue;
import static seedu.address.testutil.TypicalPersons.HOON;
import static seedu.address.testutil.TypicalPersons.TYPICAL_PERSONS;

import org.junit.Test;

Expand All @@ -14,12 +16,12 @@ public class ClearCommandTest extends AddressBookGuiTest {
public void clear() throws Exception {

//verify a non-empty list can be cleared
assertTrue(getPersonListPanel().isListMatching(td.getTypicalPersons()));
assertTrue(getPersonListPanel().isListMatching(TYPICAL_PERSONS));
assertClearCommandSuccess();

//verify other commands can work after a clear command
runCommand(PersonUtil.getAddCommand(td.hoon));
assertTrue(getPersonListPanel().isListMatching(td.hoon));
runCommand(PersonUtil.getAddCommand(HOON));
assertTrue(getPersonListPanel().isListMatching(HOON));
runCommand(DeleteCommand.COMMAND_WORD + " 1");
assertListSize(0);

Expand Down
8 changes: 4 additions & 4 deletions src/test/java/guitests/DeleteCommandTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import static org.junit.Assert.assertTrue;
import static seedu.address.logic.commands.DeleteCommand.MESSAGE_DELETE_PERSON_SUCCESS;
import static seedu.address.testutil.TypicalPersons.INDEX_FIRST_PERSON;
import static seedu.address.testutil.TypicalPersons.TYPICAL_PERSONS;

import java.util.ArrayList;
import java.util.Arrays;
Expand All @@ -12,7 +13,6 @@

import seedu.address.commons.core.index.Index;
import seedu.address.logic.commands.DeleteCommand;
import seedu.address.model.person.Person;
import seedu.address.model.person.ReadOnlyPerson;

public class DeleteCommandTest extends AddressBookGuiTest {
Expand All @@ -21,7 +21,7 @@ public class DeleteCommandTest extends AddressBookGuiTest {
public void delete() throws Exception {

//delete the first in the list
ArrayList<Person> expectedList = new ArrayList<>(Arrays.asList(td.getTypicalPersons()));
ArrayList<ReadOnlyPerson> expectedList = new ArrayList<>(Arrays.asList(TYPICAL_PERSONS));
Index targetIndex = INDEX_FIRST_PERSON;
expectedList.remove(targetIndex.getZeroBased());
assertDeleteSuccess(targetIndex, expectedList);
Expand All @@ -46,13 +46,13 @@ public void delete() throws Exception {
* Runs the delete command to delete the person at {@code index} and confirms resulting list equals to
* {@code expectedList} and that the displayed result message is correct.
*/
private void assertDeleteSuccess(Index index, final List<Person> expectedList) throws Exception {
private void assertDeleteSuccess(Index index, final List<ReadOnlyPerson> expectedList) throws Exception {
ReadOnlyPerson personToDelete = getPersonListPanel().getCard(index.getZeroBased()).person;

runCommand(DeleteCommand.COMMAND_WORD + " " + index.getOneBased());

//confirm the list now contains all previous persons except the deleted person
assertTrue(getPersonListPanel().isListMatching(expectedList.toArray(new Person[expectedList.size()])));
assertTrue(getPersonListPanel().isListMatching(expectedList.toArray(new ReadOnlyPerson[expectedList.size()])));

//confirm the result message is correct
assertResultMessage(String.format(MESSAGE_DELETE_PERSON_SUCCESS, personToDelete));
Expand Down
16 changes: 7 additions & 9 deletions src/test/java/guitests/EditCommandTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import static seedu.address.testutil.TypicalPersons.INDEX_FIRST_PERSON;
import static seedu.address.testutil.TypicalPersons.INDEX_SECOND_PERSON;
import static seedu.address.testutil.TypicalPersons.INDEX_THIRD_PERSON;
import static seedu.address.testutil.TypicalPersons.TYPICAL_PERSONS;

import org.junit.Test;

Expand All @@ -23,16 +24,13 @@
import seedu.address.model.person.Name;
import seedu.address.model.person.Person;
import seedu.address.model.person.Phone;
import seedu.address.model.person.ReadOnlyPerson;
import seedu.address.model.tag.Tag;
import seedu.address.testutil.PersonBuilder;

// TODO: reduce GUI tests by transferring some tests to be covered by lower level tests.
public class EditCommandTest extends AddressBookGuiTest {

// The list of persons in the person list panel is expected to match this list.
// This list is updated with every successful call to assertEditSuccess().
private Person[] expectedPersonsList = td.getTypicalPersons();

@Test
public void edit_allFieldsSpecified_success() throws Exception {
String detailsToEdit = PREFIX_NAME + "Bobby " + PREFIX_PHONE + "91234567 "
Expand All @@ -53,7 +51,7 @@ public void edit_notAllFieldsSpecified_success() throws Exception {
+ PREFIX_TAG + "bestie";
Index addressBookIndex = INDEX_SECOND_PERSON;

Person personToEdit = expectedPersonsList[addressBookIndex.getZeroBased()];
ReadOnlyPerson personToEdit = TYPICAL_PERSONS[addressBookIndex.getZeroBased()];
Person editedPerson = new PersonBuilder(personToEdit).withTags("sweetie", "bestie").build();

assertEditSuccess(addressBookIndex, addressBookIndex, detailsToEdit, editedPerson);
Expand All @@ -64,7 +62,7 @@ public void edit_clearTags_success() throws Exception {
String detailsToEdit = PREFIX_TAG.getPrefix();
Index addressBookIndex = INDEX_SECOND_PERSON;

Person personToEdit = expectedPersonsList[addressBookIndex.getZeroBased()];
ReadOnlyPerson personToEdit = TYPICAL_PERSONS[addressBookIndex.getZeroBased()];
Person editedPerson = new PersonBuilder(personToEdit).withTags().build();

assertEditSuccess(addressBookIndex, addressBookIndex, detailsToEdit, editedPerson);
Expand All @@ -77,7 +75,7 @@ public void edit_findThenEdit_success() throws Exception {
String detailsToEdit = PREFIX_NAME + "Carrle";
Index addressBookIndex = INDEX_THIRD_PERSON;

Person personToEdit = expectedPersonsList[addressBookIndex.getZeroBased()];
ReadOnlyPerson personToEdit = TYPICAL_PERSONS[addressBookIndex.getZeroBased()];
Person editedPerson = new PersonBuilder(personToEdit).withName("Carrle").build();

assertEditSuccess(INDEX_FIRST_PERSON, addressBookIndex, detailsToEdit, editedPerson);
Expand Down Expand Up @@ -150,8 +148,8 @@ private void assertEditSuccess(Index filteredPersonListIndex, Index addressBookI
assertCardMatchesPerson(editedCard, editedPerson);

// confirm the list now contains all previous persons plus the person with updated details
expectedPersonsList[addressBookIndex.getZeroBased()] = editedPerson;
assertTrue(getPersonListPanel().isListMatching(expectedPersonsList));
TYPICAL_PERSONS[addressBookIndex.getZeroBased()] = editedPerson;
assertTrue(getPersonListPanel().isListMatching(TYPICAL_PERSONS));
assertResultMessage(String.format(EditCommand.MESSAGE_EDIT_PERSON_SUCCESS, editedPerson));
}
}
10 changes: 6 additions & 4 deletions src/test/java/guitests/FindCommandTest.java
Original file line number Diff line number Diff line change
@@ -1,25 +1,27 @@
package guitests;

import static org.junit.Assert.assertTrue;
import static seedu.address.testutil.TypicalPersons.BENSON;
import static seedu.address.testutil.TypicalPersons.DANIEL;

import org.junit.Test;

import seedu.address.commons.core.Messages;
import seedu.address.logic.commands.ClearCommand;
import seedu.address.logic.commands.DeleteCommand;
import seedu.address.logic.commands.FindCommand;
import seedu.address.model.person.Person;
import seedu.address.model.person.ReadOnlyPerson;

public class FindCommandTest extends AddressBookGuiTest {

@Test
public void find_nonEmptyList() throws Exception {
assertFindResult(FindCommand.COMMAND_WORD + " Mark"); // no results
assertFindResult(FindCommand.COMMAND_WORD + " Meier", td.benson, td.daniel); // multiple results
assertFindResult(FindCommand.COMMAND_WORD + " Meier", BENSON, DANIEL); // multiple results

//find after deleting one result
runCommand(DeleteCommand.COMMAND_WORD + " 1");
assertFindResult(FindCommand.COMMAND_WORD + " Meier", td.daniel);
assertFindResult(FindCommand.COMMAND_WORD + " Meier", DANIEL);
}

@Test
Expand All @@ -34,7 +36,7 @@ public void find_invalidCommand_fail() {
assertResultMessage(Messages.MESSAGE_UNKNOWN_COMMAND);
}

private void assertFindResult(String command, Person... expectedHits) throws Exception {
private void assertFindResult(String command, ReadOnlyPerson... expectedHits) throws Exception {
runCommand(command);
assertListSize(expectedHits.length);
assertResultMessage(expectedHits.length + " persons listed!");
Expand Down
3 changes: 2 additions & 1 deletion src/test/java/guitests/SelectCommandTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static seedu.address.testutil.TypicalPersons.INDEX_FIRST_PERSON;
import static seedu.address.testutil.TypicalPersons.TYPICAL_PERSONS;

import org.junit.Test;

Expand All @@ -21,7 +22,7 @@ public void selectPerson_nonEmptyList() {
assertNoCardSelected();

assertSelectionSuccess(INDEX_FIRST_PERSON); // first person in the list
Index personCount = Index.fromOneBased(td.getTypicalPersons().length);
Index personCount = Index.fromOneBased(TYPICAL_PERSONS.length);
assertSelectionSuccess(personCount); // last person in the list
Index middleIndex = Index.fromOneBased(personCount.getOneBased() / 2);
assertSelectionSuccess(middleIndex); // a person in the middle of the list
Expand Down
3 changes: 2 additions & 1 deletion src/test/java/guitests/StatusBarFooterTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

import static seedu.address.testutil.TypicalPersons.HOON;
import static seedu.address.ui.StatusBarFooter.SYNC_STATUS_INITIAL;
import static seedu.address.ui.StatusBarFooter.SYNC_STATUS_UPDATED;

Expand Down Expand Up @@ -49,7 +50,7 @@ public void syncStatus_initialValue() {
public void syncStatus_mutatingCommandSucceeds_syncStatusUpdated() {
String timestamp = new Date(injectedClock.millis()).toString();
String expected = String.format(SYNC_STATUS_UPDATED, timestamp);
assertTrue(runCommand(PersonUtil.getAddCommand(td.hoon))); // mutating command succeeds
assertTrue(runCommand(PersonUtil.getAddCommand(HOON))); // mutating command succeeds
assertEquals(expected, getStatusBarFooter().getSyncStatus());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import static seedu.address.logic.commands.CommandTestUtil.assertCommandFailure;
import static seedu.address.logic.commands.CommandTestUtil.assertCommandSuccess;
import static seedu.address.testutil.TypicalPersons.getTypicalAddressBook;

import org.junit.Before;
import org.junit.Test;
Expand All @@ -12,7 +13,6 @@
import seedu.address.model.UserPrefs;
import seedu.address.model.person.Person;
import seedu.address.testutil.PersonBuilder;
import seedu.address.testutil.TypicalPersons;

/**
* Contains integration tests (interaction with the Model) for {@code AddCommand}.
Expand All @@ -23,7 +23,7 @@ public class AddCommandIntegrationTest {

@Before
public void setUp() {
model = new ModelManager(new TypicalPersons().getTypicalAddressBook(), new UserPrefs());
model = new ModelManager(getTypicalAddressBook(), new UserPrefs());
}

@Test
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package seedu.address.logic.commands;

import static seedu.address.logic.commands.CommandTestUtil.assertCommandSuccess;
import static seedu.address.testutil.TypicalPersons.getTypicalAddressBook;

import org.junit.Test;

import seedu.address.logic.CommandHistory;
import seedu.address.model.Model;
import seedu.address.model.ModelManager;
import seedu.address.model.UserPrefs;
import seedu.address.testutil.TypicalPersons;

public class ClearCommandTest {

Expand All @@ -20,7 +20,7 @@ public void execute_emptyAddressBook_success() throws Exception {

@Test
public void execute_nonEmptyAddressBook_success() throws Exception {
Model model = new ModelManager(new TypicalPersons().getTypicalAddressBook(), new UserPrefs());
Model model = new ModelManager(getTypicalAddressBook(), new UserPrefs());
assertCommandSuccess(prepareCommand(model), model, ClearCommand.MESSAGE_SUCCESS, model);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import static seedu.address.logic.commands.CommandTestUtil.showFirstPersonOnly;
import static seedu.address.testutil.TypicalPersons.INDEX_FIRST_PERSON;
import static seedu.address.testutil.TypicalPersons.INDEX_SECOND_PERSON;
import static seedu.address.testutil.TypicalPersons.getTypicalAddressBook;

import org.junit.Test;

Expand All @@ -17,14 +18,13 @@
import seedu.address.model.ModelManager;
import seedu.address.model.UserPrefs;
import seedu.address.model.person.ReadOnlyPerson;
import seedu.address.testutil.TypicalPersons;

/**
* Contains integration tests (interaction with the Model) and unit tests for {@code DeleteCommand}.
*/
public class DeleteCommandTest {

private Model model = new ModelManager(new TypicalPersons().getTypicalAddressBook(), new UserPrefs());
private Model model = new ModelManager(getTypicalAddressBook(), new UserPrefs());

@Test
public void execute_validIndexUnfilteredList_success() throws Exception {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import static seedu.address.logic.commands.CommandTestUtil.showFirstPersonOnly;
import static seedu.address.testutil.TypicalPersons.INDEX_FIRST_PERSON;
import static seedu.address.testutil.TypicalPersons.INDEX_SECOND_PERSON;
import static seedu.address.testutil.TypicalPersons.getTypicalAddressBook;

import org.junit.Test;

Expand All @@ -27,14 +28,13 @@
import seedu.address.model.person.ReadOnlyPerson;
import seedu.address.testutil.EditPersonDescriptorBuilder;
import seedu.address.testutil.PersonBuilder;
import seedu.address.testutil.TypicalPersons;

/**
* Contains integration tests (interaction with the Model) and unit tests for EditCommand.
*/
public class EditCommandTest {

private Model model = new ModelManager(new TypicalPersons().getTypicalAddressBook(), new UserPrefs());
private Model model = new ModelManager(getTypicalAddressBook(), new UserPrefs());

@Test
public void execute_allFieldsSpecifiedUnfilteredList_success() throws Exception {
Expand Down
Loading

0 comments on commit e3bdbae

Please sign in to comment.