Package org.olat.commons.calendar

Source Code of org.olat.commons.calendar.ICalFileCalendarManagerTest

/**
* OLAT - Online Learning and Training<br>
* http://www.olat.org
* <p>
* Licensed under the Apache License, Version 2.0 (the "License"); <br>
* you may not use this file except in compliance with the License.<br>
* You may obtain a copy of the License at
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing,<br>
* software distributed under the License is distributed on an "AS IS" BASIS, <br>
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. <br>
* See the License for the specific language governing permissions and <br>
* limitations under the License.
* <p>
* Copyright (c) since 2004 at Multimedia- & E-Learning Services (MELS),<br>
* University of Zurich, Switzerland.
* <p>
*/

package org.olat.commons.calendar;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

import junit.framework.Test;
import junit.framework.TestSuite;

import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.command.ActiveMQQueue;
import org.apache.log4j.Logger;
import org.olat.basesecurity.ManagerFactory;
import org.olat.commons.calendar.model.Kalendar;
import org.olat.commons.calendar.model.KalendarEvent;
import org.olat.core.commons.persistence.DBFactory;
import org.olat.core.id.Identity;
import org.olat.core.test.OlatTestCase;
import org.olat.core.util.Encoder;
import org.olat.login.OLATAuthenticationController;
import org.olat.testutils.codepoints.client.BreakpointStateException;
import org.olat.testutils.codepoints.client.CodepointClient;
import org.olat.testutils.codepoints.client.CodepointClientFactory;
import org.olat.testutils.codepoints.client.CodepointRef;
import org.olat.testutils.codepoints.client.CommunicationException;
import org.olat.testutils.codepoints.client.TemporaryPausedThread;
import org.olat.testutils.codepoints.server.CodepointInstaller;
import org.olat.testutils.codepoints.server.impl.JMSCodepointServer;


/**
*
*/
public class ICalFileCalendarManagerTest extends OlatTestCase {

  private static Logger log = Logger.getLogger(ICalFileCalendarManagerTest.class.getName());
  private static boolean isInitialized = false;
  private static String  CODEPOINT_SERVER_ID = "ICalFileCalendarManagerTest";
  private static Identity test = null;
  private JMSCodepointServer codepointServer_;


  /**
   * @param name
   */
  public ICalFileCalendarManagerTest(String name) {
    super(name);
  }

  /**
   * @return Test
   */
  public static Test suite() {
    return new TestSuite(ICalFileCalendarManagerTest.class);
  }
 
  public void testAddChangeRemoveEvent() {
    String TEST_EVENT_ID = "id-testAddEvent";
    CalendarManager manager = CalendarManagerFactory.getJUnitInstance().getCalendarManager();
    Kalendar cal = manager.getPersonalCalendar(test).getKalendar();
    // 1. Test Add Event
    KalendarEvent testEvent = new KalendarEvent(TEST_EVENT_ID,"testEvent", new Date(), 1);
    manager.addEventTo(cal, testEvent);
    // set manager null to force reload of calendar from file-system
    manager = null;
    manager = CalendarManagerFactory.getJUnitInstance().getCalendarManager();
    cal = manager.getPersonalCalendar(test).getKalendar();
    KalendarEvent reloadedEvent = cal.getEvent(TEST_EVENT_ID);
    assertNotNull("Could not found added event", reloadedEvent);
    assertEquals("Added event has wrong subject", testEvent.getSubject(),reloadedEvent.getSubject());
    // 2. Test Change event
    reloadedEvent.setSubject("testEvent changed");
    manager.updateEventFrom(cal, reloadedEvent);
    // set manager null to force reload of calendar from file-system
    manager = null;
    manager = CalendarManagerFactory.getJUnitInstance().getCalendarManager();
    cal = manager.getPersonalCalendar(test).getKalendar();
    KalendarEvent updatedEvent = cal.getEvent(TEST_EVENT_ID);
    assertNotNull("Could not found updated event", updatedEvent);
    assertEquals("Added event has wrong subject", reloadedEvent.getSubject(),updatedEvent.getSubject());
    // 3. Test Remove event
    manager.removeEventFrom(cal, updatedEvent);
    manager = null;
    manager = CalendarManagerFactory.getJUnitInstance().getCalendarManager();
    cal = manager.getPersonalCalendar(test).getKalendar();
    KalendarEvent removedEvent = cal.getEvent(TEST_EVENT_ID);
    assertNull("Found removed event", removedEvent);
  }


  /**
   * Test concurrent add event with two threads and code-point to control concurrency.
   *
   */
  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 List<Exception> exceptionHolder = Collections.synchronizedList(new ArrayList<Exception>(1));
    final List<Boolean> statusList = Collections.synchronizedList(new ArrayList<Boolean>(1));

    // enable breakpoint

    CodepointClient codepointClient = null;
    CodepointRef codepointRef = null;
    try {
      codepointClient = CodepointClientFactory.createCodepointClient("vm://localhost?broker.persistent=false", CODEPOINT_SERVER_ID);
      codepointRef = codepointClient.getCodepoint("org.olat.commons.coordinate.cluster.ClusterSyncer.doInSync-in-sync.org.olat.commons.calendar.ICalFileCalendarManager.addEventTo");
      codepointRef.enableBreakpoint();
      System.out.println();
    } catch (Exception e) {
      e.printStackTrace();
      fail("Could not initialzed CodepointClient");
    }

    // thread 1
    new Thread(new Runnable() {
      public void run() {
        try {
          // 1. load calendar
          CalendarManager calManager = CalendarManagerFactory.getJUnitInstance().getCalendarManager();
          Kalendar cal = calManager.getPersonalCalendar(test).getKalendar();
         
          // 2. add Event1 => breakpoint hit         
          System.out.println("testConcurrentAddEvent thread1 addEvent1");
          calManager.addEventTo(cal, new KalendarEvent(TEST_EVENT_ID_1,TEST_EVENT_SUBJECT_1, new Date(), 1));
          System.out.println("testConcurrentAddEvent thread1 addEvent1 DONE");
          // 3. check event1 exist
          cal = calManager.getPersonalCalendar(test).getKalendar();
          KalendarEvent event1 = cal.getEvent(TEST_EVENT_ID_1);
          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 = calManager.getPersonalCalendar(test).getKalendar();
          event1 = cal.getEvent(TEST_EVENT_ID_1);
          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);
          System.out.println("testConcurrentAddEvent thread1 finished");
        } catch (Exception ex) {
          exceptionHolder.add(ex);// no exception should happen
        }
      }}).start();
   
    // thread 2
    new Thread(new Runnable() {
      public void run() {
        try {
          // 1. load calendar
          CalendarManager calManager = CalendarManagerFactory.getJUnitInstance().getCalendarManager();
          Kalendar cal = calManager.getPersonalCalendar(test).getKalendar();
          // 2. sleep 1sec
          sleep(1000);
          // 3. add Event2 (breakpoint of thread1 blocks)
          System.out.println("testConcurrentAddEvent thread2 addEvent2");
          calManager.addEventTo(cal, new KalendarEvent(TEST_EVENT_ID_2,TEST_EVENT_SUBJECT_2, new Date(), 1));
          System.out.println("testConcurrentAddEvent thread1 addEvent2 DONE");
          // 4. check event2 exist
          cal = calManager.getPersonalCalendar(test).getKalendar();
          KalendarEvent event2 = cal.getEvent(TEST_EVENT_ID_2);
          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 = calManager.getPersonalCalendar(test).getKalendar();
          KalendarEvent event1 = cal.getEvent(TEST_EVENT_ID_1);
          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);
          System.out.println("testConcurrentAddEvent thread2 finished");
        } catch (Exception ex) {
          exceptionHolder.add(ex);// no exception should happen
        }
      }}).start();

    sleep(2000);
    try {
      // to see all registered code-points: comment-in next 2 lines
      // List<CodepointRef> codepointList = codepointClient.listAllCodepoints();
      // System.out.println("codepointList=" + codepointList);
      System.out.println("testConcurrentAddEvent start waiting for breakpoint reached");
      TemporaryPausedThread[] threads = codepointRef.waitForBreakpointReached(1000);
      assertTrue("Did not reach breakpoint", threads.length > 0);
      System.out.println("threads[0].getCodepointRef()=" + threads[0].getCodepointRef());
      codepointRef.disableBreakpoint(true);
      System.out.println("testConcurrentAddEvent breakpoint reached => continue");
    } catch (BreakpointStateException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
      fail("Codepoints: BreakpointStateException=" + e.getMessage());
    } catch (CommunicationException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
      fail("Codepoints: CommunicationException=" + e.getMessage());
    }
 
    // sleep until t1 and t2 should have terminated/excepted
    int loopCount = 0;
    while ( (statusList.size()<2) && (exceptionHolder.size()<1) && (loopCount<5)) {
      sleep(1000);
      loopCount++;
    }
    assertTrue("Threads did not finish in 5sec", loopCount<5);
    // if not -> they are in deadlock and the db did not detect it
    for (Exception exception : exceptionHolder) {
      System.out.println("exception: "+exception.getMessage());
      exception.printStackTrace();
    }
    if (exceptionHolder.size() > 0) {
      assertTrue("It throws an exception in test => see sysout exception[0]=" + exceptionHolder.get(0).getMessage(), exceptionHolder.size() == 0)
    }
    codepointClient.close();
    System.out.println("testConcurrentAddEvent finish successful");
  }
 
  /**
   * Test concurrent add/update event with two threads and code-point to control concurrency.
   *
   */
  public void testConcurrentAddUpdateEvent() {
    final String TEST_EVENT_ID_1 = "id-testConcurrentAddUpdateEvent-1";
    final String TEST_EVENT_SUBJECT_1 = "testEvent1";
    final String TEST_EVENT_ID_2 = "id-testConcurrentAddUpdateEvent-2";
    final String TEST_EVENT_SUBJECT_2 = "testEvent2";
    final String TEST_EVENT_SUBJECT_2_UPDATED = "testUpdatedEvent2";
   

    final List<Exception> exceptionHolder = Collections.synchronizedList(new ArrayList<Exception>(1));
    final List<Boolean> statusList = Collections.synchronizedList(new ArrayList<Boolean>(1));

    // Generate event for update
    CalendarManager calManager = CalendarManagerFactory.getJUnitInstance().getCalendarManager();
    Kalendar cal = calManager.getPersonalCalendar(test).getKalendar();
    calManager.addEventTo(cal, new KalendarEvent(TEST_EVENT_ID_2,TEST_EVENT_SUBJECT_2, new Date(), 1));
    cal = calManager.getPersonalCalendar(test).getKalendar();
    KalendarEvent event2 = cal.getEvent(TEST_EVENT_ID_2);
    assertNotNull("Did not found event with id=" + TEST_EVENT_ID_2, event2);
    assertEquals("Wrong calendar-event subject",event2.getSubject(), TEST_EVENT_SUBJECT_2);
    System.out.println("testConcurrentAddUpdateEvent thread2 addEvent2 DONE");

    // enable breakpoint
    CodepointClient codepointClient = null;
    CodepointRef codepointRef = null;
    try {
      codepointClient = CodepointClientFactory.createCodepointClient("vm://localhost?broker.persistent=false", CODEPOINT_SERVER_ID);
      codepointRef = codepointClient.getCodepoint("org.olat.commons.coordinate.cluster.ClusterSyncer.doInSync-in-sync.org.olat.commons.calendar.ICalFileCalendarManager.addEventTo");
      codepointRef.enableBreakpoint();
      System.out.println();
    } catch (Exception e) {
      e.printStackTrace();
      fail("Could not initialzed CodepointClient");
    }

    // thread 1
    new Thread(new Runnable() {
      public void run() {
        try {
          // 1. load calendar
          CalendarManager calManager = CalendarManagerFactory.getJUnitInstance().getCalendarManager();
          Kalendar cal = calManager.getPersonalCalendar(test).getKalendar();
         
          // 2. add Event1 => breakpoint hit         
          System.out.println("testConcurrentAddUpdateEvent thread1 addEvent1");
          calManager.addEventTo(cal, new KalendarEvent(TEST_EVENT_ID_1,TEST_EVENT_SUBJECT_1, new Date(), 1));
          System.out.println("testConcurrentAddUpdateEvent thread1 addEvent1 DONE");
          // 3. check event1 exist
          cal = calManager.getPersonalCalendar(test).getKalendar();
          KalendarEvent event1 = cal.getEvent(TEST_EVENT_ID_1);
          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 = calManager.getPersonalCalendar(test).getKalendar();
          event1 = cal.getEvent(TEST_EVENT_ID_1);
          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);
          System.out.println("testConcurrentAddUpdateEvent thread1 finished");
        } catch (Exception ex) {
          exceptionHolder.add(ex);// no exception should happen
        }
      }}).start();
   
    // thread 2
    new Thread(new Runnable() {
      public void run() {
        try {
          CalendarManager calManager = CalendarManagerFactory.getJUnitInstance().getCalendarManager();
          Kalendar cal = calManager.getPersonalCalendar(test).getKalendar();
          // 2. sleep 1sec
          sleep(1000);
          // 3. add Event2 (breakpoint of thread1 blocks)
          System.out.println("testConcurrentAddUpdateEvent thread2 updateEvent2");
          calManager.updateEventFrom(cal, new KalendarEvent(TEST_EVENT_ID_2,TEST_EVENT_SUBJECT_2_UPDATED, new Date(), 1));
          System.out.println("testConcurrentAddUpdateEvent thread1 updateEvent2 DONE");
          // 4. check event2 exist
          cal = calManager.getPersonalCalendar(test).getKalendar();
          KalendarEvent updatedEvent = cal.getEvent(TEST_EVENT_ID_2);
          assertNotNull("Did not found event with id=" + TEST_EVENT_ID_2, updatedEvent);
          assertEquals("Wrong calendar-event subject",updatedEvent.getSubject(), TEST_EVENT_SUBJECT_2_UPDATED);
          // 5. check event1 exist
          cal = calManager.getPersonalCalendar(test).getKalendar();
          KalendarEvent event1 = cal.getEvent(TEST_EVENT_ID_1);
          assertNotNull("Did not found event with id=" + TEST_EVENT_ID_1, event1);
          assertEquals("Wrong calendar-event subject",event1.getSubject(), TEST_EVENT_SUBJECT_1);
          // Delete Event
          boolean removed = calManager.removeEventFrom(cal, new KalendarEvent(TEST_EVENT_ID_2,TEST_EVENT_SUBJECT_2_UPDATED, new Date(), 1));
          assertTrue(removed);
          statusList.add(Boolean.TRUE);
          System.out.println("testConcurrentAddUpdateEvent thread2 finished");
        } catch (Exception ex) {
          exceptionHolder.add(ex);// no exception should happen
        }
      }}).start();

    sleep(2000);
    try {
      // to see all registered code-points: comment-in next 2 lines
      // List<CodepointRef> codepointList = codepointClient.listAllCodepoints();
      // System.out.println("codepointList=" + codepointList);
      System.out.println("testConcurrentAddUpdateEvent start waiting for breakpoint reached");
      TemporaryPausedThread[] threads = codepointRef.waitForBreakpointReached(1000);
      assertTrue("Did not reach breakpoint", threads.length > 0);
      System.out.println("threads[0].getCodepointRef()=" + threads[0].getCodepointRef());
      codepointRef.disableBreakpoint(true);
      System.out.println("testConcurrentAddUpdateEvent breakpoint reached => continue");
    } catch (BreakpointStateException e) {
      e.printStackTrace();
      fail("Codepoints: BreakpointStateException=" + e.getMessage());
    } catch (CommunicationException e) {
      e.printStackTrace();
      fail("Codepoints: CommunicationException=" + e.getMessage());
    }
 
    // sleep until t1 and t2 should have terminated/excepted
    int loopCount = 0;
    while ( (statusList.size()<2) && (exceptionHolder.size()<1) && (loopCount<5)) {
      sleep(1000);
      loopCount++;
    }
    assertTrue("Threads did not finish in 5sec", loopCount<5);
    // if not -> they are in deadlock and the db did not detect it
    for (Exception exception : exceptionHolder) {
      System.out.println("exception: "+exception.getMessage());
      exception.printStackTrace();
    }
    if (exceptionHolder.size() > 0) {
      assertTrue("It throws an exception in test => see sysout exception[0]=" + exceptionHolder.get(0).getMessage(), exceptionHolder.size() == 0)
    }
    codepointClient.close();
    System.out.println("testConcurrentAddUpdateEvent finish successful");
  }
 
  /**
   * Test concurrent add/delete event with two threads and code-point to control concurrency.
   *
   */
  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 List<Exception> exceptionHolder = Collections.synchronizedList(new ArrayList<Exception>(1));
    final List<Boolean> statusList = Collections.synchronizedList(new ArrayList<Boolean>(1));

    // Generate event for update
    CalendarManager calManager = CalendarManagerFactory.getJUnitInstance().getCalendarManager();
    Kalendar cal = calManager.getPersonalCalendar(test).getKalendar();
    calManager.addEventTo(cal, new KalendarEvent(TEST_EVENT_ID_2,TEST_EVENT_SUBJECT_2, new Date(), 1));
    cal = calManager.getPersonalCalendar(test).getKalendar();
    KalendarEvent event2 = cal.getEvent(TEST_EVENT_ID_2);
    assertNotNull("Did not found event with id=" + TEST_EVENT_ID_2, event2);
    assertEquals("Wrong calendar-event subject",event2.getSubject(), TEST_EVENT_SUBJECT_2);
    System.out.println("testConcurrentAddRemoveEvent thread2 addEvent2 DONE");

    // enable breakpoint
    CodepointClient codepointClient = null;
    CodepointRef codepointRef = null;
    try {
      codepointClient = CodepointClientFactory.createCodepointClient("vm://localhost?broker.persistent=false", CODEPOINT_SERVER_ID);
      codepointRef = codepointClient.getCodepoint("org.olat.commons.coordinate.cluster.ClusterSyncer.doInSync-in-sync.org.olat.commons.calendar.ICalFileCalendarManager.addEventTo");
      codepointRef.enableBreakpoint();
      System.out.println();
    } catch (Exception e) {
      e.printStackTrace();
      fail("Could not initialzed CoepointClient");
    }

    // thread 1
    new Thread(new Runnable() {
      public void run() {
        try {
          // 1. load calendar
          CalendarManager calManager = CalendarManagerFactory.getJUnitInstance().getCalendarManager();
          Kalendar cal = calManager.getPersonalCalendar(test).getKalendar();
         
          // 2. add Event1 => breakpoint hit         
          System.out.println("testConcurrentAddRemoveEvent thread1 addEvent1");
          calManager.addEventTo(cal, new KalendarEvent(TEST_EVENT_ID_1,TEST_EVENT_SUBJECT_1, new Date(), 1));
          System.out.println("testConcurrentAddRemoveEvent thread1 addEvent1 DONE");
          // 3. check event1 exist
          cal = calManager.getPersonalCalendar(test).getKalendar();
          KalendarEvent event1 = cal.getEvent(TEST_EVENT_ID_1);
          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 = calManager.getPersonalCalendar(test).getKalendar();
          event1 = cal.getEvent(TEST_EVENT_ID_1);
          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);
          System.out.println("testConcurrentAddRemoveEvent thread1 finished");
        } catch (Exception ex) {
          exceptionHolder.add(ex);// no exception should happen
        }
      }}).start();
   
    // thread 2
    new Thread(new Runnable() {
      public void run() {
        try {
          CalendarManager calManager = CalendarManagerFactory.getJUnitInstance().getCalendarManager();
          Kalendar cal = calManager.getPersonalCalendar(test).getKalendar();
          // 2. sleep 1sec
          sleep(1000);
          // 3. add Event2 (breakpoint of thread1 blocks)
          System.out.println("testConcurrentAddRemoveEvent thread2 removeEvent2");
          boolean removed = calManager.removeEventFrom(cal, new KalendarEvent(TEST_EVENT_ID_2,TEST_EVENT_SUBJECT_2, new Date(), 1));
          assertTrue(removed);
          System.out.println("testConcurrentAddRemoveEvent thread1 removeEvent2 DONE");
          // 4. check event2 exist
          cal = calManager.getPersonalCalendar(test).getKalendar();
          KalendarEvent updatedEvent = cal.getEvent(TEST_EVENT_ID_2);
          assertNull("Still found deleted event with id=" + TEST_EVENT_ID_2, updatedEvent);
          // 5. check event1 exist
          cal = calManager.getPersonalCalendar(test).getKalendar();
          KalendarEvent event1 = cal.getEvent(TEST_EVENT_ID_1);
          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);
          System.out.println("testConcurrentAddRemoveEvent thread2 finished");
        } catch (Exception ex) {
          exceptionHolder.add(ex);// no exception should happen
        }
      }}).start();

    sleep(2000);
    try {
      // to see all registered code-points: comment-in next 2 lines
      // List<CodepointRef> codepointList = codepointClient.listAllCodepoints();
      // System.out.println("codepointList=" + codepointList);
      System.out.println("testConcurrentAddRemoveEvent start waiting for breakpoint reached");
      TemporaryPausedThread[] threads = codepointRef.waitForBreakpointReached(1000);
      assertTrue("Did not reach breakpoint", threads.length > 0);
      System.out.println("threads[0].getCodepointRef()=" + threads[0].getCodepointRef());
      codepointRef.disableBreakpoint(true);
      System.out.println("testConcurrentAddRemoveEvent breakpoint reached => continue");
    } catch (BreakpointStateException e) {
      e.printStackTrace();
      fail("Codepoints: BreakpointStateException=" + e.getMessage());
    } catch (CommunicationException e) {
      e.printStackTrace();
      fail("Codepoints: CommunicationException=" + e.getMessage());
    }
 
    // sleep until t1 and t2 should have terminated/excepted
    int loopCount = 0;
    while ( (statusList.size()<2) && (exceptionHolder.size()<1) && (loopCount<5)) {
      sleep(1000);
      loopCount++;
    }
    assertTrue("Threads did not finish in 5sec", loopCount<5);
    // if not -> they are in deadlock and the db did not detect it
    for (Exception exception : exceptionHolder) {
      System.out.println("exception: "+exception.getMessage());
      exception.printStackTrace();
    }
    if (exceptionHolder.size() > 0) {
      assertTrue("It throws an exception in test => see sysout exception[0]=" + exceptionHolder.get(0).getMessage(), exceptionHolder.size() == 0)
    }
    codepointClient.close();
    System.out.println("testConcurrentAddRemoveEvent finish successful");
  }

 
  /**
   *
   * @param milis the duration in miliseconds to sleep
   */
  private void sleep(int milis) {
    try {
      Thread.sleep(milis);
    } catch (InterruptedException e) {
      e.printStackTrace();
    }
  }

  /*
   * (non-Javadoc)
   *
   * @see junit.framework.TestCase#setUp()
   */
  protected void setUp() throws Exception {
    super.setUp();
    if (ICalFileCalendarManagerTest.isInitialized == false) {
      DBFactory.getJunitInstance().clearDatabase();
      test = ManagerFactory.getManager().createAndPersistIdentity("test", null, OLATAuthenticationController.PROVIDER_OLAT, "test",
          Encoder.encrypt("test"));
      // Setup for code-points
      ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory("vm://localhost?broker.persistent=false");
      ActiveMQQueue queue = new ActiveMQQueue("olat/codepoints");
      codepointServer_ = new JMSCodepointServer(connectionFactory, queue, CODEPOINT_SERVER_ID);
      CodepointInstaller.installCodepointServer(codepointServer_);
      System.out.println("Codepoint server startet");
      ICalFileCalendarManagerTest.isInitialized = true;
    }
    DBFactory.getInstance().closeSession();
  }

  /*
   * (non-Javadoc)
   *
   * @see junit.framework.TestCase#tearDown()
   */
  protected void tearDown() throws Exception {
    try {
      DBFactory.getInstance().closeSession();
    } catch (Exception e) {
      log.error("tearDown failed: ", e);
    }
  }

}
TOP

Related Classes of org.olat.commons.calendar.ICalFileCalendarManagerTest

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.