/********************************************************************************** * $URL: https://source.sakaiproject.org/svn/kernel/trunk/test-harness/src/main/java/org/sakaiproject/test/SakaiTestBase.java $ * $Id: SakaiTestBase.java 105077 2012-02-24 22:54:29Z ottenhoff@longsight.com $ *********************************************************************************** * * Copyright (c) 2005, 2006, 2007, 2008 The Sakai Foundation * * Licensed under the Educational Community License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.opensource.org/licenses/ECL-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * **********************************************************************************/ package org.sakaiproject.test; import static org.sakaiproject.test.ComponentContainerEmulator.setTestSakaiHome; import java.lang.reflect.InvocationHandler; import java.lang.reflect.Proxy; import junit.framework.TestCase; /** * An extension of JUnit's TestCase that launches the Sakai component manager. * Extend this class to run tests in a simulated Sakai environment. * * <p> * <strong>NOTE:</strong> * Starting the component manager is an expensive operation, since it loads all * of the service implementations in the system, including database connection * pools, hibernate mappings, etc. To run a test suite, please collect all tests * into a single class rather than running a variety of individual test cases. * See {@link org.sakaiproject.test.SakaiIntegrationTest} for an example. * </p> * * @author <a href="mailto:jholtzman@berkeley.edu">Josh Holtzman</a> * */ public abstract class SakaiTestBase extends TestCase { /** * Initialize the component manager once for all tests. */ public static void oneTimeSetup() throws Exception { if(!ComponentContainerEmulator.isStarted()) { ComponentContainerEmulator.startComponentManagerForTest(); } } /** * Close the component manager when the tests finish. */ public static void oneTimeTearDown() { if(ComponentContainerEmulator.isStarted()) { ComponentContainerEmulator.stopComponentManager(); } } /** * Convenience method to get a service bean from the Sakai component manager. * * @param beanId The id of the service * * @return The service, or null if the ID is not registered */ protected static Object getService(String beanId) { return ComponentContainerEmulator.getService(beanId); } protected static <T> T getService(Class<T> clazz) { return ComponentContainerEmulator.getService(clazz); } /** * Convenience method to create a somewhat unique site id for testing. Useful * in tests that need to create a site to run tests upon. * * @return A string suitable for using as a site id. */ protected String generateSiteId() { return "site-" + getClass().getName() + "-" + Math.floor(Math.random()*100000); } protected static final void setSakaiHome(String path) { setTestSakaiHome(path); } /** * Returns a dynamic proxy for a service interface. Useful for testing with * customized service implementations without needing to write custom stubs. * * @param clazz The service interface class * @param handler The invocation handler that defines how the dynamic proxy should behave * * @return The dynamic proxy to use as a collaborator */ public static final <T> T getServiceProxy(Class<T> clazz, InvocationHandler handler) { return (T)Proxy.newProxyInstance(Thread.currentThread().getContextClassLoader(), new Class[] {clazz}, handler); } }