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