]> git.argeo.org Git - gpl/argeo-slc.git/blob - runtime/org.argeo.slc.core/src/test/java/org/argeo/slc/core/test/context/AbstractInternalSpringTestCase.java
Add license headers
[gpl/argeo-slc.git] / runtime / org.argeo.slc.core / src / test / java / org / argeo / slc / core / test / context / AbstractInternalSpringTestCase.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.slc.core.test.context;
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.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;
30
31 /** Helper for tests using a Spring application context. */
32 public abstract class AbstractInternalSpringTestCase 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 }
46 return context;
47 }
48
49 /** Returns a bean from the underlying context */
50 @SuppressWarnings(value = { "unchecked" })
51 protected <T> T getBean(String beanId) {
52 return (T) getContext().getBean(beanId);
53 }
54
55 protected <T> T getBean(Class<? extends T> clss) {
56 T bean = loadSingleFromContext(getContext(), clss);
57 if (bean == null) {
58 throw new SlcException("Cannot retrieve a unique bean of type "
59 + clss);
60 } else {
61 return bean;
62 }
63 }
64
65 /**
66 * Th location of the application to load. The default implementation
67 * returns <i>applicationContext.xml</i> found in the same package as the
68 * test.
69 */
70 protected String getApplicationContextLocation() {
71 return inPackage("applicationContext.xml");
72 }
73
74 /**
75 * Prefixes the package of the class after converting the '.' to '/' in
76 * order to have a resource path.
77 */
78 protected String inPackage(String suffix) {
79 String prefix = getClass().getPackage().getName().replace('.', '/');
80 return prefix + '/' + suffix;
81 }
82
83 @SuppressWarnings(value = { "unchecked" })
84 protected <T> T loadSingleFromContext(ListableBeanFactory context,
85 Class<T> clss) {
86 Map<String, T> beans = BeanFactoryUtils.beansOfTypeIncludingAncestors(
87 context, clss, false, false);
88 if (beans.size() == 1) {
89 return beans.values().iterator().next();
90 } else if (beans.size() > 1) {
91 if (log.isDebugEnabled()) {
92 log
93 .debug(("Found more that on bean for type " + clss
94 + ": " + beans.keySet()));
95 }
96 return null;
97 } else {
98 return null;
99 }
100 }
101
102 }