mirror of
https://github.com/Kingsrook/qqq.git
synced 2025-07-18 13:10:44 +00:00
Added test, cleaned up class some
This commit is contained in:
@ -21,5 +21,7 @@ public enum QCriteriaOperator
|
||||
GREATER_THAN,
|
||||
GREATER_THAN_OR_EQUALS,
|
||||
IS_BLANK,
|
||||
IS_NOT_BLANK
|
||||
IS_NOT_BLANK,
|
||||
BETWEEN,
|
||||
NOT_BETWEEN
|
||||
}
|
||||
|
@ -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<? extends Object> 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<String> 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<String> split(String original, String pattern)
|
||||
{
|
||||
return (new LinkedList<String>(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<String> split(String original, String pattern, Integer limit)
|
||||
{
|
||||
return (new LinkedList<String>(Arrays.asList(original.split(pattern, limit))));
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** Trims leading white spaces from a String. Returns a blank ("") value if NULL
|
||||
**
|
||||
|
@ -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"));
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user