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