camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From zregv...@apache.org
Subject [camel] 02/03: Additional URISupport methods
Date Sat, 06 Oct 2018 11:01:01 GMT
This is an automated email from the ASF dual-hosted git repository.

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

commit a4a79f912406c626793fe415dd710f58177eb977
Author: Zoran Regvart <zregvart@apache.org>
AuthorDate: Sat Oct 6 12:58:56 2018 +0200

    Additional URISupport methods
    
    This adds `stripSuffix` and `joinPaths` methods to URISupport, seemingly
    also a common pattern of utility methods that we can take advantage of
    doing only once.
    
    Also refactors `stripPrefix` for additional `null` safety in case
    `prefix` is `null`.
---
 .../java/org/apache/camel/util/URISupport.java     | 60 +++++++++++++++++++++-
 .../java/org/apache/camel/util/URISupportTest.java | 45 ++++++++++++++++
 2 files changed, 104 insertions(+), 1 deletion(-)

diff --git a/camel-core/src/main/java/org/apache/camel/util/URISupport.java b/camel-core/src/main/java/org/apache/camel/util/URISupport.java
index 3d4bd42..96f2cfa 100644
--- a/camel-core/src/main/java/org/apache/camel/util/URISupport.java
+++ b/camel-core/src/main/java/org/apache/camel/util/URISupport.java
@@ -408,9 +408,35 @@ public final class URISupport {
      * @return the value without the prefix
      */
     public static String stripPrefix(String value, String prefix) {
-        if (value != null && value.startsWith(prefix)) {
+        if (value == null || prefix == null) {
+            return value;
+        }
+
+        if (value.startsWith(prefix)) {
             return value.substring(prefix.length());
         }
+
+        return value;
+    }
+
+    /**
+     * Strips the suffix from the value.
+     * <p/>
+     * Returns the value as-is if not ending with the prefix.
+     *
+     * @param value the value
+     * @param suffix the suffix to remove from value
+     * @return the value without the suffix
+     */
+    public static String stripSuffix(final String value, final String suffix) {
+        if (value == null || suffix == null) {
+            return value;
+        }
+
+        if (value.endsWith(suffix)) {
+            return value.substring(0, value.length() - suffix.length());
+        }
+
         return value;
     }
 
@@ -632,4 +658,36 @@ public final class URISupport {
 
         return pathAndQuery;
     }
+
+    public static String joinPaths(final String... paths) {
+        if (paths == null || paths.length == 0) {
+            return "";
+        }
+
+        final StringBuilder joined = new StringBuilder();
+
+        boolean addedLast = false;
+        for (int i = paths.length -1 ; i >= 0 ; i--) {
+            String path = paths[i];
+            if (ObjectHelper.isNotEmpty(path)) {
+                if (addedLast) {
+                    path = stripSuffix(path, "/");
+                }
+
+                addedLast = true;
+
+                if (path.charAt(0) == '/') {
+                    joined.insert(0, path);
+                } else {
+                    if (i > 0) {
+                        joined.insert(0, '/').insert(1, path);
+                    } else {
+                        joined.insert(0, path);
+                    }
+                }
+            }
+        }
+
+        return joined.toString();
+    }
 }
diff --git a/camel-core/src/test/java/org/apache/camel/util/URISupportTest.java b/camel-core/src/test/java/org/apache/camel/util/URISupportTest.java
index f9a642a..11bf67f 100644
--- a/camel-core/src/test/java/org/apache/camel/util/URISupportTest.java
+++ b/camel-core/src/test/java/org/apache/camel/util/URISupportTest.java
@@ -23,6 +23,7 @@ import java.util.LinkedHashMap;
 import java.util.Map;
 
 import org.apache.camel.ContextTestSupport;
+import org.assertj.core.api.Assertions;
 import org.junit.Test;
 
 /**
@@ -376,4 +377,48 @@ public class URISupportTest extends ContextTestSupport {
         assertEquals("/path/", URISupport.pathAndQueryOf(URI.create("http://localhost:80/path/")));
         assertEquals("/path?query=value", URISupport.pathAndQueryOf(URI.create("http://localhost:80/path?query=value")));
     }
+
+    @Test
+    public void shouldStripPrefixes() {
+        Assertions.assertThat(URISupport.stripPrefix(null, null)).isNull();
+        Assertions.assertThat(URISupport.stripPrefix("", null)).isEmpty();
+        Assertions.assertThat(URISupport.stripPrefix(null, "")).isNull();
+        Assertions.assertThat(URISupport.stripPrefix("", "")).isEmpty();
+        Assertions.assertThat(URISupport.stripPrefix("a", "b")).isEqualTo("a");
+        Assertions.assertThat(URISupport.stripPrefix("a", "a")).isEmpty();
+        Assertions.assertThat(URISupport.stripPrefix("ab", "b")).isEqualTo("ab");
+        Assertions.assertThat(URISupport.stripPrefix("a", "ab")).isEqualTo("a");
+    }
+
+    @Test
+    public void shouldStripSuffixes() {
+        Assertions.assertThat(URISupport.stripSuffix(null, null)).isNull();
+        Assertions.assertThat(URISupport.stripSuffix("", null)).isEmpty();
+        Assertions.assertThat(URISupport.stripSuffix(null, "")).isNull();
+        Assertions.assertThat(URISupport.stripSuffix("", "")).isEmpty();
+        Assertions.assertThat(URISupport.stripSuffix("a", "b")).isEqualTo("a");
+        Assertions.assertThat(URISupport.stripSuffix("a", "a")).isEmpty();
+        Assertions.assertThat(URISupport.stripSuffix("ab", "b")).isEqualTo("a");
+        Assertions.assertThat(URISupport.stripSuffix("a", "ab")).isEqualTo("a");
+    }
+
+    @Test
+    public void shouldJoinPaths() {
+        Assertions.assertThat(URISupport.joinPaths(null, null)).isEmpty();
+        Assertions.assertThat(URISupport.joinPaths("", null)).isEmpty();
+        Assertions.assertThat(URISupport.joinPaths(null, "")).isEmpty();
+        Assertions.assertThat(URISupport.joinPaths("", "")).isEmpty();
+        Assertions.assertThat(URISupport.joinPaths("a", "")).isEqualTo("a");
+        Assertions.assertThat(URISupport.joinPaths("a", "b")).isEqualTo("a/b");
+        Assertions.assertThat(URISupport.joinPaths("/a", "b")).isEqualTo("/a/b");
+        Assertions.assertThat(URISupport.joinPaths("/a", "b/")).isEqualTo("/a/b/");
+        Assertions.assertThat(URISupport.joinPaths("/a/", "b/")).isEqualTo("/a/b/");
+        Assertions.assertThat(URISupport.joinPaths("/a/", "/b/")).isEqualTo("/a/b/");
+        Assertions.assertThat(URISupport.joinPaths("a", "b", "c")).isEqualTo("a/b/c");
+        Assertions.assertThat(URISupport.joinPaths("a", null, "c")).isEqualTo("a/c");
+        Assertions.assertThat(URISupport.joinPaths("/a/", "/b", "c/", "/d/")).isEqualTo("/a/b/c/d/");
+        Assertions.assertThat(URISupport.joinPaths("/a/", "/b", "c/", null)).isEqualTo("/a/b/c/");
+        Assertions.assertThat(URISupport.joinPaths("/a/", null, null, null)).isEqualTo("/a/");
+        Assertions.assertThat(URISupport.joinPaths("a/", "/b", null, null)).isEqualTo("a/b");
+    }
 }
\ No newline at end of file


Mime
View raw message