1 package org
.argeo
.slc
.unit
;
5 import junit
.framework
.TestCase
;
7 import org
.springframework
.context
.ApplicationContext
;
8 import org
.springframework
.context
.support
.ClassPathXmlApplicationContext
;
10 import org
.argeo
.slc
.core
.SlcException
;
11 import org
.argeo
.slc
.core
.process
.SlcExecution
;
13 /** Helper for tests using a Spring application co,text. */
14 public abstract class AbstractSpringTestCase
extends TestCase
{
15 private ApplicationContext context
;
18 * Gets (and create if necessary) the application context to use. Default
19 * implementation uses a class path xml application context and calls
20 * {@link #getApplicationContextLocation()}.
22 protected ApplicationContext
getContext() {
23 if (context
== null) {
24 context
= new ClassPathXmlApplicationContext(
25 getApplicationContextLocation());
30 /** Returns a bean from the underlying context */
31 protected <T
> T
getBean(String beanId
) {
32 return (T
) getContext().getBean(beanId
);
35 protected <T
> T
getBean(Class
<?
extends T
> clss
) {
36 Map
<String
, T
> map
= getContext().getBeansOfType(clss
);
37 if (map
.size() == 1) {
38 return map
.values().iterator().next();
40 throw new SlcException("Cannot retrieve a unique bean of type "
46 * Th location of the application to load. The default implementation
47 * returns <i>applicationContext.xml</i> found in the same package as the
50 protected String
getApplicationContextLocation() {
51 return inPackage("applicationContext.xml");
55 * Prefixes the package of the class after converting the '.' to '/' in
56 * order to have a resource path.
58 protected String
inPackage(String suffix
) {
59 String prefix
= getClass().getPackage().getName().replace('.', '/');
60 return prefix
+ '/' + suffix
;