You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2014/09/21 17:35:51 UTC
[5/7] git commit: CAMEL-7835: camel-test-spring40 for spring 4.0
only. And camel-test-spring for spring 4.1 or newer.
CAMEL-7835: camel-test-spring40 for spring 4.0 only. And camel-test-spring for spring 4.1 or newer.
Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/7764c326
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/7764c326
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/7764c326
Branch: refs/heads/master
Commit: 7764c326406b8484e0094a711883df92db70d934
Parents: 366ad8d
Author: Claus Ibsen <da...@apache.org>
Authored: Sun Sep 21 16:59:28 2014 +0200
Committer: Claus Ibsen <da...@apache.org>
Committed: Sun Sep 21 16:59:28 2014 +0200
----------------------------------------------------------------------
apache-camel/pom.xml | 2 +-
.../src/main/descriptors/common-bin.xml | 2 +-
components/camel-test-spring/pom.xml | 4 +-
.../spring/CamelSpringJUnit4ClassRunner.java | 42 +-
.../spring/CamelTestContextBootstrapper.java | 31 ++
.../spring/StopWatchTestExecutionListener.java | 20 +-
.../CamelSpringJUnit4ClassRunnerPlainTest.java | 9 +-
components/camel-test-spring40/pom.xml | 95 ++++
.../CamelSpringDelegatingTestContextLoader.java | 321 +++++++++++++
.../spring/CamelSpringJUnit4ClassRunner.java | 88 ++++
.../spring/CamelSpringTestContextLoader.java | 473 +++++++++++++++++++
...gTestContextLoaderTestExecutionListener.java | 36 ++
.../test/spring/CamelSpringTestHelper.java | 99 ++++
.../test/spring/CamelSpringTestSupport.java | 220 +++++++++
.../apache/camel/test/spring/DisableJmx.java | 43 ++
.../spring/DisableJmxTestExecutionListener.java | 39 ++
.../apache/camel/test/spring/ExcludeRoutes.java | 44 ++
.../test/spring/LazyLoadTypeConverters.java | 44 ++
.../apache/camel/test/spring/MockEndpoints.java | 43 ++
.../camel/test/spring/MockEndpointsAndSkip.java | 43 ++
.../camel/test/spring/ProvidesBreakpoint.java | 36 ++
.../camel/test/spring/ShutdownTimeout.java | 49 ++
.../spring/StopWatchTestExecutionListener.java | 60 +++
.../apache/camel/test/spring/UseAdviceWith.java | 49 ++
.../src/main/resources/META-INF/LICENSE.txt | 203 ++++++++
.../src/main/resources/META-INF/NOTICE.txt | 11 +
.../AdviceWithOnExceptionMultipleIssueTest.java | 116 +++++
.../test/issues/MockEndpointsAndSkipTest.java | 46 ++
.../patterns/DebugSpringCamelContextTest.java | 40 ++
.../camel/test/patterns/DebugSpringTest.java | 81 ++++
.../camel/test/patterns/MyProduceBean.java | 41 ++
.../apache/camel/test/patterns/MySender.java | 25 +
.../camel/test/patterns/ProduceBeanTest.java | 42 ++
.../test/patterns/ProducerBeanInjectTest.java | 38 ++
.../spring/CamelSpringActiveProfileTest.java | 56 +++
...ssRunnerDisableJmxInheritedOverrideTest.java | 32 ++
...Unit4ClassRunnerDisableJmxInheritedTest.java | 21 +
...elSpringJUnit4ClassRunnerDisableJmxTest.java | 33 ++
...pringJUnit4ClassRunnerExcludeRoutesTest.java | 29 ++
...LoadTypeConvertersInheritedOverrideTest.java | 33 ++
...nnerLazyLoadTypeConvertersInheritedTest.java | 21 +
...t4ClassRunnerLazyLoadTypeConvertersTest.java | 33 ++
...nit4ClassRunnerMockEndpointsAndSkipTest.java | 56 +++
...pringJUnit4ClassRunnerMockEndpointsTest.java | 57 +++
.../CamelSpringJUnit4ClassRunnerPlainTest.java | 126 +++++
...sRunnerProvidesBreakpointInherritedTest.java | 22 +
...JUnit4ClassRunnerProvidesBreakpointTest.java | 67 +++
...nerShutdownTimeoutInheritedOverrideTest.java | 34 ++
...ClassRunnerShutdownTimeoutInheritedTest.java | 22 +
...ingJUnit4ClassRunnerShutdownTimeoutTest.java | 34 ++
...pringJUnit4ClassRunnerUseAdviceWithTest.java | 52 ++
...CamelSpringTestSupportActiveProfileTest.java | 51 ++
.../camel/test/spring/TestRouteBuilder.java | 30 ++
.../src/test/resources/jndi.properties | 22 +
.../src/test/resources/log4j.properties | 37 ++
.../AdviceWithOnExceptionMultipleIssueTest.xml | 47 ++
.../test/issues/MockEndpointsAndSkipTest.xml | 35 ++
.../test/patterns/ProduceBeanInjectTest.xml | 39 ++
.../camel/test/patterns/ProduceBeanTest.xml | 33 ++
.../camel/test/patterns/applicationContext.xml | 38 ++
.../CamelSpringActiveProfileTest-context.xml | 41 ++
...SpringJUnit4ClassRunnerPlainTest-context.xml | 50 ++
.../apache/camel/test/spring/test.properties | 18 +
components/camel-test-spring41/pom.xml | 96 ----
.../CamelSpringDelegatingTestContextLoader.java | 321 -------------
.../spring/CamelSpringJUnit4ClassRunner.java | 68 ---
.../spring/CamelSpringTestContextLoader.java | 473 -------------------
...gTestContextLoaderTestExecutionListener.java | 36 --
.../test/spring/CamelSpringTestHelper.java | 99 ----
.../test/spring/CamelSpringTestSupport.java | 220 ---------
.../spring/CamelTestContextBootstrapper.java | 31 --
.../apache/camel/test/spring/DisableJmx.java | 43 --
.../spring/DisableJmxTestExecutionListener.java | 39 --
.../apache/camel/test/spring/ExcludeRoutes.java | 44 --
.../test/spring/LazyLoadTypeConverters.java | 44 --
.../apache/camel/test/spring/MockEndpoints.java | 43 --
.../camel/test/spring/MockEndpointsAndSkip.java | 43 --
.../camel/test/spring/ProvidesBreakpoint.java | 36 --
.../camel/test/spring/ShutdownTimeout.java | 49 --
.../spring/StopWatchTestExecutionListener.java | 62 ---
.../apache/camel/test/spring/UseAdviceWith.java | 49 --
.../src/main/resources/META-INF/LICENSE.txt | 203 --------
.../src/main/resources/META-INF/NOTICE.txt | 11 -
.../AdviceWithOnExceptionMultipleIssueTest.java | 116 -----
.../test/issues/MockEndpointsAndSkipTest.java | 46 --
.../patterns/DebugSpringCamelContextTest.java | 40 --
.../camel/test/patterns/DebugSpringTest.java | 81 ----
.../camel/test/patterns/MyProduceBean.java | 41 --
.../apache/camel/test/patterns/MySender.java | 25 -
.../camel/test/patterns/ProduceBeanTest.java | 42 --
.../test/patterns/ProducerBeanInjectTest.java | 38 --
.../spring/CamelSpringActiveProfileTest.java | 56 ---
...ssRunnerDisableJmxInheritedOverrideTest.java | 32 --
...Unit4ClassRunnerDisableJmxInheritedTest.java | 21 -
...elSpringJUnit4ClassRunnerDisableJmxTest.java | 33 --
...pringJUnit4ClassRunnerExcludeRoutesTest.java | 29 --
...LoadTypeConvertersInheritedOverrideTest.java | 33 --
...nnerLazyLoadTypeConvertersInheritedTest.java | 21 -
...t4ClassRunnerLazyLoadTypeConvertersTest.java | 33 --
...nit4ClassRunnerMockEndpointsAndSkipTest.java | 56 ---
...pringJUnit4ClassRunnerMockEndpointsTest.java | 57 ---
.../CamelSpringJUnit4ClassRunnerPlainTest.java | 127 -----
...sRunnerProvidesBreakpointInherritedTest.java | 22 -
...JUnit4ClassRunnerProvidesBreakpointTest.java | 67 ---
...nerShutdownTimeoutInheritedOverrideTest.java | 34 --
...ClassRunnerShutdownTimeoutInheritedTest.java | 22 -
...ingJUnit4ClassRunnerShutdownTimeoutTest.java | 34 --
...pringJUnit4ClassRunnerUseAdviceWithTest.java | 52 --
...CamelSpringTestSupportActiveProfileTest.java | 51 --
.../camel/test/spring/TestRouteBuilder.java | 30 --
.../src/test/resources/jndi.properties | 22 -
.../src/test/resources/log4j.properties | 37 --
.../AdviceWithOnExceptionMultipleIssueTest.xml | 47 --
.../test/issues/MockEndpointsAndSkipTest.xml | 35 --
.../test/patterns/ProduceBeanInjectTest.xml | 39 --
.../camel/test/patterns/ProduceBeanTest.xml | 33 --
.../camel/test/patterns/applicationContext.xml | 38 --
.../CamelSpringActiveProfileTest-context.xml | 41 --
...SpringJUnit4ClassRunnerPlainTest-context.xml | 50 --
.../apache/camel/test/spring/test.properties | 18 -
components/pom.xml | 2 +-
parent/pom.xml | 6 +-
122 files changed, 3660 insertions(+), 3661 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/camel/blob/7764c326/apache-camel/pom.xml
----------------------------------------------------------------------
diff --git a/apache-camel/pom.xml b/apache-camel/pom.xml
index 892150c..b22eb69 100644
--- a/apache-camel/pom.xml
+++ b/apache-camel/pom.xml
@@ -704,7 +704,7 @@
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
- <artifactId>camel-test-spring41</artifactId>
+ <artifactId>camel-test-spring40</artifactId>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
http://git-wip-us.apache.org/repos/asf/camel/blob/7764c326/apache-camel/src/main/descriptors/common-bin.xml
----------------------------------------------------------------------
diff --git a/apache-camel/src/main/descriptors/common-bin.xml b/apache-camel/src/main/descriptors/common-bin.xml
index f5145f5..e061006 100644
--- a/apache-camel/src/main/descriptors/common-bin.xml
+++ b/apache-camel/src/main/descriptors/common-bin.xml
@@ -187,7 +187,7 @@
<include>org.apache.camel:camel-test-blueprint</include>
<include>org.apache.camel:camel-test-spring</include>
<include>org.apache.camel:camel-test-spring3</include>
- <include>org.apache.camel:camel-test-spring41</include>
+ <include>org.apache.camel:camel-test-spring40</include>
<include>org.apache.camel:camel-testng</include>
<include>org.apache.camel:camel-twitter</include>
<include>org.apache.camel:camel-urlrewrite</include>
http://git-wip-us.apache.org/repos/asf/camel/blob/7764c326/components/camel-test-spring/pom.xml
----------------------------------------------------------------------
diff --git a/components/camel-test-spring/pom.xml b/components/camel-test-spring/pom.xml
index 3e8057d..a15f34a 100644
--- a/components/camel-test-spring/pom.xml
+++ b/components/camel-test-spring/pom.xml
@@ -28,8 +28,8 @@
<artifactId>camel-test-spring</artifactId>
<packaging>bundle</packaging>
- <name>Camel :: Test :: Spring 4.0</name>
- <description>Camel Testing Library using JUnit and Spring 4.0</description>
+ <name>Camel :: Test :: Spring</name>
+ <description>Camel Testing Library using JUnit and Spring 4.1+</description>
<properties>
<camel.osgi.export.pkg>org.apache.camel.test.spring.*</camel.osgi.export.pkg>
http://git-wip-us.apache.org/repos/asf/camel/blob/7764c326/components/camel-test-spring/src/main/java/org/apache/camel/test/spring/CamelSpringJUnit4ClassRunner.java
----------------------------------------------------------------------
diff --git a/components/camel-test-spring/src/main/java/org/apache/camel/test/spring/CamelSpringJUnit4ClassRunner.java b/components/camel-test-spring/src/main/java/org/apache/camel/test/spring/CamelSpringJUnit4ClassRunner.java
index 9416e9f..3d88020 100644
--- a/components/camel-test-spring/src/main/java/org/apache/camel/test/spring/CamelSpringJUnit4ClassRunner.java
+++ b/components/camel-test-spring/src/main/java/org/apache/camel/test/spring/CamelSpringJUnit4ClassRunner.java
@@ -16,8 +16,7 @@
*/
package org.apache.camel.test.spring;
-import java.util.LinkedHashSet;
-import java.util.Set;
+import java.util.List;
import org.junit.runners.model.InitializationError;
import org.springframework.test.context.TestContextManager;
@@ -44,18 +43,7 @@ public class CamelSpringJUnit4ClassRunner extends SpringJUnit4ClassRunner {
*/
@Override
protected TestContextManager createTestContextManager(Class<?> clazz) {
- return new CamelTestContextManager(clazz, getDefaultContextLoaderClassName(clazz));
- }
-
- /**
- * Returns the specialized loader for tight integration between Camel testing features
- * and the application context initialization.
- *
- * @return Returns the class name for {@link org.apache.camel.test.spring.CamelSpringTestContextLoader}
- */
- @Override
- protected String getDefaultContextLoaderClassName(Class<?> clazz) {
- return CamelSpringTestContextLoader.class.getName();
+ return new CamelTestContextManager(clazz);
}
/**
@@ -64,25 +52,17 @@ public class CamelSpringJUnit4ClassRunner extends SpringJUnit4ClassRunner {
*/
public static final class CamelTestContextManager extends TestContextManager {
- public CamelTestContextManager(Class<?> testClass, String defaultContextLoaderClassName) {
- super(testClass, defaultContextLoaderClassName);
- }
-
- /**
- * Augments the default listeners with additional listeners to provide support
- * for the Camel testing features.
- */
- @Override
- protected Set<Class<? extends TestExecutionListener>> getDefaultTestExecutionListenerClasses() {
- Set<Class<? extends TestExecutionListener>> classes = new LinkedHashSet<Class<? extends TestExecutionListener>>();
+ public CamelTestContextManager(Class<?> testClass) {
+ super(testClass);
- classes.add(CamelSpringTestContextLoaderTestExecutionListener.class);
- classes.addAll(super.getDefaultTestExecutionListenerClasses());
- classes.add(DisableJmxTestExecutionListener.class);
- classes.add(StopWatchTestExecutionListener.class);
-
- return classes;
+ // inject Camel first, and then disable jmx and add the stop-watch
+ List<TestExecutionListener> list = getTestExecutionListeners();
+ list.add(0, new CamelSpringTestContextLoaderTestExecutionListener());
+ list.add(new DisableJmxTestExecutionListener());
+ list.add(new StopWatchTestExecutionListener());
+ registerTestExecutionListeners(list);
}
+
}
}
http://git-wip-us.apache.org/repos/asf/camel/blob/7764c326/components/camel-test-spring/src/main/java/org/apache/camel/test/spring/CamelTestContextBootstrapper.java
----------------------------------------------------------------------
diff --git a/components/camel-test-spring/src/main/java/org/apache/camel/test/spring/CamelTestContextBootstrapper.java b/components/camel-test-spring/src/main/java/org/apache/camel/test/spring/CamelTestContextBootstrapper.java
new file mode 100644
index 0000000..169eed1
--- /dev/null
+++ b/components/camel-test-spring/src/main/java/org/apache/camel/test/spring/CamelTestContextBootstrapper.java
@@ -0,0 +1,31 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.test.spring;
+
+import org.springframework.test.context.ContextLoader;
+import org.springframework.test.context.support.DefaultTestContextBootstrapper;
+
+/**
+ * To boostrap Camel for testing with Spring 4.1 onwards.
+ */
+public class CamelTestContextBootstrapper extends DefaultTestContextBootstrapper {
+
+ @Override
+ protected Class<? extends ContextLoader> getDefaultContextLoaderClass(Class<?> testClass) {
+ return CamelSpringTestContextLoader.class;
+ }
+}
http://git-wip-us.apache.org/repos/asf/camel/blob/7764c326/components/camel-test-spring/src/main/java/org/apache/camel/test/spring/StopWatchTestExecutionListener.java
----------------------------------------------------------------------
diff --git a/components/camel-test-spring/src/main/java/org/apache/camel/test/spring/StopWatchTestExecutionListener.java b/components/camel-test-spring/src/main/java/org/apache/camel/test/spring/StopWatchTestExecutionListener.java
index d3cc369..8796dfe 100644
--- a/components/camel-test-spring/src/main/java/org/apache/camel/test/spring/StopWatchTestExecutionListener.java
+++ b/components/camel-test-spring/src/main/java/org/apache/camel/test/spring/StopWatchTestExecutionListener.java
@@ -41,20 +41,22 @@ public class StopWatchTestExecutionListener extends AbstractTestExecutionListene
public void beforeTestMethod(TestContext testContext) throws Exception {
StopWatch stopWatch = new StopWatch();
threadStopWatch.set(stopWatch);
-
- stopWatch.restart();
}
@Override
public void afterTestMethod(TestContext testContext) throws Exception {
- long time = threadStopWatch.get().stop();
- threadStopWatch.remove();
- Logger log = LoggerFactory.getLogger(testContext.getTestClass());
+ StopWatch watch = threadStopWatch.get();
+ if (watch != null) {
+ long time = watch.stop();
+ Logger log = LoggerFactory.getLogger(testContext.getTestClass());
- log.info("********************************************************************************");
- log.info("Testing done: " + testContext.getTestMethod().getName() + "(" + testContext.getTestClass().getName() + ")");
- log.info("Took: " + TimeUtils.printDuration(time) + " (" + time + " millis)");
- log.info("********************************************************************************");
+ log.info("********************************************************************************");
+ log.info("Testing done: " + testContext.getTestMethod().getName() + "(" + testContext.getTestClass().getName() + ")");
+ log.info("Took: " + TimeUtils.printDuration(time) + " (" + time + " millis)");
+ log.info("********************************************************************************");
+
+ threadStopWatch.remove();
+ }
}
}
http://git-wip-us.apache.org/repos/asf/camel/blob/7764c326/components/camel-test-spring/src/test/java/org/apache/camel/test/spring/CamelSpringJUnit4ClassRunnerPlainTest.java
----------------------------------------------------------------------
diff --git a/components/camel-test-spring/src/test/java/org/apache/camel/test/spring/CamelSpringJUnit4ClassRunnerPlainTest.java b/components/camel-test-spring/src/test/java/org/apache/camel/test/spring/CamelSpringJUnit4ClassRunnerPlainTest.java
index 337d387..8b1504f 100644
--- a/components/camel-test-spring/src/test/java/org/apache/camel/test/spring/CamelSpringJUnit4ClassRunnerPlainTest.java
+++ b/components/camel-test-spring/src/test/java/org/apache/camel/test/spring/CamelSpringJUnit4ClassRunnerPlainTest.java
@@ -26,13 +26,12 @@ import org.apache.camel.ServiceStatus;
import org.apache.camel.component.mock.MockEndpoint;
import org.apache.camel.management.DefaultManagementStrategy;
import org.apache.camel.util.StopWatch;
-
import org.junit.Test;
import org.junit.runner.RunWith;
-
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.annotation.DirtiesContext.ClassMode;
+import org.springframework.test.context.BootstrapWith;
import org.springframework.test.context.ContextConfiguration;
import static org.junit.Assert.assertEquals;
@@ -42,8 +41,10 @@ import static org.junit.Assert.assertTrue;
// START SNIPPET: e1
@RunWith(CamelSpringJUnit4ClassRunner.class)
-@ContextConfiguration
-// Put here to prevent Spring context caching across tests and test methods since some tests inherit
+// must tell Spring to bootstrap with Camel
+@BootstrapWith(CamelTestContextBootstrapper.class)
+@ContextConfiguration()
+// Put here to prevent Spring context caching across tests and test methods since some tests inherit
// from this test and therefore use the same Spring context. Also because we want to reset the
// Camel context and mock endpoints between test methods automatically.
@DirtiesContext(classMode = ClassMode.AFTER_EACH_TEST_METHOD)
http://git-wip-us.apache.org/repos/asf/camel/blob/7764c326/components/camel-test-spring40/pom.xml
----------------------------------------------------------------------
diff --git a/components/camel-test-spring40/pom.xml b/components/camel-test-spring40/pom.xml
new file mode 100644
index 0000000..f13e99d
--- /dev/null
+++ b/components/camel-test-spring40/pom.xml
@@ -0,0 +1,95 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+ Licensed to the Apache Software Foundation (ASF) under one or more
+ contributor license agreements. See the NOTICE file distributed with
+ this work for additional information regarding copyright ownership.
+ The ASF licenses this file to You under the Apache License, Version 2.0
+ (the "License"); you may not use this file except in compliance with
+ the License. You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+-->
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
+
+ <modelVersion>4.0.0</modelVersion>
+
+ <parent>
+ <groupId>org.apache.camel</groupId>
+ <artifactId>components</artifactId>
+ <version>2.15-SNAPSHOT</version>
+ </parent>
+
+ <artifactId>camel-test-spring40</artifactId>
+ <packaging>bundle</packaging>
+ <name>Camel :: Test :: Spring 4.0</name>
+ <description>Camel Testing Library using JUnit and Spring 4.0</description>
+
+ <properties>
+ <camel.osgi.export.pkg>org.apache.camel.test.spring.*</camel.osgi.export.pkg>
+ <spring-version>${spring40-version}</spring-version>
+ </properties>
+
+ <dependencies>
+
+ <dependency>
+ <groupId>org.apache.camel</groupId>
+ <artifactId>camel-test</artifactId>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.camel</groupId>
+ <artifactId>camel-spring</artifactId>
+ </dependency>
+ <dependency>
+ <groupId>org.springframework</groupId>
+ <artifactId>spring-test</artifactId>
+ <version>${spring40-version}</version>
+ </dependency>
+ <!-- we need to override the other spring version -->
+ <dependency>
+ <groupId>org.springframework</groupId>
+ <artifactId>spring-context</artifactId>
+ <version>${spring40-version}</version>
+ </dependency>
+ <dependency>
+ <groupId>org.springframework</groupId>
+ <artifactId>spring-beans</artifactId>
+ <version>${spring40-version}</version>
+ </dependency>
+ <dependency>
+ <groupId>org.springframework</groupId>
+ <artifactId>spring-expression</artifactId>
+ <version>${spring40-version}</version>
+ </dependency>
+ <dependency>
+ <groupId>org.springframework</groupId>
+ <artifactId>spring-aop</artifactId>
+ <version>${spring40-version}</version>
+ </dependency>
+ <dependency>
+ <groupId>org.springframework</groupId>
+ <artifactId>spring-tx</artifactId>
+ <version>${spring40-version}</version>
+ </dependency>
+ <dependency>
+ <groupId>org.springframework</groupId>
+ <artifactId>spring-core</artifactId>
+ <version>${spring40-version}</version>
+ </dependency>
+
+
+ <!-- test dependencies -->
+ <dependency>
+ <groupId>org.slf4j</groupId>
+ <artifactId>slf4j-log4j12</artifactId>
+ <scope>test</scope>
+ </dependency>
+ </dependencies>
+
+</project>
http://git-wip-us.apache.org/repos/asf/camel/blob/7764c326/components/camel-test-spring40/src/main/java/org/apache/camel/test/spring/CamelSpringDelegatingTestContextLoader.java
----------------------------------------------------------------------
diff --git a/components/camel-test-spring40/src/main/java/org/apache/camel/test/spring/CamelSpringDelegatingTestContextLoader.java b/components/camel-test-spring40/src/main/java/org/apache/camel/test/spring/CamelSpringDelegatingTestContextLoader.java
new file mode 100644
index 0000000..a3bb4a3
--- /dev/null
+++ b/components/camel-test-spring40/src/main/java/org/apache/camel/test/spring/CamelSpringDelegatingTestContextLoader.java
@@ -0,0 +1,321 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.test.spring;
+
+import java.lang.reflect.Method;
+import java.lang.reflect.Modifier;
+import java.util.Collection;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.concurrent.TimeUnit;
+
+import org.apache.camel.impl.DefaultDebugger;
+import org.apache.camel.impl.InterceptSendToMockEndpointStrategy;
+import org.apache.camel.management.JmxSystemPropertyKeys;
+import org.apache.camel.spi.Breakpoint;
+import org.apache.camel.spi.Debugger;
+import org.apache.camel.spring.SpringCamelContext;
+import org.apache.camel.test.spring.CamelSpringTestHelper.DoToSpringCamelContextsStrategy;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.beans.factory.support.BeanDefinitionRegistry;
+import org.springframework.context.ApplicationContext;
+import org.springframework.context.ConfigurableApplicationContext;
+import org.springframework.context.annotation.AnnotationConfigUtils;
+import org.springframework.core.annotation.AnnotationUtils;
+import org.springframework.test.context.MergedContextConfiguration;
+import org.springframework.test.context.support.DelegatingSmartContextLoader;
+
+import static org.apache.camel.test.spring.CamelSpringTestHelper.getAllMethods;
+
+
+
+/**
+ * CamelSpringDelegatingTestContextLoader which fixes issues in Camel's JavaConfigContextLoader. (adds support for Camel's test annotations)
+ * <br>
+ * <em>This loader can handle either classes or locations for configuring the context.</em>
+ * <br>
+ * NOTE: This TestContextLoader doesn't support the annotation of ExcludeRoutes now.
+ */
+public class CamelSpringDelegatingTestContextLoader extends DelegatingSmartContextLoader {
+
+ protected final Logger logger = LoggerFactory.getLogger(getClass());
+
+ @Override
+ public ApplicationContext loadContext(MergedContextConfiguration mergedConfig) throws Exception {
+
+ Class<?> testClass = getTestClass();
+
+ if (logger.isDebugEnabled()) {
+ logger.debug("Loading ApplicationContext for merged context configuration [{}].", mergedConfig);
+ }
+
+ // Pre CamelContext(s) instantiation setup
+ handleDisableJmx(null, testClass);
+
+ try {
+ SpringCamelContext.setNoStart(true);
+ ConfigurableApplicationContext context = (ConfigurableApplicationContext) super.loadContext(mergedConfig);
+ SpringCamelContext.setNoStart(false);
+ return loadContext(context, testClass);
+ } finally {
+ cleanup(testClass);
+ }
+ }
+
+ /**
+ * Performs the bulk of the Spring application context loading/customization.
+ *
+ * @param context the partially configured context. The context should have the bean definitions loaded, but nothing else.
+ * @param testClass the test class being executed
+ * @return the initialized (refreshed) Spring application context
+ *
+ * @throws Exception if there is an error during initialization/customization
+ */
+ public ApplicationContext loadContext(ConfigurableApplicationContext context, Class<?> testClass)
+ throws Exception {
+
+ AnnotationConfigUtils.registerAnnotationConfigProcessors((BeanDefinitionRegistry) context);
+
+ // Post CamelContext(s) instantiation but pre CamelContext(s) start setup
+ handleProvidesBreakpoint(context, testClass);
+ handleShutdownTimeout(context, testClass);
+ handleMockEndpoints(context, testClass);
+ handleMockEndpointsAndSkip(context, testClass);
+
+ // CamelContext(s) startup
+ handleCamelContextStartup(context, testClass);
+
+ return context;
+ }
+
+ /**
+ * Cleanup/restore global state to defaults / pre-test values after the test setup
+ * is complete.
+ *
+ * @param testClass the test class being executed
+ */
+ protected void cleanup(Class<?> testClass) {
+ SpringCamelContext.setNoStart(false);
+
+ if (testClass.isAnnotationPresent(DisableJmx.class)) {
+ if (CamelSpringTestHelper.getOriginalJmxDisabled() == null) {
+ System.clearProperty(JmxSystemPropertyKeys.DISABLED);
+ } else {
+ System.setProperty(JmxSystemPropertyKeys.DISABLED,
+ CamelSpringTestHelper.getOriginalJmxDisabled());
+ }
+ }
+ }
+
+ /**
+ * Handles disabling of JMX on Camel contexts based on {@link DisableJmx}.
+ *
+ * @param context the initialized Spring context
+ * @param testClass the test class being executed
+ */
+ protected void handleDisableJmx(ConfigurableApplicationContext context, Class<?> testClass) {
+ CamelSpringTestHelper.setOriginalJmxDisabledValue(System.getProperty(JmxSystemPropertyKeys.DISABLED));
+
+ if (testClass.isAnnotationPresent(DisableJmx.class)) {
+ if (testClass.getAnnotation(DisableJmx.class).value()) {
+ logger.info("Disabling Camel JMX globally as DisableJmx annotation was found and disableJmx is set to true.");
+ System.setProperty(JmxSystemPropertyKeys.DISABLED, "true");
+
+ } else {
+ logger.info("Enabling Camel JMX as DisableJmx annotation was found and disableJmx is set to false.");
+ System.clearProperty(JmxSystemPropertyKeys.DISABLED);
+ }
+ } else {
+ logger.info("Disabling Camel JMX globally for tests by default. Use the DisableJMX annotation to override the default setting.");
+ System.setProperty(JmxSystemPropertyKeys.DISABLED, "true");
+ }
+ }
+
+ /**
+ * Handles the processing of the {@link ProvidesBreakpoint} annotation on a test class. Exists here
+ * as it is needed in
+ *
+ * @param context the initialized Spring context containing the Camel context(s) to insert breakpoints into
+ * @param testClass the test class being processed
+ *
+ * @throws Exception if there is an error processing the class
+ */
+ protected void handleProvidesBreakpoint(ConfigurableApplicationContext context, Class<?> testClass) throws Exception {
+ Collection<Method> methods = getAllMethods(testClass);
+ final List<Breakpoint> breakpoints = new LinkedList<Breakpoint>();
+
+ for (Method method : methods) {
+ if (AnnotationUtils.findAnnotation(method, ProvidesBreakpoint.class) != null) {
+ Class<?>[] argTypes = method.getParameterTypes();
+ if (argTypes.length != 0) {
+ throw new IllegalArgumentException("Method [" + method.getName()
+ + "] is annotated with ProvidesBreakpoint but is not a no-argument method.");
+ } else if (!Breakpoint.class.isAssignableFrom(method.getReturnType())) {
+ throw new IllegalArgumentException("Method [" + method.getName()
+ + "] is annotated with ProvidesBreakpoint but does not return a Breakpoint.");
+ } else if (!Modifier.isStatic(method.getModifiers())) {
+ throw new IllegalArgumentException("Method [" + method.getName()
+ + "] is annotated with ProvidesBreakpoint but is not static.");
+ } else if (!Modifier.isPublic(method.getModifiers())) {
+ throw new IllegalArgumentException("Method [" + method.getName()
+ + "] is annotated with ProvidesBreakpoint but is not public.");
+ }
+
+ try {
+ breakpoints.add((Breakpoint) method.invoke(null));
+ } catch (Exception e) {
+ throw new RuntimeException("Method [" + method.getName()
+ + "] threw exception during evaluation.", e);
+ }
+ }
+ }
+
+ if (breakpoints.size() != 0) {
+ CamelSpringTestHelper.doToSpringCamelContexts(context, new DoToSpringCamelContextsStrategy() {
+
+ public void execute(String contextName, SpringCamelContext camelContext)
+ throws Exception {
+ Debugger debugger = camelContext.getDebugger();
+ if (debugger == null) {
+ debugger = new DefaultDebugger();
+ camelContext.setDebugger(debugger);
+ }
+
+ for (Breakpoint breakpoint : breakpoints) {
+ logger.info("Adding Breakpoint [{}] to CamelContext with name [{}].", breakpoint, contextName);
+ debugger.addBreakpoint(breakpoint);
+ }
+ }
+ });
+ }
+ }
+
+
+ /**
+ * Handles updating shutdown timeouts on Camel contexts based on {@link ShutdownTimeout}.
+ *
+ * @param context the initialized Spring context
+ * @param testClass the test class being executed
+ */
+ protected void handleShutdownTimeout(ConfigurableApplicationContext context, Class<?> testClass) throws Exception {
+ final int shutdownTimeout;
+ final TimeUnit shutdownTimeUnit;
+ if (testClass.isAnnotationPresent(ShutdownTimeout.class)) {
+ shutdownTimeout = testClass.getAnnotation(ShutdownTimeout.class).value();
+ shutdownTimeUnit = testClass.getAnnotation(ShutdownTimeout.class).timeUnit();
+ } else {
+ shutdownTimeout = 10;
+ shutdownTimeUnit = TimeUnit.SECONDS;
+ }
+
+ CamelSpringTestHelper.doToSpringCamelContexts(context, new DoToSpringCamelContextsStrategy() {
+
+ public void execute(String contextName, SpringCamelContext camelContext)
+ throws Exception {
+ logger.info("Setting shutdown timeout to [{} {}] on CamelContext with name [{}].", new Object[]{shutdownTimeout, shutdownTimeUnit, contextName});
+ camelContext.getShutdownStrategy().setTimeout(shutdownTimeout);
+ camelContext.getShutdownStrategy().setTimeUnit(shutdownTimeUnit);
+ }
+ });
+ }
+
+ /**
+ * Handles auto-intercepting of endpoints with mocks based on {@link MockEndpoints}.
+ *
+ * @param context the initialized Spring context
+ * @param testClass the test class being executed
+ */
+ protected void handleMockEndpoints(ConfigurableApplicationContext context, Class<?> testClass) throws Exception {
+ if (testClass.isAnnotationPresent(MockEndpoints.class)) {
+ final String mockEndpoints = testClass.getAnnotation(MockEndpoints.class).value();
+ CamelSpringTestHelper.doToSpringCamelContexts(context, new DoToSpringCamelContextsStrategy() {
+
+ public void execute(String contextName, SpringCamelContext camelContext)
+ throws Exception {
+ logger.info("Enabling auto mocking of endpoints matching pattern [{}] on CamelContext with name [{}].", mockEndpoints, contextName);
+ camelContext.addRegisterEndpointCallback(new InterceptSendToMockEndpointStrategy(mockEndpoints));
+ }
+ });
+ }
+ }
+
+ /**
+ * Handles auto-intercepting of endpoints with mocks based on {@link MockEndpointsAndSkip} and skipping the
+ * original endpoint.
+ *
+ * @param context the initialized Spring context
+ * @param testClass the test class being executed
+ */
+ protected void handleMockEndpointsAndSkip(ConfigurableApplicationContext context, Class<?> testClass) throws Exception {
+ if (testClass.isAnnotationPresent(MockEndpointsAndSkip.class)) {
+ final String mockEndpoints = testClass.getAnnotation(MockEndpointsAndSkip.class).value();
+ CamelSpringTestHelper.doToSpringCamelContexts(context, new DoToSpringCamelContextsStrategy() {
+
+ public void execute(String contextName, SpringCamelContext camelContext)
+ throws Exception {
+ logger.info("Enabling auto mocking and skipping of endpoints matching pattern [{}] on CamelContext with name [{}].", mockEndpoints, contextName);
+ camelContext.addRegisterEndpointCallback(new InterceptSendToMockEndpointStrategy(mockEndpoints, true));
+ }
+ });
+ }
+ }
+
+
+ /**
+ * Handles starting of Camel contexts based on {@link UseAdviceWith} and other state in the JVM.
+ *
+ * @param context the initialized Spring context
+ * @param testClass the test class being executed
+ */
+ protected void handleCamelContextStartup(ConfigurableApplicationContext context, Class<?> testClass) throws Exception {
+ boolean skip = "true".equalsIgnoreCase(System.getProperty("skipStartingCamelContext"));
+ if (skip) {
+ logger.info("Skipping starting CamelContext(s) as system property skipStartingCamelContext is set to be true.");
+ } else if (testClass.isAnnotationPresent(UseAdviceWith.class)) {
+ if (testClass.getAnnotation(UseAdviceWith.class).value()) {
+ logger.info("Skipping starting CamelContext(s) as UseAdviceWith annotation was found and isUseAdviceWith is set to true.");
+ skip = true;
+ } else {
+ logger.info("Starting CamelContext(s) as UseAdviceWith annotation was found, but isUseAdviceWith is set to false.");
+ skip = false;
+ }
+ }
+
+ if (!skip) {
+ CamelSpringTestHelper.doToSpringCamelContexts(context, new DoToSpringCamelContextsStrategy() {
+ public void execute(String contextName,
+ SpringCamelContext camelContext) throws Exception {
+ logger.info("Starting CamelContext with name [{}].", contextName);
+ camelContext.start();
+ }
+ });
+ }
+ }
+
+ /**
+ * Returns the class under test in order to enable inspection of annotations while the
+ * Spring context is being created.
+ *
+ * @return the test class that is being executed
+ * @see CamelSpringTestHelper
+ */
+ protected Class<?> getTestClass() {
+ return CamelSpringTestHelper.getTestClass();
+ }
+
+}
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/camel/blob/7764c326/components/camel-test-spring40/src/main/java/org/apache/camel/test/spring/CamelSpringJUnit4ClassRunner.java
----------------------------------------------------------------------
diff --git a/components/camel-test-spring40/src/main/java/org/apache/camel/test/spring/CamelSpringJUnit4ClassRunner.java b/components/camel-test-spring40/src/main/java/org/apache/camel/test/spring/CamelSpringJUnit4ClassRunner.java
new file mode 100644
index 0000000..9416e9f
--- /dev/null
+++ b/components/camel-test-spring40/src/main/java/org/apache/camel/test/spring/CamelSpringJUnit4ClassRunner.java
@@ -0,0 +1,88 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.test.spring;
+
+import java.util.LinkedHashSet;
+import java.util.Set;
+
+import org.junit.runners.model.InitializationError;
+import org.springframework.test.context.TestContextManager;
+import org.springframework.test.context.TestExecutionListener;
+import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
+
+/**
+ * An implementation bringing the functionality of {@link org.apache.camel.test.spring.CamelSpringTestSupport} to
+ * Spring Test based test cases. This approach allows developers to implement tests
+ * for their Spring based applications/routes using the typical Spring Test conventions
+ * for test development.
+ */
+public class CamelSpringJUnit4ClassRunner extends SpringJUnit4ClassRunner {
+
+ public CamelSpringJUnit4ClassRunner(Class<?> clazz) throws InitializationError {
+ super(clazz);
+ }
+
+ /**
+ * Returns the specialized manager instance that provides tight integration between Camel testing
+ * features and Spring.
+ *
+ * @return a new instance of {@link CamelTestContextManager}.
+ */
+ @Override
+ protected TestContextManager createTestContextManager(Class<?> clazz) {
+ return new CamelTestContextManager(clazz, getDefaultContextLoaderClassName(clazz));
+ }
+
+ /**
+ * Returns the specialized loader for tight integration between Camel testing features
+ * and the application context initialization.
+ *
+ * @return Returns the class name for {@link org.apache.camel.test.spring.CamelSpringTestContextLoader}
+ */
+ @Override
+ protected String getDefaultContextLoaderClassName(Class<?> clazz) {
+ return CamelSpringTestContextLoader.class.getName();
+ }
+
+ /**
+ * An implementation providing additional integration between Spring Test and Camel
+ * testing features.
+ */
+ public static final class CamelTestContextManager extends TestContextManager {
+
+ public CamelTestContextManager(Class<?> testClass, String defaultContextLoaderClassName) {
+ super(testClass, defaultContextLoaderClassName);
+ }
+
+ /**
+ * Augments the default listeners with additional listeners to provide support
+ * for the Camel testing features.
+ */
+ @Override
+ protected Set<Class<? extends TestExecutionListener>> getDefaultTestExecutionListenerClasses() {
+ Set<Class<? extends TestExecutionListener>> classes = new LinkedHashSet<Class<? extends TestExecutionListener>>();
+
+ classes.add(CamelSpringTestContextLoaderTestExecutionListener.class);
+ classes.addAll(super.getDefaultTestExecutionListenerClasses());
+ classes.add(DisableJmxTestExecutionListener.class);
+ classes.add(StopWatchTestExecutionListener.class);
+
+ return classes;
+ }
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/camel/blob/7764c326/components/camel-test-spring40/src/main/java/org/apache/camel/test/spring/CamelSpringTestContextLoader.java
----------------------------------------------------------------------
diff --git a/components/camel-test-spring40/src/main/java/org/apache/camel/test/spring/CamelSpringTestContextLoader.java b/components/camel-test-spring40/src/main/java/org/apache/camel/test/spring/CamelSpringTestContextLoader.java
new file mode 100644
index 0000000..e973b44
--- /dev/null
+++ b/components/camel-test-spring40/src/main/java/org/apache/camel/test/spring/CamelSpringTestContextLoader.java
@@ -0,0 +1,473 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.test.spring;
+
+import java.lang.reflect.Method;
+import java.lang.reflect.Modifier;
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.HashSet;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.concurrent.TimeUnit;
+
+import org.apache.camel.impl.DefaultDebugger;
+import org.apache.camel.impl.InterceptSendToMockEndpointStrategy;
+import org.apache.camel.management.JmxSystemPropertyKeys;
+import org.apache.camel.spi.Breakpoint;
+import org.apache.camel.spi.Debugger;
+import org.apache.camel.spring.SpringCamelContext;
+import org.apache.camel.test.ExcludingPackageScanClassResolver;
+import org.apache.camel.test.spring.CamelSpringTestHelper.DoToSpringCamelContextsStrategy;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.beans.factory.support.RootBeanDefinition;
+import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
+import org.springframework.context.ApplicationContext;
+import org.springframework.context.annotation.AnnotationConfigUtils;
+import org.springframework.context.support.GenericApplicationContext;
+import org.springframework.core.annotation.AnnotationUtils;
+import org.springframework.test.context.MergedContextConfiguration;
+import org.springframework.test.context.support.AbstractContextLoader;
+import org.springframework.test.context.support.AbstractGenericContextLoader;
+import org.springframework.test.context.support.GenericXmlContextLoader;
+import org.springframework.util.StringUtils;
+
+import static org.apache.camel.test.spring.CamelSpringTestHelper.getAllMethods;
+
+/**
+ * Replacement for the default {@link GenericXmlContextLoader} that provides hooks for
+ * processing some class level Camel related test annotations.
+ */
+public class CamelSpringTestContextLoader extends AbstractContextLoader {
+
+ private static final Logger LOG = LoggerFactory.getLogger(CamelSpringTestContextLoader.class);
+
+ /**
+ * Modeled after the Spring implementation in {@link AbstractGenericContextLoader},
+ * this method creates and refreshes the application context while providing for
+ * processing of additional Camel specific post-refresh actions. We do not provide the
+ * pre-post hooks for customization seen in {@link AbstractGenericContextLoader} because
+ * they probably are unnecessary for 90+% of users.
+ * <p/>
+ * For some functionality, we cannot use {@link org.springframework.test.context.TestExecutionListener} because we need
+ * to both produce the desired outcome during application context loading, and also cleanup
+ * after ourselves even if the test class never executes. Thus the listeners, which
+ * only run if the application context is successfully initialized are insufficient to
+ * provide the behavior described above.
+ */
+ @Override
+ public ApplicationContext loadContext(MergedContextConfiguration mergedConfig) throws Exception {
+ Class<?> testClass = getTestClass();
+
+ if (LOG.isDebugEnabled()) {
+ LOG.debug("Loading ApplicationContext for merged context configuration [{}].", mergedConfig);
+ }
+
+ try {
+ GenericApplicationContext context = createContext(testClass, mergedConfig);
+ context.getEnvironment().setActiveProfiles(mergedConfig.getActiveProfiles());
+ loadBeanDefinitions(context, mergedConfig);
+ return loadContext(context, testClass);
+ } finally {
+ cleanup(testClass);
+ }
+ }
+
+ /**
+ * Modeled after the Spring implementation in {@link AbstractGenericContextLoader},
+ * this method creates and refreshes the application context while providing for
+ * processing of additional Camel specific post-refresh actions. We do not provide the
+ * pre-post hooks for customization seen in {@link AbstractGenericContextLoader} because
+ * they probably are unnecessary for 90+% of users.
+ * <p/>
+ * For some functionality, we cannot use {@link org.springframework.test.context.TestExecutionListener} because we need
+ * to both produce the desired outcome during application context loading, and also cleanup
+ * after ourselves even if the test class never executes. Thus the listeners, which
+ * only run if the application context is successfully initialized are insufficient to
+ * provide the behavior described above.
+ */
+ @Override
+ public ApplicationContext loadContext(String... locations) throws Exception {
+
+ Class<?> testClass = getTestClass();
+
+ if (LOG.isDebugEnabled()) {
+ LOG.debug("Loading ApplicationContext for locations [" + StringUtils.arrayToCommaDelimitedString(locations) + "].");
+ }
+
+ try {
+ GenericApplicationContext context = createContext(testClass, null);
+ loadBeanDefinitions(context, locations);
+ return loadContext(context, testClass);
+ } finally {
+ cleanup(testClass);
+ }
+ }
+
+ /**
+ * Returns "<code>-context.xml</code>".
+ */
+ @Override
+ public String getResourceSuffix() {
+ return "-context.xml";
+ }
+
+ /**
+ * Performs the bulk of the Spring application context loading/customization.
+ *
+ * @param context the partially configured context. The context should have the bean definitions loaded, but nothing else.
+ * @param testClass the test class being executed
+ * @return the initialized (refreshed) Spring application context
+ *
+ * @throws Exception if there is an error during initialization/customization
+ */
+ protected ApplicationContext loadContext(GenericApplicationContext context, Class<?> testClass) throws Exception {
+
+ AnnotationConfigUtils.registerAnnotationConfigProcessors(context);
+
+ // Pre CamelContext(s) instantiation setup
+ handleDisableJmx(context, testClass);
+
+ // Temporarily disable CamelContext start while the contexts are instantiated.
+ SpringCamelContext.setNoStart(true);
+ context.refresh();
+ context.registerShutdownHook();
+ // Turn CamelContext startup back on since the context's have now been instantiated.
+ SpringCamelContext.setNoStart(false);
+
+ // Post CamelContext(s) instantiation but pre CamelContext(s) start setup
+ handleProvidesBreakpoint(context, testClass);
+ handleShutdownTimeout(context, testClass);
+ handleMockEndpoints(context, testClass);
+ handleMockEndpointsAndSkip(context, testClass);
+ handleLazyLoadTypeConverters(context, testClass);
+
+ // CamelContext(s) startup
+ handleCamelContextStartup(context, testClass);
+
+ return context;
+ }
+
+ /**
+ * Cleanup/restore global state to defaults / pre-test values after the test setup
+ * is complete.
+ *
+ * @param testClass the test class being executed
+ */
+ protected void cleanup(Class<?> testClass) {
+ SpringCamelContext.setNoStart(false);
+
+ if (testClass.isAnnotationPresent(DisableJmx.class)) {
+ if (CamelSpringTestHelper.getOriginalJmxDisabled() == null) {
+ System.clearProperty(JmxSystemPropertyKeys.DISABLED);
+ } else {
+ System.setProperty(JmxSystemPropertyKeys.DISABLED,
+ CamelSpringTestHelper.getOriginalJmxDisabled());
+ }
+ }
+ }
+
+ protected void loadBeanDefinitions(GenericApplicationContext context, MergedContextConfiguration mergedConfig) {
+ (new XmlBeanDefinitionReader(context)).loadBeanDefinitions(mergedConfig.getLocations());
+ }
+
+ protected void loadBeanDefinitions(GenericApplicationContext context, String... locations) {
+ (new XmlBeanDefinitionReader(context)).loadBeanDefinitions(locations);
+ }
+
+ /**
+ * Creates and starts the Spring context while optionally starting any loaded Camel contexts.
+ *
+ * @param testClass the test class that is being executed
+ * @return the loaded Spring context
+ */
+ protected GenericApplicationContext createContext(Class<?> testClass, MergedContextConfiguration mergedConfig) {
+ ApplicationContext parentContext = null;
+ GenericApplicationContext routeExcludingContext = null;
+
+ if (mergedConfig != null) {
+ parentContext = mergedConfig.getParentApplicationContext();
+
+ }
+
+ if (testClass.isAnnotationPresent(ExcludeRoutes.class)) {
+ Class<?>[] excludedClasses = testClass.getAnnotation(ExcludeRoutes.class).value();
+
+ if (excludedClasses.length > 0) {
+ if (LOG.isDebugEnabled()) {
+ LOG.debug("Setting up package scanning excluded classes as ExcludeRoutes "
+ + "annotation was found. Excluding [" + StringUtils.arrayToCommaDelimitedString(excludedClasses) + "].");
+ }
+
+ if (parentContext == null) {
+ routeExcludingContext = new GenericApplicationContext();
+ } else {
+ routeExcludingContext = new GenericApplicationContext(parentContext);
+ }
+ routeExcludingContext.registerBeanDefinition("excludingResolver", new RootBeanDefinition(ExcludingPackageScanClassResolver.class));
+ routeExcludingContext.refresh();
+
+ ExcludingPackageScanClassResolver excludingResolver = routeExcludingContext.getBean("excludingResolver", ExcludingPackageScanClassResolver.class);
+ List<Class<?>> excluded = Arrays.asList(excludedClasses);
+ excludingResolver.setExcludedClasses(new HashSet<Class<?>>(excluded));
+ } else {
+ if (LOG.isDebugEnabled()) {
+ LOG.debug("Not enabling package scanning excluded classes as ExcludeRoutes "
+ + "annotation was found but no classes were excluded.");
+ }
+ }
+ }
+
+ GenericApplicationContext context;
+
+ if (routeExcludingContext != null) {
+ context = new GenericApplicationContext(routeExcludingContext);
+ } else {
+ if (parentContext != null) {
+ context = new GenericApplicationContext(parentContext);
+ } else {
+ context = new GenericApplicationContext();
+ }
+ }
+
+ return context;
+ }
+
+ /**
+ * Handles disabling of JMX on Camel contexts based on {@link DisableJmx}.
+ *
+ * @param context the initialized Spring context
+ * @param testClass the test class being executed
+ */
+ protected void handleDisableJmx(GenericApplicationContext context, Class<?> testClass) {
+ CamelSpringTestHelper.setOriginalJmxDisabledValue(System.getProperty(JmxSystemPropertyKeys.DISABLED));
+
+ if (testClass.isAnnotationPresent(DisableJmx.class)) {
+ if (testClass.getAnnotation(DisableJmx.class).value()) {
+ LOG.info("Disabling Camel JMX globally as DisableJmx annotation was found and disableJmx is set to true.");
+ System.setProperty(JmxSystemPropertyKeys.DISABLED, "true");
+ } else {
+ LOG.info("Enabling Camel JMX as DisableJmx annotation was found and disableJmx is set to false.");
+ System.clearProperty(JmxSystemPropertyKeys.DISABLED);
+ }
+ } else {
+ LOG.info("Disabling Camel JMX globally for tests by default. Use the DisableJMX annotation to override the default setting.");
+ System.setProperty(JmxSystemPropertyKeys.DISABLED, "true");
+ }
+ }
+
+ /**
+ * Handles the processing of the {@link ProvidesBreakpoint} annotation on a test class. Exists here
+ * as it is needed in
+ *
+ * @param context the initialized Spring context containing the Camel context(s) to insert breakpoints into
+ * @param testClass the test class being processed
+ *
+ * @throws Exception if there is an error processing the class
+ */
+ protected void handleProvidesBreakpoint(GenericApplicationContext context, Class<?> testClass) throws Exception {
+ Collection<Method> methods = getAllMethods(testClass);
+ final List<Breakpoint> breakpoints = new LinkedList<Breakpoint>();
+
+ for (Method method : methods) {
+ if (AnnotationUtils.findAnnotation(method, ProvidesBreakpoint.class) != null) {
+ Class<?>[] argTypes = method.getParameterTypes();
+ if (argTypes.length != 0) {
+ throw new IllegalArgumentException("Method [" + method.getName()
+ + "] is annotated with ProvidesBreakpoint but is not a no-argument method.");
+ } else if (!Breakpoint.class.isAssignableFrom(method.getReturnType())) {
+ throw new IllegalArgumentException("Method [" + method.getName()
+ + "] is annotated with ProvidesBreakpoint but does not return a Breakpoint.");
+ } else if (!Modifier.isStatic(method.getModifiers())) {
+ throw new IllegalArgumentException("Method [" + method.getName()
+ + "] is annotated with ProvidesBreakpoint but is not static.");
+ } else if (!Modifier.isPublic(method.getModifiers())) {
+ throw new IllegalArgumentException("Method [" + method.getName()
+ + "] is annotated with ProvidesBreakpoint but is not public.");
+ }
+
+ try {
+ breakpoints.add((Breakpoint) method.invoke(null));
+ } catch (Exception e) {
+ throw new RuntimeException("Method [" + method.getName()
+ + "] threw exception during evaluation.", e);
+ }
+ }
+ }
+
+ if (breakpoints.size() != 0) {
+ CamelSpringTestHelper.doToSpringCamelContexts(context, new DoToSpringCamelContextsStrategy() {
+
+ @Override
+ public void execute(String contextName, SpringCamelContext camelContext)
+ throws Exception {
+ Debugger debugger = camelContext.getDebugger();
+ if (debugger == null) {
+ debugger = new DefaultDebugger();
+ camelContext.setDebugger(debugger);
+ }
+
+ for (Breakpoint breakpoint : breakpoints) {
+ LOG.info("Adding Breakpoint [{}] to CamelContext with name [{}].", breakpoint, contextName);
+ debugger.addBreakpoint(breakpoint);
+ }
+ }
+ });
+ }
+ }
+
+
+ /**
+ * Handles updating shutdown timeouts on Camel contexts based on {@link ShutdownTimeout}.
+ *
+ * @param context the initialized Spring context
+ * @param testClass the test class being executed
+ */
+ protected void handleShutdownTimeout(GenericApplicationContext context, Class<?> testClass) throws Exception {
+ final int shutdownTimeout;
+ final TimeUnit shutdownTimeUnit;
+ if (testClass.isAnnotationPresent(ShutdownTimeout.class)) {
+ shutdownTimeout = testClass.getAnnotation(ShutdownTimeout.class).value();
+ shutdownTimeUnit = testClass.getAnnotation(ShutdownTimeout.class).timeUnit();
+ } else {
+ shutdownTimeout = 10;
+ shutdownTimeUnit = TimeUnit.SECONDS;
+ }
+
+ CamelSpringTestHelper.doToSpringCamelContexts(context, new DoToSpringCamelContextsStrategy() {
+
+ @Override
+ public void execute(String contextName, SpringCamelContext camelContext)
+ throws Exception {
+ LOG.info("Setting shutdown timeout to [{} {}] on CamelContext with name [{}].", new Object[]{shutdownTimeout, shutdownTimeUnit, contextName});
+ camelContext.getShutdownStrategy().setTimeout(shutdownTimeout);
+ camelContext.getShutdownStrategy().setTimeUnit(shutdownTimeUnit);
+ }
+ });
+ }
+
+ /**
+ * Handles auto-intercepting of endpoints with mocks based on {@link MockEndpoints}.
+ *
+ * @param context the initialized Spring context
+ * @param testClass the test class being executed
+ */
+ protected void handleMockEndpoints(GenericApplicationContext context, Class<?> testClass) throws Exception {
+ if (testClass.isAnnotationPresent(MockEndpoints.class)) {
+ final String mockEndpoints = testClass.getAnnotation(MockEndpoints.class).value();
+ CamelSpringTestHelper.doToSpringCamelContexts(context, new DoToSpringCamelContextsStrategy() {
+
+ @Override
+ public void execute(String contextName, SpringCamelContext camelContext)
+ throws Exception {
+ LOG.info("Enabling auto mocking of endpoints matching pattern [{}] on CamelContext with name [{}].", mockEndpoints, contextName);
+ camelContext.addRegisterEndpointCallback(new InterceptSendToMockEndpointStrategy(mockEndpoints));
+ }
+ });
+ }
+ }
+
+ /**
+ * Handles auto-intercepting of endpoints with mocks based on {@link MockEndpointsAndSkip} and skipping the
+ * original endpoint.
+ *
+ * @param context the initialized Spring context
+ * @param testClass the test class being executed
+ */
+ protected void handleMockEndpointsAndSkip(GenericApplicationContext context, Class<?> testClass) throws Exception {
+ if (testClass.isAnnotationPresent(MockEndpointsAndSkip.class)) {
+ final String mockEndpoints = testClass.getAnnotation(MockEndpointsAndSkip.class).value();
+ CamelSpringTestHelper.doToSpringCamelContexts(context, new DoToSpringCamelContextsStrategy() {
+
+ @Override
+ public void execute(String contextName, SpringCamelContext camelContext)
+ throws Exception {
+ LOG.info("Enabling auto mocking and skipping of endpoints matching pattern [{}] on CamelContext with name [{}].", mockEndpoints, contextName);
+ camelContext.addRegisterEndpointCallback(new InterceptSendToMockEndpointStrategy(mockEndpoints, true));
+ }
+ });
+ }
+ }
+
+ @SuppressWarnings("deprecation")
+ protected void handleLazyLoadTypeConverters(GenericApplicationContext context, Class<?> testClass) throws Exception {
+ final boolean lazy;
+
+ if (testClass.isAnnotationPresent(LazyLoadTypeConverters.class)) {
+ lazy = testClass.getAnnotation(LazyLoadTypeConverters.class).value();
+ } else {
+ lazy = true;
+ }
+
+ if (lazy) {
+ CamelSpringTestHelper.doToSpringCamelContexts(context, new DoToSpringCamelContextsStrategy() {
+
+ @Override
+ public void execute(String contextName, SpringCamelContext camelContext)
+ throws Exception {
+ LOG.info("Enabling lazy loading of type converters on CamelContext with name [{}].", contextName);
+ camelContext.setLazyLoadTypeConverters(lazy);
+ }
+ });
+ }
+ }
+
+ /**
+ * Handles starting of Camel contexts based on {@link UseAdviceWith} and other state in the JVM.
+ *
+ * @param context the initialized Spring context
+ * @param testClass the test class being executed
+ */
+ protected void handleCamelContextStartup(GenericApplicationContext context, Class<?> testClass) throws Exception {
+ boolean skip = "true".equalsIgnoreCase(System.getProperty("skipStartingCamelContext"));
+ if (skip) {
+ LOG.info("Skipping starting CamelContext(s) as system property skipStartingCamelContext is set to be true.");
+ } else if (testClass.isAnnotationPresent(UseAdviceWith.class)) {
+ if (testClass.getAnnotation(UseAdviceWith.class).value()) {
+ LOG.info("Skipping starting CamelContext(s) as UseAdviceWith annotation was found and isUseAdviceWith is set to true.");
+ skip = true;
+ } else {
+ LOG.info("Starting CamelContext(s) as UseAdviceWith annotation was found, but isUseAdviceWith is set to false.");
+ skip = false;
+ }
+ }
+
+ if (!skip) {
+ CamelSpringTestHelper.doToSpringCamelContexts(context, new DoToSpringCamelContextsStrategy() {
+
+ @Override
+ public void execute(String contextName,
+ SpringCamelContext camelContext) throws Exception {
+ LOG.info("Starting CamelContext with name [{}].", contextName);
+ camelContext.start();
+ }
+ });
+ }
+ }
+
+ /**
+ * Returns the class under test in order to enable inspection of annotations while the
+ * Spring context is being created.
+ *
+ * @return the test class that is being executed
+ * @see CamelSpringTestHelper
+ */
+ protected Class<?> getTestClass() {
+ return CamelSpringTestHelper.getTestClass();
+ }
+}
http://git-wip-us.apache.org/repos/asf/camel/blob/7764c326/components/camel-test-spring40/src/main/java/org/apache/camel/test/spring/CamelSpringTestContextLoaderTestExecutionListener.java
----------------------------------------------------------------------
diff --git a/components/camel-test-spring40/src/main/java/org/apache/camel/test/spring/CamelSpringTestContextLoaderTestExecutionListener.java b/components/camel-test-spring40/src/main/java/org/apache/camel/test/spring/CamelSpringTestContextLoaderTestExecutionListener.java
new file mode 100644
index 0000000..c96943d
--- /dev/null
+++ b/components/camel-test-spring40/src/main/java/org/apache/camel/test/spring/CamelSpringTestContextLoaderTestExecutionListener.java
@@ -0,0 +1,36 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.test.spring;
+
+import org.springframework.test.context.TestContext;
+import org.springframework.test.context.support.AbstractTestExecutionListener;
+
+/**
+ * Helper for {@link CamelSpringTestContextLoader} that sets the test class state
+ * in {@link CamelSpringTestHelper} almost immediately before the loader initializes
+ * the Spring context.
+ * <p/>
+ * Implemented as a listener as the state can be set on a {@code ThreadLocal} and we are pretty sure
+ * that the same thread will be used to initialize the Spring context.
+ */
+public class CamelSpringTestContextLoaderTestExecutionListener extends AbstractTestExecutionListener {
+
+ @Override
+ public void prepareTestInstance(TestContext testContext) throws Exception {
+ CamelSpringTestHelper.setTestClass(testContext.getTestClass());
+ }
+}
http://git-wip-us.apache.org/repos/asf/camel/blob/7764c326/components/camel-test-spring40/src/main/java/org/apache/camel/test/spring/CamelSpringTestHelper.java
----------------------------------------------------------------------
diff --git a/components/camel-test-spring40/src/main/java/org/apache/camel/test/spring/CamelSpringTestHelper.java b/components/camel-test-spring40/src/main/java/org/apache/camel/test/spring/CamelSpringTestHelper.java
new file mode 100644
index 0000000..aa75c0e
--- /dev/null
+++ b/components/camel-test-spring40/src/main/java/org/apache/camel/test/spring/CamelSpringTestHelper.java
@@ -0,0 +1,99 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.test.spring;
+
+import java.lang.reflect.Method;
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.LinkedHashSet;
+import java.util.Map;
+import java.util.Map.Entry;
+import java.util.Set;
+
+import org.apache.camel.spring.SpringCamelContext;
+
+import org.springframework.context.ApplicationContext;
+
+/**
+ * Helper that provides state information across the levels of Spring Test that do not expose the
+ * necessary context/state for integration of Camel testing features into Spring test. Also
+ * provides utility methods.
+ * <p/>
+ * Note that this class makes use of {@link ThreadLocal}s to maintain some state. It is imperative
+ * that the state setters and getters are accessed within the scope of a single thread in order
+ * for this class to work right.
+ */
+public final class CamelSpringTestHelper {
+
+ private static ThreadLocal<String> originalJmxDisabledValue = new ThreadLocal<String>();
+ private static ThreadLocal<Class<?>> testClazz = new ThreadLocal<Class<?>>();
+
+ private CamelSpringTestHelper() {
+ }
+
+ public static String getOriginalJmxDisabled() {
+ return originalJmxDisabledValue.get();
+ }
+
+ public static void setOriginalJmxDisabledValue(String originalValue) {
+ originalJmxDisabledValue.set(originalValue);
+ }
+
+ public static Class<?> getTestClass() {
+ return testClazz.get();
+ }
+
+ public static void setTestClass(Class<?> testClass) {
+ testClazz.set(testClass);
+ }
+
+ /**
+ * Returns all methods defined in {@code clazz} and its superclasses/interfaces.
+ */
+ public static Collection<Method> getAllMethods(Class<?> clazz) {
+ Set<Method> methods = new LinkedHashSet<Method>();
+ Class<?> currentClass = clazz;
+
+ while (currentClass != null) {
+ methods.addAll(Arrays.asList(clazz.getMethods()));
+ currentClass = currentClass.getSuperclass();
+ }
+
+ return methods;
+ }
+
+ /**
+ * Executes {@code strategy} against all {@link SpringCamelContext}s found in the Spring context.
+ * This method reduces the amount of repeated find and loop code throughout this class.
+ *
+ * @param context the Spring context to search
+ * @param strategy the strategy to execute against the found {@link SpringCamelContext}s
+ *
+ * @throws Exception if there is an error executing any of the strategies
+ */
+ public static void doToSpringCamelContexts(ApplicationContext context, DoToSpringCamelContextsStrategy strategy) throws Exception {
+ Map<String, SpringCamelContext> contexts = context.getBeansOfType(SpringCamelContext.class);
+
+ for (Entry<String, SpringCamelContext> entry : contexts.entrySet()) {
+ strategy.execute(entry.getKey(), entry.getValue());
+ }
+ }
+
+ public interface DoToSpringCamelContextsStrategy {
+ void execute(String contextName, SpringCamelContext camelContext) throws Exception;
+ }
+}
http://git-wip-us.apache.org/repos/asf/camel/blob/7764c326/components/camel-test-spring40/src/main/java/org/apache/camel/test/spring/CamelSpringTestSupport.java
----------------------------------------------------------------------
diff --git a/components/camel-test-spring40/src/main/java/org/apache/camel/test/spring/CamelSpringTestSupport.java b/components/camel-test-spring40/src/main/java/org/apache/camel/test/spring/CamelSpringTestSupport.java
new file mode 100644
index 0000000..b855b40
--- /dev/null
+++ b/components/camel-test-spring40/src/main/java/org/apache/camel/test/spring/CamelSpringTestSupport.java
@@ -0,0 +1,220 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.test.spring;
+
+import java.util.Arrays;
+import java.util.HashSet;
+import java.util.List;
+
+import org.apache.camel.CamelContext;
+import org.apache.camel.spring.CamelBeanPostProcessor;
+import org.apache.camel.spring.SpringCamelContext;
+import org.apache.camel.test.ExcludingPackageScanClassResolver;
+import org.apache.camel.test.junit4.CamelTestSupport;
+import org.apache.camel.util.IOHelper;
+import org.apache.camel.util.ObjectHelper;
+import org.junit.After;
+import org.junit.AfterClass;
+import org.springframework.beans.factory.support.RootBeanDefinition;
+import org.springframework.context.ApplicationContext;
+import org.springframework.context.support.AbstractApplicationContext;
+import org.springframework.context.support.GenericApplicationContext;
+
+/**
+ * @version
+ */
+public abstract class CamelSpringTestSupport extends CamelTestSupport {
+ protected static ThreadLocal<AbstractApplicationContext> threadAppContext = new ThreadLocal<AbstractApplicationContext>();
+ protected static Object lock = new Object();
+
+ protected AbstractApplicationContext applicationContext;
+ protected abstract AbstractApplicationContext createApplicationContext();
+
+ /**
+ * Lets post process this test instance to process any Camel annotations.
+ * Note that using Spring Test or Guice is a more powerful approach.
+ */
+ @Override
+ public void postProcessTest() throws Exception {
+ super.postProcessTest();
+ if (isCreateCamelContextPerClass()) {
+ applicationContext = threadAppContext.get();
+ }
+
+ // use the bean post processor from camel-spring
+ CamelBeanPostProcessor processor = new CamelBeanPostProcessor();
+ processor.setApplicationContext(applicationContext);
+ processor.setCamelContext(context);
+ processor.postProcessBeforeInitialization(this, getClass().getName());
+ processor.postProcessAfterInitialization(this, getClass().getName());
+ }
+
+ @Override
+ public void doPreSetup() throws Exception {
+ if (!"true".equalsIgnoreCase(System.getProperty("skipStartingCamelContext"))) {
+ // tell camel-spring it should not trigger starting CamelContext, since we do that later
+ // after we are finished setting up the unit test
+ synchronized (lock) {
+ SpringCamelContext.setNoStart(true);
+ if (isCreateCamelContextPerClass()) {
+ applicationContext = threadAppContext.get();
+ if (applicationContext == null) {
+ applicationContext = doCreateApplicationContext();
+ threadAppContext.set(applicationContext);
+ }
+ } else {
+ applicationContext = doCreateApplicationContext();
+ }
+ SpringCamelContext.setNoStart(false);
+ }
+ } else {
+ log.info("Skipping starting CamelContext as system property skipStartingCamelContext is set to be true.");
+ }
+ }
+
+ private AbstractApplicationContext doCreateApplicationContext() {
+ AbstractApplicationContext context = createApplicationContext();
+ assertNotNull("Should have created a valid Spring application context", context);
+
+ String[] profiles = activeProfiles();
+ if (profiles != null && profiles.length > 0) {
+ // the context must not be active
+ if (context.isActive()) {
+ throw new IllegalStateException("Cannot active profiles: " + Arrays.asList(profiles) + " on active Spring application context: " + context
+ + ". The code in your createApplicationContext() method should be adjusted to create the application context with refresh = false as parameter");
+ }
+ log.info("Spring activating profiles: {}", Arrays.asList(profiles));
+ context.getEnvironment().setActiveProfiles(profiles);
+ }
+
+ // ensure the context has been refreshed at least once
+ if (!context.isActive()) {
+ context.refresh();
+ }
+
+ return context;
+ }
+
+ @Override
+ @After
+ public void tearDown() throws Exception {
+ super.tearDown();
+
+ if (!isCreateCamelContextPerClass()) {
+ IOHelper.close(applicationContext);
+ applicationContext = null;
+ }
+ }
+
+ @AfterClass
+ public static void tearSpringDownAfterClass() throws Exception {
+ if (threadAppContext.get() != null) {
+ IOHelper.close(threadAppContext.get());
+ threadAppContext.remove();
+ }
+ }
+
+ /**
+ * Create a parent context that initializes a
+ * {@link org.apache.camel.spi.PackageScanClassResolver} to exclude a set of given classes from
+ * being resolved. Typically this is used at test time to exclude certain routes,
+ * which might otherwise be just noisy, from being discovered and initialized.
+ * <p/>
+ * To use this filtering mechanism it is necessary to provide the
+ * {@link org.springframework.context.ApplicationContext} returned from here as the parent context to
+ * your test context e.g.
+ *
+ * <pre>
+ * protected AbstractXmlApplicationContext createApplicationContext() {
+ * return new ClassPathXmlApplicationContext(new String[] {"test-context.xml"}, getRouteExcludingApplicationContext());
+ * }
+ * </pre>
+ *
+ * This will, in turn, call the template methods <code>excludedRoutes</code>
+ * and <code>excludedRoute</code> to determine the classes to be excluded from scanning.
+ *
+ * @return ApplicationContext a parent {@link org.springframework.context.ApplicationContext} configured
+ * to exclude certain classes from package scanning
+ */
+ protected ApplicationContext getRouteExcludingApplicationContext() {
+ GenericApplicationContext routeExcludingContext = new GenericApplicationContext();
+ routeExcludingContext.registerBeanDefinition("excludingResolver", new RootBeanDefinition(ExcludingPackageScanClassResolver.class));
+ routeExcludingContext.refresh();
+
+ ExcludingPackageScanClassResolver excludingResolver = routeExcludingContext.getBean("excludingResolver", ExcludingPackageScanClassResolver.class);
+ List<Class<?>> excluded = Arrays.asList(excludeRoutes());
+ excludingResolver.setExcludedClasses(new HashSet<Class<?>>(excluded));
+
+ return routeExcludingContext;
+ }
+
+ /**
+ * Template method used to exclude {@link org.apache.camel.Route} from the test time context
+ * route scanning
+ *
+ * @return Class[] the classes to be excluded from test time context route scanning
+ */
+ protected Class<?>[] excludeRoutes() {
+ Class<?> excludedRoute = excludeRoute();
+ return excludedRoute != null ? new Class[] {excludedRoute} : new Class[0];
+ }
+
+ /**
+ * Template method used to exclude a {@link org.apache.camel.Route} from the test camel context
+ */
+ protected Class<?> excludeRoute() {
+ return null;
+ }
+
+ /**
+ * Looks up the mandatory spring bean of the given name and type, failing if
+ * it is not present or the correct type
+ */
+ public <T> T getMandatoryBean(Class<T> type, String name) {
+ Object value = applicationContext.getBean(name);
+ assertNotNull("No spring bean found for name <" + name + ">", value);
+ if (type.isInstance(value)) {
+ return type.cast(value);
+ } else {
+ fail("Spring bean <" + name + "> is not an instanceof " + type.getName() + " but is of type " + ObjectHelper.className(value));
+ return null;
+ }
+ }
+
+ /**
+ * Which active profiles should be used.
+ * <p/>
+ * <b>Important:</b> When using active profiles, then the code in {@link #createApplicationContext()} should create
+ * the Spring {@link org.springframework.context.support.AbstractApplicationContext} without refreshing. For example creating an
+ * {@link org.springframework.context.support.ClassPathXmlApplicationContext} you would need to pass in
+ * <tt>false</tt> in the refresh parameter, in the constructor.
+ * Camel will thrown an {@link IllegalStateException} if this is not correct stating this problem.
+ * The reason is that we cannot active profiles <b>after</b> a Spring application context has already
+ * been refreshed, and is active.
+ *
+ * @return an array of active profiles to use, use <tt>null</tt> to not use any active profiles.
+ */
+ protected String[] activeProfiles() {
+ return null;
+ }
+
+ @Override
+ protected CamelContext createCamelContext() throws Exception {
+ // don't start the springCamelContext if we
+ return SpringCamelContext.springCamelContext(applicationContext, false);
+ }
+}
http://git-wip-us.apache.org/repos/asf/camel/blob/7764c326/components/camel-test-spring40/src/main/java/org/apache/camel/test/spring/DisableJmx.java
----------------------------------------------------------------------
diff --git a/components/camel-test-spring40/src/main/java/org/apache/camel/test/spring/DisableJmx.java b/components/camel-test-spring40/src/main/java/org/apache/camel/test/spring/DisableJmx.java
new file mode 100644
index 0000000..1dd0ab1
--- /dev/null
+++ b/components/camel-test-spring40/src/main/java/org/apache/camel/test/spring/DisableJmx.java
@@ -0,0 +1,43 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.test.spring;
+
+import java.lang.annotation.Documented;
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Inherited;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+/**
+ * Indicates if JMX should be globally disabled in the {@code CamelContext}s that are bootstrapped
+ * during the test through the use of Spring Test loaded application contexts. Note that the
+ * presence of this annotation will result in the manipulation of System Properties that
+ * will affect Camel contexts constructed outside of the Spring Test loaded application contexts.
+ */
+@Documented
+@Inherited
+@Retention(RetentionPolicy.RUNTIME)
+@Target({ElementType.TYPE})
+public @interface DisableJmx {
+
+ /**
+ * Whether the test annotated with this annotation should be run with JMX disabled in Camel.
+ * Defaults to {@code true}.
+ */
+ boolean value() default true;
+}