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