/*
* VolunteerControllerTest.java
* JUnit based test
*
* Created on 26 May 2007, 19:12
*/
package org.spw.controller;
import java.text.DateFormat;
import java.util.ArrayList;
import java.util.Date;
import junit.framework.*;
import org.spw.model.Program;
import org.spw.model.Volunteer;
import java.util.List;
import java.util.Collection;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.persistence.EntityManager;
import javax.persistence.Query;
import org.spw.model.VolunteerApplication;
/**
*
* @author PSe
*/
public class VolunteerControllerTest extends TestCase {
private final static Long TEST_ID = 9999999L;
public VolunteerControllerTest(String testName) {
super(testName);
}
protected void setUp() throws Exception {
}
protected void tearDown() throws Exception {
VolunteerController vc = new VolunteerController();
Volunteer v = vc.read(TEST_ID);
if (v != null) vc.delete(v);
VolunteerApplicationController vac = new VolunteerApplicationController();
VolunteerApplication va = vac.read(TEST_ID);
if (va != null) vac.delete(va);
}
/**
* Test of parse method, of class org.spw.controller.VolunteerController.
*/
public void testParse() {
System.out.println("parse");
String volunteer = "";
VolunteerController instance = new VolunteerController();
// --- Tests incorrects cases (return null)
Volunteer expResult = null;
//test empty string
Volunteer result = instance.parse(volunteer);
assertEquals("Parsing empty string should give a null volunteer",
expResult, result);
// test null string
volunteer = null;
result = instance.parse(volunteer);
assertEquals("Parsing empty string should give a null volunteer",
expResult, result);
// test incorrect number
volunteer = "[199999999]"; //this case can't occur until few thousand of years
result = instance.parse(volunteer);
assertEquals("Parsing empty string should give a null volunteer",
expResult, result);
// --- Test correct case
// test correct number
expResult = new Volunteer();
expResult.setNote("Test case");
instance.create(expResult);
volunteer = "[" + expResult.getIdContact().toString() + "]";
result = instance.parse(volunteer);
assertEquals("Parsing empty string should give a null volunteer",
expResult, result);
instance.delete(expResult);
}
/**
* Test of getVolunteers method, of class org.spw.controller.VolunteerController.
*/
public void testGetVolunteers() {
System.out.println("getVolunteers");
VolunteerController instance = new VolunteerController();
List<Volunteer> result = instance.getVolunteers();
assertNotNull(result);
assertTrue(result.size()>0);
}
/**
* Test of getRecentVolunteers method, of class org.spw.controller.VolunteerController.
*/
public void testGetRecentVolunteers() {
System.out.println("getRecentVolunteers");
int year = 2006;
VolunteerController instance = new VolunteerController();
List<Volunteer> result = instance.getRecentVolunteers(year);
assertNotNull(result);
assertTrue(result.size() > 0);
for (Volunteer volunteer : result) {
assertTrue("year applying is incorrect, expected " + year + ", found " +
volunteer.getYearApplyingFor(), volunteer.getYearApplyingFor() >= year);
}
}
public void testGetSelectionVolunteers() throws CriteriaException {
System.out.println("getSelectionVolunteers");
VolunteerController instance = new VolunteerController();
List<Volunteer> result = instance.getSelectionVolunteers(new Criteria[0]);
assertNotNull(result);
assertTrue(result.size() > 0);
//with two criterias String
Criteria[] criterias = new Criteria[2];
Criteria c1 = new Criteria();
c1.operator = ">";
c1.property = "firstName";
c1.value = " ";
Criteria c2 = new Criteria();
c2.operator = ">";
c2.property = "application.reasonCancelled";
c2.value = " ";
criterias[0] = c1;
criterias[1] = c2;
result = instance.getSelectionVolunteers(criterias);
assertNotNull(result);
assertTrue(result.size() > 0);
for (Volunteer volunteer : result) {
assertNotNull(volunteer.getFirstName());
assertTrue(volunteer.getFirstName().length() > 0);
assertTrue(volunteer.getFirstName().compareTo(" ") > 0);
assertNotNull(volunteer.getApplication());
assertTrue(volunteer.getApplication().getReasonCancelled().length() > 0);
assertTrue(volunteer.getApplication().getReasonCancelled().compareTo(" ") > 0);
}
//and a boolean
criterias = new Criteria[1];
c1.operator = "=";
c1.property = "parents.actionNetwork";
c1.value = "true";
criterias[0] = c1;
result = instance.getSelectionVolunteers(criterias);
assertNotNull(result); //don't expect any result
//and a date
criterias = new Criteria[1];
c1.operator = "<";
c1.property = "dateOfBirth";
c1.value = "2007-07-21"; //pb localization with DateFormat.getDateInstance().format(new Date());
criterias[0] = c1;
result = instance.getSelectionVolunteers(criterias);
assertNotNull(result);
assertTrue(result.size() > 0);
for (Volunteer volunteer : result) {
assertNotNull(volunteer.getDateOfBirth());
assertTrue(volunteer.getDateOfBirth().compareTo(new Date()) < 0);
}
}
/**
* Test of create method, of class org.spw.controller.VolunteerController.
*/
public void testCreate() {
//Assume that JPA is bug free
}
/**
* Test of read method, of class org.spw.controller.VolunteerController.
*/
public void testRead() {
//Assume that JPA is bug free
}
/**
* Test of update method, of class org.spw.controller.VolunteerController.
*/
public void testUpdate() {
//Assume that JPA is bug free
}
/**
* Test of delete method, of class org.spw.controller.VolunteerController.
*/
public void testDelete() {
//Assume that JPA is bug free
}
/**
* Test of addApplication method, of class org.spw.controller.VolunteerController.
*/
public void testAddApplication() {
System.out.println("addApplication");
Volunteer volunteer = new Volunteer();
volunteer.setIdContact(TEST_ID);
volunteer.setFirstName(getName());
volunteer.setLastName(getName());
volunteer.setTypeVolunteer("Potential");
VolunteerApplication application = new VolunteerApplication();
application.setIdApplication(TEST_ID);
//obtain a program
ProgramController pc = new ProgramController();
Program aProgram = pc.getPrograms().get(0);
application.setProgram(aProgram);
VolunteerController instance = new VolunteerController();
Volunteer result = instance.addApplication(volunteer, application, null);
assertNotNull(result);
assertEquals(volunteer, result);
assertEquals(application, result.getApplication());
assertEquals(aProgram, result.getApplication().getProgram());
aProgram = pc.read(aProgram.getIdProgram());
List<VolunteerApplication> theApplications =
new ArrayList<VolunteerApplication> (aProgram.getApplications());
assertTrue("Cannot find the inserted application in the program", theApplications.contains(application));
//Check the application
VolunteerApplicationController vac = new VolunteerApplicationController ();
application = vac.read(application.getIdApplication());
assertEquals(volunteer, application.getVolunteer());
// Change the program
Program anotherProgram = pc.getPrograms().get(1);
application.setProgram(anotherProgram);
volunteer = instance.addApplication(volunteer, application, aProgram);
aProgram = pc.read(aProgram.getIdProgram());
theApplications =
new ArrayList<VolunteerApplication> (aProgram.getApplications());
assertFalse("Find the stale application in the previous program", theApplications.contains(application));
anotherProgram= pc.read(anotherProgram.getIdProgram());
theApplications =
new ArrayList<VolunteerApplication> (anotherProgram.getApplications());
assertTrue("Cannot find the application in the new program", theApplications.contains(application));
}
}