📄 clientfacadetest.java
字号:
/*--------------------------------------------------------------------------*
| Copyright (C) 2006 Christopher Kohlhaas |
| |
| This program is free software; you can redistribute it and/or modify |
| it under the terms of the GNU General Public License as published by the |
| Free Software Foundation. A copy of the license has been included with |
| these distribution in the COPYING file, if not go to www.fsf.org |
| |
| As a special exception, you are granted the permissions to link this |
| program with every library, which license fulfills the Open Source |
| Definition as published by the Open Source Initiative (OSI). |
*--------------------------------------------------------------------------*/
package org.rapla.facade.tests;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Collections;
import java.util.Date;
import java.util.HashSet;
import java.util.Locale;
import junit.framework.Test;
import junit.framework.TestSuite;
import org.rapla.RaplaTestCase;
import org.rapla.components.util.DateTools;
import org.rapla.components.util.SerializableDateTimeFormat;
import org.rapla.entities.DependencyException;
import org.rapla.entities.Entity;
import org.rapla.entities.EntityNotFoundException;
import org.rapla.entities.ReadOnlyException;
import org.rapla.entities.User;
import org.rapla.entities.configuration.CalendarModelConfiguration;
import org.rapla.entities.configuration.Preferences;
import org.rapla.entities.configuration.RaplaMap;
import org.rapla.entities.domain.Allocatable;
import org.rapla.entities.domain.Appointment;
import org.rapla.entities.domain.Reservation;
import org.rapla.entities.dynamictype.ClassificationFilter;
import org.rapla.facade.ClientFacade;
import org.rapla.facade.Conflict;
import org.rapla.facade.ModificationModule;
import org.rapla.facade.QueryModule;
import org.rapla.facade.UserModule;
import org.rapla.framework.RaplaException;
import org.rapla.framework.RaplaLocale;
import org.rapla.plugin.weekview.WeekViewFactory;
public class ClientFacadeTest extends RaplaTestCase {
ClientFacade facade;
Locale locale;
public ClientFacadeTest(String name) {
super(name);
}
public static Test suite() {
return new TestSuite(ClientFacadeTest.class);
}
protected void setUp() throws Exception {
super.setUp();
facade = (ClientFacade)
getContext().lookup(ClientFacade.ROLE + "/local-facade");
facade.login("homer","duffs".toCharArray());
locale = Locale.getDefault();
}
protected void tearDown() throws Exception {
facade.logout();
super.tearDown();
}
private Reservation findReservation(QueryModule queryMod,String name) throws RaplaException {
Reservation[] reservations = queryMod.getReservations(null,null,null,null);
for (int i=0;i<reservations.length;i++) {
if (reservations[i].getName(locale).equals(name))
return reservations[i];
}
return null;
}
public void testConflicts() throws Exception {
//
Conflict[] conflicts= facade.getConflicts( (Date)null);
Reservation[] all = facade.getReservations(null, null, null, null);
facade.removeObjects( all );
Reservation orig = (Reservation) facade.newReservation();
orig.getClassification().setValue("name","new");
Date start = getRaplaLocale().toDate( 2004, 12, 1);
Date end = getRaplaLocale().toDate( start, getRaplaLocale().toTime( 12,0,0));
orig.addAppointment( facade.newAppointment( start, end));
orig.addAllocatable( facade.getAllocatables()[0]);
Reservation clone = (Reservation) facade.clone( orig );
facade.store( orig );
facade.store( clone );
conflicts = facade.getConflicts( (Date) null );
assertEquals( 2, conflicts.length );
HashSet set = new HashSet( Arrays.asList( conflicts ));
Conflict[] conflictsAfter = facade.getConflicts( (Date)null );
assertEquals( conflicts[0], conflictsAfter[0] );
assertTrue ( set.containsAll( new HashSet( Arrays.asList( conflictsAfter ))));
}
// Make some Changes to the Reservation in another client
private void changeInSecondFacade(String name) throws Exception {
ClientFacade facade2 = (ClientFacade)
getContext().lookup(ClientFacade.ROLE + "/local-facade2");
facade2.login("homer","duffs".toCharArray());
UserModule userMod2 = (UserModule) facade2;
QueryModule queryMod2 = (QueryModule) facade2;
ModificationModule modificationMod2 = (ModificationModule) facade2;
boolean bLogin = userMod2.login("homer","duffs".toCharArray());
assertTrue(bLogin);
Reservation reservation = findReservation(queryMod2,name);
Reservation mutableReseravation = (Reservation) modificationMod2.edit(reservation);
Appointment appointment = (Appointment) mutableReseravation.getAppointments()[0];
RaplaLocale loc = getRaplaLocale();
Calendar cal = loc.createCalendar();
cal.set(Calendar.DAY_OF_WEEK,Calendar.MONDAY);
Date startTime1 = loc.toDate(cal.getTime(), loc.toTime( 17,0,0));
Date endTime1 = loc.toDate(cal.getTime(), loc.toTime( 19,0,0));
appointment.move(startTime1,endTime1);
modificationMod2.store( mutableReseravation );
userMod2.logout();
}
public void testClone() throws Exception {
ClassificationFilter filter = facade.getDynamicType("event").newClassificationFilter();
filter.addEqualsRule("name","power planting");
Reservation orig = facade.getReservations(null, null, null, new ClassificationFilter[] { filter})[0];
Reservation clone = (Reservation) facade.clone( orig );
Appointment a = clone.getAppointments()[0];
Date newStart = new SerializableDateTimeFormat().parseDateTime("2005-10-10","10:20:00");
Date newEnd = new SerializableDateTimeFormat().parseDateTime("2005-10-12", null);
a.move( newStart );
a.getRepeating().setEnd( newEnd );
facade.store( clone );
Reservation[] allPowerPlantings = facade.getReservations(null, null, null, new ClassificationFilter[] { filter});
assertEquals( 2, allPowerPlantings.length);
Reservation[] onlyClones = facade.getReservations(null, newStart, null, new ClassificationFilter[] { filter});
assertEquals( 1, onlyClones.length);
}
public void testRefresh() throws Exception {
changeInSecondFacade("bowling");
facade.refresh();
Reservation resAfter = findReservation(facade,"bowling");
Appointment appointment = resAfter.getAppointments()[0];
Calendar cal = Calendar.getInstance(DateTools.getTimeZone());
cal.setTime(appointment.getStart());
assertEquals(17, cal.get(Calendar.HOUR_OF_DAY) );
assertEquals(Calendar.MONDAY, cal.get(Calendar.DAY_OF_WEEK) );
cal.setTime(appointment.getEnd());
assertEquals(19, cal.get(Calendar.HOUR_OF_DAY));
assertEquals( Calendar.MONDAY, cal.get(Calendar.DAY_OF_WEEK));
}
public void testExampleEdit() throws Exception {
Allocatable nonPersistantAllocatable = getFacade().newResource();
nonPersistantAllocatable.getClassification().setValue("name", "Bla");
Reservation nonPeristantEvent = getFacade().newReservation();
nonPeristantEvent.getClassification().setValue("name","dummy-event");
assertEquals( "event", nonPeristantEvent.getClassification().getType().getElementKey());
nonPeristantEvent.addAllocatable( nonPersistantAllocatable );
nonPeristantEvent.addAppointment( getFacade().newAppointment( new Date(), new Date()));
getFacade().storeObjects( new Entity[] { nonPersistantAllocatable, nonPeristantEvent} );
// Store the allocatable it a second time to test if it is still modifiable after storing
nonPersistantAllocatable.getClassification().setValue("name", "Blubs");
getFacade().store( nonPersistantAllocatable );
// query the allocatable from the store
ClassificationFilter filter = getFacade().getDynamicType("room").newClassificationFilter();
filter.addEqualsRule("name","Blubs");
Allocatable persistantAllocatable = getFacade().getAllocatables( new ClassificationFilter[] { filter} )[0];
// query the event from the store
ClassificationFilter eventFilter = getFacade().getDynamicType("event").newClassificationFilter();
eventFilter.addEqualsRule("name","dummy-event");
Reservation persistantEvent = getFacade().getReservations( null, null, null,new ClassificationFilter[] { eventFilter} )[0];
// Another way to get the persistant event would have been
//Reservation persistantEvent = getFacade().getPersistant( nonPeristantEvent );
// test if the ids of editable Versions are equal to the persistant ones
assertEquals( persistantAllocatable, nonPersistantAllocatable);
assertEquals( persistantEvent, nonPeristantEvent);
assertEquals( persistantEvent.getAllocatables()[0], nonPeristantEvent.getAllocatables()[0]);
// Check if the modifiable/original versions are different to the persistant versions
assertTrue( persistantAllocatable != nonPersistantAllocatable );
assertTrue( persistantEvent != nonPeristantEvent );
assertTrue( persistantEvent.getAllocatables()[0] != nonPeristantEvent.getAllocatables()[0]);
// Test the read only constraints
try {
persistantAllocatable.getClassification().setValue("name","asdflkj");
fail("ReadOnlyException should have been thrown");
} catch (ReadOnlyException ex) {
}
try {
persistantEvent.getClassification().setValue("name","dummy-event");
fail("ReadOnlyException should have been thrown");
} catch (ReadOnlyException ex) {
}
try {
persistantEvent.removeAllocatable( nonPersistantAllocatable);
fail("ReadOnlyException should have been thrown");
} catch (ReadOnlyException ex) {
}
// now we get a second edit copy of the event
Reservation nonPersistantEventVersion2 = (Reservation) getFacade().edit( persistantEvent);
assertTrue( nonPersistantEventVersion2 != nonPeristantEvent );
// Both allocatables are persitant, so they have the same reference
assertTrue( persistantEvent.getAllocatables()[0] == nonPersistantEventVersion2.getAllocatables()[0]);
}
public void testLogin() throws Exception {
ClientFacade facade2 = (ClientFacade)
getContext().lookup(ClientFacade.ROLE + "/local-facade2");
assertEquals(false, facade2.login("non_existant_user","".toCharArray()));
assertEquals(false, facade2.login("non_existant_user","fake".toCharArray()));
assertTrue(facade2.login("homer","duffs".toCharArray()));
assertEquals("homer",facade2.getUser().getUsername());
facade.logout();
}
public void testSavePreferences() throws Exception {
ClientFacade facade2 = (ClientFacade)
getContext().lookup(ClientFacade.ROLE + "/local-facade2");
assertTrue(facade2.login("monty","burns".toCharArray()));
Preferences prefs = (Preferences) facade.edit( facade.getPreferences() );
facade2.store( prefs );
facade2.logout();
}
public void testNewUser() throws RaplaException {
User newUser = facade.newUser();
try {
facade.getPreferences(newUser);
fail( "getPreferences should throw an Exception for non existant user");
} catch (EntityNotFoundException ex ){
}
facade.store( newUser );
Preferences prefs = (Preferences) facade.edit( facade.getPreferences(newUser) );
facade.store( prefs );
}
public void testPreferenceDependencies() throws RaplaException {
Allocatable allocatable = facade.newResource();
facade.store( allocatable);
RaplaMap selected = facade.newRaplaMap( Collections.singleton( allocatable));
CalendarModelConfiguration config = facade.newRaplaCalendarModel( selected, null, "", null, null, null, WeekViewFactory.WEEK_VIEW, null);
RaplaMap calendarList = facade.newRaplaMap( Collections.singleton( config ));
Preferences preferences = facade.getPreferences();
Preferences editPref = (Preferences) facade.edit( preferences );
editPref.putEntry("TEST", calendarList );
facade.store( editPref );
try {
facade.remove( allocatable );
fail("DependencyException should have thrown");
} catch (DependencyException ex) {
}
calendarList = facade.newRaplaMap( Collections.EMPTY_LIST );
editPref = (Preferences) facade.edit( preferences );
editPref.putEntry( "TEST", calendarList );
facade.store( editPref );
facade.remove( allocatable );
}
public void testResourcesNotEmpty() throws RaplaException {
Allocatable[] resources = facade.getAllocatables(null);
assertTrue(resources.length > 0);
}
void printConflicts(Conflict[] c) {
System.out.println(c.length + " Conflicts:");
for (int i=0;i<c.length;i++) {
printConflict(c[i]);
}
}
void printConflict(Conflict c) {
System.out.println("Conflict: " + c.getReservation1().getName(locale)
+ " with " + c.getReservation2().getName(locale));
System.out.println(" " + c.getAllocatable().getName(locale)) ;
System.out.println(" " + c.getAppointment1() + " overlapps " + c.getAppointment2());
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -