You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by ac...@apache.org on 2019/05/19 14:08:33 UTC

[camel] branch camel-2.24.x updated (e6ab8ea -> d5e6053)

This is an automated email from the ASF dual-hosted git repository.

acosentino pushed a change to branch camel-2.24.x
in repository https://gitbox.apache.org/repos/asf/camel.git.


    from e6ab8ea  Add flattern pom to git ignore
     new 6a230f2  CAMEL-13543: Make CamelSpringTestContextLoader set property overrides before the CamelContext is injected into any other bean
     new d5e6053  CAMEL-13543 - Fixed CS

The 2 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


Summary of changes:
 .../org/apache/camel/util/CamelContextHelper.java  |  2 +-
 .../test/spring/CamelSpringTestContextLoader.java  | 40 +++++++++++++---------
 ...ngOverridePropertiesForPropertyInjectTest.java} | 37 ++++++--------------
 .../spring/TestPropertyInjectRouteBuilder.java}    | 14 +++++---
 ...idePropertiesForPropertyInjectTest-context.xml} | 12 ++-----
 .../org/apache/camel/test/spring/test.properties   |  1 +
 6 files changed, 49 insertions(+), 57 deletions(-)
 copy components/camel-test-spring/src/test/java/org/apache/camel/test/spring/{CamelSpringOverridePropertiesTest.java => CamelSpringOverridePropertiesForPropertyInjectTest.java} (62%)
 copy components/{camel-spring-boot/src/test/java/org/apache/camel/spring/boot/actuate/health/MyCamelRoute.java => camel-test-spring/src/test/java/org/apache/camel/test/spring/TestPropertyInjectRouteBuilder.java} (76%)
 copy components/camel-test-spring/src/test/resources/org/apache/camel/test/spring/{CamelSpringOverridePropertiesTest-context.xml => CamelSpringOverridePropertiesForPropertyInjectTest-context.xml} (88%)


[camel] 01/02: CAMEL-13543: Make CamelSpringTestContextLoader set property overrides before the CamelContext is injected into any other bean

Posted by ac...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

acosentino pushed a commit to branch camel-2.24.x
in repository https://gitbox.apache.org/repos/asf/camel.git

commit 6a230f24449238c64970c9b42c7066a251126389
Author: Stig Rohde Døssing <st...@gmail.com>
AuthorDate: Fri May 17 15:31:21 2019 +0200

    CAMEL-13543: Make CamelSpringTestContextLoader set property overrides before the CamelContext is injected into any other bean
---
 .../org/apache/camel/util/CamelContextHelper.java  |  2 +-
 .../test/spring/CamelSpringTestContextLoader.java  | 41 ++++++++++------
 ...ingOverridePropertiesForPropertyInjectTest.java | 57 ++++++++++++++++++++++
 .../spring/TestPropertyInjectRouteBuilder.java     | 33 +++++++++++++
 ...ridePropertiesForPropertyInjectTest-context.xml | 36 ++++++++++++++
 .../org/apache/camel/test/spring/test.properties   |  1 +
 6 files changed, 153 insertions(+), 17 deletions(-)

diff --git a/camel-core/src/main/java/org/apache/camel/util/CamelContextHelper.java b/camel-core/src/main/java/org/apache/camel/util/CamelContextHelper.java
index ade99d4..a64d307 100644
--- a/camel-core/src/main/java/org/apache/camel/util/CamelContextHelper.java
+++ b/camel-core/src/main/java/org/apache/camel/util/CamelContextHelper.java
@@ -702,7 +702,7 @@ public final class CamelContextHelper {
      *                   has been configured.
      * @return the properties component, or <tt>null</tt> if none has been defined, and auto create is <tt>false</tt>.
      */
-    public static Component lookupPropertiesComponent(CamelContext camelContext, boolean autoCreate) {
+    public static PropertiesComponent lookupPropertiesComponent(CamelContext camelContext, boolean autoCreate) {
         // no existing properties component so lookup and add as component if possible
         PropertiesComponent answer = (PropertiesComponent) camelContext.hasComponent("properties");
         if (answer == null) {
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 6ef1026..86bfc45 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
@@ -51,8 +51,13 @@ 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;
 
+import org.apache.camel.CamelContext;
+import org.apache.camel.util.CamelContextHelper;
+import org.springframework.beans.factory.config.BeanPostProcessor;
+
 /**
  * Replacement for the default {@link GenericXmlContextLoader} that provides hooks for
  * processing some class level Camel related test annotations.
@@ -145,8 +150,9 @@ public class CamelSpringTestContextLoader extends AbstractContextLoader {
         AnnotationConfigUtils.registerAnnotationConfigProcessors(context);
         
         // Pre CamelContext(s) instantiation setup
-        handleDisableJmx(context, testClass);
-
+        handleDisableJmx(context, testClass);        
+        handleUseOverridePropertiesWithPropertiesComponent(context, testClass);
+        
         // Temporarily disable CamelContext start while the contexts are instantiated.
         SpringCamelContext.setNoStart(true);
         context.refresh();
@@ -160,7 +166,6 @@ public class CamelSpringTestContextLoader extends AbstractContextLoader {
         handleShutdownTimeout(context, testClass);
         handleMockEndpoints(context, testClass);
         handleMockEndpointsAndSkip(context, testClass);
-        handleUseOverridePropertiesWithPropertiesComponent(context, testClass);
 
         // CamelContext(s) startup
         handleCamelContextStartup(context, testClass);
@@ -435,9 +440,9 @@ public class CamelSpringTestContextLoader extends AbstractContextLoader {
     }
     
     /**
-     * Handles override this method to include and override properties with the Camel {@link org.apache.camel.component.properties.PropertiesComponent}.
+     * Sets property overrides for the Camel {@link org.apache.camel.component.properties.PropertiesComponent}.
      *
-     * @param context the initialized Spring context
+     * @param context the pre-refresh Spring context
      * @param testClass the test class being executed
      */
     protected void handleUseOverridePropertiesWithPropertiesComponent(ConfigurableApplicationContext context, Class<?> testClass) throws Exception {
@@ -469,21 +474,25 @@ public class CamelSpringTestContextLoader extends AbstractContextLoader {
                 }
             }
         }
+        
+        Properties extra = new Properties();
+        for (Properties prop : properties) {
+            extra.putAll(prop);
+        }
 
-        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);
+        if (!extra.isEmpty()) {
+            context.addBeanFactoryPostProcessor(beanFactory -> beanFactory.addBeanPostProcessor(new BeanPostProcessor() {
+                @Override
+                public Object postProcessAfterInitialization(Object bean, String beanName) {
+                    if (bean instanceof CamelContext) {
+                        CamelContext camelContext = (CamelContext) bean;
+                        PropertiesComponent pc = CamelContextHelper.lookupPropertiesComponent(camelContext, true);
+                        LOG.info("Using {} properties to override any existing properties on the PropertiesComponent on CamelContext with name [{}].", extra.size(), camelContext.getName());
                         pc.setOverrideProperties(extra);
                     }
+                    return bean;
                 }
-            });
+            }));
         }
     }
 
diff --git a/components/camel-test-spring/src/test/java/org/apache/camel/test/spring/CamelSpringOverridePropertiesForPropertyInjectTest.java b/components/camel-test-spring/src/test/java/org/apache/camel/test/spring/CamelSpringOverridePropertiesForPropertyInjectTest.java
new file mode 100644
index 0000000..bf79523
--- /dev/null
+++ b/components/camel-test-spring/src/test/java/org/apache/camel/test/spring/CamelSpringOverridePropertiesForPropertyInjectTest.java
@@ -0,0 +1,57 @@
+/**
+ * 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 static org.hamcrest.CoreMatchers.is;
+import static org.junit.Assert.assertThat;
+
+import java.util.Properties;
+
+import org.apache.camel.Produce;
+import org.apache.camel.ProducerTemplate;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.springframework.test.annotation.DirtiesContext;
+import org.springframework.test.context.BootstrapWith;
+import org.springframework.test.context.ContextConfiguration;
+
+@RunWith(CamelSpringRunner.class)
+@BootstrapWith(CamelTestContextBootstrapper.class)
+@ContextConfiguration()
+@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_EACH_TEST_METHOD)
+public class CamelSpringOverridePropertiesForPropertyInjectTest {
+    
+    private static final String EXPECTED_PROPERTY_VALUE = "The value is overriden";
+    
+    @Produce(uri = "direct:start-override-route")
+    private ProducerTemplate start;
+
+    @UseOverridePropertiesWithPropertiesComponent
+    public static Properties override() {
+        Properties answer = new Properties();
+        answer.put("property.to.override", EXPECTED_PROPERTY_VALUE);
+        return answer;
+    }
+
+    @Test
+    public void testOverride() throws Exception {
+        String response = start.requestBody((Object)"ignored", String.class);
+
+        assertThat(response, is(EXPECTED_PROPERTY_VALUE));
+    }
+
+}
diff --git a/components/camel-test-spring/src/test/java/org/apache/camel/test/spring/TestPropertyInjectRouteBuilder.java b/components/camel-test-spring/src/test/java/org/apache/camel/test/spring/TestPropertyInjectRouteBuilder.java
new file mode 100644
index 0000000..b56c94b
--- /dev/null
+++ b/components/camel-test-spring/src/test/java/org/apache/camel/test/spring/TestPropertyInjectRouteBuilder.java
@@ -0,0 +1,33 @@
+/**
+ * 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.apache.camel.PropertyInject;
+import org.apache.camel.builder.RouteBuilder;
+
+public class TestPropertyInjectRouteBuilder extends RouteBuilder {
+
+    @PropertyInject("{{property.to.override}}")
+    private String propertyToOverride;
+    
+    @Override
+    public void configure() throws Exception {
+        from("direct:start-override-route")
+            .transform(constant(propertyToOverride));
+    }
+
+}
diff --git a/components/camel-test-spring/src/test/resources/org/apache/camel/test/spring/CamelSpringOverridePropertiesForPropertyInjectTest-context.xml b/components/camel-test-spring/src/test/resources/org/apache/camel/test/spring/CamelSpringOverridePropertiesForPropertyInjectTest-context.xml
new file mode 100644
index 0000000..a757b69
--- /dev/null
+++ b/components/camel-test-spring/src/test/resources/org/apache/camel/test/spring/CamelSpringOverridePropertiesForPropertyInjectTest-context.xml
@@ -0,0 +1,36 @@
+<?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>
+  
+  <bean id="testRouteBuilder" class="org.apache.camel.test.spring.TestPropertyInjectRouteBuilder"></bean>
+
+  <camelContext id="camelContext" xmlns="http://camel.apache.org/schema/spring">
+      <routeBuilder ref="testRouteBuilder"/>
+  </camelContext>
+
+</beans>
\ No newline at end of file
diff --git a/components/camel-test-spring/src/test/resources/org/apache/camel/test/spring/test.properties b/components/camel-test-spring/src/test/resources/org/apache/camel/test/spring/test.properties
index 2d1d49f..e8a539c 100644
--- a/components/camel-test-spring/src/test/resources/org/apache/camel/test/spring/test.properties
+++ b/components/camel-test-spring/src/test/resources/org/apache/camel/test/spring/test.properties
@@ -17,3 +17,4 @@
 
 cool.end=mock:test
 mock.skip=seda:*
+property.to.override=This value should be overriden


[camel] 02/02: CAMEL-13543 - Fixed CS

Posted by ac...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

acosentino pushed a commit to branch camel-2.24.x
in repository https://gitbox.apache.org/repos/asf/camel.git

commit d5e60533402ef367845cbee47a62553f91bd6167
Author: Andrea Cosentino <an...@gmail.com>
AuthorDate: Sun May 19 16:05:20 2019 +0200

    CAMEL-13543 - Fixed CS
---
 .../org/apache/camel/test/spring/CamelSpringTestContextLoader.java | 7 +++----
 .../spring/CamelSpringOverridePropertiesForPropertyInjectTest.java | 6 +++---
 2 files changed, 6 insertions(+), 7 deletions(-)

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 86bfc45..622c401 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
@@ -26,6 +26,7 @@ import java.util.List;
 import java.util.Properties;
 import java.util.concurrent.TimeUnit;
 
+import org.apache.camel.CamelContext;
 import org.apache.camel.component.properties.PropertiesComponent;
 import org.apache.camel.impl.DefaultDebugger;
 import org.apache.camel.impl.InterceptSendToMockEndpointStrategy;
@@ -37,8 +38,10 @@ import org.apache.camel.spring.SpringCamelContext;
 import org.apache.camel.test.ExcludingPackageScanClassResolver;
 import org.apache.camel.test.junit4.CamelTestSupport;
 import org.apache.camel.test.spring.CamelSpringTestHelper.DoToSpringCamelContextsStrategy;
+import org.apache.camel.util.CamelContextHelper;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
+import org.springframework.beans.factory.config.BeanPostProcessor;
 import org.springframework.beans.factory.support.RootBeanDefinition;
 import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
 import org.springframework.context.ApplicationContext;
@@ -54,10 +57,6 @@ import org.springframework.util.StringUtils;
 
 import static org.apache.camel.test.spring.CamelSpringTestHelper.getAllMethods;
 
-import org.apache.camel.CamelContext;
-import org.apache.camel.util.CamelContextHelper;
-import org.springframework.beans.factory.config.BeanPostProcessor;
-
 /**
  * Replacement for the default {@link GenericXmlContextLoader} that provides hooks for
  * processing some class level Camel related test annotations.
diff --git a/components/camel-test-spring/src/test/java/org/apache/camel/test/spring/CamelSpringOverridePropertiesForPropertyInjectTest.java b/components/camel-test-spring/src/test/java/org/apache/camel/test/spring/CamelSpringOverridePropertiesForPropertyInjectTest.java
index bf79523..4c583ba 100644
--- a/components/camel-test-spring/src/test/java/org/apache/camel/test/spring/CamelSpringOverridePropertiesForPropertyInjectTest.java
+++ b/components/camel-test-spring/src/test/java/org/apache/camel/test/spring/CamelSpringOverridePropertiesForPropertyInjectTest.java
@@ -16,9 +16,6 @@
  */
 package org.apache.camel.test.spring;
 
-import static org.hamcrest.CoreMatchers.is;
-import static org.junit.Assert.assertThat;
-
 import java.util.Properties;
 
 import org.apache.camel.Produce;
@@ -29,6 +26,9 @@ import org.springframework.test.annotation.DirtiesContext;
 import org.springframework.test.context.BootstrapWith;
 import org.springframework.test.context.ContextConfiguration;
 
+import static org.hamcrest.CoreMatchers.is;
+import static org.junit.Assert.assertThat;
+
 @RunWith(CamelSpringRunner.class)
 @BootstrapWith(CamelTestContextBootstrapper.class)
 @ContextConfiguration()