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 2021/02/27 07:58:30 UTC

[camel] 01/02: CAMEL-16250 - support more edge cases of dashToCamelCase conversion

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 c5d327f8dd530b3219c0cab695cfe34411f314ba
Author: Aurélien Pupier <ap...@redhat.com>
AuthorDate: Tue Feb 23 15:13:35 2021 +0100

    CAMEL-16250 - support more edge cases of dashToCamelCase conversion
    
    Signed-off-by: Aurélien Pupier <ap...@redhat.com>
---
 .../java/org/apache/camel/util/StringHelper.java   | 16 ++++----
 .../org/apache/camel/util/StringHelperTest.java    | 48 +++++++++++++++++++++-
 2 files changed, 55 insertions(+), 9 deletions(-)

diff --git a/core/camel-util/src/main/java/org/apache/camel/util/StringHelper.java b/core/camel-util/src/main/java/org/apache/camel/util/StringHelper.java
index 34607c5..686af48 100644
--- a/core/camel-util/src/main/java/org/apache/camel/util/StringHelper.java
+++ b/core/camel-util/src/main/java/org/apache/camel/util/StringHelper.java
@@ -470,14 +470,14 @@ public final class StringHelper {
         }
 
         StringBuilder sb = new StringBuilder();
-
-        for (int i = 0; i < text.length(); i++) {
-            char c = text.charAt(i);
-            if (c == '-') {
-                i++;
-                sb.append(Character.toUpperCase(text.charAt(i)));
-            } else {
-                sb.append(c);
+        String[] splittedString = text.split("\\-");
+        for (int i = 0; i < splittedString.length; i++) {
+            String currentToken = splittedString[i];
+            if (i == 0) {
+                sb.append(currentToken);
+            } else if (!currentToken.isEmpty()) {
+                sb.append(Character.toUpperCase(currentToken.charAt(0)));
+                sb.append(currentToken.substring(1));
             }
         }
         return sb.toString();
diff --git a/core/camel-util/src/test/java/org/apache/camel/util/StringHelperTest.java b/core/camel-util/src/test/java/org/apache/camel/util/StringHelperTest.java
index a75e476..ec122ec 100644
--- a/core/camel-util/src/test/java/org/apache/camel/util/StringHelperTest.java
+++ b/core/camel-util/src/test/java/org/apache/camel/util/StringHelperTest.java
@@ -16,10 +16,12 @@
  */
 package org.apache.camel.util;
 
+import org.junit.jupiter.api.Nested;
 import org.junit.jupiter.api.Test;
 
 import static org.apache.camel.util.StringHelper.*;
-import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.assertj.core.api.Assertions.*;
+import static org.junit.jupiter.api.Assertions.*;
 
 public class StringHelperTest {
 
@@ -54,6 +56,50 @@ public class StringHelperTest {
         assertEquals("available-phone-number-country", camelCaseToDash("availablePhoneNumberCountry"));
         assertEquals("available-phone-number-country", camelCaseToDash("AvailablePhoneNumberCountry"));
     }
+    
+    @Nested
+    class DashToCamelCase {
+        
+        @Test
+        void testDashToCamelCaseWithNull() throws Exception {
+            assertThat(dashToCamelCase(null)).isNull();
+        }
+        
+        @Test
+        void testDashToCamelCaseWithEmptyValue() throws Exception {
+            assertThat(dashToCamelCase("")).isEmpty();
+        }
+        
+        @Test
+        void testDashToCamelCaseWithNoDash() throws Exception {
+            assertThat(dashToCamelCase("a")).isEqualTo("a");
+        }
+        
+        @Test
+        void testDashToCamelCaseWithOneDash() throws Exception {
+            assertThat(dashToCamelCase("a-b")).isEqualTo("aB");
+        }
+        
+        @Test
+        void testDashToCamelCaseWithSeveralDashes() throws Exception {
+            assertThat(dashToCamelCase("a-bb-cc-dd")).isEqualTo("aBbCcDd");
+        }
+        
+        @Test
+        void testDashToCamelCaseWithEndDash() throws Exception {
+            assertThat(dashToCamelCase("a-")).isEqualTo("a");
+        }
+        
+        @Test
+        void testDashToCamelCaseWithEndDashes() throws Exception {
+            assertThat(dashToCamelCase("a----")).isEqualTo("a");
+        }
+        
+        @Test
+        void testDashToCamelCaseWithSeceralDashesGrouped() throws Exception {
+            assertThat(dashToCamelCase("a--b")).isEqualTo("aB");
+        }
+    }
 
     @Test
     public void testSplitWords() throws Exception {