2 * Copyright (C) 2007-2012 Mathieu Baudier
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.
16 package org
.argeo
.slc
.core
.test
.context
;
20 import junit
.framework
.TestCase
;
22 import org
.apache
.commons
.logging
.Log
;
23 import org
.apache
.commons
.logging
.LogFactory
;
24 import org
.argeo
.slc
.SlcException
;
25 import org
.springframework
.beans
.factory
.BeanFactoryUtils
;
26 import org
.springframework
.beans
.factory
.ListableBeanFactory
;
27 import org
.springframework
.context
.ConfigurableApplicationContext
;
28 import org
.springframework
.context
.support
.ClassPathXmlApplicationContext
;
30 /** Helper for tests using a Spring application context. */
31 public abstract class AbstractInternalSpringTestCase
extends TestCase
{
32 protected final Log log
= LogFactory
.getLog(getClass());
33 private ConfigurableApplicationContext context
;
36 * Gets (and create if necessary) the application context to use. Default
37 * implementation uses a class path xml application context and calls
38 * {@link #getApplicationContextLocation()}.
40 protected ConfigurableApplicationContext
getContext() {
41 if (context
== null) {
42 context
= new ClassPathXmlApplicationContext(
43 getApplicationContextLocation());
48 /** Returns a bean from the underlying context */
49 @SuppressWarnings(value
= { "unchecked" })
50 protected <T
> T
getBean(String beanId
) {
51 return (T
) getContext().getBean(beanId
);
54 protected <T
> T
getBean(Class
<?
extends T
> clss
) {
55 T bean
= loadSingleFromContext(getContext(), clss
);
57 throw new SlcException("Cannot retrieve a unique bean of type "
65 * Th location of the application to load. The default implementation
66 * returns <i>applicationContext.xml</i> found in the same package as the
69 protected String
getApplicationContextLocation() {
70 return inPackage("applicationContext.xml");
74 * Prefixes the package of the class after converting the '.' to '/' in
75 * order to have a resource path.
77 protected String
inPackage(String suffix
) {
78 String prefix
= getClass().getPackage().getName().replace('.', '/');
79 return prefix
+ '/' + suffix
;
82 @SuppressWarnings(value
= { "unchecked" })
83 protected <T
> T
loadSingleFromContext(ListableBeanFactory context
,
85 Map
<String
, T
> beans
= BeanFactoryUtils
.beansOfTypeIncludingAncestors(
86 context
, clss
, false, false);
87 if (beans
.size() == 1) {
88 return beans
.values().iterator().next();
89 } else if (beans
.size() > 1) {
90 if (log
.isDebugEnabled()) {
92 .debug(("Found more that on bean for type " + clss
93 + ": " + beans
.keySet()));