2 * Copyright (C) 2010 Mathieu Baudier <mbaudier@argeo.org>
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package org
.argeo
.slc
.unit
;
21 import junit
.framework
.TestCase
;
23 import org
.apache
.commons
.logging
.Log
;
24 import org
.apache
.commons
.logging
.LogFactory
;
25 import org
.argeo
.slc
.SlcException
;
26 import org
.springframework
.beans
.factory
.BeanFactoryUtils
;
27 import org
.springframework
.beans
.factory
.ListableBeanFactory
;
28 import org
.springframework
.context
.ConfigurableApplicationContext
;
29 import org
.springframework
.context
.support
.ClassPathXmlApplicationContext
;
31 /** Helper for tests using a Spring application co,text. */
32 public abstract class AbstractSpringTestCase
extends TestCase
{
33 protected final Log log
= LogFactory
.getLog(getClass());
34 private ConfigurableApplicationContext context
;
37 * Gets (and create if necessary) the application context to use. Default
38 * implementation uses a class path xml application context and calls
39 * {@link #getApplicationContextLocation()}.
41 protected ConfigurableApplicationContext
getContext() {
42 if (context
== null) {
43 context
= new ClassPathXmlApplicationContext(
44 getApplicationContextLocation());
45 if (getIsStartContext())
52 protected void tearDown() throws Exception
{
53 if (context
!= null && context
.isActive())
58 /** Whether the context should be started after being created. */
59 protected Boolean
getIsStartContext() {
63 /** Returns a bean from the underlying context */
64 @SuppressWarnings(value
= { "unchecked" })
65 protected <T
> T
getBean(String beanId
) {
66 return (T
) getContext().getBean(beanId
);
69 protected <T
> T
getBean(Class
<?
extends T
> clss
) {
70 T bean
= loadSingleFromContext(getContext(), clss
);
72 throw new SlcException("Cannot retrieve a unique bean of type "
80 * Th location of the application to load. The default implementation
81 * returns <i>applicationContext.xml</i> found in the same package as the
84 protected String
getApplicationContextLocation() {
85 return inPackage("applicationContext.xml");
89 * Prefixes the package of the class after converting the '.' to '/' in
90 * order to have a resource path.
92 protected String
inPackage(String suffix
) {
93 String prefix
= getClass().getPackage().getName().replace('.', '/');
94 return prefix
+ '/' + suffix
;
97 @SuppressWarnings(value
= { "unchecked" })
98 protected <T
> T
loadSingleFromContext(ListableBeanFactory context
,
100 Map
<String
, T
> beans
= BeanFactoryUtils
.beansOfTypeIncludingAncestors(
101 context
, clss
, false, false);
102 if (beans
.size() == 1) {
103 return beans
.values().iterator().next();
104 } else if (beans
.size() > 1) {
105 if (log
.isDebugEnabled()) {
107 .debug(("Found more that on bean for type " + clss
108 + ": " + beans
.keySet()));