]> git.argeo.org Git - gpl/argeo-slc.git/blob - org.argeo.slc.core/src/main/java/org/argeo/slc/unit/AbstractSpringTestCase.java
Various fixes so that SLC example works again
[gpl/argeo-slc.git] / org.argeo.slc.core / src / main / java / org / argeo / slc / unit / AbstractSpringTestCase.java
1 package org.argeo.slc.unit;
2
3 import junit.framework.TestCase;
4
5 import org.springframework.context.ApplicationContext;
6 import org.springframework.context.support.ClassPathXmlApplicationContext;
7
8 import org.apache.commons.logging.Log;
9 import org.apache.commons.logging.LogFactory;
10
11 import org.argeo.slc.core.SlcException;
12 import org.argeo.slc.spring.SpringUtils;
13
14 /** Helper for tests using a Spring application co,text. */
15 public abstract class AbstractSpringTestCase extends TestCase {
16 protected final Log log = LogFactory.getLog(getClass());
17 private ApplicationContext 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 ApplicationContext getContext() {
25 if (context == null) {
26 context = new ClassPathXmlApplicationContext(
27 getApplicationContextLocation());
28 }
29 return context;
30 }
31
32 /** Returns a bean from the underlying context */
33 protected <T> T getBean(String beanId) {
34 return (T) getContext().getBean(beanId);
35 }
36
37 protected <T> T getBean(Class<? extends T> clss) {
38 T bean = SpringUtils.loadSingleFromContext(getContext(), clss);
39 if (bean == null) {
40 throw new SlcException("Cannot retrieve a unique bean of type "
41 + clss);
42 } else {
43 return bean;
44 }
45 }
46
47 /**
48 * Th location of the application to load. The default implementation
49 * returns <i>applicationContext.xml</i> found in the same package as the
50 * test.
51 */
52 protected String getApplicationContextLocation() {
53 return inPackage("applicationContext.xml");
54 }
55
56 /**
57 * Prefixes the package of the class after converting the '.' to '/' in
58 * order to have a resource path.
59 */
60 protected String inPackage(String suffix) {
61 String prefix = getClass().getPackage().getName().replace('.', '/');
62 return prefix + '/' + suffix;
63 }
64 }