This is an automated email from the ASF dual-hosted git repository. orpiske pushed a commit to branch main in repository https://gitbox.apache.org/repos/asf/camel.git
commit 41bc791f85042ae86dd0acbe7576813da4cfbb31 Author: Otavio Rodolfo Piske <angusyo...@gmail.com> AuthorDate: Fri Apr 14 14:32:28 2023 +0200 (chores) camel-core: remove duplicated properties extraction code --- .../camel/impl/engine/IntrospectionSupport.java | 22 ---------- .../impl/engine/IntrospectionSupportTest.java | 21 ---------- .../org/apache/camel/util/PropertiesHelper.java | 5 +++ .../apache/camel/util/PropertiesHelperTest.java | 47 ++++++++++++++++++++++ 4 files changed, 52 insertions(+), 43 deletions(-) diff --git a/core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/IntrospectionSupport.java b/core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/IntrospectionSupport.java index 61cfb71294b..424f4f7822d 100644 --- a/core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/IntrospectionSupport.java +++ b/core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/IntrospectionSupport.java @@ -439,28 +439,6 @@ final class IntrospectionSupport { return rc; } - static Map<String, Object> extractProperties(Map<String, Object> properties, String optionPrefix, boolean remove) { - ObjectHelper.notNull(properties, "properties"); - - Map<String, Object> rc = new LinkedHashMap<>(properties.size()); - - for (Iterator<Map.Entry<String, Object>> it = properties.entrySet().iterator(); it.hasNext();) { - Map.Entry<String, Object> entry = it.next(); - String name = entry.getKey(); - if (name.startsWith(optionPrefix)) { - Object value = properties.get(name); - name = name.substring(optionPrefix.length()); - rc.put(name, value); - - if (remove) { - it.remove(); - } - } - } - - return rc; - } - static boolean setProperties( CamelContext context, TypeConverter typeConverter, Object target, Map<String, Object> properties) throws Exception { diff --git a/core/camel-core/src/test/java/org/apache/camel/impl/engine/IntrospectionSupportTest.java b/core/camel-core/src/test/java/org/apache/camel/impl/engine/IntrospectionSupportTest.java index baa327cb02c..90a3e8bf48d 100644 --- a/core/camel-core/src/test/java/org/apache/camel/impl/engine/IntrospectionSupportTest.java +++ b/core/camel-core/src/test/java/org/apache/camel/impl/engine/IntrospectionSupportTest.java @@ -21,7 +21,6 @@ import java.util.ArrayList; import java.util.Collection; import java.util.Date; import java.util.HashMap; -import java.util.LinkedHashMap; import java.util.List; import java.util.Locale; import java.util.Map; @@ -32,7 +31,6 @@ import org.apache.camel.util.AnotherExampleBean; import org.apache.camel.util.OtherExampleBean; import org.junit.jupiter.api.Test; -import static org.apache.camel.impl.engine.IntrospectionSupport.extractProperties; import static org.apache.camel.impl.engine.IntrospectionSupport.findSetterMethodsOrderedByParameterType; import static org.apache.camel.impl.engine.IntrospectionSupport.getProperties; import static org.apache.camel.impl.engine.IntrospectionSupport.getProperty; @@ -520,25 +518,6 @@ public class IntrospectionSupportTest extends ContextTestSupport { assertFalse(isSetter(setupSomething)); } - @Test - public void testExtractProperties() throws Exception { - Map<String, Object> params = new LinkedHashMap<>(); - params.put("foo.name", "Camel"); - params.put("foo.age", 5); - params.put("bar", "yes"); - - // extract all "foo." properties - // and their keys should have the prefix removed - Map<String, Object> foo = extractProperties(params, "foo.", true); - assertEquals(2, foo.size()); - assertEquals("Camel", foo.get("name")); - assertEquals(5, foo.get("age")); - - // the extracted properties should be removed from original - assertEquals(1, params.size()); - assertEquals("yes", params.get("bar")); - } - @Test public void testFindSetterMethodsOrderedByParameterType() throws Exception { List<Method> setters = findSetterMethodsOrderedByParameterType(MyOverloadedBean.class, "bean", diff --git a/core/camel-util/src/main/java/org/apache/camel/util/PropertiesHelper.java b/core/camel-util/src/main/java/org/apache/camel/util/PropertiesHelper.java index 7895bb97a88..e5ea213764a 100644 --- a/core/camel-util/src/main/java/org/apache/camel/util/PropertiesHelper.java +++ b/core/camel-util/src/main/java/org/apache/camel/util/PropertiesHelper.java @@ -35,6 +35,11 @@ public final class PropertiesHelper { if (properties == null) { return new LinkedHashMap<>(0); } + + return doExtractProperties(properties, optionPrefix, remove); + } + + static Map<String, Object> doExtractProperties(Map<String, Object> properties, String optionPrefix, boolean remove) { Map<String, Object> rc = new LinkedHashMap<>(properties.size()); for (Iterator<Map.Entry<String, Object>> it = properties.entrySet().iterator(); it.hasNext();) { diff --git a/core/camel-util/src/test/java/org/apache/camel/util/PropertiesHelperTest.java b/core/camel-util/src/test/java/org/apache/camel/util/PropertiesHelperTest.java new file mode 100644 index 00000000000..df2f0c22c4d --- /dev/null +++ b/core/camel-util/src/test/java/org/apache/camel/util/PropertiesHelperTest.java @@ -0,0 +1,47 @@ +/* + * 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.util; + +import java.util.LinkedHashMap; +import java.util.Map; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.*; + +class PropertiesHelperTest { + + @Test + public void testExtractProperties() { + Map<String, Object> params = new LinkedHashMap<>(); + params.put("foo.name", "Camel"); + params.put("foo.age", 5); + params.put("bar", "yes"); + + // extract all "foo." properties + // and their keys should have the prefix removed + Map<String, Object> foo = PropertiesHelper.extractProperties(params, "foo.", true); + assertEquals(2, foo.size()); + assertEquals("Camel", foo.get("name")); + assertEquals(5, foo.get("age")); + + // the extracted properties should be removed from original + assertEquals(1, params.size()); + assertEquals("yes", params.get("bar")); + } +}