]> git.argeo.org Git - lgpl/argeo-commons.git/blob - junit/AbstractSpringTestCase.java
Prepare next development cycle
[lgpl/argeo-commons.git] / junit / AbstractSpringTestCase.java
1 /*
2 * Copyright (C) 2007-2012 Argeo GmbH
3 *
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
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
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.
15 */
16 package org.argeo.support.junit;
17
18 import java.util.Map;
19
20 import junit.framework.TestCase;
21
22 import org.apache.commons.logging.Log;
23 import org.apache.commons.logging.LogFactory;
24 import org.springframework.beans.factory.BeanFactoryUtils;
25 import org.springframework.beans.factory.ListableBeanFactory;
26 import org.springframework.context.ConfigurableApplicationContext;
27 import org.springframework.context.support.ClassPathXmlApplicationContext;
28
29
30 /** Helper for tests using a Spring application context. */
31 public abstract class AbstractSpringTestCase extends TestCase {
32 protected final Log log = LogFactory.getLog(getClass());
33 private ConfigurableApplicationContext context;
34
35 /**
36 * Gets (and creates if necessary) the application context to use. Default
37 * implementation uses a class path xml application context and calls
38 * {@link #getApplicationContextLocation()}.
39 */
40 protected ConfigurableApplicationContext getContext() {
41 if (context == null) {
42 context = new ClassPathXmlApplicationContext(
43 getApplicationContextLocation());
44 if (getIsStartContext())
45 context.start();
46 }
47 return context;
48 }
49
50 /** Whether the context should be started after being created. */
51 protected Boolean getIsStartContext() {
52 return false;
53 }
54
55 /** Returns a bean from the underlying context */
56 @SuppressWarnings(value = { "unchecked" })
57 protected <T> T getBean(String beanId) {
58 return (T) getContext().getBean(beanId);
59 }
60
61 protected <T> T getBean(Class<? extends T> clss) {
62 T bean = loadSingleFromContext(getContext(), clss);
63 if (bean == null) {
64 throw new RuntimeException("Cannot retrieve a unique bean of type "
65 + clss);
66 } else {
67 return bean;
68 }
69 }
70
71 /**
72 * The location of the application to load. The default implementation
73 * returns <i>applicationContext.xml</i> found in the same package as the
74 * test.
75 */
76 protected String getApplicationContextLocation() {
77 return inPackage("applicationContext.xml");
78 }
79
80 /**
81 * Prefixes the package of the class after converting the '.' to '/' in
82 * order to have a resource path.
83 */
84 protected String inPackage(String suffix) {
85 String prefix = getClass().getPackage().getName().replace('.', '/');
86 return prefix + '/' + suffix;
87 }
88
89 @SuppressWarnings(value = { "unchecked" })
90 protected <T> T loadSingleFromContext(ListableBeanFactory context,
91 Class<T> clss) {
92 Map<String, T> beans = BeanFactoryUtils.beansOfTypeIncludingAncestors(
93 context, clss, false, false);
94 if (beans.size() == 1) {
95 return beans.values().iterator().next();
96 } else if (beans.size() > 1) {
97 if (log.isDebugEnabled()) {
98 log
99 .debug(("Found more that one bean for type " + clss
100 + ": " + beans.keySet()));
101 }
102 return null;
103 } else {
104 return null;
105 }
106 }
107
108 }