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 2022/01/12 15:20:23 UTC

[camel] branch main updated (2ff600f -> 9d2ffe2)

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

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


    from 2ff600f  Regen for commit c648a6c34a9e53d5fed005d3a9e8ab424b7a1314
     new 3131d46  CAMEL-17479: configuring beans with {{?foo}} supported. Added unit test.
     new 9d2ffe2  CAMEL-17479: configuring beans with {{?foo}} supported.

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:
 ...va => OptionalPropertyPlaceholderBeanTest.java} | 51 ++++++++++++----------
 .../camel/support/PropertyBindingSupport.java      | 19 +++++++-
 2 files changed, 46 insertions(+), 24 deletions(-)
 copy core/camel-core/src/test/java/org/apache/camel/component/properties/{ComponentResolvePropertyPlaceholdersTest.java => OptionalPropertyPlaceholderBeanTest.java} (51%)

[camel] 01/02: CAMEL-17479: configuring beans with {{?foo}} supported. Added unit test.

Posted by da...@apache.org.
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.git

commit 3131d46dfef19449703660d6309ba5bacaefe0d2
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Wed Jan 12 15:52:22 2022 +0100

    CAMEL-17479: configuring beans with {{?foo}} supported. Added unit test.
---
 .../OptionalPropertyPlaceholderBeanTest.java       | 79 ++++++++++++++++++++++
 1 file changed, 79 insertions(+)

diff --git a/core/camel-core/src/test/java/org/apache/camel/component/properties/OptionalPropertyPlaceholderBeanTest.java b/core/camel-core/src/test/java/org/apache/camel/component/properties/OptionalPropertyPlaceholderBeanTest.java
new file mode 100644
index 0000000..f388097
--- /dev/null
+++ b/core/camel-core/src/test/java/org/apache/camel/component/properties/OptionalPropertyPlaceholderBeanTest.java
@@ -0,0 +1,79 @@
+/*
+ * 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.component.properties;
+
+import org.apache.camel.CamelContext;
+import org.apache.camel.ContextTestSupport;
+import org.apache.camel.component.pojo.SayService;
+import org.apache.camel.support.PropertyBindingSupport;
+import org.junit.jupiter.api.Disabled;
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+public class OptionalPropertyPlaceholderBeanTest extends ContextTestSupport {
+
+    @Override
+    public boolean isUseRouteBuilder() {
+        return false;
+    }
+
+    @Test
+    public void testQueryOptionalPresent() throws Exception {
+        SayService say = new SayService();
+        assertEquals("Hello", say.getMessage());
+
+        PropertyBindingSupport.build().withTarget(say).withProperty("message", "{{?cool.name}}")
+                .withMandatory(false)
+                .withCamelContext(context)
+                .bind();
+        assertEquals("Camel", say.getMessage());
+    }
+
+    @Test
+    @Disabled("TODO: https://issues.apache.org/jira/browse/CAMEL-17479")
+    public void testQueryOptionalNotPresent() throws Exception {
+        SayService say = new SayService();
+        assertEquals("Hello", say.getMessage());
+
+        PropertyBindingSupport.build().withTarget(say).withProperty("message", "{{?unknown}}")
+                .withMandatory(false)
+                .withCamelContext(context)
+                .bind();
+        assertEquals("Hello", say.getMessage());
+    }
+
+    @Test
+    public void testQueryOptionalNotPresentDefaultValue() throws Exception {
+        SayService say = new SayService();
+        assertEquals("Hello", say.getMessage());
+
+        PropertyBindingSupport.build().withTarget(say).withProperty("message", "{{?unknown:Bye}}")
+                .withMandatory(false)
+                .withCamelContext(context)
+                .bind();
+        assertEquals("Bye", say.getMessage());
+    }
+
+    @Override
+    protected CamelContext createCamelContext() throws Exception {
+        CamelContext context = super.createCamelContext();
+        context.getPropertiesComponent().setLocation("classpath:org/apache/camel/component/properties/myproperties.properties");
+        return context;
+    }
+
+}

[camel] 02/02: CAMEL-17479: configuring beans with {{?foo}} supported.

Posted by da...@apache.org.
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.git

commit 9d2ffe2d3fc815deb2a06af346d1ee666b2e5f5b
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Wed Jan 12 16:19:39 2022 +0100

    CAMEL-17479: configuring beans with {{?foo}} supported.
---
 .../OptionalPropertyPlaceholderBeanTest.java          |  5 -----
 .../apache/camel/support/PropertyBindingSupport.java  | 19 ++++++++++++++++++-
 2 files changed, 18 insertions(+), 6 deletions(-)

diff --git a/core/camel-core/src/test/java/org/apache/camel/component/properties/OptionalPropertyPlaceholderBeanTest.java b/core/camel-core/src/test/java/org/apache/camel/component/properties/OptionalPropertyPlaceholderBeanTest.java
index f388097..c281670 100644
--- a/core/camel-core/src/test/java/org/apache/camel/component/properties/OptionalPropertyPlaceholderBeanTest.java
+++ b/core/camel-core/src/test/java/org/apache/camel/component/properties/OptionalPropertyPlaceholderBeanTest.java
@@ -20,7 +20,6 @@ import org.apache.camel.CamelContext;
 import org.apache.camel.ContextTestSupport;
 import org.apache.camel.component.pojo.SayService;
 import org.apache.camel.support.PropertyBindingSupport;
-import org.junit.jupiter.api.Disabled;
 import org.junit.jupiter.api.Test;
 
 import static org.junit.jupiter.api.Assertions.assertEquals;
@@ -38,20 +37,17 @@ public class OptionalPropertyPlaceholderBeanTest extends ContextTestSupport {
         assertEquals("Hello", say.getMessage());
 
         PropertyBindingSupport.build().withTarget(say).withProperty("message", "{{?cool.name}}")
-                .withMandatory(false)
                 .withCamelContext(context)
                 .bind();
         assertEquals("Camel", say.getMessage());
     }
 
     @Test
-    @Disabled("TODO: https://issues.apache.org/jira/browse/CAMEL-17479")
     public void testQueryOptionalNotPresent() throws Exception {
         SayService say = new SayService();
         assertEquals("Hello", say.getMessage());
 
         PropertyBindingSupport.build().withTarget(say).withProperty("message", "{{?unknown}}")
-                .withMandatory(false)
                 .withCamelContext(context)
                 .bind();
         assertEquals("Hello", say.getMessage());
@@ -63,7 +59,6 @@ public class OptionalPropertyPlaceholderBeanTest extends ContextTestSupport {
         assertEquals("Hello", say.getMessage());
 
         PropertyBindingSupport.build().withTarget(say).withProperty("message", "{{?unknown:Bye}}")
-                .withMandatory(false)
                 .withCamelContext(context)
                 .bind();
         assertEquals("Bye", say.getMessage());
diff --git a/core/camel-support/src/main/java/org/apache/camel/support/PropertyBindingSupport.java b/core/camel-support/src/main/java/org/apache/camel/support/PropertyBindingSupport.java
index ee379b9..789b13a 100644
--- a/core/camel-support/src/main/java/org/apache/camel/support/PropertyBindingSupport.java
+++ b/core/camel-support/src/main/java/org/apache/camel/support/PropertyBindingSupport.java
@@ -38,6 +38,7 @@ import org.apache.camel.CamelContext;
 import org.apache.camel.ExtendedCamelContext;
 import org.apache.camel.PropertyBindingException;
 import org.apache.camel.spi.BeanIntrospection;
+import org.apache.camel.spi.PropertiesComponent;
 import org.apache.camel.spi.PropertyConfigurer;
 import org.apache.camel.spi.PropertyConfigurerGetter;
 import org.apache.camel.util.StringHelper;
@@ -84,6 +85,17 @@ import static org.apache.camel.util.StringHelper.startsWithIgnoreCase;
  * </pre>
  * <p>
  * Where foo is mandatory, and bar is optional.
+ *
+ * <p>
+ * Values can be marked as optional property placeholder if the values name starts with a question mark, such as:
+ *
+ * <pre>
+ * username={{?clientUserName}}
+ * </pre>
+ * <p>
+ * Where the username property will only be set if the property placeholder <tt>clientUserName</tt> exists, otherwise
+ * the username is not affected.
+ * </p>
  */
 public final class PropertyBindingSupport {
 
@@ -420,7 +432,12 @@ public final class PropertyBindingSupport {
             key = camelContext.resolvePropertyPlaceholders(key);
             if (text instanceof String) {
                 // resolve property placeholders
-                text = camelContext.resolvePropertyPlaceholders(text.toString());
+                String s = text.toString();
+                text = camelContext.resolvePropertyPlaceholders(s);
+                if (text == null && s.startsWith(PropertiesComponent.PREFIX_TOKEN + "?")) {
+                    // it was an optional value, so we should not try to set the property but regard it as a "hit"
+                    return true;
+                }
             }
         }