/*
* EventControllerTest.java
* JUnit based test
*
* Created on 13 July 2007, 18:14
*/
package org.spw.controller;
import java.util.Calendar;
import java.util.Date;
import junit.framework.*;
import java.util.List;
import org.spw.model.Event;
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 javax.persistence.TemporalType;
import org.spw.model.Contact;
import org.spw.model.Person;
/**
*
* @author PSe
*/
public class EventControllerTest extends TestCase {
private static final long TEST_ID_NO_REMINDER = 9999991L;
private static final long TEST_ID_REMINDER_FUTURE = 9999992L;
private static final long TEST_ID_REMINDER_TODAY = 9999993L;
private static final long TEST_ID_REMINDER_PASSED = 9999994L;
private static final EventController ctrl = new EventController();
private static final PersonController contactCtrl = new PersonController();
private Calendar cal = Calendar.getInstance();
private Date today;
private Date yesterday;
private Date tomorrow;
public EventControllerTest(String testName) {
super(testName);
this.today = cal.getTime();
int theDay = cal.get(Calendar.DAY_OF_YEAR);
cal.set(Calendar.DAY_OF_YEAR, theDay - 1);
this.yesterday = cal.getTime();
cal.set(Calendar.DAY_OF_YEAR, theDay + 1);
this.tomorrow = cal.getTime();
}
protected void setUp() throws Exception {
//No Reminder
Event evt1 = new Event();
evt1.setIdEvent(TEST_ID_NO_REMINDER);
evt1.setReminderNote("Test no reminder");
//Remind me NOW!
Event evt2 = new Event();
evt2.setIdEvent(TEST_ID_REMINDER_TODAY);
evt2.setReminderNote("Test with current reminder");
evt2.setReminderDate(today);
//Reminder passed
Event evt3 = new Event();
evt3.setIdEvent(TEST_ID_REMINDER_PASSED);
evt3.setReminderNote("Test don't forget my reminder");
evt3.setReminderDate(yesterday);
//Reminder futur
Event evt4 = new Event();
evt4.setIdEvent(TEST_ID_REMINDER_FUTURE);
evt4.setReminderNote("Test stay quiet");
evt4.setReminderDate(tomorrow);
ctrl.create(evt1);
ctrl.create(evt2);
ctrl.create(evt3);
ctrl.create(evt4);
}
protected void tearDown() throws Exception {
ctrl.delete(ctrl.read(TEST_ID_NO_REMINDER));
ctrl.delete(ctrl.read(TEST_ID_REMINDER_FUTURE));
ctrl.delete(ctrl.read(TEST_ID_REMINDER_PASSED));
ctrl.delete(ctrl.read(TEST_ID_REMINDER_TODAY));
Contact contact = contactCtrl.read(TEST_ID_NO_REMINDER);
if (contact != null) contactCtrl.delete(contact);
}
/**
* Test of getReminders method, of class org.spw.controller.EventController.
*/
public void testGetReminders() {
System.out.println("getReminders");
EventController instance = new EventController();
List<Event> result = instance.getReminders();
assertFalse("The action not completed must not be empty",
result.isEmpty());
boolean findPassed = false;
boolean findToday = false;
for (Event evt : result) {
if (evt.getIdEvent() == TEST_ID_NO_REMINDER) fail("An event without " +
"reminder date should not apear in reminders");
if (evt.getIdEvent() == TEST_ID_REMINDER_FUTURE) fail("An event with " +
"a future reminder date should not apear in reminders");
if (evt.getIdEvent() == TEST_ID_REMINDER_PASSED) findPassed = true;
if (evt.getIdEvent() == TEST_ID_REMINDER_TODAY) findToday = true;
}
assertTrue("Reminded passed not found", findPassed);
assertTrue("Reminded today not found", findToday);
}
/**
* Test of getEvents method, of class org.spw.controller.EventController.
*/
public void testGetEvents() {
boolean foundTEST_ID_REMINDER_TODAY = false;
boolean foundTEST_ID_REMINDER_PASSED = false;
boolean foundTEST_ID_REMINDER_FUTURE = false;
boolean foundTEST_ID_NO_REMINDER = false;
System.out.println("getEvents");
EventController ctrl = new EventController();
List<Event> result = ctrl.getEvents();
assertNotNull(result);
assertTrue(result.size() > 0);
for (Event evt : result) {
if (evt.getIdEvent() == TEST_ID_NO_REMINDER) foundTEST_ID_NO_REMINDER = true;
if (evt.getIdEvent() == TEST_ID_REMINDER_FUTURE) foundTEST_ID_REMINDER_FUTURE = true;
if (evt.getIdEvent() == TEST_ID_REMINDER_PASSED) foundTEST_ID_REMINDER_PASSED = true;
if (evt.getIdEvent() == TEST_ID_REMINDER_TODAY) foundTEST_ID_REMINDER_TODAY = true;
}
assertTrue("Cannot find the test entity.", foundTEST_ID_NO_REMINDER);
assertTrue("Cannot find the test entity.", foundTEST_ID_REMINDER_FUTURE);
assertTrue("Cannot find the test entity.", foundTEST_ID_REMINDER_PASSED);
assertTrue("Cannot find the test entity.", foundTEST_ID_REMINDER_TODAY);
}
/**
* Test of create method, of class org.spw.controller.EventController.
*/
public void testCreate() {
System.out.println("create");
// assume that JPA is bug free
}
/**
* Test of read method, of class org.spw.controller.EventController.
*/
public void testRead() {
System.out.println("read");
// assume that JPA is bug free
}
/**
* Test of update method, of class org.spw.controller.EventController.
*/
public void testUpdate() {
System.out.println("update");
// assume that JPA is bug free
}
/**
* Test of delete method, of class org.spw.controller.EventController.
*/
public void testDelete() {
System.out.println("delete");
// assume that JPA is bug free
}
/**
* Test of addAttendance method, of class org.spw.controller.EventController.
*/
public void testAddAttendance() {
System.out.println("addAttendance");
Event event = ctrl.read(TEST_ID_NO_REMINDER);
Person person = new Person();
person.setIdContact(TEST_ID_NO_REMINDER);
person.setFirstName(getName());
person.setLastName(getName());
person = contactCtrl.update(person);
person.getEventsAttended().add(event);
person = contactCtrl.update(person);
ctrl.addAttendance(person, event);
event = ctrl.read(TEST_ID_NO_REMINDER);
assertTrue(event.getContactsAttended().contains(person));
person = contactCtrl.read(TEST_ID_NO_REMINDER);
assertTrue(person.getEventsAttended().contains(event));
}
/**
* Test of removeAttendance method, of class org.spw.controller.EventController.
*/
public void testRemoveAttendance() {
System.out.println("removeAttendance");
Event event = ctrl.read(TEST_ID_NO_REMINDER);
Person person = new Person();
person.setIdContact(TEST_ID_NO_REMINDER);
person.setFirstName(getName());
person.setLastName(getName());
person = contactCtrl.update(person);
person.getEventsAttended().add(event);
person = contactCtrl.update(person);
event.getContactsAttended().add(person);
event = ctrl.update(event);
ctrl.removeAttendance(person, event);
event = ctrl.read(TEST_ID_NO_REMINDER);
assertFalse(event.getContactsAttended().contains(person));
person = contactCtrl.read(TEST_ID_NO_REMINDER);
assertFalse(person.getEventsAttended().contains(event));
}
/**
* Test of parse method, of class org.spw.controller.EventController.
*/
public void testParse() {
System.out.println("parse");
EventController instance = new EventController();
String eventString = null;
Event result = instance.parse(eventString); //check no runtime error
eventString = "test";
result = instance.parse(eventString); //check no runtime error
eventString = "Test[" + Long.toString(TEST_ID_NO_REMINDER) + "]";
result = instance.parse(eventString);
assertEquals((long)TEST_ID_NO_REMINDER, (long)result.getIdEvent());
}
/**
* Test of addOrganizer method, of class org.spw.controller.EventController.
*/
public void testAddOrganizer() {
System.out.println("addOrganizer");
Event event = ctrl.read(TEST_ID_NO_REMINDER);
Person person = new Person();
person.setIdContact(TEST_ID_NO_REMINDER);
person.setFirstName(getName());
person.setLastName(getName());
person = contactCtrl.update(person);
person.getOrganize().add(event);
person = contactCtrl.update(person);
ctrl.addOrganizer(person, event);
event = ctrl.read(TEST_ID_NO_REMINDER);
assertTrue(event.getOrganizedBy().contains(person));
person = contactCtrl.read(TEST_ID_NO_REMINDER);
assertTrue(person.getOrganize().contains(event));
}
/**
* Test of removeOrganizer method, of class org.spw.controller.EventController.
*/
public void testRemoveOrganizer() {
System.out.println("removeOrganizer");
Event event = ctrl.read(TEST_ID_NO_REMINDER);
Person person = new Person();
person.setIdContact(TEST_ID_NO_REMINDER);
person.setFirstName(getName());
person.setLastName(getName());
person = contactCtrl.update(person);
person.getOrganize().add(event);
person = contactCtrl.update(person);
event.getOrganizedBy().add(person);
event = ctrl.update(event);
ctrl.removeOrganizer(person, event);
event = ctrl.read(TEST_ID_NO_REMINDER);
assertFalse(event.getOrganizedBy().contains(person));
person = contactCtrl.read(TEST_ID_NO_REMINDER);
assertFalse(person.getOrganize().contains(event));
}
}