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 2021/03/23 08:51:06 UTC

[camel] branch master updated: CAMEL-16230: AdviceWithRouteBuilder should do property placeholder for its text based inputs.

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

davsclaus pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/camel.git


The following commit(s) were added to refs/heads/master by this push:
     new b37a4e7  CAMEL-16230: AdviceWithRouteBuilder should do property placeholder for its text based inputs.
b37a4e7 is described below

commit b37a4e7e19c67617ad2dfa3fcf664215c4622034
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Tue Mar 23 09:50:15 2021 +0100

    CAMEL-16230: AdviceWithRouteBuilder should do property placeholder for its text based inputs.
---
 .../camel/builder/AdviceWithRouteBuilder.java      | 14 +++++
 .../issues/AdviceWithPropertyPlaceholderTest.java  | 65 ++++++++++++++++++++++
 2 files changed, 79 insertions(+)

diff --git a/core/camel-core-model/src/main/java/org/apache/camel/builder/AdviceWithRouteBuilder.java b/core/camel-core-model/src/main/java/org/apache/camel/builder/AdviceWithRouteBuilder.java
index 367290b..2a06b18 100644
--- a/core/camel-core-model/src/main/java/org/apache/camel/builder/AdviceWithRouteBuilder.java
+++ b/core/camel-core-model/src/main/java/org/apache/camel/builder/AdviceWithRouteBuilder.java
@@ -190,6 +190,8 @@ public abstract class AdviceWithRouteBuilder extends RouteBuilder {
      */
     public void mockEndpoints(String... pattern) throws Exception {
         for (String s : pattern) {
+            // the text based input may be property placeholders
+            s = getContext().resolvePropertyPlaceholders(s);
             getContext().adapt(ExtendedCamelContext.class).registerEndpointCallback(createMockEndpointStrategy(s, false));
         }
     }
@@ -204,6 +206,8 @@ public abstract class AdviceWithRouteBuilder extends RouteBuilder {
      */
     public void mockEndpointsAndSkip(String... pattern) throws Exception {
         for (String s : pattern) {
+            // the text based input may be property placeholders
+            s = getContext().resolvePropertyPlaceholders(s);
             getContext().adapt(ExtendedCamelContext.class)
                     .registerEndpointCallback(createMockEndpointStrategy(s, true));
         }
@@ -216,6 +220,8 @@ public abstract class AdviceWithRouteBuilder extends RouteBuilder {
      */
     public void replaceFromWith(String uri) {
         ObjectHelper.notNull(originalRoute, "originalRoute", this);
+        // the text based input may be property placeholders
+        uri = getContext().resolvePropertyPlaceholders(uri);
         getAdviceWithTasks().add(AdviceWithTasks.replaceFromWith(originalRoute, uri));
     }
 
@@ -240,6 +246,8 @@ public abstract class AdviceWithRouteBuilder extends RouteBuilder {
      */
     public <T extends ProcessorDefinition<?>> AdviceWithBuilder<T> weaveById(String pattern) {
         ObjectHelper.notNull(originalRoute, "originalRoute", this);
+        // the text based input may be property placeholders
+        pattern = getContext().resolvePropertyPlaceholders(pattern);
         return new AdviceWithBuilder<>(this, pattern, null, null, null);
     }
 
@@ -254,6 +262,8 @@ public abstract class AdviceWithRouteBuilder extends RouteBuilder {
      */
     public <T extends ProcessorDefinition<?>> AdviceWithBuilder<T> weaveByToString(String pattern) {
         ObjectHelper.notNull(originalRoute, "originalRoute", this);
+        // the text based input may be property placeholders
+        pattern = getContext().resolvePropertyPlaceholders(pattern);
         return new AdviceWithBuilder<>(this, null, pattern, null, null);
     }
 
@@ -268,6 +278,8 @@ public abstract class AdviceWithRouteBuilder extends RouteBuilder {
      */
     public <T extends ProcessorDefinition<?>> AdviceWithBuilder<T> weaveByToUri(String pattern) {
         ObjectHelper.notNull(originalRoute, "originalRoute", this);
+        // the text based input may be property placeholders
+        pattern = getContext().resolvePropertyPlaceholders(pattern);
         return new AdviceWithBuilder<>(this, null, null, pattern, null);
     }
 
@@ -303,6 +315,8 @@ public abstract class AdviceWithRouteBuilder extends RouteBuilder {
     }
 
     private EndpointStrategy createMockEndpointStrategy(String pattern, boolean skip) {
+        // the text based input may be property placeholders
+        pattern = getContext().resolvePropertyPlaceholders(pattern);
         MockSendToEndpointStrategyFactory factory = getContext().adapt(ExtendedCamelContext.class)
                 .getFactoryFinder(DEFAULT_PATH)
                 .newInstance(MockSendToEndpointStrategyFactory.FACTORY, MockSendToEndpointStrategyFactory.class)
diff --git a/core/camel-core/src/test/java/org/apache/camel/issues/AdviceWithPropertyPlaceholderTest.java b/core/camel-core/src/test/java/org/apache/camel/issues/AdviceWithPropertyPlaceholderTest.java
new file mode 100644
index 0000000..c459b2e
--- /dev/null
+++ b/core/camel-core/src/test/java/org/apache/camel/issues/AdviceWithPropertyPlaceholderTest.java
@@ -0,0 +1,65 @@
+/*
+ * 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.issues;
+
+import java.util.Properties;
+
+import org.apache.camel.ContextTestSupport;
+import org.apache.camel.builder.AdviceWith;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.spi.PropertiesComponent;
+import org.junit.jupiter.api.Test;
+
+public class AdviceWithPropertyPlaceholderTest extends ContextTestSupport {
+
+    @Test
+    public void testAdvicePropertyPlaceholder() throws Exception {
+        Properties props = new Properties();
+        props.put("myPattern", "seda*");
+        props.put("myEnd", "mock:result");
+        PropertiesComponent pc = context.getPropertiesComponent();
+        pc.setInitialProperties(props);
+
+        AdviceWith.adviceWith(context, null, r -> {
+            r.mockEndpointsAndSkip("{{myPattern}}");
+            r.weaveAddLast().to("{{myEnd}}");
+        });
+
+        getMockEndpoint("mock:seda:a").expectedMessageCount(1);
+        getMockEndpoint("mock:seda:b").expectedMessageCount(1);
+        getMockEndpoint("mock:seda:c").expectedMessageCount(1);
+        getMockEndpoint("mock:result").expectedMessageCount(1);
+
+        template.sendBody("direct:start", "Hello World");
+
+        assertMockEndpointsSatisfied();
+    }
+
+    @Override
+    protected RouteBuilder createRouteBuilder() throws Exception {
+        return new RouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                from("direct:start")
+                        .to("seda:a")
+                        .to("seda:b")
+                        .to("seda:c");
+            }
+        };
+    }
+
+}