-/*\r
- * Copyright (C) 2007-2012 Argeo GmbH\r
- *\r
- * Licensed under the Apache License, Version 2.0 (the "License");\r
- * you may not use this file except in compliance with the License.\r
- * You may obtain a copy of the License at\r
- *\r
- * http://www.apache.org/licenses/LICENSE-2.0\r
- *\r
- * Unless required by applicable law or agreed to in writing, software\r
- * distributed under the License is distributed on an "AS IS" BASIS,\r
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
- * See the License for the specific language governing permissions and\r
- * limitations under the License.\r
- */\r
-package org.argeo.slc.core.test.context;\r
-\r
-import java.util.Map;\r
-\r
-import junit.framework.TestCase;\r
-\r
-import org.apache.commons.logging.Log;\r
-import org.apache.commons.logging.LogFactory;\r
-import org.argeo.slc.SlcException;\r
-import org.springframework.beans.factory.BeanFactoryUtils;\r
-import org.springframework.beans.factory.ListableBeanFactory;\r
-import org.springframework.context.ConfigurableApplicationContext;\r
-import org.springframework.context.support.ClassPathXmlApplicationContext;\r
-\r
-/** Helper for tests using a Spring application context. */\r
-public abstract class AbstractInternalSpringTestCase extends TestCase {\r
- protected final Log log = LogFactory.getLog(getClass());\r
- private ConfigurableApplicationContext context;\r
-\r
- /**\r
- * Gets (and create if necessary) the application context to use. Default\r
- * implementation uses a class path xml application context and calls\r
- * {@link #getApplicationContextLocation()}.\r
- */\r
- protected ConfigurableApplicationContext getContext() {\r
- if (context == null) {\r
- context = new ClassPathXmlApplicationContext(\r
- getApplicationContextLocation());\r
- }\r
- return context;\r
- }\r
-\r
- /** Returns a bean from the underlying context */\r
- @SuppressWarnings(value = { "unchecked" })\r
- protected <T> T getBean(String beanId) {\r
- return (T) getContext().getBean(beanId);\r
- }\r
-\r
- protected <T> T getBean(Class<? extends T> clss) {\r
- T bean = loadSingleFromContext(getContext(), clss);\r
- if (bean == null) {\r
- throw new SlcException("Cannot retrieve a unique bean of type "\r
- + clss);\r
- } else {\r
- return bean;\r
- }\r
- }\r
-\r
- /**\r
- * Th location of the application to load. The default implementation\r
- * returns <i>applicationContext.xml</i> found in the same package as the\r
- * test.\r
- */\r
- protected String getApplicationContextLocation() {\r
- return inPackage("applicationContext.xml");\r
- }\r
-\r
- /**\r
- * Prefixes the package of the class after converting the '.' to '/' in\r
- * order to have a resource path.\r
- */\r
- protected String inPackage(String suffix) {\r
- String prefix = getClass().getPackage().getName().replace('.', '/');\r
- return prefix + '/' + suffix;\r
- }\r
-\r
- @SuppressWarnings(value = { "unchecked" })\r
- protected <T> T loadSingleFromContext(ListableBeanFactory context,\r
- Class<T> clss) {\r
- Map<String, T> beans = BeanFactoryUtils.beansOfTypeIncludingAncestors(\r
- context, clss, false, false);\r
- if (beans.size() == 1) {\r
- return beans.values().iterator().next();\r
- } else if (beans.size() > 1) {\r
- if (log.isDebugEnabled()) {\r
- log\r
- .debug(("Found more that on bean for type " + clss\r
- + ": " + beans.keySet()));\r
- }\r
- return null;\r
- } else {\r
- return null;\r
- }\r
- }\r
-\r
-}\r
+package org.argeo.slc.core.test.context;
+
+import java.util.Map;
+
+import junit.framework.TestCase;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.argeo.slc.SlcException;
+import org.springframework.beans.factory.BeanFactoryUtils;
+import org.springframework.beans.factory.ListableBeanFactory;
+import org.springframework.context.ConfigurableApplicationContext;
+import org.springframework.context.support.ClassPathXmlApplicationContext;
+
+/** Helper for tests using a Spring application context. */
+public abstract class AbstractInternalSpringTestCase extends TestCase {
+ protected final Log log = LogFactory.getLog(getClass());
+ private ConfigurableApplicationContext context;
+
+ /**
+ * Gets (and create if necessary) the application context to use. Default
+ * implementation uses a class path xml application context and calls
+ * {@link #getApplicationContextLocation()}.
+ */
+ protected ConfigurableApplicationContext getContext() {
+ if (context == null) {
+ context = new ClassPathXmlApplicationContext(
+ getApplicationContextLocation());
+ }
+ return context;
+ }
+
+ /** Returns a bean from the underlying context */
+ @SuppressWarnings(value = { "unchecked" })
+ protected <T> T getBean(String beanId) {
+ return (T) getContext().getBean(beanId);
+ }
+
+ protected <T> T getBean(Class<? extends T> clss) {
+ T bean = loadSingleFromContext(getContext(), clss);
+ if (bean == null) {
+ throw new SlcException("Cannot retrieve a unique bean of type "
+ + clss);
+ } else {
+ return bean;
+ }
+ }
+
+ /**
+ * Th location of the application to load. The default implementation
+ * returns <i>applicationContext.xml</i> found in the same package as the
+ * test.
+ */
+ protected String getApplicationContextLocation() {
+ return inPackage("applicationContext.xml");
+ }
+
+ /**
+ * Prefixes the package of the class after converting the '.' to '/' in
+ * order to have a resource path.
+ */
+ protected String inPackage(String suffix) {
+ String prefix = getClass().getPackage().getName().replace('.', '/');
+ return prefix + '/' + suffix;
+ }
+
+ @SuppressWarnings(value = { "unchecked" })
+ protected <T> T loadSingleFromContext(ListableBeanFactory context,
+ Class<T> clss) {
+ Map<String, T> beans = BeanFactoryUtils.beansOfTypeIncludingAncestors(
+ context, clss, false, false);
+ if (beans.size() == 1) {
+ return beans.values().iterator().next();
+ } else if (beans.size() > 1) {
+ if (log.isDebugEnabled()) {
+ log
+ .debug(("Found more that on bean for type " + clss
+ + ": " + beans.keySet()));
+ }
+ return null;
+ } else {
+ return null;
+ }
+ }
+
+}