use of org.olat.commons.calendar.model.Kalendar in project OpenOLAT by OpenOLAT.
the class ICalFileCalendarManagerTest method testConcurrentAddEvent.
/**
* Test concurrent add event with two threads and code-point to control concurrency.
*/
@Test
public void testConcurrentAddEvent() {
final String TEST_EVENT_ID_1 = "id-testConcurrentAddEvent-1";
final String TEST_EVENT_SUBJECT_1 = "testEvent1";
final String TEST_EVENT_ID_2 = "id-testConcurrentAddEvent-2";
final String TEST_EVENT_SUBJECT_2 = "testEvent2";
final Identity test = JunitTestHelper.createAndPersistIdentityAsRndUser("ical-2-");
final List<Exception> exceptionHolder = Collections.synchronizedList(new ArrayList<Exception>(1));
final List<Boolean> statusList = Collections.synchronizedList(new ArrayList<Boolean>(1));
final CountDownLatch doneSignal = new CountDownLatch(2);
// thread 1
Thread thread1 = new Thread() {
public void run() {
try {
// 1. load calendar
Kalendar cal = calendarManager.getPersonalCalendar(test).getKalendar();
// 2. add Event1 => breakpoint hit
log.info("testConcurrentAddEvent thread1 addEvent1");
calendarManager.addEventTo(cal, new KalendarEvent(TEST_EVENT_ID_1, TEST_EVENT_SUBJECT_1, new Date(), 1));
log.info("testConcurrentAddEvent thread1 addEvent1 DONE");
// 3. check event1 exist
cal = calendarManager.getPersonalCalendar(test).getKalendar();
KalendarEvent event1 = cal.getEvent(TEST_EVENT_ID_1, null);
assertNotNull("Did not found event with id=" + TEST_EVENT_ID_1, event1);
assertEquals("Wrong calendar-event subject", event1.getSubject(), TEST_EVENT_SUBJECT_1);
// 4. sleep 2sec
// 5. check event1 still exist (event2 added in meantime)
cal = calendarManager.getPersonalCalendar(test).getKalendar();
event1 = cal.getEvent(TEST_EVENT_ID_1, null);
assertNotNull("Did not found event with id=" + TEST_EVENT_ID_1, event1);
assertEquals("Wrong calendar-event subject", event1.getSubject(), TEST_EVENT_SUBJECT_1);
statusList.add(Boolean.TRUE);
log.info("testConcurrentAddEvent thread1 finished");
} catch (Exception ex) {
// no exception should happen
exceptionHolder.add(ex);
} finally {
doneSignal.countDown();
DBFactory.getInstance().commitAndCloseSession();
}
}
};
// thread 2
Thread thread2 = new Thread() {
public void run() {
try {
// 1. load calendar
Kalendar cal = calendarManager.getPersonalCalendar(test).getKalendar();
// 3. add Event2 (breakpoint of thread1 blocks)
log.info("testConcurrentAddEvent thread2 addEvent2");
calendarManager.addEventTo(cal, new KalendarEvent(TEST_EVENT_ID_2, TEST_EVENT_SUBJECT_2, new Date(), 1));
log.info("testConcurrentAddEvent thread1 addEvent2 DONE");
// 4. check event2 exist
cal = calendarManager.getPersonalCalendar(test).getKalendar();
KalendarEvent event2 = cal.getEvent(TEST_EVENT_ID_2, null);
assertNotNull("Did not found event with id=" + TEST_EVENT_ID_2, event2);
assertEquals("Wrong calendar-event subject", event2.getSubject(), TEST_EVENT_SUBJECT_2);
// 5. check event1 exist
cal = calendarManager.getPersonalCalendar(test).getKalendar();
KalendarEvent event1 = cal.getEvent(TEST_EVENT_ID_1, null);
assertNotNull("Did not found event with id=" + TEST_EVENT_ID_1, event1);
assertEquals("Wrong calendar-event subject", event1.getSubject(), TEST_EVENT_SUBJECT_1);
statusList.add(Boolean.TRUE);
log.info("testConcurrentAddEvent thread2 finished");
} catch (Exception ex) {
// no exception should happen
exceptionHolder.add(ex);
} finally {
doneSignal.countDown();
DBFactory.getInstance().commitAndCloseSession();
}
}
};
thread1.start();
thread2.start();
try {
boolean interrupt = doneSignal.await(10, TimeUnit.SECONDS);
assertTrue("Test takes too long (more than 10s)", interrupt);
} catch (InterruptedException e) {
fail("" + e.getMessage());
}
// if not -> they are in deadlock and the db did not detect it
for (Exception exception : exceptionHolder) {
log.info("exception: " + exception.getMessage());
exception.printStackTrace();
}
assertTrue("It throws an exception in test => see sysout", exceptionHolder.isEmpty());
log.info("testConcurrentAddEvent finish successful");
}
use of org.olat.commons.calendar.model.Kalendar in project openolat by klemens.
the class CalendarUserConfigurationDAOTest method getCalendarUserConfigurations_byTypes.
@Test
public void getCalendarUserConfigurations_byTypes() {
Identity user = JunitTestHelper.createAndPersistIdentityAsRndUser("Cal-3");
String calendarId = UUID.randomUUID().toString();
Kalendar courseCalendar = new Kalendar(calendarId, CalendarManager.TYPE_COURSE);
CalendarUserConfiguration courseCalConfig = calendarDao.createCalendarUserConfiguration(courseCalendar, user);
Kalendar groupCalendar = new Kalendar(calendarId, CalendarManager.TYPE_GROUP);
CalendarUserConfiguration groupCalConfig = calendarDao.createCalendarUserConfiguration(groupCalendar, user);
Kalendar personalCalendar = new Kalendar(user.getName(), CalendarManager.TYPE_USER);
CalendarUserConfiguration personalCalConfig = calendarDao.createCalendarUserConfiguration(personalCalendar, user);
dbInstance.commit();
Assert.assertNotNull(courseCalConfig);
// get all
List<CalendarUserConfiguration> configList = calendarDao.getCalendarUserConfigurations(user);
Assert.assertNotNull(configList);
Assert.assertEquals(3, configList.size());
Assert.assertTrue(configList.contains(courseCalConfig));
Assert.assertTrue(configList.contains(groupCalConfig));
Assert.assertTrue(configList.contains(personalCalConfig));
// get course
List<CalendarUserConfiguration> courseConfigList = calendarDao.getCalendarUserConfigurations(user, CalendarManager.TYPE_COURSE);
Assert.assertNotNull(courseConfigList);
Assert.assertEquals(1, courseConfigList.size());
Assert.assertTrue(courseConfigList.contains(courseCalConfig));
// null check
List<CalendarUserConfiguration> nullConfigList = calendarDao.getCalendarUserConfigurations(user, (String) null);
Assert.assertNotNull(nullConfigList);
Assert.assertEquals(3, nullConfigList.size());
}
use of org.olat.commons.calendar.model.Kalendar in project openolat by klemens.
the class CalendarUserConfigurationDAOTest method createConfiguration.
@Test
public void createConfiguration() {
Identity user = JunitTestHelper.createAndPersistIdentityAsRndUser("Cal-1");
String calendarId = UUID.randomUUID().toString();
Kalendar calendar = new Kalendar(calendarId, CalendarManager.TYPE_USER);
CalendarUserConfiguration config = calendarDao.createCalendarUserConfiguration(calendar, user);
dbInstance.commit();
Assert.assertNotNull(config);
Assert.assertNotNull(config.getKey());
Assert.assertNotNull(config.getCreationDate());
Assert.assertNotNull(config.getLastModified());
Assert.assertEquals(calendarId, config.getCalendarId());
Assert.assertEquals(CalendarManager.TYPE_USER, config.getType());
Assert.assertEquals(user, config.getIdentity());
}
use of org.olat.commons.calendar.model.Kalendar in project openolat by klemens.
the class ICalFileCalendarManagerTest method testAddChangeRemoveEvent.
@Test
public void testAddChangeRemoveEvent() {
Identity test = JunitTestHelper.createAndPersistIdentityAsRndUser("ical-1-");
String TEST_EVENT_ID = "id-testAddEvent";
Kalendar cal = calendarManager.getPersonalCalendar(test).getKalendar();
// 1. Test Add Event
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 13);
calendar.set(Calendar.MINUTE, 12);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MILLISECOND, 0);
Date start = calendar.getTime();
// 1 hour
KalendarEvent testEvent = new KalendarEvent(TEST_EVENT_ID, "testEvent", start, 60 * 60 * 1000);
calendarManager.addEventTo(cal, testEvent);
// set manager null to force reload of calendar from file-system
emptyCalendarCache();
cal = calendarManager.getPersonalCalendar(test).getKalendar();
KalendarEvent reloadedEvent = cal.getEvent(TEST_EVENT_ID, null);
Assert.assertNotNull("Could not found added event", reloadedEvent);
Assert.assertEquals("Added event has wrong subject", testEvent.getSubject(), reloadedEvent.getSubject());
Assert.assertEquals(start, reloadedEvent.getBegin());
// calculate and check end date
calendar.add(Calendar.HOUR_OF_DAY, 1);
Date end = calendar.getTime();
Assert.assertEquals(end, reloadedEvent.getEnd());
Assert.assertFalse(reloadedEvent.isAllDayEvent());
Assert.assertTrue(reloadedEvent.isToday());
// 2. Test Change event
calendar.add(Calendar.HOUR_OF_DAY, 1);
Date updatedEnd = calendar.getTime();
reloadedEvent.setSubject("testEvent changed");
reloadedEvent.setEnd(updatedEnd);
calendarManager.updateEventFrom(cal, reloadedEvent);
// set manager null to force reload of calendar from file-system
emptyCalendarCache();
cal = calendarManager.getPersonalCalendar(test).getKalendar();
KalendarEvent updatedEvent = cal.getEvent(TEST_EVENT_ID, null);
Assert.assertNotNull("Could not found updated event", updatedEvent);
Assert.assertEquals("Added event has wrong subject", reloadedEvent.getSubject(), updatedEvent.getSubject());
Assert.assertEquals(start, reloadedEvent.getBegin());
Assert.assertEquals(updatedEnd, reloadedEvent.getEnd());
// 3. Test Remove event
calendarManager.removeEventFrom(cal, updatedEvent);
emptyCalendarCache();
cal = calendarManager.getPersonalCalendar(test).getKalendar();
KalendarEvent removedEvent = cal.getEvent(TEST_EVENT_ID, null);
assertNull("Found removed event", removedEvent);
}
use of org.olat.commons.calendar.model.Kalendar in project openolat by klemens.
the class ICalFileCalendarManagerTest method testTodayEvent.
/**
* Check a NPE
* @throws IOException
*/
@Test
public void testTodayEvent() throws IOException {
Identity test = JunitTestHelper.createAndPersistIdentityAsRndUser("ical-3-");
Kalendar cal = calendarManager.getPersonalCalendar(test).getKalendar();
// 1. Test Today Event
String eventId = "today-" + UUID.randomUUID();
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.MILLISECOND, 0);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.HOUR_OF_DAY, 8);
Date start = calendar.getTime();
calendar.set(Calendar.HOUR_OF_DAY, 12);
Date end = calendar.getTime();
KalendarEvent testEvent = new KalendarEvent(eventId, null, "Today Event", start, end);
calendarManager.addEventTo(cal, testEvent);
// Next days event
String nextEventId = "next-" + UUID.randomUUID();
calendar = Calendar.getInstance();
calendar.set(Calendar.MILLISECOND, 0);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.add(Calendar.DATE, 3);
calendar.set(Calendar.HOUR_OF_DAY, 8);
Date nextStart = calendar.getTime();
calendar.set(Calendar.HOUR_OF_DAY, 12);
Date nextEnd = calendar.getTime();
KalendarEvent nextEvent = new KalendarEvent(nextEventId, null, "Next Event", nextStart, nextEnd);
calendarManager.addEventTo(cal, nextEvent);
// 2. reload and test
emptyCalendarCache();
KalendarEvent reloadedEvent = calendarManager.getPersonalCalendar(test).getKalendar().getEvent(eventId, null);
Assert.assertNotNull(reloadedEvent);
Assert.assertEquals("Today Event", reloadedEvent.getSubject());
Assert.assertEquals(start, reloadedEvent.getBegin());
Assert.assertEquals(end, reloadedEvent.getEnd());
Assert.assertTrue(reloadedEvent.isToday());
Assert.assertTrue(reloadedEvent.isWithinOneDay());
Assert.assertFalse(reloadedEvent.isAllDayEvent());
KalendarEvent reloadedNextEvent = calendarManager.getPersonalCalendar(test).getKalendar().getEvent(nextEventId, null);
Assert.assertNotNull(reloadedNextEvent);
Assert.assertEquals("Next Event", reloadedNextEvent.getSubject());
Assert.assertEquals(nextStart, reloadedNextEvent.getBegin());
Assert.assertEquals(nextEnd, reloadedNextEvent.getEnd());
Assert.assertFalse(reloadedNextEvent.isToday());
Assert.assertTrue(reloadedNextEvent.isWithinOneDay());
Assert.assertFalse(reloadedNextEvent.isAllDayEvent());
}
Aggregations