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/08/02 09:35:18 UTC

[camel] branch camel-3.18.x updated: camel-jbang - Do not change keys in application.properties when exporting.

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

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


The following commit(s) were added to refs/heads/camel-3.18.x by this push:
     new b82f21457ff camel-jbang - Do not change keys in application.properties when exporting.
b82f21457ff is described below

commit b82f21457ffa8ba2e205777b21a0bb6bb95aaa0d
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Tue Aug 2 11:34:33 2022 +0200

    camel-jbang - Do not change keys in application.properties when exporting.
---
 .../camel/util/CamelCaseOrderedProperties.java     | 39 +++++++++++++++++-----
 .../camel/util/CamelCaseOrderedPropertiesTest.java | 20 +++++++++--
 2 files changed, 48 insertions(+), 11 deletions(-)

diff --git a/core/camel-util/src/main/java/org/apache/camel/util/CamelCaseOrderedProperties.java b/core/camel-util/src/main/java/org/apache/camel/util/CamelCaseOrderedProperties.java
index f011c86ddd8..b68cd98d1b2 100644
--- a/core/camel-util/src/main/java/org/apache/camel/util/CamelCaseOrderedProperties.java
+++ b/core/camel-util/src/main/java/org/apache/camel/util/CamelCaseOrderedProperties.java
@@ -22,18 +22,41 @@ import java.util.Properties;
  * This class is a camelCase ordered {@link Properties} where the key/values are stored in the order they are added or
  * loaded.
  * <p/>
- * The keys are stored in camelCase style, for example a key of <code>camel.main.stream-caching-enabled</code> is stored
- * as <code>camel.main.streamCachingEnabled</code>
+ * The keys are stored in the original case, for example a key of <code>camel.main.stream-caching-enabled</code> is
+ * stored as <code>camel.main.stream-caching-enabled</code>.
  * <p/>
- * Note: This implementation is only intended as implementation detail for the Camel properties component, and has only
- * been designed to provide the needed functionality. The complex logic for loading properties has been kept from the
- * JDK {@link Properties} class.
+ * However the lookup of a value by key with the <tt>get</tt> methods, will support camelCase or dash style.
+ * <p/>
+ * Note: This implementation is only intended as implementation detail for Camel tooling such as camel-jbang, and has
+ * only been designed to provide the needed functionality. The complex logic for loading properties has been kept from
+ * the JDK {@link Properties} class.
  */
 public final class CamelCaseOrderedProperties extends BaseOrderedProperties {
 
     @Override
-    protected Object doPut(String key, String value) {
-        key = StringHelper.dashToCamelCase(key);
-        return super.doPut(key, value);
+    public synchronized Object get(Object key) {
+        return getProperty(key.toString());
+    }
+
+    @Override
+    public String getProperty(String key) {
+        String answer = super.getProperty(key);
+        if (answer == null) {
+            answer = super.getProperty(StringHelper.dashToCamelCase(key));
+        }
+        if (answer == null) {
+            answer = super.getProperty(StringHelper.camelCaseToDash(key));
+        }
+        return answer;
     }
+
+    @Override
+    public String getProperty(String key, String defaultValue) {
+        String answer = getProperty(key);
+        if (answer == null) {
+            answer = defaultValue;
+        }
+        return answer;
+    }
+
 }
diff --git a/core/camel-util/src/test/java/org/apache/camel/util/CamelCaseOrderedPropertiesTest.java b/core/camel-util/src/test/java/org/apache/camel/util/CamelCaseOrderedPropertiesTest.java
index d67df549e85..b61b08debd3 100644
--- a/core/camel-util/src/test/java/org/apache/camel/util/CamelCaseOrderedPropertiesTest.java
+++ b/core/camel-util/src/test/java/org/apache/camel/util/CamelCaseOrderedPropertiesTest.java
@@ -34,12 +34,19 @@ public class CamelCaseOrderedPropertiesTest {
         assertEquals(2, prop.size());
 
         Iterator it = prop.keySet().iterator();
-        assertEquals("helloWorld", it.next());
-        assertEquals("camel.main.streamCachingEnabled", it.next());
+        assertEquals("hello-world", it.next());
+        assertEquals("camel.main.stream-caching-enabled", it.next());
 
         it = prop.values().iterator();
         assertEquals("Hi Camel", it.next());
         assertEquals("true", it.next());
+
+        assertEquals("Hi Camel", prop.getProperty("hello-world"));
+        assertEquals("Hi Camel", prop.getProperty("helloWorld"));
+        assertEquals("true", prop.getProperty("camel.main.stream-caching-enabled"));
+        assertEquals("true", prop.getProperty("camel.main.streamCachingEnabled"));
+
+        assertEquals("Davs", prop.getProperty("bye-world", "Davs"));
     }
 
     @Test
@@ -51,7 +58,7 @@ public class CamelCaseOrderedPropertiesTest {
 
         Iterator it = prop.keySet().iterator();
         assertEquals("hello", it.next());
-        assertEquals("camel.component.seda.concurrentConsumers", it.next());
+        assertEquals("camel.component.seda.concurrent-consumers", it.next());
         assertEquals("camel.component.seda.queueSize", it.next());
         assertEquals("camel.component.direct.timeout", it.next());
 
@@ -61,6 +68,13 @@ public class CamelCaseOrderedPropertiesTest {
         assertEquals("2", it.next());
         assertEquals("500", it.next());
         assertEquals("1234", it.next());
+
+        assertEquals("World", prop.getProperty("hello", "MyDefault"));
+        assertEquals("2", prop.getProperty("camel.component.seda.concurrent-consumers", "MyDefault"));
+        assertEquals("2", prop.getProperty("camel.component.seda.concurrentConsumers", "MyDefault"));
+        assertEquals("500", prop.getProperty("camel.component.seda.queue-size", "MyDefault"));
+        assertEquals("500", prop.getProperty("camel.component.seda.queueSize", "MyDefault"));
+        assertEquals("1234", prop.getProperty("camel.component.direct.timeout", "MyDefault"));
     }
 
 }