package org.argeo.slc.core.test.context; import java.util.Map; import junit.framework.TestCase; import org.argeo.api.cms.CmsLog; import org.argeo.slc.SlcException; import org.springframework.beans.factory.BeanFactoryUtils; import org.springframework.beans.factory.ListableBeanFactory; import org.springframework.context.ConfigurableApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; /** Helper for tests using a Spring application context. */ public abstract class AbstractInternalSpringTestCase extends TestCase { protected final CmsLog log = CmsLog.getLog(getClass()); private ConfigurableApplicationContext context; /** * Gets (and create if necessary) the application context to use. Default * implementation uses a class path xml application context and calls * {@link #getApplicationContextLocation()}. */ protected ConfigurableApplicationContext getContext() { if (context == null) { context = new ClassPathXmlApplicationContext( getApplicationContextLocation()); } return context; } /** Returns a bean from the underlying context */ @SuppressWarnings(value = { "unchecked" }) protected T getBean(String beanId) { return (T) getContext().getBean(beanId); } protected T getBean(Class clss) { T bean = loadSingleFromContext(getContext(), clss); if (bean == null) { throw new SlcException("Cannot retrieve a unique bean of type " + clss); } else { return bean; } } /** * Th location of the application to load. The default implementation * returns applicationContext.xml found in the same package as the * test. */ protected String getApplicationContextLocation() { return inPackage("applicationContext.xml"); } /** * Prefixes the package of the class after converting the '.' to '/' in * order to have a resource path. */ protected String inPackage(String suffix) { String prefix = getClass().getPackage().getName().replace('.', '/'); return prefix + '/' + suffix; } @SuppressWarnings(value = { "unchecked" }) protected T loadSingleFromContext(ListableBeanFactory context, Class clss) { Map beans = BeanFactoryUtils.beansOfTypeIncludingAncestors( context, clss, false, false); if (beans.size() == 1) { return beans.values().iterator().next(); } else if (beans.size() > 1) { if (log.isDebugEnabled()) { log .debug(("Found more that on bean for type " + clss + ": " + beans.keySet())); } return null; } else { return null; } } }