Repository: camel Updated Branches: refs/heads/master 3909b0a82 -> 3b5c6aeef
CAMEL-8981: camel-test-spring - Allow to use UseOverridePropertiesWithPropertiesComponent Project: http://git-wip-us.apache.org/repos/asf/camel/repo Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/3b5c6aee Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/3b5c6aee Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/3b5c6aee Branch: refs/heads/master Commit: 3b5c6aeeff1254d9e11f8e669041dd693496cfcb Parents: 3909b0a Author: Claus Ibsen <davscl...@apache.org> Authored: Sun Jul 19 11:27:26 2015 +0200 Committer: Claus Ibsen <davscl...@apache.org> Committed: Sun Jul 19 11:27:26 2015 +0200 ---------------------------------------------------------------------- components/camel-test-spring/pom.xml | 6 +- .../CamelSpringDelegatingTestContextLoader.java | 63 +++++++++++++++- .../spring/CamelSpringTestContextLoader.java | 59 ++++++++++++++- ...erridePropertiesWithPropertiesComponent.java | 36 +++++++++ .../CamelSpringOverridePropertiesTest.java | 77 ++++++++++++++++++++ .../src/test/resources/log4j.properties | 5 +- ...amelSpringOverridePropertiesTest-context.xml | 40 ++++++++++ 7 files changed, 277 insertions(+), 9 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/3b5c6aee/components/camel-test-spring/pom.xml ---------------------------------------------------------------------- diff --git a/components/camel-test-spring/pom.xml b/components/camel-test-spring/pom.xml index d9017f8..65a7ab7 100644 --- a/components/camel-test-spring/pom.xml +++ b/components/camel-test-spring/pom.xml @@ -82,13 +82,17 @@ <version>${spring4-version}</version> </dependency> - <!-- test dependencies --> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <scope>test</scope> </dependency> + <dependency> + <groupId>log4j</groupId> + <artifactId>log4j</artifactId> + <scope>test</scope> + </dependency> </dependencies> </project> http://git-wip-us.apache.org/repos/asf/camel/blob/3b5c6aee/components/camel-test-spring/src/main/java/org/apache/camel/test/spring/CamelSpringDelegatingTestContextLoader.java ---------------------------------------------------------------------- diff --git a/components/camel-test-spring/src/main/java/org/apache/camel/test/spring/CamelSpringDelegatingTestContextLoader.java b/components/camel-test-spring/src/main/java/org/apache/camel/test/spring/CamelSpringDelegatingTestContextLoader.java index cea1656..c7511ff 100644 --- a/components/camel-test-spring/src/main/java/org/apache/camel/test/spring/CamelSpringDelegatingTestContextLoader.java +++ b/components/camel-test-spring/src/main/java/org/apache/camel/test/spring/CamelSpringDelegatingTestContextLoader.java @@ -21,8 +21,10 @@ import java.lang.reflect.Modifier; import java.util.Collection; import java.util.LinkedList; import java.util.List; +import java.util.Properties; import java.util.concurrent.TimeUnit; +import org.apache.camel.component.properties.PropertiesComponent; import org.apache.camel.impl.DefaultDebugger; import org.apache.camel.impl.InterceptSendToMockEndpointStrategy; import org.apache.camel.management.JmxSystemPropertyKeys; @@ -42,8 +44,6 @@ 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> @@ -90,12 +90,15 @@ public class CamelSpringDelegatingTestContextLoader extends DelegatingSmartConte throws Exception { AnnotationConfigUtils.registerAnnotationConfigProcessors((BeanDefinitionRegistry) context); - + + logger.info(">>>> I was here <<<<<"); + // Post CamelContext(s) instantiation but pre CamelContext(s) start setup handleProvidesBreakpoint(context, testClass); handleShutdownTimeout(context, testClass); handleMockEndpoints(context, testClass); handleMockEndpointsAndSkip(context, testClass); + handleUseOverridePropertiesWithPropertiesComponent(context, testClass); // CamelContext(s) startup handleCamelContextStartup(context, testClass); @@ -277,7 +280,59 @@ public class CamelSpringDelegatingTestContextLoader extends DelegatingSmartConte } } - + /** + * Handles override this method to include and override properties with the Camel {@link org.apache.camel.component.properties.PropertiesComponent}. + * + * @param context the initialized Spring context + * @param testClass the test class being executed + */ + protected void handleUseOverridePropertiesWithPropertiesComponent(ConfigurableApplicationContext context, Class<?> testClass) throws Exception { + Collection<Method> methods = getAllMethods(testClass); + final List<Properties> properties = new LinkedList<Properties>(); + + for (Method method : methods) { + if (AnnotationUtils.findAnnotation(method, UseOverridePropertiesWithPropertiesComponent.class) != null) { + Class<?>[] argTypes = method.getParameterTypes(); + if (argTypes.length > 0) { + throw new IllegalArgumentException("Method [" + method.getName() + + "] is annotated with UseOverridePropertiesWithPropertiesComponent but is not a no-argument method."); + } else if (!Properties.class.isAssignableFrom(method.getReturnType())) { + throw new IllegalArgumentException("Method [" + method.getName() + + "] is annotated with UseOverridePropertiesWithPropertiesComponent but does not return a java.util.Properties."); + } else if (!Modifier.isStatic(method.getModifiers())) { + throw new IllegalArgumentException("Method [" + method.getName() + + "] is annotated with UseOverridePropertiesWithPropertiesComponent but is not static."); + } else if (!Modifier.isPublic(method.getModifiers())) { + throw new IllegalArgumentException("Method [" + method.getName() + + "] is annotated with UseOverridePropertiesWithPropertiesComponent but is not public."); + } + + try { + properties.add((Properties) method.invoke(null)); + } catch (Exception e) { + throw new RuntimeException("Method [" + method.getName() + + "] threw exception during evaluation.", e); + } + } + } + + if (properties.size() != 0) { + CamelSpringTestHelper.doToSpringCamelContexts(context, new DoToSpringCamelContextsStrategy() { + public void execute(String contextName, SpringCamelContext camelContext) throws Exception { + PropertiesComponent pc = camelContext.getComponent("properties", PropertiesComponent.class); + Properties extra = new Properties(); + for (Properties prop : properties) { + extra.putAll(prop); + } + if (!extra.isEmpty()) { + logger.info("Using {} properties to override any existing properties on the PropertiesComponent on CamelContext with name [{}].", extra.size(), contextName); + pc.setOverrideProperties(extra); + } + } + }); + } + } + /** * Handles starting of Camel contexts based on {@link UseAdviceWith} and other state in the JVM. * http://git-wip-us.apache.org/repos/asf/camel/blob/3b5c6aee/components/camel-test-spring/src/main/java/org/apache/camel/test/spring/CamelSpringTestContextLoader.java ---------------------------------------------------------------------- diff --git a/components/camel-test-spring/src/main/java/org/apache/camel/test/spring/CamelSpringTestContextLoader.java b/components/camel-test-spring/src/main/java/org/apache/camel/test/spring/CamelSpringTestContextLoader.java index ad294e2..4fdde29 100644 --- a/components/camel-test-spring/src/main/java/org/apache/camel/test/spring/CamelSpringTestContextLoader.java +++ b/components/camel-test-spring/src/main/java/org/apache/camel/test/spring/CamelSpringTestContextLoader.java @@ -23,8 +23,10 @@ import java.util.Collection; import java.util.HashSet; import java.util.LinkedList; import java.util.List; +import java.util.Properties; import java.util.concurrent.TimeUnit; +import org.apache.camel.component.properties.PropertiesComponent; import org.apache.camel.impl.DefaultDebugger; import org.apache.camel.impl.InterceptSendToMockEndpointStrategy; import org.apache.camel.management.JmxSystemPropertyKeys; @@ -38,6 +40,7 @@ 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.ConfigurableApplicationContext; import org.springframework.context.annotation.AnnotationConfigUtils; import org.springframework.context.support.GenericApplicationContext; import org.springframework.core.annotation.AnnotationUtils; @@ -154,6 +157,7 @@ public class CamelSpringTestContextLoader extends AbstractContextLoader { handleShutdownTimeout(context, testClass); handleMockEndpoints(context, testClass); handleMockEndpointsAndSkip(context, testClass); + handleUseOverridePropertiesWithPropertiesComponent(context, testClass); handleLazyLoadTypeConverters(context, testClass); // CamelContext(s) startup @@ -427,7 +431,60 @@ public class CamelSpringTestContextLoader extends AbstractContextLoader { }); } } - + + /** + * Handles override this method to include and override properties with the Camel {@link org.apache.camel.component.properties.PropertiesComponent}. + * + * @param context the initialized Spring context + * @param testClass the test class being executed + */ + protected void handleUseOverridePropertiesWithPropertiesComponent(ConfigurableApplicationContext context, Class<?> testClass) throws Exception { + Collection<Method> methods = getAllMethods(testClass); + final List<Properties> properties = new LinkedList<Properties>(); + + for (Method method : methods) { + if (AnnotationUtils.findAnnotation(method, UseOverridePropertiesWithPropertiesComponent.class) != null) { + Class<?>[] argTypes = method.getParameterTypes(); + if (argTypes.length > 0) { + throw new IllegalArgumentException("Method [" + method.getName() + + "] is annotated with UseOverridePropertiesWithPropertiesComponent but is not a no-argument method."); + } else if (!Properties.class.isAssignableFrom(method.getReturnType())) { + throw new IllegalArgumentException("Method [" + method.getName() + + "] is annotated with UseOverridePropertiesWithPropertiesComponent but does not return a java.util.Properties."); + } else if (!Modifier.isStatic(method.getModifiers())) { + throw new IllegalArgumentException("Method [" + method.getName() + + "] is annotated with UseOverridePropertiesWithPropertiesComponent but is not static."); + } else if (!Modifier.isPublic(method.getModifiers())) { + throw new IllegalArgumentException("Method [" + method.getName() + + "] is annotated with UseOverridePropertiesWithPropertiesComponent but is not public."); + } + + try { + properties.add((Properties) method.invoke(null)); + } catch (Exception e) { + throw new RuntimeException("Method [" + method.getName() + + "] threw exception during evaluation.", e); + } + } + } + + if (properties.size() != 0) { + CamelSpringTestHelper.doToSpringCamelContexts(context, new DoToSpringCamelContextsStrategy() { + public void execute(String contextName, SpringCamelContext camelContext) throws Exception { + PropertiesComponent pc = camelContext.getComponent("properties", PropertiesComponent.class); + Properties extra = new Properties(); + for (Properties prop : properties) { + extra.putAll(prop); + } + if (!extra.isEmpty()) { + LOG.info("Using {} properties to override any existing properties on the PropertiesComponent on CamelContext with name [{}].", extra.size(), contextName); + pc.setOverrideProperties(extra); + } + } + }); + } + } + /** * Handles starting of Camel contexts based on {@link UseAdviceWith} and other state in the JVM. * http://git-wip-us.apache.org/repos/asf/camel/blob/3b5c6aee/components/camel-test-spring/src/main/java/org/apache/camel/test/spring/UseOverridePropertiesWithPropertiesComponent.java ---------------------------------------------------------------------- diff --git a/components/camel-test-spring/src/main/java/org/apache/camel/test/spring/UseOverridePropertiesWithPropertiesComponent.java b/components/camel-test-spring/src/main/java/org/apache/camel/test/spring/UseOverridePropertiesWithPropertiesComponent.java new file mode 100644 index 0000000..1e17d9b --- /dev/null +++ b/components/camel-test-spring/src/main/java/org/apache/camel/test/spring/UseOverridePropertiesWithPropertiesComponent.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 java.lang.annotation.Documented; +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +import org.junit.Test; + +/** + * Indicates that the annotated method returns a {@link java.util.Properties} for use in the test, and that + * those properties override any existing properties configured on the {@link org.apache.camel.component.properties.PropertiesComponent}. + */ +@Documented +@Retention(RetentionPolicy.RUNTIME) +@Target({ElementType.METHOD}) +public @interface UseOverridePropertiesWithPropertiesComponent { + +} http://git-wip-us.apache.org/repos/asf/camel/blob/3b5c6aee/components/camel-test-spring/src/test/java/org/apache/camel/test/spring/CamelSpringOverridePropertiesTest.java ---------------------------------------------------------------------- diff --git a/components/camel-test-spring/src/test/java/org/apache/camel/test/spring/CamelSpringOverridePropertiesTest.java b/components/camel-test-spring/src/test/java/org/apache/camel/test/spring/CamelSpringOverridePropertiesTest.java new file mode 100644 index 0000000..02a9fb8 --- /dev/null +++ b/components/camel-test-spring/src/test/java/org/apache/camel/test/spring/CamelSpringOverridePropertiesTest.java @@ -0,0 +1,77 @@ +/** + * 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.Properties; + +import org.apache.camel.CamelContext; +import org.apache.camel.EndpointInject; +import org.apache.camel.Produce; +import org.apache.camel.ProducerTemplate; +import org.apache.camel.component.mock.MockEndpoint; +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.context.BootstrapWith; +import org.springframework.test.context.ContextConfiguration; + +@RunWith(CamelSpringJUnit4ClassRunner.class) +@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 = DirtiesContext.ClassMode.AFTER_EACH_TEST_METHOD) +public class CamelSpringOverridePropertiesTest { + + @Autowired + private CamelContext camelContext; + + @Produce(uri = "direct:start") + private ProducerTemplate start; + + @EndpointInject(uri = "mock:a") + private MockEndpoint mockA; + + @EndpointInject(uri = "mock:test") + private MockEndpoint mockTest; + + @EndpointInject(uri = "mock:foo") + private MockEndpoint mockFoo; + + @UseOverridePropertiesWithPropertiesComponent + public static Properties override() { + Properties answer = new Properties(); + answer.put("cool.end", "mock:foo"); + return answer; + } + + @Test + public void testOverride() throws Exception { + mockA.expectedBodiesReceived("Camel"); + mockTest.expectedMessageCount(0); + mockFoo.expectedBodiesReceived("Hello Camel"); + + start.sendBody("Camel"); + + mockA.assertIsSatisfied(); + mockTest.assertIsSatisfied(); + mockFoo.assertIsSatisfied(); + } + +} http://git-wip-us.apache.org/repos/asf/camel/blob/3b5c6aee/components/camel-test-spring/src/test/resources/log4j.properties ---------------------------------------------------------------------- diff --git a/components/camel-test-spring/src/test/resources/log4j.properties b/components/camel-test-spring/src/test/resources/log4j.properties index 1eb13a5..0ac23ae 100644 --- a/components/camel-test-spring/src/test/resources/log4j.properties +++ b/components/camel-test-spring/src/test/resources/log4j.properties @@ -20,8 +20,7 @@ # log4j.rootLogger=INFO, file -log4j.logger.org.springframework=WARN -#log4j.logger.org.apache.camel=DEBUG +log4j.logger.org.apache.camel.test.spring=DEBUG #log4j.logger.org.apache.camel.test.junit4=DEBUG # CONSOLE appender not used by default @@ -34,4 +33,4 @@ log4j.appender.out.layout.ConversionPattern=[%30.30t] %-30.30c{1} %-5p %m%n log4j.appender.file=org.apache.log4j.FileAppender log4j.appender.file.layout=org.apache.log4j.PatternLayout log4j.appender.file.layout.ConversionPattern=%d %-5p %c{1} - %m %n -log4j.appender.file.file=target/camel-spring-test.log +log4j.appender.file.file=target/camel-test-spring.log http://git-wip-us.apache.org/repos/asf/camel/blob/3b5c6aee/components/camel-test-spring/src/test/resources/org/apache/camel/test/spring/CamelSpringOverridePropertiesTest-context.xml ---------------------------------------------------------------------- diff --git a/components/camel-test-spring/src/test/resources/org/apache/camel/test/spring/CamelSpringOverridePropertiesTest-context.xml b/components/camel-test-spring/src/test/resources/org/apache/camel/test/spring/CamelSpringOverridePropertiesTest-context.xml new file mode 100644 index 0000000..bc005f7 --- /dev/null +++ b/components/camel-test-spring/src/test/resources/org/apache/camel/test/spring/CamelSpringOverridePropertiesTest-context.xml @@ -0,0 +1,40 @@ +<?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. +--> +<beans xmlns="http://www.springframework.org/schema/beans" + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation=" + http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd + http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd "> + + <bean id="bridgePropertyPlaceholder" class="org.apache.camel.spring.spi.BridgePropertyPlaceholderConfigurer"> + <property name="location" value="classpath:org/apache/camel/test/spring/test.properties"/> + </bean> + + <camelContext id="camelContext" xmlns="http://camel.apache.org/schema/spring"> + <route> + <from uri="direct:start"/> + <to uri="mock:a"/> + <transform> + <simple>Hello ${body}</simple> + </transform> + <to uri="{{cool.end}}"/> + </route> + </camelContext> + + +</beans> \ No newline at end of file