1 package org
.argeo
.slc
.unit
;
3 import junit
.framework
.TestCase
;
5 import org
.springframework
.context
.ApplicationContext
;
6 import org
.springframework
.context
.ConfigurableApplicationContext
;
7 import org
.springframework
.context
.support
.ClassPathXmlApplicationContext
;
9 import org
.apache
.commons
.logging
.Log
;
10 import org
.apache
.commons
.logging
.LogFactory
;
12 import org
.argeo
.slc
.core
.SlcException
;
13 import org
.argeo
.slc
.spring
.SpringUtils
;
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
;
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()}.
25 protected ConfigurableApplicationContext
getContext() {
26 if (context
== null) {
27 context
= new ClassPathXmlApplicationContext(
28 getApplicationContextLocation());
33 /** Returns a bean from the underlying context */
34 protected <T
> T
getBean(String beanId
) {
35 return (T
) getContext().getBean(beanId
);
38 protected <T
> T
getBean(Class
<?
extends T
> clss
) {
39 T bean
= SpringUtils
.loadSingleFromContext(getContext(), clss
);
41 throw new SlcException("Cannot retrieve a unique bean of type "
49 * Th location of the application to load. The default implementation
50 * returns <i>applicationContext.xml</i> found in the same package as the
53 protected String
getApplicationContextLocation() {
54 return inPackage("applicationContext.xml");
58 * Prefixes the package of the class after converting the '.' to '/' in
59 * order to have a resource path.
61 protected String
inPackage(String suffix
) {
62 String prefix
= getClass().getPackage().getName().replace('.', '/');
63 return prefix
+ '/' + suffix
;