/*
* VolunteerApplicationControllerTest.java
* JUnit based test
*
* Created on 17 August 2007, 19:51
*/
package org.spw.controller;
import javax.persistence.EntityExistsException;
import junit.framework.*;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;
import org.spw.model.Contribution;
import org.spw.model.VolunteerApplication;
/**
*
* @author PSe
*/
public class VolunteerApplicationControllerTest extends TestCase {
private static EntityManagerFactory emf = Persistence.createEntityManagerFactory("VolunteerPU");
private static final long TEST_ID = 9999990L;
private static final VolunteerApplicationController ctrl =
new VolunteerApplicationController();
public VolunteerApplicationControllerTest(String testName) {
super(testName);
}
protected void setUp() throws Exception {
VolunteerApplication test = new VolunteerApplication();
test.setIdApplication(TEST_ID);
test.setDetailInfoSent(getName());
ctrl.create(test);
}
protected void tearDown() throws Exception {
VolunteerApplication va;
//TODO:check all teardown and add a check if read = null
for (long id = TEST_ID; id <= TEST_ID +1; id++) {
va = ctrl.read(id);
if (va != null) ctrl.delete(va);
}
}
/**
* Test of getApplications method, of class org.spw.controller.VolunteerApplicationController.
*/
public void testGetApplications() {
boolean found = false;
System.out.println("getApplications");
List<VolunteerApplication> result = ctrl.getApplications();
assertNotNull(result);
assertTrue("Must find at least the test entity.", result.size() > 0);
for (VolunteerApplication app : result) {
if (app.getIdApplication() == TEST_ID) {
found = true;
break;
}
}
assertTrue("Cannot find the test entity.", found);
}
/**
* Test of parse method, of class org.spw.controller.VolunteerApplicationController.
*/
public void testParse() {
System.out.println("parse");
String id = "";
VolunteerApplication result = ctrl.parse(id);
assertNull("Must not find a VolunteerApplication with incorrect string rep.", result);
id = null;
assertNull("Must not find a VolunteerApplication with incorrect string rep.", result);
id = "]" + TEST_ID + "[";
assertNull("Must not find a VolunteerApplication with incorrect string rep.", result);
id = "anything[" + TEST_ID + "]";
result = ctrl.parse(id);
assertEquals((long)TEST_ID, (long)result.getIdApplication());
}
/**
* Test of create method, of class org.spw.controller.VolunteerApplicationController.
*/
public void testCreate() {
System.out.println("create");
VolunteerApplication va = new VolunteerApplication();
va.setIdApplication(TEST_ID + 1);
va.setDetailInfoSent(getName());
VolunteerApplicationController instance = new VolunteerApplicationController();
Contribution c1 = new Contribution();
c1.setReference(getName());
va.addContribution(c1);
instance.create(va);
va = instance.read(TEST_ID + 1);
assertEquals(getName(), va.getContributions().get(0).getReference());
//double
VolunteerApplication vaDouble = new VolunteerApplication();
vaDouble.setIdApplication(TEST_ID + 1);
vaDouble.setDetailInfoSent(getName());
try {
instance.create(vaDouble);
fail("double must throw an exception");
} catch (EntityExistsException ex) {
//OK
}
}
/**
* Test of update method, of class org.spw.controller.VolunteerApplicationController.
*/
public void testUpdate() {
System.out.println("update");
VolunteerApplication va = new VolunteerApplication();
VolunteerApplicationController instance = new VolunteerApplicationController();
va = instance.read(TEST_ID);
Contribution contribution = new Contribution();
contribution.setReference(getName());
va.addContribution(contribution);
instance.update(va);
va = instance.read(TEST_ID);
assertEquals(getName(), va.getContributions().get(0).getReference());
}
/**
* Test of delete method, of class org.spw.controller.VolunteerApplicationController.
*/
public void testDelete() {
System.out.println("delete");
VolunteerApplicationController instance = new VolunteerApplicationController();
VolunteerApplication va = instance.read(TEST_ID);
Contribution contribution = new Contribution();
contribution.setReference(getName());
va.addContribution(contribution);
va = instance.update(va);
contribution = va.getContributions().get(0);
instance.delete(va);
assertNull(instance.read(TEST_ID));
assertNull(checkContribution(contribution.getIdContribution()));
}
private Contribution checkContribution(Long id) {
EntityManager em = emf.createEntityManager();
Contribution contribution = em.find(Contribution.class, id);
em.close();
return contribution;
}
/**
* Test of read method, of class org.spw.controller.VolunteerApplicationController.
*/
public void testRead() {
System.out.println("read");
// assume JPA bug free
}
/**
* Test of addContribution method, of class org.spw.controller.VolunteerApplicationController.
*/
public void testAddContribution() {
System.out.println("addContribution");
VolunteerApplicationController instance = new VolunteerApplicationController();
VolunteerApplication object = instance.read(TEST_ID);
Contribution contribution = new Contribution();
contribution.setReference(getName());
VolunteerApplication result = instance.addContribution(object, contribution);
assertEquals(1, result.getContributions().size());
Long id = result.getContributions().get(0).getIdContribution();
//refresh
object = instance.read(TEST_ID);
assertEquals(1, object.getContributions().size());
contribution = checkContribution(id);
assertEquals(getName(), contribution.getReference());
}
/**
* Test of removeContribution method, of class org.spw.controller.VolunteerApplicationController.
*/
public void testRemoveContribution() {
System.out.println("removeContribution");
VolunteerApplicationController instance = new VolunteerApplicationController();
VolunteerApplication object = instance.read(TEST_ID);
Contribution contribution = new Contribution();
contribution.setReference(getName());
VolunteerApplication result = instance.addContribution(object, contribution);
assertEquals(1, result.getContributions().size());
Long id = result.getContributions().get(0).getIdContribution();
//delete
instance.delete(result);
contribution = checkContribution(id);
assertNull(contribution);
}
}