You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by gg...@apache.org on 2019/09/11 12:54:24 UTC
[commons-lang] branch master updated: [LANG-1489] Add null-safe
APIs as StringUtils.toRoot[Lower|Upper]Case(String) #456
This is an automated email from the ASF dual-hosted git repository.
ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-lang.git
The following commit(s) were added to refs/heads/master by this push:
new ba1c22c [LANG-1489] Add null-safe APIs as StringUtils.toRoot[Lower|Upper]Case(String) #456
ba1c22c is described below
commit ba1c22ce8378d389a0ee267e2f75a17a4860c7ba
Author: Gary Gregory <ga...@users.noreply.github.com>
AuthorDate: Wed Sep 11 08:54:18 2019 -0400
[LANG-1489] Add null-safe APIs as StringUtils.toRoot[Lower|Upper]Case(String) #456
---
.../java/org/apache/commons/lang3/StringUtils.java | 22 ++++++++++++
.../org/apache/commons/lang3/StringUtilsTest.java | 41 ++++++++++++++++++++++
2 files changed, 63 insertions(+)
diff --git a/src/main/java/org/apache/commons/lang3/StringUtils.java b/src/main/java/org/apache/commons/lang3/StringUtils.java
index c0c4635..7741303 100644
--- a/src/main/java/org/apache/commons/lang3/StringUtils.java
+++ b/src/main/java/org/apache/commons/lang3/StringUtils.java
@@ -8891,6 +8891,28 @@ public class StringUtils {
}
/**
+ * Converts the given source String as a lower-case using the {@link Locale#ROOT} locale in a null-safe manner.
+ *
+ * @param source A source String or null.
+ * @return the given source String as a lower-case using the {@link Locale#ROOT} locale or null.
+ * @since 3.10
+ */
+ public static String toRootLowerCase(final String source) {
+ return source == null ? null : source.toLowerCase(Locale.ROOT);
+ }
+
+ /**
+ * Converts the given source String as a upper-case using the {@link Locale#ROOT} locale in a null-safe manner.
+ *
+ * @param source A source String or null.
+ * @return the given source String as a upper-case using the {@link Locale#ROOT} locale or null.
+ * @since 3.10
+ */
+ public static String toRootUpperCase(final String source) {
+ return source == null ? null : source.toUpperCase(Locale.ROOT);
+ }
+
+ /**
* Converts a <code>byte[]</code> to a String using the specified character encoding.
*
* @param bytes
diff --git a/src/test/java/org/apache/commons/lang3/StringUtilsTest.java b/src/test/java/org/apache/commons/lang3/StringUtilsTest.java
index b2e80e6..7063fc8 100644
--- a/src/test/java/org/apache/commons/lang3/StringUtilsTest.java
+++ b/src/test/java/org/apache/commons/lang3/StringUtilsTest.java
@@ -19,6 +19,7 @@ package org.apache.commons.lang3;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertSame;
@@ -3192,4 +3193,44 @@ public class StringUtilsTest {
assertEquals("ab/ab", StringUtils.wrapIfMissing("/", "ab"));
assertEquals("ab/ab", StringUtils.wrapIfMissing("ab/ab", "ab"));
}
+
+ @Test
+ public void testToRootLowerCase() {
+ assertEquals(null, StringUtils.toRootLowerCase(null));
+ assertEquals("a", StringUtils.toRootLowerCase("A"));
+ assertEquals("a", StringUtils.toRootLowerCase("a"));
+ final Locale TURKISH = Locale.forLanguageTag("tr");
+ // Sanity checks:
+ assertNotEquals("title", "TITLE".toLowerCase(TURKISH));
+ assertEquals("title", "TITLE".toLowerCase(Locale.ROOT));
+ assertEquals("title", StringUtils.toRootLowerCase("TITLE"));
+ // Make sure we are not using the default Locale:
+ Locale defaultLocales = Locale.getDefault();
+ try {
+ Locale.setDefault(TURKISH);
+ assertEquals("title", StringUtils.toRootLowerCase("TITLE"));
+ } finally {
+ Locale.setDefault(defaultLocales);
+ }
+ }
+
+ @Test
+ public void testToRootUpperCase() {
+ assertEquals(null, StringUtils.toRootUpperCase(null));
+ assertEquals("A", StringUtils.toRootUpperCase("a"));
+ assertEquals("A", StringUtils.toRootUpperCase("A"));
+ final Locale TURKISH = Locale.forLanguageTag("tr");
+ // Sanity checks:
+ assertNotEquals("TITLE", "title".toUpperCase(TURKISH));
+ assertEquals("TITLE", "title".toUpperCase(Locale.ROOT));
+ assertEquals("TITLE", StringUtils.toRootUpperCase("title"));
+ // Make sure we are not using the default Locale:
+ Locale defaultLocales = Locale.getDefault();
+ try {
+ Locale.setDefault(TURKISH);
+ assertEquals("TITLE", StringUtils.toRootUpperCase("title"));
+ } finally {
+ Locale.setDefault(defaultLocales);
+ }
+ }
}