/*
* PartnershipControllerTest.java
* JUnit based test
*
* Created on 28 July 2007, 09:50
*/
package org.spw.controller;
import java.util.Date;
import junit.framework.*;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.persistence.EntityManager;
import javax.persistence.Query;
import org.spw.model.Partnership;
import org.spw.model.Advertisement;
/**
*
* @author PSe
*/
public class PartnershipControllerTest extends TestCase {
private static final long TEST_ID_ABC1 = 9999991L;
private static final String TEST_NAME_ABC1 = "ABCDEF";
private static final long TEST_ID_BBC = 9999999L;
private static final String TEST_NAME_BBC = "BBC News";
private final PartnershipController partnershipCtl = new PartnershipController();
public PartnershipControllerTest(String testName) {
super(testName);
}
protected void setUp() throws Exception {
Partnership p1 = new Partnership();
p1.setIdContact(TEST_ID_ABC1);
p1.setOrganisationName(TEST_NAME_ABC1);
p1.setFirstName(TEST_NAME_ABC1);
p1.setLastName(getName());
p1.setTypeOfPartnership("Recruitment");
partnershipCtl.create(p1);
for(long id = TEST_ID_ABC1+1; id < TEST_ID_ABC1 + 8; id++) {
Partnership p = new Partnership();
p.setIdContact(id);
p.setOrganisationName(TEST_NAME_ABC1 + id);
p.setLastName(getName());
p.setTypeOfPartnership("Recruitment");
partnershipCtl.create(p);
}
Partnership p2 = new Partnership();
p2.setIdContact(TEST_ID_BBC);
p2.setOrganisationName(TEST_NAME_BBC);
p2.setLastName(getName());
p2.setTypeOfPartnership("Recruitment");
partnershipCtl.create(p2);
}
protected void tearDown() throws Exception {
Partnership p;
for(long id = TEST_ID_ABC1; id < TEST_ID_ABC1 + 8; id++) {
p = partnershipCtl.read(id);
if (p != null) partnershipCtl.delete(p);
}
partnershipCtl.delete(partnershipCtl.read(TEST_ID_BBC));
AdvertisementController advCtrl = new AdvertisementController();
advCtrl.delete(advCtrl.read(TEST_ID_ABC1));
}
/**
* Test of getPartnerships method, of class org.spw.controller.PartnershipController.
*/
public void testGetPartnerships() {
System.out.println("getPartnerships");
PartnershipController instance = new PartnershipController();
List<Partnership> result = instance.getPartnerships();
assertNotNull(result);
assertTrue(result.size() > 0);
}
/**
* Test of getInstitutions method, of class org.spw.controller.PartnershipController.
*/
public void testGetInstitutionsBeginningBy() {
System.out.println("getInstitutionsBeginningBy");
PartnershipController instance = new PartnershipController();
String beginning = TEST_NAME_ABC1.substring(0,3);
List<Partnership> result = instance.getInstitutionsBeginningBy(beginning, 0);
assertNotNull(result);
assertTrue(result.size() > 0);
boolean findABC1 = false;
for(Partnership partnership : result) {
if (!partnership.getTypeOfPartnership().equals("Recruitment")) {
fail("Type incorrect for an institution: " + partnership.getTypeOfPartnership() +
" in " + partnership.getName());
}
assertTrue(partnership.getName().startsWith(beginning));
if (partnership.getOrganisationName().equals(TEST_NAME_ABC1)) findABC1 = true;
}
assertTrue("The test to find " + TEST_NAME_ABC1 + " failed.", findABC1);
}
/**
* Test of create method, of class org.spw.controller.PartnershipController.
*/
public void testCreate() {
System.out.println("create");
// Assume JPA is bug free
}
/**
* Test of read method, of class org.spw.controller.PartnershipController.
*/
public void testRead() {
System.out.println("read");
// Assume JPA is bug free
}
/**
* Test of update method, of class org.spw.controller.PartnershipController.
*/
public void testUpdate() {
System.out.println("update");
// Assume JPA is bug free
}
/**
* Test of delete method, of class org.spw.controller.PartnershipController.
*/
public void testDelete() {
System.out.println("delete");
// Assume JPA is bug free
}
/**
* Test of addAdvertisement method, of class org.spw.controller.PartnershipController.
*/
public void testAddAdvertisement() {
System.out.println("addAdvertisement");
Partnership partnership = partnershipCtl.read(TEST_ID_ABC1);
Advertisement advertisement = new Advertisement();
advertisement.setDateAdvert(new Date());
advertisement.setIdAdvertisement(TEST_ID_ABC1);
advertisement.setNotes(getName());
Partnership result = partnershipCtl.addAdvertisement(partnership, advertisement);
assertNotNull(result);
assertEquals(partnership, result);
assertTrue(result.getAdvertisements().contains(advertisement));
}
/**
* Test of parse method, of class org.spw.controller.PartnershipController.
*/
public void testParse() {
System.out.println("parse");
String partnership = "[" + Long.toString(TEST_ID_ABC1) + "]" ;
PartnershipController instance = new PartnershipController();
Partnership expResult = instance.read(TEST_ID_ABC1);
Partnership result = instance.parse(partnership);
assertEquals("Couldn't parse " + partnership, expResult, result);
partnership = null;
assertNull(instance.parse(partnership));
partnership = "";
assertNull(instance.parse(partnership));
partnership = "ABCD";
assertNull(instance.parse(partnership));
}
/**
* Test of removeAdvertisement method, of class org.spw.controller.PartnershipController.
*/
public void testRemoveAdvertisement() {
System.out.println("removeAdvertisement");
Partnership partnership = partnershipCtl.read(TEST_ID_ABC1);
Advertisement advertisement = new Advertisement();
advertisement.setDateAdvert(new Date());
advertisement.setIdAdvertisement(TEST_ID_ABC1);
advertisement.setNotes(getName());
Partnership result = partnershipCtl.addAdvertisement(partnership, advertisement);
result = partnershipCtl.removeAdvertisement(partnership, advertisement);
assertFalse(result.getAdvertisements().contains(advertisement));
AdvertisementController advCtrl = new AdvertisementController();
assertNull(advCtrl.read(TEST_ID_ABC1));
}
}