/**
* 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.lifecycle;
import junit.framework.Test;
import junit.framework.TestSuite;
import org.apache.log4j.Logger;
import org.olat.basesecurity.ManagerFactory;
import org.olat.core.commons.persistence.DBFactory;
import org.olat.core.id.Identity;
import org.olat.core.id.OLATResourceable;
import org.olat.core.logging.Tracing;
import org.olat.core.test.OlatTestCase;
import org.olat.core.util.Encoder;
import org.olat.group.BusinessGroup;
import org.olat.group.BusinessGroupManager;
import org.olat.group.BusinessGroupManagerImpl;
import org.olat.login.OLATAuthenticationController;
import org.olat.resource.OLATResourceManager;
/**
* Initial Date: Mar 11, 2004
*
* @author Mike Stock
*
* Comment:
*
*/
public class LifeCycleManagerTest extends OlatTestCase implements OLATResourceable {
private long RESOURCE_ID = 144;
private String RESOURCE_TYPE = "org.olat.commons.lifecycle.LifeCycleManagerTest";
private static Logger log = Logger.getLogger(LifeCycleManagerTest.class);
private static boolean isInitialized = false;
private static Identity identity = null;
private static BusinessGroup group = null;
private static org.olat.resource.OLATResource res = null;
/**
* @param arg0
*/
public LifeCycleManagerTest(String arg0) {
super(arg0);
}
/**
* @return Test
* @throws Exception
*/
public static Test suite() throws Exception {
/*
* commented out as it breaks build on nightly for the moment
* follow http://bugs.olat.org/jira/browse/OLAT-2083
*/
return new TestSuite(LifeCycleManagerTest.class);
}
/**
* @see junit.framework.TestCase#setUp()
*/
public void setUp() {
/*
* commented out as it breaks build on nightly for the moment
* follow http://bugs.olat.org/jira/browse/OLAT-2083
*/
if (LifeCycleManagerTest.isInitialized == false) {
try {
Tracing.logInfo("setUp", this.getClass());
DBFactory.getJunitInstance().clearDatabase();
// identity with null User should be ok for test case
res = OLATResourceManager.getInstance().createOLATResourceInstance(this);
OLATResourceManager.getInstance().saveOLATResource(res);
identity = ManagerFactory.getManager().createAndPersistIdentity("foo", null, OLATAuthenticationController.PROVIDER_OLAT, "foo", Encoder.encrypt("hashipassi"));
BusinessGroupManager gm = BusinessGroupManagerImpl.getInstance();
group = gm.createAndPersistBusinessGroup(BusinessGroup.TYPE_BUDDYGROUP,
identity, "a buddygroup", "a desc", null, null, null/* enableWaitinglist */, null/* enableAutoCloseRanks */, null);
LifeCycleManagerTest.isInitialized = true;
} catch (Exception e) {
log.error(
"Error while generating database tables or opening hibernate session: " +
e);
}
}
}
/**
* @see junit.framework.TestCase#tearDown()
*/
public void tearDown() {
try {
Tracing.logInfo("tearDown: DB.getInstance().closeSession()", this.getClass());
DBFactory.getInstance().closeSession();
} catch (Exception e) {
log.error("tearDown failed: ", e);
}
}
/**
* Test creation of LifeCycleManager.
*/
public void testCreateInstanceFor() {
LifeCycleManager lcm1 = LifeCycleManager.createInstanceFor(group);
LifeCycleManager lcm2 = LifeCycleManager.createInstanceFor(res);
this.assertNotSame("testCreateInstanceFor should NOT return the same instance", lcm1,lcm2);
}
/**
* Test: mark two timestamp in different context.
*/
public void testMarkTimestampFor() {
String action = "doTest";
LifeCycleManager lcm1 = LifeCycleManager.createInstanceFor(group);
LifeCycleManager lcm2 = LifeCycleManager.createInstanceFor(res);
lcm1.markTimestampFor(action);
lcm2.markTimestampFor(action);
DBFactory.getInstance().closeSession();
LifeCycleEntry lce = lcm1.lookupLifeCycleEntry(action, null);
this.assertNotNull("Does not found LifeCycleEntry", lce);
this.assertEquals("Invalid action",lce.getAction(), action);
// try second instance of LifeCycleManager
LifeCycleEntry lce2 = lcm2.lookupLifeCycleEntry(action, null);
this.assertNotNull("Does not found LifeCycleEntry", lce2);
this.assertNotSame("LifeCycleEntry have nit the same reference", lce2.getPersistentRef(), lce.getPersistentRef());
this.assertNotSame("LifeCycleEntry have nit the same type-name",lce2.getPersistentTypeName(), lce.getPersistentTypeName());
}
/**
* Test: Delete Timestamp for certain action
*/
public void testDeleteTimestampFor() {
String action = "doTest";
LifeCycleManager lcm1 = LifeCycleManager.createInstanceFor(group);
LifeCycleEntry lce = lcm1.lookupLifeCycleEntry(action, null);
this.assertNotNull("Does not found LifeCycleEntry", lce);
lcm1.deleteTimestampFor(action);
LifeCycleEntry lce2 = lcm1.lookupLifeCycleEntry(action, null);
this.assertNull("Found deleted LifeCycleEntry", lce2);
}
//////////////////////////////
// Implements OLATResourceable
//////////////////////////////
/**
* @see org.olat.core.id.OLATResourceablegetResourceableTypeName()
*/
public String getResourceableTypeName() {
return RESOURCE_TYPE;
}
/**
* @see org.olat.core.id.OLATResourceablegetResourceableId()
*/
public Long getResourceableId() {
return new Long(RESOURCE_ID);
}
}