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