X-Git-Url: http://git.argeo.org/?a=blobdiff_plain;f=org.argeo.slc.core%2Fsrc%2Fmain%2Fjava%2Forg%2Fargeo%2Fslc%2Funit%2FAbstractSpringTestCase.java;h=a37c4a7ceb4147cdff4e031d2315cd02ea3d9b16;hb=42aa6ffce6a95107cd10f908e5f9c695d902d571;hp=06c6cf6b529b49dd642d11cb7baf391b7bda3878;hpb=4a32164c4b6e1b1fced50f37e503965c0e3843a9;p=gpl%2Fargeo-slc.git diff --git a/org.argeo.slc.core/src/main/java/org/argeo/slc/unit/AbstractSpringTestCase.java b/org.argeo.slc.core/src/main/java/org/argeo/slc/unit/AbstractSpringTestCase.java index 06c6cf6b5..a37c4a7ce 100644 --- a/org.argeo.slc.core/src/main/java/org/argeo/slc/unit/AbstractSpringTestCase.java +++ b/org.argeo.slc.core/src/main/java/org/argeo/slc/unit/AbstractSpringTestCase.java @@ -2,29 +2,44 @@ package org.argeo.slc.unit; import junit.framework.TestCase; -import org.springframework.context.ApplicationContext; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.argeo.slc.core.SlcException; +import org.argeo.slc.spring.SpringUtils; +import org.springframework.context.ConfigurableApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; /** Helper for tests using a Spring application co,text. */ public abstract class AbstractSpringTestCase extends TestCase { - private ApplicationContext context; + protected final Log log = LogFactory.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 ApplicationContext getContext() { + protected ConfigurableApplicationContext getContext() { if (context == null) { context = new ClassPathXmlApplicationContext( getApplicationContextLocation()); } return context; } - + /** Returns a bean from the underlying context */ - protected T getBean(String beanId){ - return (T)getContext().getBean(beanId); + protected T getBean(String beanId) { + return (T) getContext().getBean(beanId); + } + + protected T getBean(Class clss) { + T bean = SpringUtils.loadSingleFromContext(getContext(), clss); + if (bean == null) { + throw new SlcException("Cannot retrieve a unique bean of type " + + clss); + } else { + return bean; + } } /**