]> git.argeo.org Git - gpl/argeo-slc.git/blob - org.argeo.slc.spring/ext/test/org/argeo/slc/core/test/context/AbstractInternalSpringTestCase.java
Remove old license headers
[gpl/argeo-slc.git] / org.argeo.slc.spring / ext / test / org / argeo / slc / core / test / context / AbstractInternalSpringTestCase.java
1 package org.argeo.slc.core.test.context;
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 context. */
16 public abstract class AbstractInternalSpringTestCase 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 }
30 return context;
31 }
32
33 /** Returns a bean from the underlying context */
34 @SuppressWarnings(value = { "unchecked" })
35 protected <T> T getBean(String beanId) {
36 return (T) getContext().getBean(beanId);
37 }
38
39 protected <T> T getBean(Class<? extends T> clss) {
40 T bean = loadSingleFromContext(getContext(), clss);
41 if (bean == null) {
42 throw new SlcException("Cannot retrieve a unique bean of type "
43 + clss);
44 } else {
45 return bean;
46 }
47 }
48
49 /**
50 * Th location of the application to load. The default implementation
51 * returns <i>applicationContext.xml</i> found in the same package as the
52 * test.
53 */
54 protected String getApplicationContextLocation() {
55 return inPackage("applicationContext.xml");
56 }
57
58 /**
59 * Prefixes the package of the class after converting the '.' to '/' in
60 * order to have a resource path.
61 */
62 protected String inPackage(String suffix) {
63 String prefix = getClass().getPackage().getName().replace('.', '/');
64 return prefix + '/' + suffix;
65 }
66
67 @SuppressWarnings(value = { "unchecked" })
68 protected <T> T loadSingleFromContext(ListableBeanFactory context,
69 Class<T> clss) {
70 Map<String, T> beans = BeanFactoryUtils.beansOfTypeIncludingAncestors(
71 context, clss, false, false);
72 if (beans.size() == 1) {
73 return beans.values().iterator().next();
74 } else if (beans.size() > 1) {
75 if (log.isDebugEnabled()) {
76 log
77 .debug(("Found more that on bean for type " + clss
78 + ": " + beans.keySet()));
79 }
80 return null;
81 } else {
82 return null;
83 }
84 }
85
86 }