浏览代码

Merge pull request #3839 from nextcloud/simplified-string-utils

Minor refactor of StringUtils class. Added unit tests.
Andy Scherzinger 6 年之前
父节点
当前提交
30cad20f86

+ 27 - 16
src/main/java/com/owncloud/android/utils/StringUtils.java

@@ -20,12 +20,14 @@
 
 package com.owncloud.android.utils;
 
-import android.text.TextUtils;
 
+import java.util.Locale;
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
 
 import androidx.annotation.ColorInt;
+import androidx.annotation.NonNull;
+import androidx.annotation.Nullable;
 
 /**
  * Helper class for handling and manipulating strings.
@@ -36,25 +38,34 @@ public final class StringUtils {
         // prevent class from being constructed
     }
 
-    public static String searchAndColor(String text, String searchText, @ColorInt int color) {
+    public static @NonNull
+    String searchAndColor(@Nullable String text, @Nullable String searchText,
+                          @ColorInt int color) {
 
-        if (TextUtils.isEmpty(text) || TextUtils.isEmpty(searchText)) {
-            return text;
-        }
+        if (text != null) {
 
-        Matcher matcher = Pattern.compile(searchText, Pattern.CASE_INSENSITIVE | Pattern.LITERAL).matcher(text);
+            if (text.isEmpty() || searchText == null || searchText.isEmpty()) {
+                return text;
+            }
 
-        StringBuffer stringBuffer = new StringBuffer();
+            Matcher matcher = Pattern.compile(searchText,
+                                              Pattern.CASE_INSENSITIVE | Pattern.LITERAL).matcher(text);
 
-        while (matcher.find()) {
-            String replacement = matcher.group().replace(
-                matcher.group(),
-                "<font color='" + color + "'><b>" + matcher.group() + "</b></font>"
-            );
-            matcher.appendReplacement(stringBuffer, Matcher.quoteReplacement(replacement));
-        }
-        matcher.appendTail(stringBuffer);
+            StringBuffer stringBuffer = new StringBuffer();
 
-        return stringBuffer.toString();
+            while (matcher.find()) {
+                String replacement = matcher.group().replace(
+                    matcher.group(),
+                    String.format(Locale.getDefault(), "<font color='%d'><b>%s</b></font>", color,
+                                  matcher.group())
+                );
+                matcher.appendReplacement(stringBuffer, Matcher.quoteReplacement(replacement));
+            }
+            matcher.appendTail(stringBuffer);
+
+            return stringBuffer.toString();
+        } else {
+            return "";
+        }
     }
 }

+ 94 - 0
src/test/java/com/owncloud/android/utils/StringUtilsTest.java

@@ -0,0 +1,94 @@
+package com.owncloud.android.utils;
+
+import org.junit.Test;
+
+import static org.junit.Assert.assertEquals;
+
+public class StringUtilsTest {
+
+    @Test
+    public void assertCorrectSingleSearchMatch() {
+
+        String text = "this is a simple test";
+        String searchText = "simple";
+        int dummyColorInt = 44221;
+        String expectedReturn = String.format("this is a <font color='%d'><b>%s</b></font> test",
+                                              dummyColorInt, searchText);
+
+        assertEquals("returned parsed text value was incorrect",
+                     expectedReturn, StringUtils.searchAndColor(text, searchText, dummyColorInt));
+    }
+
+    @Test
+    public void assertTextReturnedIfSearchTextIsEmpty() {
+        String helloWorld = "hello world";
+        assertEquals("text returned when searchText was not empty",
+                     helloWorld, StringUtils.searchAndColor(helloWorld, "", 0));
+    }
+
+    @Test
+    public void searchStringNull() {
+        String text = "this is a simple test";
+        String searchText = null;
+        int dummyColorInt = 44221;
+        String expectedReturn = "this is a simple test";
+
+        assertEquals("returned parsed text value was incorrect",
+                     expectedReturn, StringUtils.searchAndColor(text, searchText, dummyColorInt));
+    }
+
+    @Test
+    public void searchStringEmpty() {
+        String text = "this is a simple test";
+        String searchText = "";
+        int dummyColorInt = 44221;
+        String expectedReturn = "this is a simple test";
+
+        assertEquals("returned parsed text value was incorrect",
+                     expectedReturn, StringUtils.searchAndColor(text, searchText, dummyColorInt));
+    }
+
+    @Test
+    public void textNull() {
+        String text = null;
+        String searchText = "test";
+        int dummyColorInt = 44221;
+        String expectedReturn = "";
+
+        assertEquals("returned parsed text value was incorrect",
+                     expectedReturn, StringUtils.searchAndColor(text, searchText, dummyColorInt));
+    }
+
+    @Test
+    public void textEmpty() {
+        String text = "";
+        String searchText = "test";
+        int dummyColorInt = 44221;
+        String expectedReturn = "";
+
+        assertEquals("returned parsed text value was incorrect",
+                     expectedReturn, StringUtils.searchAndColor(text, searchText, dummyColorInt));
+    }
+
+    @Test
+    public void bothNull() {
+        String text = null;
+        String searchText = null;
+        int dummyColorInt = 44221;
+        String expectedReturn = "";
+
+        assertEquals("returned parsed text value was incorrect",
+                     expectedReturn, StringUtils.searchAndColor(text, searchText, dummyColorInt));
+    }
+
+    @Test
+    public void bothEmpty() {
+        String text = "";
+        String searchText = "";
+        int dummyColorInt = 44221;
+        String expectedReturn = "";
+
+        assertEquals("returned parsed text value was incorrect",
+                     expectedReturn, StringUtils.searchAndColor(text, searchText, dummyColorInt));
+    }
+}