use of org.olat.commons.calendar.model.KalendarEvent in project openolat by klemens.
the class ICalFileCalendarManagerTest method testCalendarRecurringEventMissingEndDate.
/**
* A recurring event with missing end date. This error
* in the calendar cause the whole calendar to crash.
*
* @throws URISyntaxException
* @throws IOException
*/
@Test
public void testCalendarRecurringEventMissingEndDate() throws URISyntaxException, IOException {
Identity test = JunitTestHelper.createAndPersistIdentityAsRndUser("ur1-");
URL calendarUrl = CalendarImportTest.class.getResource("RecurringEventMissingEnd.ics");
File calendarFile = new File(calendarUrl.toURI());
String calendarName = UUID.randomUUID().toString().replace("-", "");
KalendarRenderWrapper importedCalendar = importCalendarManager.importCalendar(test, calendarName, CalendarManager.TYPE_USER, calendarFile);
List<KalendarEvent> events = importedCalendar.getKalendar().getEvents();
Assert.assertEquals(1, events.size());
Calendar cal = Calendar.getInstance();
cal.set(2018, 03, 10, 10, 00);
Date startDate = cal.getTime();
cal.set(2018, 03, 17);
Date endDate = cal.getTime();
List<KalendarEvent> recurringEvents = calendarManager.getEvents(importedCalendar.getKalendar(), startDate, endDate, true);
Assert.assertEquals(0, recurringEvents.size());
}
use of org.olat.commons.calendar.model.KalendarEvent in project openolat by klemens.
the class ICalFileCalendarManagerTest method testListEventsForPeriod.
@Test
public void testListEventsForPeriod() {
final int numEvents = 10000;
// maximum of 14 days duration
final int maxEventDuratio = 1000 * 60 * 60 * 24 * 14;
final int oneYearSec = 60 * 60 * 24 * 365;
final int goBackNumYears = 1;
final long kalendarStart = new Date().getTime() - (((long) goBackNumYears * oneYearSec) * 1000);
Identity test = JunitTestHelper.createAndPersistIdentityAsUser("test");
Kalendar kalendar = new Kalendar("test", CalendarManager.TYPE_USER);
log.info("*** Starting test with the following configuration:");
log.info("*** Number of events: " + numEvents);
log.info("*** Maximum event duration (ms): " + maxEventDuratio);
log.info("*** Generate events in between " + new Date(kalendarStart) + " and " + new Date(kalendarStart + (1000 * ((long) goBackNumYears * oneYearSec))));
Random rand = new Random();
long startUsed = Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory();
for (int i = 0; i < numEvents; i++) {
long begin = kalendarStart + (1000 * ((long) rand.nextInt(goBackNumYears * oneYearSec)));
String eventId = "id" + i;
KalendarEvent event = new KalendarEvent(eventId, "test" + i, new Date(begin), rand.nextInt(maxEventDuratio));
kalendar.addEvent(event);
}
long stopUsed = Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory();
log.info("*** SETUP: Kalendar structure uses approx. " + (stopUsed - startUsed) / 1024 + " kb memory.");
calendarManager.persistCalendar(kalendar);
log.info("*** Load calendar...");
long start = System.currentTimeMillis();
calendarManager.getPersonalCalendar(test).getKalendar();
long stop = System.currentTimeMillis();
log.info("Duration load: " + (stop - start) + " ms.");
log.info("*** Find events within period...");
start = System.currentTimeMillis();
List<KalendarEvent> events = calendarManager.getEvents(kalendar, new Date(kalendarStart), new Date(kalendarStart + (1000 * ((long) (goBackNumYears * oneYearSec)))), true);
stop = System.currentTimeMillis();
log.info("Duration find: " + (stop - start) + " ms.");
log.info("Found " + events.size() + " events out of " + kalendar.getEvents().size() + " total events.");
assertEquals(kalendar.getEvents().size(), events.size());
log.info("*** Save calendar...");
start = System.currentTimeMillis();
calendarManager.persistCalendar(kalendar);
stop = System.currentTimeMillis();
log.info("Duration save: " + (stop - start) + " ms.");
}
use of org.olat.commons.calendar.model.KalendarEvent in project openolat by klemens.
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.KalendarEvent in project openolat by klemens.
the class ICalFileCalendarManagerTest method testConcurrentAddRemoveEvent.
/**
* Test concurrent add/delete event with two threads and code-point to control concurrency.
*/
@Test
public void testConcurrentAddRemoveEvent() {
final String TEST_EVENT_ID_1 = "id-testConcurrentAddRemoveEvent-1";
final String TEST_EVENT_SUBJECT_1 = "testEvent1";
final String TEST_EVENT_ID_2 = "id-testConcurrentAddRemoveEvent-2";
final String TEST_EVENT_SUBJECT_2 = "testEvent2";
final Identity test = JunitTestHelper.createAndPersistIdentityAsRndUser("ical-1-");
final List<Exception> exceptionHolder = Collections.synchronizedList(new ArrayList<Exception>(1));
final List<Boolean> statusList = Collections.synchronizedList(new ArrayList<Boolean>(1));
// Generate event for update
Kalendar cal = calendarManager.getPersonalCalendar(test).getKalendar();
calendarManager.addEventTo(cal, new KalendarEvent(TEST_EVENT_ID_2, TEST_EVENT_SUBJECT_2, new Date(), 1));
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);
log.info("testConcurrentAddRemoveEvent thread2 addEvent2 DONE");
final CountDownLatch doneSignal = new CountDownLatch(2);
// thread 1
Thread thread1 = new Thread() {
public void run() {
try {
// 1. load calendar
Kalendar calendar = calendarManager.getPersonalCalendar(test).getKalendar();
// 2. add Event1 => breakpoint hit
log.info("testConcurrentAddRemoveEvent thread1 addEvent1");
calendarManager.addEventTo(calendar, new KalendarEvent(TEST_EVENT_ID_1, TEST_EVENT_SUBJECT_1, new Date(), 1));
log.info("testConcurrentAddRemoveEvent thread1 addEvent1 DONE");
// 3. check event1 exist
calendar = calendarManager.getPersonalCalendar(test).getKalendar();
KalendarEvent event1 = calendar.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)
calendar = calendarManager.getPersonalCalendar(test).getKalendar();
event1 = calendar.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("testConcurrentAddRemoveEvent 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 {
Kalendar calendar = calendarManager.getPersonalCalendar(test).getKalendar();
// 3. add Event2 (breakpoint of thread1 blocks)
log.info("testConcurrentAddRemoveEvent thread2 removeEvent2");
boolean removed = calendarManager.removeEventFrom(calendar, new KalendarEvent(TEST_EVENT_ID_2, TEST_EVENT_SUBJECT_2, new Date(), 1));
assertTrue(removed);
log.info("testConcurrentAddRemoveEvent thread1 removeEvent2 DONE");
// 4. check event2 exist
calendar = calendarManager.getPersonalCalendar(test).getKalendar();
KalendarEvent updatedEvent = calendar.getEvent(TEST_EVENT_ID_2, null);
assertNull("Still found deleted event with id=" + TEST_EVENT_ID_2, updatedEvent);
// 5. check event1 exist
calendar = calendarManager.getPersonalCalendar(test).getKalendar();
KalendarEvent event1 = calendar.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("testConcurrentAddRemoveEvent 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("testConcurrentAddRemoveEvent finish successful");
}
use of org.olat.commons.calendar.model.KalendarEvent in project openolat by klemens.
the class ICalFileCalendarManagerTest method testWithinOneDay.
@Test
public void testWithinOneDay() throws IOException {
Identity test = JunitTestHelper.createAndPersistIdentityAsRndUser("ical-4-");
Kalendar cal = calendarManager.getPersonalCalendar(test).getKalendar();
// 1. Test Today Event
String eventId = "short-" + 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, 14);
Date start = calendar.getTime();
calendar.set(Calendar.HOUR_OF_DAY, 15);
Date end = calendar.getTime();
KalendarEvent testEvent = new KalendarEvent(eventId, null, "Short Event", start, end);
calendarManager.addEventTo(cal, testEvent);
// Next days event
String nextEventId = "long-" + 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, 8);
Date nextStart = calendar.getTime();
calendar = Calendar.getInstance();
calendar.set(Calendar.MILLISECOND, 0);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.add(Calendar.DATE, 6);
calendar.set(Calendar.HOUR_OF_DAY, 18);
Date nextEnd = calendar.getTime();
KalendarEvent nextEvent = new KalendarEvent(nextEventId, null, "Long 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("Short 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("Long Event", reloadedNextEvent.getSubject());
Assert.assertEquals(nextStart, reloadedNextEvent.getBegin());
Assert.assertEquals(nextEnd, reloadedNextEvent.getEnd());
Assert.assertFalse(reloadedNextEvent.isToday());
Assert.assertFalse(reloadedNextEvent.isWithinOneDay());
Assert.assertFalse(reloadedNextEvent.isAllDayEvent());
}
Aggregations