Kaynağa Gözat

updated assertion failure text. return empty string if arguments are null

ardevd 6 yıl önce
ebeveyn
işleme
7d97b267af

+ 21 - 14
src/main/java/com/owncloud/android/utils/StringUtils.java

@@ -38,23 +38,30 @@ public final class StringUtils {
 
     public static String searchAndColor(String text, String searchText, @ColorInt int color) {
 
-        if (text.isEmpty() || searchText.isEmpty()) {
-            return text;
-        }
+        if (text != null && searchText != null) {
 
-        Matcher matcher = Pattern.compile(searchText, Pattern.CASE_INSENSITIVE | Pattern.LITERAL).matcher(text);
+            if (text.isEmpty() || 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(),
-                String.format(Locale.getDefault(), "<font color='%d'><b>%s</b></font>", color, matcher.group())
-            );
-            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 "";
+        }
     }
 }

+ 4 - 3
src/test/java/com/owncloud/android/utils/StringUtilsTest.java

@@ -12,16 +12,17 @@ public class StringUtilsTest {
         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);
+        String expectedReturn = String.format("this is a <font color='%d'><b>%s</b></font> test",
+                                              dummyColorInt, searchText);
 
-        assertEquals("correctly parsed text returned",
+        assertEquals("returned parsed text value was incorrect",
                      expectedReturn, StringUtils.searchAndColor(text, searchText, dummyColorInt));
     }
 
     @Test
     public void searchAndColor_assertTextReturnedIfSearchTextIsEmpty() {
         String helloWorld = "hello world";
-        assertEquals("text returned when searchText was empty",
+        assertEquals("text returned when searchText was not empty",
                      helloWorld, StringUtils.searchAndColor(helloWorld, "", 0));
     }
 }