/*
* PersonControllerTest.java
* JUnit based test
*
* Created on 26 May 2007, 19:12
*/
package org.spw.controller;
import junit.framework.*;
import org.spw.model.Person;
import java.util.List;
/**
*
* @author PSe
*/
public class PersonControllerTest extends TestCase {
private static final long TEST_ID = 9999980L;
private static final long TEST_ID_ABC1 = 9999991L;
private static final String TEST_FIRSTNAME_ABC1 = "ABCDEF";
private static final long TEST_ID_BBC = 9999999L;
private static final String TEST_FIRSTNAME_BBC = "BBC News";
private final PersonController personCtl = new PersonController();
public PersonControllerTest(String testName) {
super(testName);
}
protected void setUp() throws Exception {
Person person = new Person();
person.setIdContact(TEST_ID);
person.setFirstName("Test1");
person.setLastName("Office Staff");
person.setOfficeStaff(true);
person.setNote(getName());
personCtl.create(person);
person = new Person();
person.setIdContact(TEST_ID+1);
person.setFirstName("Test office Test");
person.setLastName("Lambda");
person.setOfficeStaff(false);
person.setOffice("Test");
person.setNote(getName());
personCtl.create(person);
// Create a person with now last name
person = new Person();
person.setIdContact(TEST_ID+2);
person.setFirstName("Test no last name");
// person.setLastName("Lambda");
person.setOfficeStaff(false);
person.setOffice("Test");
person.setNote(getName());
personCtl.create(person);
Person p1 = new Person();
p1.setIdContact(TEST_ID_ABC1);
p1.setFirstName(TEST_FIRSTNAME_ABC1);
p1.setLastName(getName());
personCtl.create(p1);
for(long id = TEST_ID_ABC1+1; id < TEST_ID_ABC1 + 8; id++) {
Person p = new Person();
p.setIdContact(id);
p.setFirstName(TEST_FIRSTNAME_ABC1 + id);
p.setLastName(getName());
personCtl.create(p);
}
Person p2 = new Person();
p2.setIdContact(TEST_ID_BBC);
p2.setFirstName(TEST_FIRSTNAME_BBC);
p2.setLastName(getName());
personCtl.create(p2);
}
protected void tearDown() throws Exception {
Person person = personCtl.read(TEST_ID);
if (person != null) personCtl.delete(person);
person = personCtl.read(TEST_ID+1);
if (person != null) personCtl.delete(person);
person = personCtl.read(TEST_ID+2);
if (person != null) personCtl.delete(person);
for(long id = TEST_ID_ABC1; id < TEST_ID_ABC1 + 8; id++) {
person = personCtl.read(id);
if (person != null) personCtl.delete(person);
}
person = personCtl.read(TEST_ID_BBC);
if (person != null) personCtl.delete(person);
}
/**
* Test of getOfficeWorkers method, of class org.spw.controller.PersonController.
*/
public void testGetOfficeStaff() {
boolean find=false;
System.out.println("getOfficeStaff");
List<Person> result = personCtl.getOfficeStaff();
assertNotNull(result);
for (Person person : result) {
assertTrue(person.isOfficeStaff());
if (person.getIdContact().equals(TEST_ID)) {
assertEquals("Test1", person.getFirstName());
assertEquals("Office Staff", person.getLastName());
find = true;
} else if (person.getIdContact().equals(TEST_ID+1)) {
fail("Found a person not in the staff as staff member.");
}
}
assertTrue("Test Office Staff contact not found.", find);
}
/**
* Test of getFromOffice method, of class org.spw.controller.PersonController.
*/
public void testGetFromOffice() {
System.out.println("getFromOffice");
boolean find = false;
String office = "Test";
List<Person> result = personCtl.getFromOffice(office);
assertNotNull(result);
for (Person person : result) {
assertEquals(office, person.getOffice());
if (person.getIdContact().equals(TEST_ID+1)) {
assertEquals("Test office Test", person.getFirstName());
assertEquals("Lambda", person.getLastName());
find = true;
} else if (person.getIdContact().equals(TEST_ID)) {
fail("Found a person not in the office as office member.");
}
}
assertTrue("Test Office Member contact not found.", find);
}
/**
* Test of getPersons method, of class org.spw.controller.PersonController.
*/
public void testGetPersons() {
System.out.println("getPersons");
PersonController instance = new PersonController();
List<Person> result = instance.getPersons();
assertNotNull(result);
assertTrue("The result must return all the persons", result.size() > 0);
}
/**
* Test of getPersonsBeginningBy method, of class org.spw.controller.PersonController.
*/
public void testGetPersonsBeginningBy() {
System.out.println("getPersonsBeginningBy");
String beginning = TEST_FIRSTNAME_ABC1.substring(0,3);
int maxResult = 0;
PersonController instance = new PersonController();
List<Person> result = instance.getPersonsBeginningBy(beginning, maxResult);
assertNotNull(result);
assertTrue("The test failed to find any person beginning by " +
beginning, result.size() > 0);
boolean findABC1 = false;
for(Person person : result) {
assertTrue(person.getName().startsWith(beginning));
if (person.getFirstName().equals(TEST_FIRSTNAME_ABC1)) findABC1 = true;
}
assertTrue("The test to find " + TEST_FIRSTNAME_ABC1 + " failed.", findABC1);
}
}