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