This is an automated email from the ASF dual-hosted git repository. lburgazzoli 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 ab6eef0 CAMEL-15887: add support for java.util.Properties ab6eef0 is described below commit ab6eef03992e3185816653c1aacd28cda65fa0c0 Author: Luca Burgazzoli <lburgazz...@gmail.com> AuthorDate: Tue Nov 24 13:27:15 2020 +0100 CAMEL-15887: add support for java.util.Properties --- .../PropertyBindingSupportPropertiesTest.java | 119 +++++++++++++++++++++ .../camel/support/PropertyBindingSupport.java | 23 ++-- 2 files changed, 136 insertions(+), 6 deletions(-) diff --git a/core/camel-core/src/test/java/org/apache/camel/support/PropertyBindingSupportPropertiesTest.java b/core/camel-core/src/test/java/org/apache/camel/support/PropertyBindingSupportPropertiesTest.java new file mode 100644 index 0000000..8f1f0c9 --- /dev/null +++ b/core/camel-core/src/test/java/org/apache/camel/support/PropertyBindingSupportPropertiesTest.java @@ -0,0 +1,119 @@ +/* + * 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.support; + +import java.util.Locale; +import java.util.Properties; + +import org.apache.camel.CamelContext; +import org.apache.camel.ContextTestSupport; +import org.apache.camel.spi.PropertyConfigurer; +import org.apache.camel.spi.PropertyConfigurerGetter; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +/** + * Unit test for PropertyBindingSupport + */ +public class PropertyBindingSupportPropertiesTest extends ContextTestSupport { + @Test + public void testProperties() { + Bar bar = new Bar(); + + PropertyBindingSupport.build() + .withCamelContext(context) + .withReflection(true) + .withTarget(bar) + .withProperty("works[acme]", "company1") + .withProperty("works[burger]", "company2") + .bind(); + + assertEquals("company1", bar.getWorks().getProperty("acme")); + assertEquals("company2", bar.getWorks().getProperty("burger")); + } + + @Test + public void testPropertiesWithConfigurer() { + Bar bar = new Bar(); + + PropertyBindingSupport.build() + .withCamelContext(context) + .withReflection(false) + .withConfigurer(new BarConfigurer()) + .withTarget(bar) + .withProperty("works[acme]", "company1") + .withProperty("works[burger]", "company2") + .bind(); + + assertEquals("company1", bar.getWorks().getProperty("acme")); + assertEquals("company2", bar.getWorks().getProperty("burger")); + } + + public static class Bar { + private Properties works; + + public Properties getWorks() { + return works; + } + + public void setWorks(Properties works) { + this.works = works; + } + } + + private static class BarConfigurer implements PropertyConfigurer, PropertyConfigurerGetter { + @Override + public boolean configure(CamelContext camelContext, Object target, String name, Object value, boolean ignoreCase) { + if (ignoreCase) { + name = name.toLowerCase(Locale.ENGLISH); + } + if (target instanceof PropertyBindingSupportPropertiesTest.Bar) { + PropertyBindingSupportPropertiesTest.Bar bar = (PropertyBindingSupportPropertiesTest.Bar) target; + if ("works".equals(name)) { + bar.setWorks((Properties) value); + return true; + } + } + return false; + } + + @Override + public Class<?> getOptionType(String name, boolean ignoreCase) { + if ("works".equals(name)) { + return Properties.class; + } + + return null; + } + + @Override + public Object getOptionValue(Object target, String name, boolean ignoreCase) { + if (ignoreCase) { + name = name.toLowerCase(Locale.ENGLISH); + } + if (target instanceof PropertyBindingSupportPropertiesTest.Bar) { + PropertyBindingSupportPropertiesTest.Bar bar = (PropertyBindingSupportPropertiesTest.Bar) target; + if ("works".equals(name)) { + return bar.getWorks(); + } + } + return null; + } + } + +} 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 0755855..735cf47 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 @@ -30,6 +30,7 @@ import java.util.LinkedHashMap; import java.util.List; import java.util.Map; import java.util.Optional; +import java.util.Properties; import java.util.Set; import java.util.TreeMap; @@ -331,8 +332,10 @@ public final class PropertyBindingSupport { if (method != null) { Class<?> parameterType = method.getParameterTypes()[0]; Object obj = null; - // special for map/list/array - if (Map.class.isAssignableFrom(parameterType)) { + // special for properties/map/list/array + if (Properties.class.isAssignableFrom(parameterType)) { + obj = new Properties(); + } else if (Map.class.isAssignableFrom(parameterType)) { obj = new LinkedHashMap<>(); } else if (Collection.class.isAssignableFrom(parameterType)) { obj = new ArrayList<>(); @@ -368,8 +371,10 @@ public final class PropertyBindingSupport { } if (parameterType != null) { Object obj = null; - // special for map/list/array - if (Map.class.isAssignableFrom(parameterType)) { + // special for properties/map/list/array + if (Properties.class.isAssignableFrom(parameterType)) { + obj = new Properties(); + } else if (Map.class.isAssignableFrom(parameterType)) { obj = new LinkedHashMap<>(); } else if (Collection.class.isAssignableFrom(parameterType)) { obj = new ArrayList<>(); @@ -483,7 +488,9 @@ public final class PropertyBindingSupport { if (getter != null) { // what type does it have Class<?> returnType = getter.getReturnType(); - if (Map.class.isAssignableFrom(returnType)) { + if (Properties.class.isAssignableFrom(returnType)) { + obj = new Properties(); + } else if (Map.class.isAssignableFrom(returnType)) { obj = new LinkedHashMap<>(); } else if (Collection.class.isAssignableFrom(returnType)) { obj = new ArrayList<>(); @@ -514,6 +521,7 @@ public final class PropertyBindingSupport { } if (obj instanceof Map) { + // this supports both Map and Properties Map map = (Map) obj; map.put(lookupKey, value); return true; @@ -593,7 +601,9 @@ public final class PropertyBindingSupport { if (returnType == null) { return false; } - if (Map.class.isAssignableFrom(returnType)) { + if (Properties.class.isAssignableFrom(returnType)) { + obj = new Properties(); + } else if (Map.class.isAssignableFrom(returnType)) { obj = new LinkedHashMap<>(); } else if (Collection.class.isAssignableFrom(returnType)) { obj = new ArrayList<>(); @@ -619,6 +629,7 @@ public final class PropertyBindingSupport { } if (obj instanceof Map) { + // this supports both Map and Properties Map map = (Map) obj; map.put(lookupKey, value); return true;