]> git.argeo.org Git - gpl/argeo-slc.git/blob - unit/AbstractSpringTestCase.java
Prepare next development cycle
[gpl/argeo-slc.git] / unit / AbstractSpringTestCase.java
1 package org.argeo.slc.spring.unit;
2
3 import java.util.Map;
4
5 import org.argeo.api.cms.CmsLog;
6 import org.argeo.slc.SlcException;
7 import org.springframework.beans.factory.BeanFactoryUtils;
8 import org.springframework.beans.factory.ListableBeanFactory;
9 import org.springframework.context.ConfigurableApplicationContext;
10 import org.springframework.context.support.ClassPathXmlApplicationContext;
11
12 import junit.framework.TestCase;
13
14 /** Helper for tests using a Spring application co,text. */
15 public abstract class AbstractSpringTestCase extends TestCase {
16 protected final CmsLog log = CmsLog.getLog(getClass());
17 private ConfigurableApplicationContext context;
18
19 /**
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()}.
23 */
24 protected ConfigurableApplicationContext getContext() {
25 if (context == null) {
26 context = new ClassPathXmlApplicationContext(
27 getApplicationContextLocation());
28 if (getIsStartContext())
29 context.start();
30 }
31 return context;
32 }
33
34 @Override
35 protected void tearDown() throws Exception {
36 if (context != null && context.isActive())
37 context.close();
38 super.tearDown();
39 }
40
41 /** Whether the context should be started after being created. */
42 protected Boolean getIsStartContext() {
43 return false;
44 }
45
46 /** Returns a bean from the underlying context */
47 @SuppressWarnings(value = { "unchecked" })
48 protected <T> T getBean(String beanId) {
49 return (T) getContext().getBean(beanId);
50 }
51
52 protected <T> T getBean(Class<? extends T> clss) {
53 T bean = loadSingleFromContext(getContext(), clss);
54 if (bean == null) {
55 throw new SlcException("Cannot retrieve a unique bean of type "
56 + clss);
57 } else {
58 return bean;
59 }
60 }
61
62 /**
63 * Th location of the application to load. The default implementation
64 * returns <i>applicationContext.xml</i> found in the same package as the
65 * test.
66 */
67 protected String getApplicationContextLocation() {
68 return inPackage("applicationContext.xml");
69 }
70
71 /**
72 * Prefixes the package of the class after converting the '.' to '/' in
73 * order to have a resource path.
74 */
75 protected String inPackage(String suffix) {
76 String prefix = getClass().getPackage().getName().replace('.', '/');
77 return prefix + '/' + suffix;
78 }
79
80 @SuppressWarnings(value = { "unchecked" })
81 protected <T> T loadSingleFromContext(ListableBeanFactory context,
82 Class<T> clss) {
83 Map<String, T> beans = BeanFactoryUtils.beansOfTypeIncludingAncestors(
84 context, clss, false, false);
85 if (beans.size() == 1) {
86 return beans.values().iterator().next();
87 } else if (beans.size() > 1) {
88 if (log.isDebugEnabled()) {
89 log
90 .debug(("Found more that on bean for type " + clss
91 + ": " + beans.keySet()));
92 }
93 return null;
94 } else {
95 return null;
96 }
97 }
98
99 }