1 package org
.argeo
.slc
.core
.test
.context
;
5 import junit
.framework
.TestCase
;
7 import org
.argeo
.api
.cms
.CmsLog
;
8 import org
.argeo
.slc
.SlcException
;
9 import org
.springframework
.beans
.factory
.BeanFactoryUtils
;
10 import org
.springframework
.beans
.factory
.ListableBeanFactory
;
11 import org
.springframework
.context
.ConfigurableApplicationContext
;
12 import org
.springframework
.context
.support
.ClassPathXmlApplicationContext
;
14 /** Helper for tests using a Spring application context. */
15 public abstract class AbstractInternalSpringTestCase
extends TestCase
{
16 protected final CmsLog log
= CmsLog
.getLog(getClass());
17 private ConfigurableApplicationContext context
;
20 * Gets (and create if necessary) the application context to use. Default
21 * implementation uses a class path xml application context and calls
22 * {@link #getApplicationContextLocation()}.
24 protected ConfigurableApplicationContext
getContext() {
25 if (context
== null) {
26 context
= new ClassPathXmlApplicationContext(
27 getApplicationContextLocation());
32 /** Returns a bean from the underlying context */
33 @SuppressWarnings(value
= { "unchecked" })
34 protected <T
> T
getBean(String beanId
) {
35 return (T
) getContext().getBean(beanId
);
38 protected <T
> T
getBean(Class
<?
extends T
> clss
) {
39 T bean
= loadSingleFromContext(getContext(), clss
);
41 throw new SlcException("Cannot retrieve a unique bean of type "
49 * Th location of the application to load. The default implementation
50 * returns <i>applicationContext.xml</i> found in the same package as the
53 protected String
getApplicationContextLocation() {
54 return inPackage("applicationContext.xml");
58 * Prefixes the package of the class after converting the '.' to '/' in
59 * order to have a resource path.
61 protected String
inPackage(String suffix
) {
62 String prefix
= getClass().getPackage().getName().replace('.', '/');
63 return prefix
+ '/' + suffix
;
66 @SuppressWarnings(value
= { "unchecked" })
67 protected <T
> T
loadSingleFromContext(ListableBeanFactory context
,
69 Map
<String
, T
> beans
= BeanFactoryUtils
.beansOfTypeIncludingAncestors(
70 context
, clss
, false, false);
71 if (beans
.size() == 1) {
72 return beans
.values().iterator().next();
73 } else if (beans
.size() > 1) {
74 if (log
.isDebugEnabled()) {
76 .debug(("Found more that on bean for type " + clss
77 + ": " + beans
.keySet()));