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.testng;
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.AfterClass;
034 import org.springframework.beans.factory.support.RootBeanDefinition;
035 import org.springframework.context.ApplicationContext;
036 import org.springframework.context.support.AbstractApplicationContext;
037 import org.springframework.context.support.GenericApplicationContext;
038 import org.testng.annotations.AfterTest;
039
040 /**
041 * @version $Revision$
042 */
043 public abstract class CamelSpringTestSupport extends CamelTestSupport {
044 protected static AbstractApplicationContext applicationContext;
045 protected abstract AbstractApplicationContext createApplicationContext();
046
047 @Override
048 public void doSetUp() throws Exception {
049 if (!"true".equalsIgnoreCase(System.getProperty("skipStartingCamelContext"))) {
050 // tell camel-spring it should not trigger starting CamelContext, since we do that later
051 // after we are finished setting up the unit test
052 System.setProperty("maybeStartCamelContext", "false");
053 applicationContext = createApplicationContext();
054 assertNotNull(applicationContext, "Should have created a valid spring context");
055 System.clearProperty("maybeStartCamelContext");
056 } else {
057 log.info("Skipping starting CamelContext as system property skipStartingCamelContext is set to be true.");
058 }
059 super.doSetUp();
060 }
061
062 @Override
063 @AfterTest
064 public void tearDown() throws Exception {
065 super.tearDown();
066
067 if (!isCreateCamelContextPerClass()) {
068 if (applicationContext != null) {
069 applicationContext.destroy();
070 applicationContext = null;
071 }
072 }
073 }
074
075 @AfterClass
076 public static void tearSpringDownAfterClass() throws Exception {
077 if (applicationContext != null) {
078 applicationContext.destroy();
079 applicationContext = null;
080 }
081 }
082
083 @SuppressWarnings("unchecked")
084 private static class ExcludingPackageScanClassResolver extends DefaultPackageScanClassResolver {
085
086 public void setExcludedClasses(Set<Class<?>> excludedClasses) {
087 excludedClasses = excludedClasses == null ? Collections.EMPTY_SET : excludedClasses;
088 addFilter(new InvertingPackageScanFilter(new AssignableToPackageScanFilter(excludedClasses)));
089 }
090
091 }
092
093 /**
094 * Create a parent context that initializes a
095 * {@link org.apache.camel.spi.PackageScanClassResolver} to exclude a set of given classes from
096 * being resolved. Typically this is used at test time to exclude certain routes,
097 * which might otherwise be just noisy, from being discovered and initialized.
098 * <p/>
099 * To use this filtering mechanism it is necessary to provide the
100 * {@link ApplicationContext} returned from here as the parent context to
101 * your test context e.g.
102 *
103 * <pre>
104 * protected AbstractXmlApplicationContext createApplicationContext() {
105 * return new ClassPathXmlApplicationContext(new String[] {"test-context.xml"}, getRouteExcludingApplicationContext());
106 * }
107 * </pre>
108 *
109 * This will, in turn, call the template methods <code>excludedRoutes</code>
110 * and <code>excludedRoute</code> to determine the classes to be excluded from scanning.
111 *
112 * @return ApplicationContext a parent {@link ApplicationContext} configured
113 * to exclude certain classes from package scanning
114 */
115 protected ApplicationContext getRouteExcludingApplicationContext() {
116 GenericApplicationContext routeExcludingContext = new GenericApplicationContext();
117 routeExcludingContext.registerBeanDefinition("excludingResolver", new RootBeanDefinition(ExcludingPackageScanClassResolver.class));
118 routeExcludingContext.refresh();
119
120 ExcludingPackageScanClassResolver excludingResolver = (ExcludingPackageScanClassResolver)routeExcludingContext.getBean("excludingResolver");
121 List<Class<?>> excluded = CastUtils.cast(Arrays.asList(excludeRoutes()));
122 excludingResolver.setExcludedClasses(new HashSet<Class<?>>(excluded));
123
124 return routeExcludingContext;
125 }
126
127 /**
128 * Template method used to exclude {@link org.apache.camel.Route} from the test time context
129 * route scanning
130 *
131 * @return Class[] the classes to be excluded from test time context route scanning
132 */
133 protected Class<?>[] excludeRoutes() {
134 Class<?> excludedRoute = excludeRoute();
135 return excludedRoute != null ? new Class[] {excludedRoute} : new Class[0];
136 }
137
138 /**
139 * Template method used to exclude a {@link org.apache.camel.Route} from the test camel context
140 */
141 protected Class<?> excludeRoute() {
142 return null;
143 }
144
145 /**
146 * Looks up the mandatory spring bean of the given name and type, failing if
147 * it is not present or the correct type
148 */
149 public <T> T getMandatoryBean(Class<T> type, String name) {
150 Object value = applicationContext.getBean(name);
151 assertNotNull(value, "No spring bean found for name <" + name + ">");
152 if (type.isInstance(value)) {
153 return type.cast(value);
154 } else {
155 fail("Spring bean <" + name + "> is not an instanceof " + type.getName() + " but is of type " + ObjectHelper.className(value));
156 return null;
157 }
158 }
159
160 @Override
161 protected void assertValidContext(CamelContext context) {
162 super.assertValidContext(context);
163
164 List<Route> routes = context.getRoutes();
165 int routeCount = getExpectedRouteCount();
166 if (routeCount > 0) {
167 assertNotNull(routes, "Should have some routes defined");
168 assertTrue(routes.size() >= routeCount, "Should have at least one route");
169 }
170 log.debug("Camel Routes: " + routes);
171 }
172
173 protected int getExpectedRouteCount() {
174 return 1;
175 }
176
177 @Override
178 protected CamelContext createCamelContext() throws Exception {
179 return SpringCamelContext.springCamelContext(applicationContext, false);
180 }
181 }