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