X-Git-Url: http://git.argeo.org/?a=blobdiff_plain;f=runtime%2Forg.argeo.slc.support.simple%2Fsrc%2Fmain%2Fjava%2Forg%2Fargeo%2Fslc%2Funit%2FAbstractSpringTestCase.java;h=8cf11f6166183031bc23339c4633fd266173996d;hb=9daa55ce316d52ffd8f30dc0d1b516ccf78a8c73;hp=65d917cfe1755bf20786b83c40ffdb5d31f3e62d;hpb=01b5a7894ada9c5ef16770e6ce962a7fd9d45276;p=gpl%2Fargeo-slc.git diff --git a/runtime/org.argeo.slc.support.simple/src/main/java/org/argeo/slc/unit/AbstractSpringTestCase.java b/runtime/org.argeo.slc.support.simple/src/main/java/org/argeo/slc/unit/AbstractSpringTestCase.java index 65d917cfe..8cf11f616 100644 --- a/runtime/org.argeo.slc.support.simple/src/main/java/org/argeo/slc/unit/AbstractSpringTestCase.java +++ b/runtime/org.argeo.slc.support.simple/src/main/java/org/argeo/slc/unit/AbstractSpringTestCase.java @@ -1,11 +1,14 @@ package org.argeo.slc.unit; +import java.util.Map; + import junit.framework.TestCase; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.argeo.slc.SlcException; -import org.argeo.slc.spring.SpringUtils; +import org.springframework.beans.factory.BeanFactoryUtils; +import org.springframework.beans.factory.ListableBeanFactory; import org.springframework.context.ConfigurableApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; @@ -23,24 +26,25 @@ public abstract class AbstractSpringTestCase extends TestCase { if (context == null) { context = new ClassPathXmlApplicationContext( getApplicationContextLocation()); - if(getIsStartContext()) + if (getIsStartContext()) context.start(); } return context; } - + /** Whether the context should be started after being created. */ - protected Boolean getIsStartContext(){ + protected Boolean getIsStartContext() { return false; } /** 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 = SpringUtils.loadSingleFromContext(getContext(), clss); + T bean = loadSingleFromContext(getContext(), clss); if (bean == null) { throw new SlcException("Cannot retrieve a unique bean of type " + clss); @@ -66,4 +70,24 @@ public abstract class AbstractSpringTestCase extends TestCase { 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; + } + } + }