From 62f27f1be4d12ea35294954c1a5957677e4e7d14 Mon Sep 17 00:00:00 2001 From: Darin Kelkhoff Date: Tue, 30 Nov 2021 18:07:42 -0600 Subject: [PATCH] Added test, cleaned up class some --- .../core/model/actions/QCriteriaOperator.java | 4 +- .../qqq/backend/core/utils/StringUtils.java | 64 +---- .../backend/core/utils/StringUtilsTest.java | 227 ++++++++++++++++++ 3 files changed, 240 insertions(+), 55 deletions(-) create mode 100644 src/test/java/com/kingsrook/qqq/backend/core/utils/StringUtilsTest.java diff --git a/src/main/java/com/kingsrook/qqq/backend/core/model/actions/QCriteriaOperator.java b/src/main/java/com/kingsrook/qqq/backend/core/model/actions/QCriteriaOperator.java index 2ae9b072..24c28a75 100644 --- a/src/main/java/com/kingsrook/qqq/backend/core/model/actions/QCriteriaOperator.java +++ b/src/main/java/com/kingsrook/qqq/backend/core/model/actions/QCriteriaOperator.java @@ -21,5 +21,7 @@ public enum QCriteriaOperator GREATER_THAN, GREATER_THAN_OR_EQUALS, IS_BLANK, - IS_NOT_BLANK + IS_NOT_BLANK, + BETWEEN, + NOT_BETWEEN } diff --git a/src/main/java/com/kingsrook/qqq/backend/core/utils/StringUtils.java b/src/main/java/com/kingsrook/qqq/backend/core/utils/StringUtils.java index c5bd430e..3008e45e 100755 --- a/src/main/java/com/kingsrook/qqq/backend/core/utils/StringUtils.java +++ b/src/main/java/com/kingsrook/qqq/backend/core/utils/StringUtils.java @@ -1,9 +1,7 @@ package com.kingsrook.qqq.backend.core.utils; -import java.util.Arrays; import java.util.Collection; -import java.util.LinkedList; import java.util.List; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -177,6 +175,11 @@ public class StringUtils *******************************************************************************/ public static String join(String glue, Collection collection) { + if(collection == null) + { + return (null); + } + StringBuffer rs = new StringBuffer(); int i = 0; @@ -208,6 +211,11 @@ public class StringUtils *******************************************************************************/ public static String joinWithCommasAndAnd(List input) { + if(input == null) + { + return (null); + } + StringBuilder rs = new StringBuilder(); int size = input.size(); @@ -234,58 +242,6 @@ public class StringUtils - /******************************************************************************* - ** isNullOrEmptyString - ** - ** @param input - *******************************************************************************/ - private static boolean isNullOrBlankString(Object input) - { - if(input == null) - { - return (true); - } - else if(input instanceof String && !StringUtils.hasContent((String) input)) - { - return (true); - } - else - { - return (false); - } - } - - - - /******************************************************************************* - ** Split a string into tokens, broken up around a given regular expression pattern. - ** - ** @param original the string to do the splitting to. - ** @param pattern the pattern to split on. - ** @return a LinkedList with the elements found in the original string - *******************************************************************************/ - public static LinkedList split(String original, String pattern) - { - return (new LinkedList(Arrays.asList(original.split(pattern)))); - } - - - - /******************************************************************************* - ** Split a string into tokens, broken up around a given regular expression pattern. - ** - ** @param original the string to do the splitting to. - ** @param pattern the pattern to split on. - ** @param limit the max number of splits to make - ** @return a List with the elements found in the original string - *******************************************************************************/ - public static LinkedList split(String original, String pattern, Integer limit) - { - return (new LinkedList(Arrays.asList(original.split(pattern, limit)))); - } - - - /******************************************************************************* ** Trims leading white spaces from a String. Returns a blank ("") value if NULL ** diff --git a/src/test/java/com/kingsrook/qqq/backend/core/utils/StringUtilsTest.java b/src/test/java/com/kingsrook/qqq/backend/core/utils/StringUtilsTest.java new file mode 100644 index 00000000..9e2dabc2 --- /dev/null +++ b/src/test/java/com/kingsrook/qqq/backend/core/utils/StringUtilsTest.java @@ -0,0 +1,227 @@ +package com.kingsrook.qqq.backend.core.utils; + + +import java.util.List; +import org.junit.jupiter.api.Test; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertTrue; + + +/******************************************************************************* + ** + *******************************************************************************/ +class StringUtilsTest +{ + + /******************************************************************************* + ** + *******************************************************************************/ + @Test + void test_hasContent() + { + assertFalse(StringUtils.hasContent("")); + assertFalse(StringUtils.hasContent(" ")); + assertFalse(StringUtils.hasContent(" \n ")); + assertFalse(StringUtils.hasContent(null)); + assertTrue(StringUtils.hasContent("a")); + assertTrue(StringUtils.hasContent(" a ")); + } + + + + /******************************************************************************* + ** + *******************************************************************************/ + @Test + void test_nvlString() + { + assertEquals("foo", StringUtils.nvl("foo", "bar")); + assertEquals("bar", StringUtils.nvl(null, "bar")); + } + + + + /******************************************************************************* + ** + *******************************************************************************/ + @Test + void test_nvlObject() + { + assertEquals("1701", StringUtils.nvl(1701, "bar")); + assertEquals("bar", StringUtils.nvl((Integer) null, "bar")); + } + + + + /******************************************************************************* + ** + *******************************************************************************/ + @Test + void test_allCapsToMixedCase() + { + assertNull(StringUtils.allCapsToMixedCase(null)); + assertEquals("Foo", StringUtils.allCapsToMixedCase("FOO")); + assertEquals("Foo Bar", StringUtils.allCapsToMixedCase("FOO BAR")); + assertEquals("Foo bar", StringUtils.allCapsToMixedCase("FOO bar")); + assertEquals("Foo bar", StringUtils.allCapsToMixedCase("FOo bar")); + assertEquals("Foo Bar", StringUtils.allCapsToMixedCase("FOo BAr")); + assertEquals("foo bar", StringUtils.allCapsToMixedCase("foo bar")); + } + + + + /******************************************************************************* + ** + *******************************************************************************/ + @Test + void test_safeTruncate2() + { + assertNull(StringUtils.safeTruncate(null, 5)); + assertEquals("123", StringUtils.safeTruncate("123", 5)); + assertEquals("12345", StringUtils.safeTruncate("1234567", 5)); + } + + + + /******************************************************************************* + ** + *******************************************************************************/ + @Test + void test_safeTruncate3() + { + assertNull(StringUtils.safeTruncate(null, 5, "...")); + assertEquals("123", StringUtils.safeTruncate("123", 5, "...")); + assertEquals("12345", StringUtils.safeTruncate("12345", 5, "...")); + assertEquals("12...", StringUtils.safeTruncate("123456", 5, "...")); + assertEquals("12...", StringUtils.safeTruncate("1234567890", 5, "...")); + } + + + + /******************************************************************************* + ** + *******************************************************************************/ + @Test + void test_safeTrim() + { + assertNull(StringUtils.safeTrim(null)); + assertEquals("foo", StringUtils.safeTrim("foo ")); + assertEquals("foo", StringUtils.safeTrim(" foo ")); + assertEquals("foo", StringUtils.safeTrim(" foo")); + assertEquals("foo", StringUtils.safeTrim(" foo \n ")); + assertEquals("foo", StringUtils.safeTrim("\nfoo \r \n ")); + } + + + + /******************************************************************************* + ** + *******************************************************************************/ + @Test + void test_join() + { + assertNull(StringUtils.join(",", null)); + assertEquals("1", StringUtils.join(",", List.of(1))); + assertEquals("", StringUtils.join(",", List.of())); + assertEquals("1,2,3", StringUtils.join(",", List.of(1, 2, 3))); + } + + + + /******************************************************************************* + ** + *******************************************************************************/ + @Test + void test_joinWithCommasAndAnd() + { + assertNull(StringUtils.joinWithCommasAndAnd(null)); + assertEquals("", StringUtils.joinWithCommasAndAnd(List.of())); + assertEquals("A", StringUtils.joinWithCommasAndAnd(List.of("A"))); + assertEquals("A and B", StringUtils.joinWithCommasAndAnd(List.of("A", "B"))); + assertEquals("A, B, and C", StringUtils.joinWithCommasAndAnd(List.of("A", "B", "C"))); + assertEquals("A, B, C, and D", StringUtils.joinWithCommasAndAnd(List.of("A", "B", "C", "D"))); + } + + + + /******************************************************************************* + ** + *******************************************************************************/ + @Test + void test_ltrim() + { + assertEquals("", StringUtils.ltrim(null)); + assertEquals("123", StringUtils.ltrim("123")); + assertEquals("123", StringUtils.ltrim(" 123")); + assertEquals("123", StringUtils.ltrim(" 123")); + assertEquals("123", StringUtils.ltrim(" \n\n 123")); + } + + + /******************************************************************************* + ** + *******************************************************************************/ + @Test + void test_rtrim() + { + assertEquals("", StringUtils.rtrim(null)); + assertEquals("123", StringUtils.rtrim("123")); + assertEquals("123", StringUtils.rtrim("123 ")); + assertEquals("123", StringUtils.rtrim("123 ")); + assertEquals("123", StringUtils.rtrim("123 \n\n ")); + } + + + + /******************************************************************************* + ** + *******************************************************************************/ + @Test + void test_pluralCollection() + { + assertEquals("s", StringUtils.plural(List.of())); + assertEquals("", StringUtils.plural(List.of(1))); + assertEquals("s", StringUtils.plural(List.of(1, 2))); + } + + + + /******************************************************************************* + ** + *******************************************************************************/ + @Test + void test_pluralInteger() + { + assertEquals("s", StringUtils.plural(0)); + assertEquals("", StringUtils.plural(1)); + assertEquals("s", StringUtils.plural(2)); + } + + + + /******************************************************************************* + ** + *******************************************************************************/ + @Test + void test_pluralCollectionStringString() + { + assertEquals("es", StringUtils.plural(List.of(), "", "es")); + assertEquals("", StringUtils.plural(List.of(1), "", "es")); + assertEquals("es", StringUtils.plural(List.of(1, 2), "", "es")); + } + + + + /******************************************************************************* + ** + *******************************************************************************/ + @Test + void test_pluralIntegerStringString() + { + assertEquals("es", StringUtils.plural(0, "", "es")); + assertEquals("", StringUtils.plural(1, "", "es")); + assertEquals("es", StringUtils.plural(2, "", "es")); + } +} \ No newline at end of file