001 /**
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017 package org.apache.camel.test.junit4;
018
019 import java.util.Arrays;
020 import java.util.Collections;
021 import java.util.HashSet;
022 import java.util.List;
023 import java.util.Set;
024
025 import org.apache.camel.CamelContext;
026 import org.apache.camel.Route;
027 import org.apache.camel.impl.DefaultPackageScanClassResolver;
028 import org.apache.camel.impl.scan.AssignableToPackageScanFilter;
029 import org.apache.camel.impl.scan.InvertingPackageScanFilter;
030 import org.apache.camel.spring.SpringCamelContext;
031 import org.apache.camel.util.CastUtils;
032 import org.apache.camel.util.ObjectHelper;
033 import org.junit.After;
034 import org.junit.AfterClass;
035 import org.junit.Before;
036 import org.springframework.beans.factory.support.RootBeanDefinition;
037 import org.springframework.context.ApplicationContext;
038 import org.springframework.context.support.AbstractApplicationContext;
039 import org.springframework.context.support.GenericApplicationContext;
040
041 /**
042 * @version
043 */
044 public abstract class CamelSpringTestSupport extends CamelTestSupport {
045 protected static AbstractApplicationContext applicationContext;
046 protected abstract AbstractApplicationContext createApplicationContext();
047
048 @Override
049 public void doPreSetup() throws Exception {
050 if (!"true".equalsIgnoreCase(System.getProperty("skipStartingCamelContext"))) {
051 // tell camel-spring it should not trigger starting CamelContext, since we do that later
052 // after we are finished setting up the unit test
053 System.setProperty("maybeStartCamelContext", "false");
054 SpringCamelContext.setNoStart(true);
055 applicationContext = createApplicationContext();
056 assertNotNull("Should have created a valid spring context", applicationContext);
057 System.clearProperty("maybeStartCamelContext");
058 SpringCamelContext.setNoStart(false);
059 } else {
060 log.info("Skipping starting CamelContext as system property skipStartingCamelContext is set to be true.");
061 }
062 }
063
064 @Override
065 @After
066 public void tearDown() throws Exception {
067 super.tearDown();
068
069 if (!isCreateCamelContextPerClass()) {
070 if (applicationContext != null) {
071 applicationContext.destroy();
072 applicationContext = null;
073 }
074 }
075 }
076
077 @AfterClass
078 public static void tearSpringDownAfterClass() throws Exception {
079 if (applicationContext != null) {
080 applicationContext.destroy();
081 applicationContext = null;
082 }
083 }
084
085 @SuppressWarnings("unchecked")
086 private static class ExcludingPackageScanClassResolver extends DefaultPackageScanClassResolver {
087
088 public void setExcludedClasses(Set<Class<?>> excludedClasses) {
089 excludedClasses = excludedClasses == null ? Collections.EMPTY_SET : excludedClasses;
090 addFilter(new InvertingPackageScanFilter(new AssignableToPackageScanFilter(excludedClasses)));
091 }
092
093 }
094
095 /**
096 * Create a parent context that initializes a
097 * {@link org.apache.camel.spi.PackageScanClassResolver} to exclude a set of given classes from
098 * being resolved. Typically this is used at test time to exclude certain routes,
099 * which might otherwise be just noisy, from being discovered and initialized.
100 * <p/>
101 * To use this filtering mechanism it is necessary to provide the
102 * {@link ApplicationContext} returned from here as the parent context to
103 * your test context e.g.
104 *
105 * <pre>
106 * protected AbstractXmlApplicationContext createApplicationContext() {
107 * return new ClassPathXmlApplicationContext(new String[] {"test-context.xml"}, getRouteExcludingApplicationContext());
108 * }
109 * </pre>
110 *
111 * This will, in turn, call the template methods <code>excludedRoutes</code>
112 * and <code>excludedRoute</code> to determine the classes to be excluded from scanning.
113 *
114 * @return ApplicationContext a parent {@link ApplicationContext} configured
115 * to exclude certain classes from package scanning
116 */
117 protected ApplicationContext getRouteExcludingApplicationContext() {
118 GenericApplicationContext routeExcludingContext = new GenericApplicationContext();
119 routeExcludingContext.registerBeanDefinition("excludingResolver", new RootBeanDefinition(ExcludingPackageScanClassResolver.class));
120 routeExcludingContext.refresh();
121
122 ExcludingPackageScanClassResolver excludingResolver = (ExcludingPackageScanClassResolver)routeExcludingContext.getBean("excludingResolver");
123 List<Class<?>> excluded = CastUtils.cast(Arrays.asList(excludeRoutes()));
124 excludingResolver.setExcludedClasses(new HashSet<Class<?>>(excluded));
125
126 return routeExcludingContext;
127 }
128
129 /**
130 * Template method used to exclude {@link org.apache.camel.Route} from the test time context
131 * route scanning
132 *
133 * @return Class[] the classes to be excluded from test time context route scanning
134 */
135 protected Class<?>[] excludeRoutes() {
136 Class<?> excludedRoute = excludeRoute();
137 return excludedRoute != null ? new Class[] {excludedRoute} : new Class[0];
138 }
139
140 /**
141 * Template method used to exclude a {@link org.apache.camel.Route} from the test camel context
142 */
143 protected Class<?> excludeRoute() {
144 return null;
145 }
146
147 /**
148 * Looks up the mandatory spring bean of the given name and type, failing if
149 * it is not present or the correct type
150 */
151 public <T> T getMandatoryBean(Class<T> type, String name) {
152 Object value = applicationContext.getBean(name);
153 assertNotNull("No spring bean found for name <" + name + ">", value);
154 if (type.isInstance(value)) {
155 return type.cast(value);
156 } else {
157 fail("Spring bean <" + name + "> is not an instanceof " + type.getName() + " but is of type " + ObjectHelper.className(value));
158 return null;
159 }
160 }
161
162 @Override
163 protected void assertValidContext(CamelContext context) {
164 super.assertValidContext(context);
165
166 List<Route> routes = context.getRoutes();
167 int routeCount = getExpectedRouteCount();
168 if (routeCount > 0) {
169 assertNotNull("Should have some routes defined", routes);
170 assertTrue("Should have at least one route", routes.size() >= routeCount);
171 }
172 log.debug("Camel Routes: " + routes);
173 }
174
175 protected int getExpectedRouteCount() {
176 return 1;
177 }
178
179 @Override
180 protected CamelContext createCamelContext() throws Exception {
181 // don't start the springCamelContext if we
182 return SpringCamelContext.springCamelContext(applicationContext, false);
183 }
184 }