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 2020/08/24 09:17:20 UTC

[camel] branch master updated (2f8f4c2 -> ac25ecd)

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

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


    from 2f8f4c2  Sync Properties
     new 052485b  CAMEL-15438: PropertyBindingSupport - Should automatic support dash key style. Added for configurer as it was automatic for reflection.
     new ac25ecd  CAMEL-15438: properties-binding: support for dash style keys out of the box for both configurer and reflection.

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:
 .../PropertyBindingSupportConfigurerTest.java      | 38 +++++++++++++---
 .../camel/support/PropertyBindingSupportTest.java  | 24 ++++++++++
 .../camel/support/PropertyBindingSupport.java      | 52 ++++++++++++++--------
 3 files changed, 89 insertions(+), 25 deletions(-)


[camel] 02/02: CAMEL-15438: properties-binding: support for dash style keys out of the box for both configurer and reflection.

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

commit ac25ecdc58dd716c312d7e39764cbda272a95c73
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Mon Aug 24 11:16:48 2020 +0200

    CAMEL-15438: properties-binding: support for dash style keys out of the box for both configurer and reflection.
---
 .../PropertyBindingSupportConfigurerTest.java      |  2 +-
 .../camel/support/PropertyBindingSupportTest.java  |  2 +-
 .../camel/support/PropertyBindingSupport.java      | 37 ++++++++++++----------
 3 files changed, 23 insertions(+), 18 deletions(-)

diff --git a/core/camel-core/src/test/java/org/apache/camel/support/PropertyBindingSupportConfigurerTest.java b/core/camel-core/src/test/java/org/apache/camel/support/PropertyBindingSupportConfigurerTest.java
index a886a06..8fd1802 100644
--- a/core/camel-core/src/test/java/org/apache/camel/support/PropertyBindingSupportConfigurerTest.java
+++ b/core/camel-core/src/test/java/org/apache/camel/support/PropertyBindingSupportConfigurerTest.java
@@ -265,7 +265,7 @@ public class PropertyBindingSupportConfigurerTest extends ContextTestSupport {
         prop.put("bar.work.id", "123");
         prop.put("bar.work.name", "{{companyName}}");
 
-        PropertyBindingSupport.build().withDash(true).bind(context, foo, prop);
+        PropertyBindingSupport.build().bind(context, foo, prop);
 
         assertEquals("James", foo.getName());
         assertEquals(33, foo.getBar().getAge());
diff --git a/core/camel-core/src/test/java/org/apache/camel/support/PropertyBindingSupportTest.java b/core/camel-core/src/test/java/org/apache/camel/support/PropertyBindingSupportTest.java
index 82b9b37..d9371b0 100644
--- a/core/camel-core/src/test/java/org/apache/camel/support/PropertyBindingSupportTest.java
+++ b/core/camel-core/src/test/java/org/apache/camel/support/PropertyBindingSupportTest.java
@@ -189,7 +189,7 @@ public class PropertyBindingSupportTest extends ContextTestSupport {
         prop.put("bar.work.id", "123");
         prop.put("bar.work.name", "{{companyName}}");
 
-        PropertyBindingSupport.build().withDash(true).bind(context, foo, prop);
+        PropertyBindingSupport.build().bind(context, foo, prop);
 
         assertEquals("James", foo.getName());
         assertEquals(33, foo.getBar().getAge());
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 4a18574..246d339 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
@@ -74,8 +74,8 @@ import static org.apache.camel.util.StringHelper.startsWithIgnoreCase;
  * </ul>
  *
  * <p>
- * Keys with dash style is supported and will internally be converted from dash to camel case style
- * (eg queue-connection-factory => queueConnectionFactory)
+ * Keys with dash style is supported and will internally be converted from dash to camel case style (eg
+ * queue-connection-factory => queueConnectionFactory)
  * <p>
  * Keys can be marked as optional if the key name starts with a question mark, such as:
  *
@@ -765,7 +765,7 @@ public final class PropertyBindingSupport {
                     // if its a map/list/array type then find out what type the collection uses
                     // so we can use that to lookup as configurer
                     Class<?> collectionType = (Class<?>) ((PropertyConfigurerGetter) configurer)
-                            .getCollectionValueType(newTarget, key, ignoreCase);
+                            .getCollectionValueType(newTarget, undashKey(key), ignoreCase);
                     if (collectionType != null) {
                         configurer = camelContext.adapt(ExtendedCamelContext.class).getConfigurerResolver()
                                 .resolvePropertyConfigurer(collectionType.getSimpleName(), camelContext);
@@ -1050,16 +1050,17 @@ public final class PropertyBindingSupport {
         int pos = name.indexOf('[');
         String lookupKey = name.substring(pos + 1, name.length() - 1);
         String key = name.substring(0, pos);
+        String undashKey = undashKey(key);
 
         Object obj = null;
         if (configurer instanceof PropertyConfigurerGetter) {
-            obj = ((PropertyConfigurerGetter) configurer).getOptionValue(target, key, ignoreCase);
+            obj = ((PropertyConfigurerGetter) configurer).getOptionValue(target, undashKey, ignoreCase);
         }
         if (obj == null) {
             // it was supposed to be a list or map, but its null, so lets create a new list or map and set it automatically
             Class<?> returnType = null;
             if (configurer instanceof PropertyConfigurerGetter) {
-                returnType = (Class<?>) ((PropertyConfigurerGetter) configurer).getAllOptions(target).get(key);
+                returnType = (Class<?>) ((PropertyConfigurerGetter) configurer).getAllOptions(target).get(undashKey);
             }
             if (returnType == null) {
                 return false;
@@ -1073,7 +1074,7 @@ public final class PropertyBindingSupport {
             }
             if (obj != null) {
                 // set
-                boolean hit = configurer.configure(camelContext, target, undashKey(key), obj, ignoreCase);
+                boolean hit = configurer.configure(camelContext, target, undashKey, obj, ignoreCase);
                 if (!hit) {
                     // not a map or list
                     throw new IllegalArgumentException(
@@ -1126,7 +1127,7 @@ public final class PropertyBindingSupport {
             if (idx >= size) {
                 obj = Arrays.copyOf((Object[]) obj, idx + 1);
                 // replace array
-                boolean hit = configurer.configure(camelContext, originalTarget, undashKey(key), obj, ignoreCase);
+                boolean hit = configurer.configure(camelContext, originalTarget, undashKey, obj, ignoreCase);
                 if (!hit) {
                     throw new IllegalArgumentException(
                             "Cannot set property: " + name
@@ -1177,6 +1178,8 @@ public final class PropertyBindingSupport {
             boolean reflection, PropertyConfigurer configurer)
             throws Exception {
 
+        String undashKey = undashKey(name);
+
         if (value instanceof String) {
             String str = value.toString();
             if (str.equals("#autowired")) {
@@ -1184,18 +1187,19 @@ public final class PropertyBindingSupport {
                 Class<?> parameterType = null;
                 if (configurer instanceof PropertyConfigurerGetter) {
                     // favour using configurer
-                    parameterType = (Class<?>) ((PropertyConfigurerGetter) configurer).getAllOptions(target).get(name);
+                    parameterType = (Class<?>) ((PropertyConfigurerGetter) configurer).getAllOptions(target).get(undashKey);
                 }
                 if (parameterType == null && reflection) {
                     // fallback to reflection
                     Method method
-                            = findBestSetterMethod(context, target.getClass(), name, fluentBuilder, allowPrivateSetter,
+                            = findBestSetterMethod(context, target.getClass(), undashKey, fluentBuilder, allowPrivateSetter,
                                     ignoreCase);
                     if (method != null) {
                         parameterType = method.getParameterTypes()[0];
                     } else {
                         throw new IllegalStateException(
-                                "Cannot find setter method with name: " + name + " on class: " + target.getClass().getName()
+                                "Cannot find setter method with name: " + undashKey + " on class: "
+                                                        + target.getClass().getName()
                                                         + " to use for autowiring");
                     }
                 }
@@ -1293,17 +1297,18 @@ public final class PropertyBindingSupport {
             lookupKey = property.substring(pos + 1, property.length() - 1);
             key = property.substring(0, pos);
         }
+        String undashKey = undashKey(key);
 
         Object answer = null;
         Class<?> type = null;
 
         if (configurer instanceof PropertyConfigurerGetter) {
-            answer = ((PropertyConfigurerGetter) configurer).getOptionValue(target, key, ignoreCase);
+            answer = ((PropertyConfigurerGetter) configurer).getOptionValue(target, undashKey, ignoreCase);
         }
         if (answer != null) {
             type = answer.getClass();
         } else if (configurer instanceof PropertyConfigurerGetter) {
-            type = (Class<?>) ((PropertyConfigurerGetter) configurer).getAllOptions(target).get(key);
+            type = (Class<?>) ((PropertyConfigurerGetter) configurer).getAllOptions(target).get(undashKey);
         }
 
         if (answer == null && type == null) {
@@ -1326,7 +1331,7 @@ public final class PropertyBindingSupport {
                                                        + " as either a Map/List/array because target bean is not a Map, List or array type: "
                                                        + target);
                 }
-                boolean hit = configurer.configure(context, target, undashKey(key), answer, ignoreCase);
+                boolean hit = configurer.configure(context, target, undashKey, answer, ignoreCase);
                 if (!hit) {
                     throw new IllegalArgumentException(
                             "Cannot set property: " + key
@@ -1342,7 +1347,7 @@ public final class PropertyBindingSupport {
                 // okay there was no element in the list, so create a new empty instance if we can know its parameter type
                 Class<?> parameterType = null;
                 if (configurer instanceof PropertyConfigurerGetter) {
-                    parameterType = (Class<?>) ((PropertyConfigurerGetter) configurer).getCollectionValueType(target, key,
+                    parameterType = (Class<?>) ((PropertyConfigurerGetter) configurer).getCollectionValueType(target, undashKey,
                             ignoreCase);
                 }
                 if (parameterType != null
@@ -1368,7 +1373,7 @@ public final class PropertyBindingSupport {
                 // okay there was no element in the list, so create a new empty instance if we can know its parameter type
                 Class<?> parameterType = null;
                 if (configurer instanceof PropertyConfigurerGetter) {
-                    parameterType = (Class<?>) ((PropertyConfigurerGetter) configurer).getCollectionValueType(target, key,
+                    parameterType = (Class<?>) ((PropertyConfigurerGetter) configurer).getCollectionValueType(target, undashKey,
                             ignoreCase);
                 }
                 if (parameterType != null
@@ -1386,7 +1391,7 @@ public final class PropertyBindingSupport {
                 // index outside current array size, so enlarge array
                 arr = Arrays.copyOf(arr, idx + 1);
                 // replace array
-                boolean hit = configurer.configure(context, target, undashKey(key), arr, true);
+                boolean hit = configurer.configure(context, target, undashKey, arr, true);
                 if (!hit) {
                     throw new IllegalArgumentException(
                             "Cannot set property: " + key


[camel] 01/02: CAMEL-15438: PropertyBindingSupport - Should automatic support dash key style. Added for configurer as it was automatic for reflection.

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

commit 052485b57c9fb0cd42eb43626c843f7da73e3020
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Mon Aug 24 10:32:55 2020 +0200

    CAMEL-15438: PropertyBindingSupport - Should automatic support dash key style. Added for configurer as it was automatic for reflection.
---
 .../PropertyBindingSupportConfigurerTest.java      | 38 ++++++++++++++++++----
 .../camel/support/PropertyBindingSupportTest.java  | 24 ++++++++++++++
 .../camel/support/PropertyBindingSupport.java      | 27 ++++++++++-----
 3 files changed, 74 insertions(+), 15 deletions(-)

diff --git a/core/camel-core/src/test/java/org/apache/camel/support/PropertyBindingSupportConfigurerTest.java b/core/camel-core/src/test/java/org/apache/camel/support/PropertyBindingSupportConfigurerTest.java
index cc33d46..a886a06 100644
--- a/core/camel-core/src/test/java/org/apache/camel/support/PropertyBindingSupportConfigurerTest.java
+++ b/core/camel-core/src/test/java/org/apache/camel/support/PropertyBindingSupportConfigurerTest.java
@@ -254,6 +254,30 @@ public class PropertyBindingSupportConfigurerTest extends ContextTestSupport {
     }
 
     @Test
+    public void testPropertiesDash() throws Exception {
+        PropertyBindingSupportTest.Foo foo = new PropertyBindingSupportTest.Foo();
+
+        Map<String, Object> prop = new HashMap<>();
+        prop.put("name", "James");
+        prop.put("bar.age", "33");
+        prop.put("bar.{{committer}}", "true");
+        prop.put("bar.gold-customer", "true");
+        prop.put("bar.work.id", "123");
+        prop.put("bar.work.name", "{{companyName}}");
+
+        PropertyBindingSupport.build().withDash(true).bind(context, foo, prop);
+
+        assertEquals("James", foo.getName());
+        assertEquals(33, foo.getBar().getAge());
+        assertTrue(foo.getBar().isRider());
+        assertTrue(foo.getBar().isGoldCustomer());
+        assertEquals(123, foo.getBar().getWork().getId());
+        assertEquals("Acme", foo.getBar().getWork().getName());
+
+        assertTrue(prop.isEmpty(), "Should bind all properties");
+    }
+
+    @Test
     public void testConfigurerShouldNotFailForAnonymousClasses() throws Exception {
         PropertyBindingSupport.autowireSingletonPropertiesFromRegistry(context, new Bar() {
             @Override
@@ -317,8 +341,9 @@ public class PropertyBindingSupportConfigurerTest extends ContextTestSupport {
 
         @Override
         public boolean configure(CamelContext camelContext, Object target, String name, Object value, boolean ignoreCase) {
-            name = name.toLowerCase(Locale.ENGLISH);
-            name = name.replaceAll("-", "");
+            if (ignoreCase) {
+                name = name.toLowerCase(Locale.ENGLISH);
+            }
             if (target instanceof Bar) {
                 Bar bar = (Bar) target;
                 if ("age".equals(name)) {
@@ -333,7 +358,7 @@ public class PropertyBindingSupportConfigurerTest extends ContextTestSupport {
                     bar.work((Company) value);
                     counter++;
                     return true;
-                } else if ("goldcustomer".equals(name)) {
+                } else if ("goldCustomer".equals(name) || "goldcustomer".equals(name)) {
                     bar.goldCustomer(Boolean.parseBoolean(value.toString()));
                     counter++;
                     return true;
@@ -364,8 +389,9 @@ public class PropertyBindingSupportConfigurerTest extends ContextTestSupport {
 
         @Override
         public Object getOptionValue(Object target, String name, boolean ignoreCase) {
-            name = name.toLowerCase(Locale.ENGLISH);
-            name = name.replaceAll("-", "");
+            if (ignoreCase) {
+                name = name.toLowerCase(Locale.ENGLISH);
+            }
             if (target instanceof Bar) {
                 Bar bar = (Bar) target;
                 if ("age".equals(name)) {
@@ -377,7 +403,7 @@ public class PropertyBindingSupportConfigurerTest extends ContextTestSupport {
                 } else if ("work".equals(name)) {
                     counter++;
                     return bar.getWork();
-                } else if ("goldcustomer".equals(name)) {
+                } else if ("goldCustomer".equals(name) || "goldcustomer".equals(name)) {
                     counter++;
                     return bar.isGoldCustomer();
                 }
diff --git a/core/camel-core/src/test/java/org/apache/camel/support/PropertyBindingSupportTest.java b/core/camel-core/src/test/java/org/apache/camel/support/PropertyBindingSupportTest.java
index 5a61244..82b9b37 100644
--- a/core/camel-core/src/test/java/org/apache/camel/support/PropertyBindingSupportTest.java
+++ b/core/camel-core/src/test/java/org/apache/camel/support/PropertyBindingSupportTest.java
@@ -178,6 +178,30 @@ public class PropertyBindingSupportTest extends ContextTestSupport {
     }
 
     @Test
+    public void testPropertiesDash() throws Exception {
+        Foo foo = new Foo();
+
+        Map<String, Object> prop = new HashMap<>();
+        prop.put("name", "James");
+        prop.put("bar.age", "33");
+        prop.put("bar.{{committer}}", "true");
+        prop.put("bar.gold-customer", "true");
+        prop.put("bar.work.id", "123");
+        prop.put("bar.work.name", "{{companyName}}");
+
+        PropertyBindingSupport.build().withDash(true).bind(context, foo, prop);
+
+        assertEquals("James", foo.getName());
+        assertEquals(33, foo.getBar().getAge());
+        assertTrue(foo.getBar().isRider());
+        assertTrue(foo.getBar().isGoldCustomer());
+        assertEquals(123, foo.getBar().getWork().getId());
+        assertEquals("Acme", foo.getBar().getWork().getName());
+
+        assertTrue(prop.isEmpty(), "Should bind all properties");
+    }
+
+    @Test
     public void testBindPropertiesWithOptionPrefix() throws Exception {
         Foo foo = new Foo();
 
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 92bc1d6..4a18574 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
@@ -70,9 +70,12 @@ import static org.apache.camel.util.StringHelper.startsWithIgnoreCase;
  * #class:com.foo.MyClassType#myFactoryMethod('Hello World', 5, true). Or if you need to create the instance via
  * constructor parameters then you can specify the parameters as shown: #class:com.foo.MyClass('Hello World', 5,
  * true)</li>.
- * <li>ignore case - Whether to ignore case for property keys
- * <li>
+ * <li>ignore case - Whether to ignore case for property keys</li>
  * </ul>
+ *
+ * <p>
+ * Keys with dash style is supported and will internally be converted from dash to camel case style
+ * (eg queue-connection-factory => queueConnectionFactory)
  * <p>
  * Keys can be marked as optional if the key name starts with a question mark, such as:
  *
@@ -464,7 +467,7 @@ public final class PropertyBindingSupport {
                         if (value != null) {
                             if (configurer != null) {
                                 // favour using source code generated configurer
-                                hit = configurer.configure(camelContext, target, key, value, true);
+                                hit = configurer.configure(camelContext, target, undashKey(key), value, true);
                             }
                             if (!hit) {
                                 // fallback to use reflection based
@@ -849,7 +852,7 @@ public final class PropertyBindingSupport {
                 obj = camelContext.getInjector().newInstance(parameterType);
             }
             if (obj != null) {
-                boolean hit = configurer.configure(camelContext, newTarget, key, obj, ignoreCase);
+                boolean hit = configurer.configure(camelContext, newTarget, undashKey(key), obj, ignoreCase);
                 if (hit) {
                     answer = obj;
                 }
@@ -1070,7 +1073,7 @@ public final class PropertyBindingSupport {
             }
             if (obj != null) {
                 // set
-                boolean hit = configurer.configure(camelContext, target, key, obj, ignoreCase);
+                boolean hit = configurer.configure(camelContext, target, undashKey(key), obj, ignoreCase);
                 if (!hit) {
                     // not a map or list
                     throw new IllegalArgumentException(
@@ -1123,7 +1126,7 @@ public final class PropertyBindingSupport {
             if (idx >= size) {
                 obj = Arrays.copyOf((Object[]) obj, idx + 1);
                 // replace array
-                boolean hit = configurer.configure(camelContext, originalTarget, key, obj, ignoreCase);
+                boolean hit = configurer.configure(camelContext, originalTarget, undashKey(key), obj, ignoreCase);
                 if (!hit) {
                     throw new IllegalArgumentException(
                             "Cannot set property: " + name
@@ -1145,7 +1148,7 @@ public final class PropertyBindingSupport {
             CamelContext camelContext, Object target, String key, Object value,
             boolean ignoreCase, PropertyConfigurer configurer) {
         try {
-            return configurer.configure(camelContext, target, key, value, ignoreCase);
+            return configurer.configure(camelContext, target, undashKey(key), value, ignoreCase);
         } catch (Exception e) {
             throw new PropertyBindingException(target, key, value, e);
         }
@@ -1323,7 +1326,7 @@ public final class PropertyBindingSupport {
                                                        + " as either a Map/List/array because target bean is not a Map, List or array type: "
                                                        + target);
                 }
-                boolean hit = configurer.configure(context, target, key, answer, ignoreCase);
+                boolean hit = configurer.configure(context, target, undashKey(key), answer, ignoreCase);
                 if (!hit) {
                     throw new IllegalArgumentException(
                             "Cannot set property: " + key
@@ -1383,7 +1386,7 @@ public final class PropertyBindingSupport {
                 // index outside current array size, so enlarge array
                 arr = Arrays.copyOf(arr, idx + 1);
                 // replace array
-                boolean hit = configurer.configure(context, target, key, arr, true);
+                boolean hit = configurer.configure(context, target, undashKey(key), arr, true);
                 if (!hit) {
                     throw new IllegalArgumentException(
                             "Cannot set property: " + key
@@ -1943,4 +1946,10 @@ public final class PropertyBindingSupport {
         return value;
     }
 
+    private static String undashKey(String key) {
+        // as we un-dash property keys then we need to prepare this for the configurer (reflection does this automatic)
+        key = StringHelper.dashToCamelCase(key);
+        return key;
+    }
+
 }