]> git.argeo.org Git - gpl/argeo-slc.git/blob - org.argeo.slc.core/src/main/java/org/argeo/slc/unit/AbstractSpringTestCase.java
Add elements and tags in tree test result
[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 /** Helper for tests using a Spring application co,text. */
9 public abstract class AbstractSpringTestCase extends TestCase {
10 private ApplicationContext context;
11
12 /**
13 * Gets (and create if necessary) the application context to use. Default
14 * implementation uses a class path xml application context and calls
15 * {@link #getApplicationContextLocation()}.
16 */
17 protected ApplicationContext getContext() {
18 if (context == null) {
19 context = new ClassPathXmlApplicationContext(
20 getApplicationContextLocation());
21 }
22 return context;
23 }
24
25 /** Returns a bean from the underlying context */
26 protected <T> T getBean(String beanId){
27 return (T)getContext().getBean(beanId);
28 }
29
30 /**
31 * Th location of the application to load. The default implementation
32 * returns <i>applicationContext.xml</i> found in the same package as the
33 * test.
34 */
35 protected String getApplicationContextLocation() {
36 return inPackage("applicationContext.xml");
37 }
38
39 /**
40 * Prefixes the package of the class after converting the '.' to '/' in
41 * order to have a resource path.
42 */
43 protected String inPackage(String suffix) {
44 String prefix = getClass().getPackage().getName().replace('.', '/');
45 return prefix + '/' + suffix;
46 }
47 }