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 2023/10/21 14:30:54 UTC

[camel-spring-boot] branch main updated: CAMEL-20001: camel-core - PropertiesParser should have a hook in a different phase that Spring Boot uses. This ensures that override properties from Camel will take precedence.

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

davsclaus pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel-spring-boot.git


The following commit(s) were added to refs/heads/main by this push:
     new 660a27179ee CAMEL-20001: camel-core - PropertiesParser should have a hook in a different phase that Spring Boot uses. This ensures that override properties from Camel will take precedence.
660a27179ee is described below

commit 660a27179eee32a147447bfe4b5d454dabb1fef8
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Sat Oct 21 16:30:47 2023 +0200

    CAMEL-20001: camel-core - PropertiesParser should have a hook in a different phase that Spring Boot uses. This ensures that override properties from Camel will take precedence.
---
 .../camel/spring/boot/SpringPropertiesParser.java  |  13 +--
 .../spring/boot/issues/OverridePropertiesTest.java | 101 +++++++++++++++++++++
 2 files changed, 108 insertions(+), 6 deletions(-)

diff --git a/core/camel-spring-boot/src/main/java/org/apache/camel/spring/boot/SpringPropertiesParser.java b/core/camel-spring-boot/src/main/java/org/apache/camel/spring/boot/SpringPropertiesParser.java
index d05f5bd2e34..1706bb942f6 100644
--- a/core/camel-spring-boot/src/main/java/org/apache/camel/spring/boot/SpringPropertiesParser.java
+++ b/core/camel-spring-boot/src/main/java/org/apache/camel/spring/boot/SpringPropertiesParser.java
@@ -17,22 +17,23 @@
 package org.apache.camel.spring.boot;
 
 import org.apache.camel.component.properties.DefaultPropertiesParser;
-import org.apache.camel.component.properties.PropertiesLookup;
 import org.springframework.core.env.Environment;
 
-class SpringPropertiesParser extends DefaultPropertiesParser {
+/**
+ * Extension to {@link DefaultPropertiesParser} that will lookup
+ * in Spring via {@link Environment#getProperty(String)}.
+ */
+public class SpringPropertiesParser extends DefaultPropertiesParser {
 
     // Members
     private final Environment env;
 
-    SpringPropertiesParser(Environment env) {
+    public SpringPropertiesParser(Environment env) {
         this.env = env;
     }
 
-    // Overridden
-
     @Override
-    public String parseProperty(String key, String value, PropertiesLookup properties) {
+    public String customLookup(String key) {
         return env.getProperty(key);
     }
 
diff --git a/core/camel-spring-boot/src/test/java/org/apache/camel/spring/boot/issues/OverridePropertiesTest.java b/core/camel-spring-boot/src/test/java/org/apache/camel/spring/boot/issues/OverridePropertiesTest.java
new file mode 100644
index 00000000000..ddd1fef1260
--- /dev/null
+++ b/core/camel-spring-boot/src/test/java/org/apache/camel/spring/boot/issues/OverridePropertiesTest.java
@@ -0,0 +1,101 @@
+/*
+ * 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.spring.boot.issues;
+
+import org.apache.camel.CamelContext;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.apache.camel.component.properties.PropertiesComponent;
+import org.apache.camel.spring.boot.SpringPropertiesParser;
+import org.apache.camel.test.spring.junit5.CamelSpringTestSupport;
+import org.junit.jupiter.api.Test;
+import org.springframework.context.support.AbstractApplicationContext;
+import org.springframework.context.support.ClassPathXmlApplicationContext;
+import org.springframework.core.env.PropertiesPropertySource;
+
+import java.util.Properties;
+
+public class OverridePropertiesTest extends CamelSpringTestSupport {
+
+    @Override
+    protected Properties useOverridePropertiesWithPropertiesComponent() {
+        Properties prop = new Properties();
+        prop.put("name", "Donald Duck");
+        return prop;
+    }
+
+    @Override
+    protected CamelContext createCamelContext() throws Exception {
+        CamelContext context = super.createCamelContext();
+        PropertiesComponent pc = (PropertiesComponent) context.getPropertiesComponent();
+        pc.setPropertiesParser(new SpringPropertiesParser(applicationContext.getEnvironment()));
+        return context;
+    }
+
+    @Test
+    public void testOverrideProperties() throws Exception {
+        context.addRoutes(new RouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                from("direct:start")
+                        .setBody().simple("Name is {{name}}")
+                        .to("mock:result");
+            }
+        });
+
+        // should find camel override property value
+        getMockEndpoint("mock:result").expectedBodiesReceived("Name is Donald Duck");
+
+        template.sendBody("direct:start", "Hello World");
+
+        MockEndpoint.assertIsSatisfied(context);
+    }
+
+    @Test
+    public void testNotOverrideProperties() throws Exception {
+        PropertiesComponent pc = (PropertiesComponent) context.getPropertiesComponent();
+        pc.getOverrideProperties().clear();
+
+        context.addRoutes(new RouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                from("direct:start")
+                        .setBody().simple("Name is {{name}}")
+                        .to("mock:result");
+            }
+        });
+
+        // should find spring property value
+        getMockEndpoint("mock:result").expectedBodiesReceived("Name is Jack Sparrow");
+
+        template.sendBody("direct:start", "Hello World");
+
+        MockEndpoint.assertIsSatisfied(context);
+    }
+
+    @Override
+    protected AbstractApplicationContext createApplicationContext() {
+        AbstractApplicationContext ac = new ClassPathXmlApplicationContext();
+
+        // set property into spring
+        Properties prop = new Properties();
+        prop.put("name", "Jack Sparrow");
+        ac.getEnvironment().getPropertySources().addFirst(new PropertiesPropertySource("custom", prop));
+
+        return ac;
+    }
+}