You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by ac...@apache.org on 2016/10/28 07:02:17 UTC
camel git commit: CAMEL-10389: Moved string related function from
ObjectHelper to StringHelper
Repository: camel
Updated Branches:
refs/heads/master 4db001143 -> 4f9448d83
CAMEL-10389: Moved string related function from ObjectHelper to StringHelper
Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/4f9448d8
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/4f9448d8
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/4f9448d8
Branch: refs/heads/master
Commit: 4f9448d83cc21a348f92cca961907b0f72d9db79
Parents: 4db0011
Author: aldettinger <al...@gmail.com>
Authored: Thu Oct 27 15:48:53 2016 +0200
Committer: Andrea Cosentino <an...@gmail.com>
Committed: Fri Oct 28 08:27:06 2016 +0200
----------------------------------------------------------------------
.../org/apache/camel/util/ObjectHelper.java | 245 +++++----------
.../org/apache/camel/util/StringHelper.java | 306 +++++++++++++++++++
.../org/apache/camel/util/ObjectHelperTest.java | 60 ----
.../org/apache/camel/util/StringHelperTest.java | 59 ++++
4 files changed, 445 insertions(+), 225 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/camel/blob/4f9448d8/camel-core/src/main/java/org/apache/camel/util/ObjectHelper.java
----------------------------------------------------------------------
diff --git a/camel-core/src/main/java/org/apache/camel/util/ObjectHelper.java b/camel-core/src/main/java/org/apache/camel/util/ObjectHelper.java
index 5f852c7..dcfbf87 100644
--- a/camel-core/src/main/java/org/apache/camel/util/ObjectHelper.java
+++ b/camel-core/src/main/java/org/apache/camel/util/ObjectHelper.java
@@ -335,36 +335,32 @@ public final class ObjectHelper {
/**
* Asserts whether the string is <b>not</b> empty.
*
- * @param value the string to test
- * @param name the key that resolved the value
+ * @param value the string to test
+ * @param name the key that resolved the value
* @return the passed {@code value} as is
* @throws IllegalArgumentException is thrown if assertion fails
+ * @deprecated use {@link StringHelper#notEmpty(String, String)} instead
*/
+ @Deprecated
public static String notEmpty(String value, String name) {
- if (isEmpty(value)) {
- throw new IllegalArgumentException(name + " must be specified and not empty");
- }
-
- return value;
+ return StringHelper.notEmpty(value, name);
}
/**
* Asserts whether the string is <b>not</b> empty.
*
- * @param value the string to test
- * @param on additional description to indicate where this problem occurred (appended as toString())
- * @param name the key that resolved the value
+ * @param value the string to test
+ * @param on additional description to indicate where this problem occurred
+ * (appended as toString())
+ * @param name the key that resolved the value
* @return the passed {@code value} as is
* @throws IllegalArgumentException is thrown if assertion fails
+ * @deprecated use {@link StringHelper#notEmpty(String, String, Object)}
+ * instead
*/
+ @Deprecated
public static String notEmpty(String value, String name, Object on) {
- if (on == null) {
- notNull(value, name);
- } else if (isEmpty(value)) {
- throw new IllegalArgumentException(name + " must be specified and not empty on: " + on);
- }
-
- return value;
+ return StringHelper.notEmpty(value, name, on);
}
/**
@@ -394,19 +390,13 @@ public final class ObjectHelper {
}
}
+ /**
+ * @deprecated use
+ * {@link StringHelper#splitOnCharacter(String, String, int)} instead
+ */
+ @Deprecated
public static String[] splitOnCharacter(String value, String needle, int count) {
- String rc[] = new String[count];
- rc[0] = value;
- for (int i = 1; i < count; i++) {
- String v = rc[i - 1];
- int p = v.indexOf(needle);
- if (p < 0) {
- return rc;
- }
- rc[i - 1] = v.substring(0, p);
- rc[i] = v.substring(p + 1);
- }
- return rc;
+ return StringHelper.splitOnCharacter(value, needle, count);
}
/**
@@ -416,31 +406,19 @@ public final class ObjectHelper {
* @param text the string
* @param ch the initial characters to remove
* @return either the original string or the new substring
+ * @deprecated use {@link StringHelper#removeStartingCharacters(String, char)} instead
*/
+ @Deprecated
public static String removeStartingCharacters(String text, char ch) {
- int idx = 0;
- while (text.charAt(idx) == ch) {
- idx++;
- }
- if (idx > 0) {
- return text.substring(idx);
- }
- return text;
+ return StringHelper.removeStartingCharacters(text, ch);
}
+ /**
+ * @deprecated use {@link StringHelper#capitalize(String)} instead
+ */
+ @Deprecated
public static String capitalize(String text) {
- if (text == null) {
- return null;
- }
- int length = text.length();
- if (length == 0) {
- return text;
- }
- String answer = text.substring(0, 1).toUpperCase(Locale.ENGLISH);
- if (length > 1) {
- answer += text.substring(1, length);
- }
- return answer;
+ return StringHelper.capitalize(text);
}
/**
@@ -449,29 +427,29 @@ public final class ObjectHelper {
* @param text the text
* @param after the token
* @return the text after the token, or <tt>null</tt> if text does not contain the token
+ * @deprecated use {@link StringHelper#after(String, String)} instead
*/
+ @Deprecated
public static String after(String text, String after) {
- if (!text.contains(after)) {
- return null;
- }
- return text.substring(text.indexOf(after) + after.length());
+ return StringHelper.after(text, after);
}
/**
* Returns an object after the given token
*
- * @param text the text
+ * @param text the text
* @param after the token
- * @param mapper a mapping function to convert the string after the token to type T
- * @return an Optional describing the result of applying a mapping function to the text after the token.
+ * @param mapper a mapping function to convert the string after the token to
+ * type T
+ * @return an Optional describing the result of applying a mapping function
+ * to the text after the token.
+ * @deprecated use {@link StringHelper#after(String, String, Function)
+ * StringHelper.after(String, String, Function<String,T>)}
+ * instead
*/
+ @Deprecated
public static <T> Optional<T> after(String text, String after, Function<String, T> mapper) {
- String result = after(text, after);
- if (result == null) {
- return Optional.empty();
- } else {
- return Optional.ofNullable(mapper.apply(result));
- }
+ return StringHelper.after(text, after, mapper);
}
/**
@@ -480,32 +458,31 @@ public final class ObjectHelper {
* @param text the text
* @param before the token
* @return the text before the token, or <tt>null</tt> if text does not contain the token
+ * @deprecated use {@link StringHelper#before(String, String)} instead
*/
+ @Deprecated
public static String before(String text, String before) {
- if (!text.contains(before)) {
- return null;
- }
- return text.substring(0, text.indexOf(before));
+ return StringHelper.before(text, before);
}
/**
* Returns an object before the given token
*
- * @param text the text
+ * @param text the text
* @param before the token
- * @param mapper a mapping function to convert the string before the token to type T
- * @return an Optional describing the result of applying a mapping function to the text before the token.
+ * @param mapper a mapping function to convert the string before the token
+ * to type T
+ * @return an Optional describing the result of applying a mapping function
+ * to the text before the token.
+ * @deprecated use {@link StringHelper#before(String, String, Function)
+ * StringHelper.before(String, String, Function<String,T>)}
+ * instead
*/
+ @Deprecated
public static <T> Optional<T> before(String text, String before, Function<String, T> mapper) {
- String result = before(text, before);
- if (result == null) {
- return Optional.empty();
- } else {
- return Optional.ofNullable(mapper.apply(result));
- }
+ return StringHelper.before(text, before, mapper);
}
-
/**
* Returns the string between the given tokens
*
@@ -513,13 +490,11 @@ public final class ObjectHelper {
* @param after the before token
* @param before the after token
* @return the text between the tokens, or <tt>null</tt> if text does not contain the tokens
+ * @deprecated use {@link StringHelper#between(String, String, String)} instead
*/
+ @Deprecated
public static String between(String text, String after, String before) {
- text = after(text, after);
- if (text == null) {
- return null;
- }
- return before(text, before);
+ return StringHelper.between(text, after, before);
}
/**
@@ -530,14 +505,13 @@ public final class ObjectHelper {
* @param before the after token
* @param mapper a mapping function to convert the string between the token to type T
* @return an Optional describing the result of applying a mapping function to the text between the token.
+ * @deprecated use {@link StringHelper#between(String, String, String, Function)
+ * StringHelper.between(String, String, String, Function<String,T>)}
+ * instead
*/
+ @Deprecated
public static <T> Optional<T> between(String text, String after, String before, Function<String, T> mapper) {
- String result = between(text, after, before);
- if (result == null) {
- return Optional.empty();
- } else {
- return Optional.ofNullable(mapper.apply(result));
- }
+ return StringHelper.between(text, after, before, mapper);
}
/**
@@ -554,53 +528,11 @@ public final class ObjectHelper {
* @param after the before token
* @param before the after token
* @return the text between the outer most tokens, or <tt>null</tt> if text does not contain the tokens
+ * @deprecated use {@link StringHelper#betweenOuterPair(String, char, char)} instead
*/
+ @Deprecated
public static String betweenOuterPair(String text, char before, char after) {
- if (text == null) {
- return null;
- }
-
- int pos = -1;
- int pos2 = -1;
- int count = 0;
- int count2 = 0;
-
- boolean singleQuoted = false;
- boolean doubleQuoted = false;
- for (int i = 0; i < text.length(); i++) {
- char ch = text.charAt(i);
- if (!doubleQuoted && ch == '\'') {
- singleQuoted = !singleQuoted;
- } else if (!singleQuoted && ch == '\"') {
- doubleQuoted = !doubleQuoted;
- }
- if (singleQuoted || doubleQuoted) {
- continue;
- }
-
- if (ch == before) {
- count++;
- } else if (ch == after) {
- count2++;
- }
-
- if (ch == before && pos == -1) {
- pos = i;
- } else if (ch == after) {
- pos2 = i;
- }
- }
-
- if (pos == -1 || pos2 == -1) {
- return null;
- }
-
- // must be even paris
- if (count != count2) {
- return null;
- }
-
- return text.substring(pos + 1, pos2);
+ return StringHelper.betweenOuterPair(text, before, after);
}
/**
@@ -611,16 +543,15 @@ public final class ObjectHelper {
* @param before the after token
* @param mapper a mapping function to convert the string between the most outer pair of tokens to type T
* @return an Optional describing the result of applying a mapping function to the text between the most outer pair of tokens.
+ * @deprecated use {@link StringHelper#betweenOuterPair(String, char, char, Function)
+ * StringHelper.betweenOuterPair(String, char, char, Function<String,T>)}
+ * instead
*/
+ @Deprecated
public static <T> Optional<T> betweenOuterPair(String text, char before, char after, Function<String, T> mapper) {
- String result = betweenOuterPair(text, before, after);
- if (result == null) {
- return Optional.empty();
- } else {
- return Optional.ofNullable(mapper.apply(result));
- }
+ return StringHelper.betweenOuterPair(text, before, after, mapper);
}
-
+
/**
* Returns true if the collection contains the specified value
*/
@@ -1693,24 +1624,11 @@ public final class ObjectHelper {
/**
* Returns true if the given name is a valid java identifier
+ * @deprecated use {@link StringHelper#isJavaIdentifier(String)} instead
*/
+ @Deprecated
public static boolean isJavaIdentifier(String name) {
- if (name == null) {
- return false;
- }
- int size = name.length();
- if (size < 1) {
- return false;
- }
- if (Character.isJavaIdentifierStart(name.charAt(0))) {
- for (int i = 1; i < size; i++) {
- if (!Character.isJavaIdentifierPart(name.charAt(i))) {
- return false;
- }
- }
- return true;
- }
- return false;
+ return StringHelper.isJavaIdentifier(name);
}
/**
@@ -1781,22 +1699,19 @@ public final class ObjectHelper {
}
/**
- * Cleans the string to a pure Java identifier so we can use it for loading class names.
+ * Cleans the string to a pure Java identifier so we can use it for loading
+ * class names.
* <p/>
- * Especially from Spring DSL people can have \n \t or other characters that otherwise
- * would result in ClassNotFoundException
+ * Especially from Spring DSL people can have \n \t or other characters that
+ * otherwise would result in ClassNotFoundException
*
* @param name the class name
* @return normalized classname that can be load by a class loader.
+ * @deprecated use {@link StringHelper#normalizeClassName(String)} instead
*/
+ @Deprecated
public static String normalizeClassName(String name) {
- StringBuilder sb = new StringBuilder(name.length());
- for (char ch : name.toCharArray()) {
- if (ch == '.' || ch == '[' || ch == ']' || ch == '-' || Character.isJavaIdentifierPart(ch)) {
- sb.append(ch);
- }
- }
- return sb.toString();
+ return StringHelper.normalizeClassName(name);
}
/**
http://git-wip-us.apache.org/repos/asf/camel/blob/4f9448d8/camel-core/src/main/java/org/apache/camel/util/StringHelper.java
----------------------------------------------------------------------
diff --git a/camel-core/src/main/java/org/apache/camel/util/StringHelper.java b/camel-core/src/main/java/org/apache/camel/util/StringHelper.java
index 1fa6852..6e0f3de 100644
--- a/camel-core/src/main/java/org/apache/camel/util/StringHelper.java
+++ b/camel-core/src/main/java/org/apache/camel/util/StringHelper.java
@@ -16,6 +16,10 @@
*/
package org.apache.camel.util;
+import java.util.Locale;
+import java.util.Optional;
+import java.util.function.Function;
+
import static org.apache.camel.util.StringQuoteHelper.doubleQuote;
/**
@@ -269,4 +273,306 @@ public final class StringHelper {
}
}
+ /**
+ * Asserts whether the string is <b>not</b> empty.
+ *
+ * @param value the string to test
+ * @param name the key that resolved the value
+ * @return the passed {@code value} as is
+ * @throws IllegalArgumentException is thrown if assertion fails
+ */
+ public static String notEmpty(String value, String name) {
+ if (ObjectHelper.isEmpty(value)) {
+ throw new IllegalArgumentException(name + " must be specified and not empty");
+ }
+
+ return value;
+ }
+
+ /**
+ * Asserts whether the string is <b>not</b> empty.
+ *
+ * @param value the string to test
+ * @param on additional description to indicate where this problem occurred (appended as toString())
+ * @param name the key that resolved the value
+ * @return the passed {@code value} as is
+ * @throws IllegalArgumentException is thrown if assertion fails
+ */
+ public static String notEmpty(String value, String name, Object on) {
+ if (on == null) {
+ ObjectHelper.notNull(value, name);
+ } else if (ObjectHelper.isEmpty(value)) {
+ throw new IllegalArgumentException(name + " must be specified and not empty on: " + on);
+ }
+
+ return value;
+ }
+
+ public static String[] splitOnCharacter(String value, String needle, int count) {
+ String rc[] = new String[count];
+ rc[0] = value;
+ for (int i = 1; i < count; i++) {
+ String v = rc[i - 1];
+ int p = v.indexOf(needle);
+ if (p < 0) {
+ return rc;
+ }
+ rc[i - 1] = v.substring(0, p);
+ rc[i] = v.substring(p + 1);
+ }
+ return rc;
+ }
+
+ /**
+ * Removes any starting characters on the given text which match the given
+ * character
+ *
+ * @param text the string
+ * @param ch the initial characters to remove
+ * @return either the original string or the new substring
+ */
+ public static String removeStartingCharacters(String text, char ch) {
+ int idx = 0;
+ while (text.charAt(idx) == ch) {
+ idx++;
+ }
+ if (idx > 0) {
+ return text.substring(idx);
+ }
+ return text;
+ }
+
+ public static String capitalize(String text) {
+ if (text == null) {
+ return null;
+ }
+ int length = text.length();
+ if (length == 0) {
+ return text;
+ }
+ String answer = text.substring(0, 1).toUpperCase(Locale.ENGLISH);
+ if (length > 1) {
+ answer += text.substring(1, length);
+ }
+ return answer;
+ }
+
+ /**
+ * Returns the string after the given token
+ *
+ * @param text the text
+ * @param after the token
+ * @return the text after the token, or <tt>null</tt> if text does not contain the token
+ */
+ public static String after(String text, String after) {
+ if (!text.contains(after)) {
+ return null;
+ }
+ return text.substring(text.indexOf(after) + after.length());
+ }
+
+ /**
+ * Returns an object after the given token
+ *
+ * @param text the text
+ * @param after the token
+ * @param mapper a mapping function to convert the string after the token to type T
+ * @return an Optional describing the result of applying a mapping function to the text after the token.
+ */
+ public static <T> Optional<T> after(String text, String after, Function<String, T> mapper) {
+ String result = after(text, after);
+ if (result == null) {
+ return Optional.empty();
+ } else {
+ return Optional.ofNullable(mapper.apply(result));
+ }
+ }
+
+ /**
+ * Returns the string before the given token
+ *
+ * @param text the text
+ * @param before the token
+ * @return the text before the token, or <tt>null</tt> if text does not
+ * contain the token
+ */
+ public static String before(String text, String before) {
+ if (!text.contains(before)) {
+ return null;
+ }
+ return text.substring(0, text.indexOf(before));
+ }
+
+ /**
+ * Returns an object before the given token
+ *
+ * @param text the text
+ * @param before the token
+ * @param mapper a mapping function to convert the string before the token to type T
+ * @return an Optional describing the result of applying a mapping function to the text before the token.
+ */
+ public static <T> Optional<T> before(String text, String before, Function<String, T> mapper) {
+ String result = before(text, before);
+ if (result == null) {
+ return Optional.empty();
+ } else {
+ return Optional.ofNullable(mapper.apply(result));
+ }
+ }
+
+ /**
+ * Returns the string between the given tokens
+ *
+ * @param text the text
+ * @param after the before token
+ * @param before the after token
+ * @return the text between the tokens, or <tt>null</tt> if text does not contain the tokens
+ */
+ public static String between(String text, String after, String before) {
+ text = after(text, after);
+ if (text == null) {
+ return null;
+ }
+ return before(text, before);
+ }
+
+ /**
+ * Returns an object between the given token
+ *
+ * @param text the text
+ * @param after the before token
+ * @param before the after token
+ * @param mapper a mapping function to convert the string between the token to type T
+ * @return an Optional describing the result of applying a mapping function to the text between the token.
+ */
+ public static <T> Optional<T> between(String text, String after, String before, Function<String, T> mapper) {
+ String result = between(text, after, before);
+ if (result == null) {
+ return Optional.empty();
+ } else {
+ return Optional.ofNullable(mapper.apply(result));
+ }
+ }
+
+ /**
+ * Returns the string between the most outer pair of tokens
+ * <p/>
+ * The number of token pairs must be evenly, eg there must be same number of before and after tokens, otherwise <tt>null</tt> is returned
+ * <p/>
+ * This implementation skips matching when the text is either single or double quoted.
+ * For example:
+ * <tt>${body.matches("foo('bar')")</tt>
+ * Will not match the parenthesis from the quoted text.
+ *
+ * @param text the text
+ * @param after the before token
+ * @param before the after token
+ * @return the text between the outer most tokens, or <tt>null</tt> if text does not contain the tokens
+ */
+ public static String betweenOuterPair(String text, char before, char after) {
+ if (text == null) {
+ return null;
+ }
+
+ int pos = -1;
+ int pos2 = -1;
+ int count = 0;
+ int count2 = 0;
+
+ boolean singleQuoted = false;
+ boolean doubleQuoted = false;
+ for (int i = 0; i < text.length(); i++) {
+ char ch = text.charAt(i);
+ if (!doubleQuoted && ch == '\'') {
+ singleQuoted = !singleQuoted;
+ } else if (!singleQuoted && ch == '\"') {
+ doubleQuoted = !doubleQuoted;
+ }
+ if (singleQuoted || doubleQuoted) {
+ continue;
+ }
+
+ if (ch == before) {
+ count++;
+ } else if (ch == after) {
+ count2++;
+ }
+
+ if (ch == before && pos == -1) {
+ pos = i;
+ } else if (ch == after) {
+ pos2 = i;
+ }
+ }
+
+ if (pos == -1 || pos2 == -1) {
+ return null;
+ }
+
+ // must be even paris
+ if (count != count2) {
+ return null;
+ }
+
+ return text.substring(pos + 1, pos2);
+ }
+
+ /**
+ * Returns an object between the most outer pair of tokens
+ *
+ * @param text the text
+ * @param after the before token
+ * @param before the after token
+ * @param mapper a mapping function to convert the string between the most outer pair of tokens to type T
+ * @return an Optional describing the result of applying a mapping function to the text between the most outer pair of tokens.
+ */
+ public static <T> Optional<T> betweenOuterPair(String text, char before, char after, Function<String, T> mapper) {
+ String result = betweenOuterPair(text, before, after);
+ if (result == null) {
+ return Optional.empty();
+ } else {
+ return Optional.ofNullable(mapper.apply(result));
+ }
+ }
+
+ /**
+ * Returns true if the given name is a valid java identifier
+ */
+ public static boolean isJavaIdentifier(String name) {
+ if (name == null) {
+ return false;
+ }
+ int size = name.length();
+ if (size < 1) {
+ return false;
+ }
+ if (Character.isJavaIdentifierStart(name.charAt(0))) {
+ for (int i = 1; i < size; i++) {
+ if (!Character.isJavaIdentifierPart(name.charAt(i))) {
+ return false;
+ }
+ }
+ return true;
+ }
+ return false;
+ }
+
+ /**
+ * Cleans the string to a pure Java identifier so we can use it for loading class names.
+ * <p/>
+ * Especially from Spring DSL people can have \n \t or other characters that otherwise
+ * would result in ClassNotFoundException
+ *
+ * @param name the class name
+ * @return normalized classname that can be load by a class loader.
+ */
+ public static String normalizeClassName(String name) {
+ StringBuilder sb = new StringBuilder(name.length());
+ for (char ch : name.toCharArray()) {
+ if (ch == '.' || ch == '[' || ch == ']' || ch == '-' || Character.isJavaIdentifierPart(ch)) {
+ sb.append(ch);
+ }
+ }
+ return sb.toString();
+ }
}
http://git-wip-us.apache.org/repos/asf/camel/blob/4f9448d8/camel-core/src/test/java/org/apache/camel/util/ObjectHelperTest.java
----------------------------------------------------------------------
diff --git a/camel-core/src/test/java/org/apache/camel/util/ObjectHelperTest.java b/camel-core/src/test/java/org/apache/camel/util/ObjectHelperTest.java
index dcb7432..2efa01e 100644
--- a/camel-core/src/test/java/org/apache/camel/util/ObjectHelperTest.java
+++ b/camel-core/src/test/java/org/apache/camel/util/ObjectHelperTest.java
@@ -28,7 +28,6 @@ import java.util.List;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.Properties;
-import java.util.function.Function;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
@@ -65,12 +64,6 @@ public class ObjectHelperTest extends TestCase {
assertNotNull("Cannot load resource with leading \"/\"", url2);
}
- public void testRemoveInitialCharacters() throws Exception {
- assertEquals(ObjectHelper.removeStartingCharacters("foo", '/'), "foo");
- assertEquals(ObjectHelper.removeStartingCharacters("/foo", '/'), "foo");
- assertEquals(ObjectHelper.removeStartingCharacters("//foo", '/'), "foo");
- }
-
public void testGetPropertyName() throws Exception {
Method method = getClass().getMethod("setCheese", String.class);
assertNotNull("should have found a method!", method);
@@ -678,52 +671,6 @@ public class ObjectHelperTest extends TestCase {
assertFalse(ObjectHelper.isPrimitiveArrayType(null));
}
- public void testBefore() {
- assertEquals("Hello ", ObjectHelper.before("Hello World", "World"));
- assertEquals("Hello ", ObjectHelper.before("Hello World Again", "World"));
- assertEquals(null, ObjectHelper.before("Hello Again", "Foo"));
-
- assertTrue(ObjectHelper.before("mykey:ignore", ":", "mykey"::equals).orElse(false));
- assertFalse(ObjectHelper.before("ignore:ignore", ":", "mykey"::equals).orElse(false));
- }
-
- public void testAfter() {
- assertEquals(" World", ObjectHelper.after("Hello World", "Hello"));
- assertEquals(" World Again", ObjectHelper.after("Hello World Again", "Hello"));
- assertEquals(null, ObjectHelper.after("Hello Again", "Foo"));
-
- assertTrue(ObjectHelper.after("ignore:mykey", ":", "mykey"::equals).orElse(false));
- assertFalse(ObjectHelper.after("ignore:ignore", ":", "mykey"::equals).orElse(false));
- }
-
- public void testBetween() {
- assertEquals("foo bar", ObjectHelper.between("Hello 'foo bar' how are you", "'", "'"));
- assertEquals("foo bar", ObjectHelper.between("Hello ${foo bar} how are you", "${", "}"));
- assertEquals(null, ObjectHelper.between("Hello ${foo bar} how are you", "'", "'"));
-
- assertTrue(ObjectHelper.between("begin:mykey:end", "begin:", ":end", "mykey"::equals).orElse(false));
- assertFalse(ObjectHelper.between("begin:ignore:end", "begin:", ":end", "mykey"::equals).orElse(false));
- }
-
- public void testBetweenOuterPair() {
- assertEquals("bar(baz)123", ObjectHelper.betweenOuterPair("foo(bar(baz)123)", '(', ')'));
- assertEquals(null, ObjectHelper.betweenOuterPair("foo(bar(baz)123))", '(', ')'));
- assertEquals(null, ObjectHelper.betweenOuterPair("foo(bar(baz123", '(', ')'));
- assertEquals(null, ObjectHelper.betweenOuterPair("foo)bar)baz123", '(', ')'));
- assertEquals("bar", ObjectHelper.betweenOuterPair("foo(bar)baz123", '(', ')'));
- assertEquals("'bar', 'baz()123', 123", ObjectHelper.betweenOuterPair("foo('bar', 'baz()123', 123)", '(', ')'));
-
- assertTrue(ObjectHelper.betweenOuterPair("foo(bar)baz123", '(', ')', "bar"::equals).orElse(false));
- assertFalse(ObjectHelper.betweenOuterPair("foo[bar)baz123", '(', ')', "bar"::equals).orElse(false));
- }
-
- public void testIsJavaIdentifier() {
- assertEquals(true, ObjectHelper.isJavaIdentifier("foo"));
- assertEquals(false, ObjectHelper.isJavaIdentifier("foo.bar"));
- assertEquals(false, ObjectHelper.isJavaIdentifier(""));
- assertEquals(false, ObjectHelper.isJavaIdentifier(null));
- }
-
public void testGetDefaultCharSet() {
assertNotNull(ObjectHelper.getDefaultCharacterSet());
}
@@ -858,13 +805,6 @@ public class ObjectHelperTest extends TestCase {
}
}
- public void testNormalizeClassName() {
- assertEquals("Should get the right class name", "my.package-info", ObjectHelper.normalizeClassName("my.package-info"));
- assertEquals("Should get the right class name", "Integer[]", ObjectHelper.normalizeClassName("Integer[] \r"));
- assertEquals("Should get the right class name", "Hello_World", ObjectHelper.normalizeClassName("Hello_World"));
- assertEquals("Should get the right class name", "", ObjectHelper.normalizeClassName("////"));
- }
-
public void testLookupConstantFieldValue() {
assertEquals("CamelFileName", ObjectHelper.lookupConstantFieldValue(Exchange.class, "FILE_NAME"));
assertEquals(null, ObjectHelper.lookupConstantFieldValue(Exchange.class, "XXX"));
http://git-wip-us.apache.org/repos/asf/camel/blob/4f9448d8/camel-core/src/test/java/org/apache/camel/util/StringHelperTest.java
----------------------------------------------------------------------
diff --git a/camel-core/src/test/java/org/apache/camel/util/StringHelperTest.java b/camel-core/src/test/java/org/apache/camel/util/StringHelperTest.java
index 23cb1ac..d9a4631 100644
--- a/camel-core/src/test/java/org/apache/camel/util/StringHelperTest.java
+++ b/camel-core/src/test/java/org/apache/camel/util/StringHelperTest.java
@@ -147,4 +147,63 @@ public class StringHelperTest extends TestCase {
assertEquals("fooDOT", StringHelper.replaceAll("foo.", ".", "DOT"));
}
+ public void testRemoveInitialCharacters() throws Exception {
+ assertEquals(StringHelper.removeStartingCharacters("foo", '/'), "foo");
+ assertEquals(StringHelper.removeStartingCharacters("/foo", '/'), "foo");
+ assertEquals(StringHelper.removeStartingCharacters("//foo", '/'), "foo");
+ }
+
+ public void testBefore() {
+ assertEquals("Hello ", StringHelper.before("Hello World", "World"));
+ assertEquals("Hello ", StringHelper.before("Hello World Again", "World"));
+ assertEquals(null, StringHelper.before("Hello Again", "Foo"));
+
+ assertTrue(StringHelper.before("mykey:ignore", ":", "mykey"::equals).orElse(false));
+ assertFalse(StringHelper.before("ignore:ignore", ":", "mykey"::equals).orElse(false));
+ }
+
+ public void testAfter() {
+ assertEquals(" World", StringHelper.after("Hello World", "Hello"));
+ assertEquals(" World Again", StringHelper.after("Hello World Again", "Hello"));
+ assertEquals(null, StringHelper.after("Hello Again", "Foo"));
+
+ assertTrue(StringHelper.after("ignore:mykey", ":", "mykey"::equals).orElse(false));
+ assertFalse(StringHelper.after("ignore:ignore", ":", "mykey"::equals).orElse(false));
+ }
+
+ public void testBetween() {
+ assertEquals("foo bar", StringHelper.between("Hello 'foo bar' how are you", "'", "'"));
+ assertEquals("foo bar", StringHelper.between("Hello ${foo bar} how are you", "${", "}"));
+ assertEquals(null, StringHelper.between("Hello ${foo bar} how are you", "'", "'"));
+
+ assertTrue(StringHelper.between("begin:mykey:end", "begin:", ":end", "mykey"::equals).orElse(false));
+ assertFalse(StringHelper.between("begin:ignore:end", "begin:", ":end", "mykey"::equals).orElse(false));
+ }
+
+ public void testBetweenOuterPair() {
+ assertEquals("bar(baz)123", StringHelper.betweenOuterPair("foo(bar(baz)123)", '(', ')'));
+ assertEquals(null, StringHelper.betweenOuterPair("foo(bar(baz)123))", '(', ')'));
+ assertEquals(null, StringHelper.betweenOuterPair("foo(bar(baz123", '(', ')'));
+ assertEquals(null, StringHelper.betweenOuterPair("foo)bar)baz123", '(', ')'));
+ assertEquals("bar", StringHelper.betweenOuterPair("foo(bar)baz123", '(', ')'));
+ assertEquals("'bar', 'baz()123', 123", StringHelper.betweenOuterPair("foo('bar', 'baz()123', 123)", '(', ')'));
+
+ assertTrue(StringHelper.betweenOuterPair("foo(bar)baz123", '(', ')', "bar"::equals).orElse(false));
+ assertFalse(StringHelper.betweenOuterPair("foo[bar)baz123", '(', ')', "bar"::equals).orElse(false));
+ }
+
+ public void testIsJavaIdentifier() {
+ assertEquals(true, StringHelper.isJavaIdentifier("foo"));
+ assertEquals(false, StringHelper.isJavaIdentifier("foo.bar"));
+ assertEquals(false, StringHelper.isJavaIdentifier(""));
+ assertEquals(false, StringHelper.isJavaIdentifier(null));
+ }
+
+ public void testNormalizeClassName() {
+ assertEquals("Should get the right class name", "my.package-info", StringHelper.normalizeClassName("my.package-info"));
+ assertEquals("Should get the right class name", "Integer[]", StringHelper.normalizeClassName("Integer[] \r"));
+ assertEquals("Should get the right class name", "Hello_World", StringHelper.normalizeClassName("Hello_World"));
+ assertEquals("Should get the right class name", "", StringHelper.normalizeClassName("////"));
+ }
+
}