]> git.argeo.org Git - gpl/argeo-slc.git/blob - AbstractSpringTestCase.java
76b7fb6609b97975a473127a4ff3a141c6b07a88
[gpl/argeo-slc.git] / 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.slc.unit;
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.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;
29
30 /** Helper for tests using a Spring application co,text. */
31 public abstract class AbstractSpringTestCase extends TestCase {
32 protected final Log log = LogFactory.getLog(getClass());
33 private ConfigurableApplicationContext context;
34
35 /**
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()}.
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 @Override
51 protected void tearDown() throws Exception {
52 if (context != null && context.isActive())
53 context.close();
54 super.tearDown();
55 }
56
57 /** Whether the context should be started after being created. */
58 protected Boolean getIsStartContext() {
59 return false;
60 }
61
62 /** Returns a bean from the underlying context */
63 @SuppressWarnings(value = { "unchecked" })
64 protected <T> T getBean(String beanId) {
65 return (T) getContext().getBean(beanId);
66 }
67
68 protected <T> T getBean(Class<? extends T> clss) {
69 T bean = loadSingleFromContext(getContext(), clss);
70 if (bean == null) {
71 throw new SlcException("Cannot retrieve a unique bean of type "
72 + clss);
73 } else {
74 return bean;
75 }
76 }
77
78 /**
79 * Th location of the application to load. The default implementation
80 * returns <i>applicationContext.xml</i> found in the same package as the
81 * test.
82 */
83 protected String getApplicationContextLocation() {
84 return inPackage("applicationContext.xml");
85 }
86
87 /**
88 * Prefixes the package of the class after converting the '.' to '/' in
89 * order to have a resource path.
90 */
91 protected String inPackage(String suffix) {
92 String prefix = getClass().getPackage().getName().replace('.', '/');
93 return prefix + '/' + suffix;
94 }
95
96 @SuppressWarnings(value = { "unchecked" })
97 protected <T> T loadSingleFromContext(ListableBeanFactory context,
98 Class<T> clss) {
99 Map<String, T> beans = BeanFactoryUtils.beansOfTypeIncludingAncestors(
100 context, clss, false, false);
101 if (beans.size() == 1) {
102 return beans.values().iterator().next();
103 } else if (beans.size() > 1) {
104 if (log.isDebugEnabled()) {
105 log
106 .debug(("Found more that on bean for type " + clss
107 + ": " + beans.keySet()));
108 }
109 return null;
110 } else {
111 return null;
112 }
113 }
114
115 }