Эх сурвалжийг харах

added nullable
add null checks

Signed-off-by: tobiasKaminsky <tobias@kaminsky.me>
wip

Signed-off-by: tobiasKaminsky <tobias@kaminsky.me>

tobiasKaminsky 6 жил өмнө
parent
commit
f932ab3f5b

+ 7 - 3
src/main/java/com/owncloud/android/utils/StringUtils.java

@@ -26,6 +26,8 @@ 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,11 +38,13 @@ 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 (text != null && searchText != null) {
+        if (text != null) {
 
-            if (text.isEmpty() || searchText.isEmpty()) {
+            if (text.isEmpty() || searchText == null || searchText.isEmpty()) {
                 return text;
             }
 

+ 67 - 1
src/test/java/com/owncloud/android/utils/StringUtilsTest.java

@@ -2,7 +2,7 @@ package com.owncloud.android.utils;
 
 import org.junit.Test;
 
-import static org.junit.Assert.*;
+import static org.junit.Assert.assertEquals;
 
 public class StringUtilsTest {
 
@@ -25,4 +25,70 @@ public class StringUtilsTest {
         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));
+    }
 }