diff --git a/bonus-common/bonus-common-core/src/main/java/com/bonus/common/core/utils/StringHelper.java b/bonus-common/bonus-common-core/src/main/java/com/bonus/common/core/utils/StringHelper.java deleted file mode 100644 index ef9c0ef..0000000 --- a/bonus-common/bonus-common-core/src/main/java/com/bonus/common/core/utils/StringHelper.java +++ /dev/null @@ -1,330 +0,0 @@ -package com.bonus.common.core.utils; - -import java.nio.charset.StandardCharsets; -import java.util.UUID; - -/** - * 字符串工具类,提供常见的字符串处理方法。 - * 作者: bonus - */ -public class StringHelper { - - private static final String EMPTY_STRING = ""; - - /** - * 判断字符串是否为空 - * - * @param str 字符串 - * @return 是否为空 - */ - public static boolean isEmpty(String str) { - return str == null || str.isEmpty(); - } - - /** - * 判断字符串是否不为空 - * - * @param str 字符串 - * @return 是否不为空 - */ - public static boolean isNotEmpty(String str) { - return !isEmpty(str); - } - - /** - * 判断字符串是否为空白 - * - * @param str 字符串 - * @return 是否为空白 - */ - public static boolean isBlank(String str) { - return str == null || str.trim().isEmpty(); - } - - /** - * 判断字符串是否不为空白 - * - * @param str 字符串 - * @return 是否不为空白 - */ - public static boolean isNotBlank(String str) { - return !isBlank(str); - } - - /** - * 去除字符串两端的空白字符 - * - * @param str 字符串 - * @return 去除空白后的字符串 - */ - public static String trim(String str) { - return str == null ? null : str.trim(); - } - - /** - * 将字符串转换为大写 - * - * @param str 字符串 - * @return 大写字符串 - */ - public static String toUpperCase(String str) { - return str == null ? null : str.toUpperCase(); - } - - /** - * 将字符串转换为小写 - * - * @param str 字符串 - * @return 小写字符串 - */ - public static String toLowerCase(String str) { - return str == null ? null : str.toLowerCase(); - } - - /** - * 检查字符串是否为数字 - * - * @param str 字符串 - * @return 是否为数字 - */ - public static boolean isNumeric(String str) { - if (isEmpty(str)) { - return false; - } - return str.chars().allMatch(Character::isDigit); - } - - /** - * 检查字符串是否为字母 - * - * @param str 字符串 - * @return 是否为字母 - */ - public static boolean isAlphabetic(String str) { - if (isEmpty(str)) { - return false; - } - return str.chars().allMatch(Character::isLetter); - } - - /** - * 检查字符串是否为字母或数字 - * - * @param str 字符串 - * @return 是否为字母或数字 - */ - public static boolean isAlphanumeric(String str) { - if (isEmpty(str)) { - return false; - } - return str.chars().allMatch(Character::isLetterOrDigit); - } - - /** - * 判断字符串是否包含特定子字符串 - * - * @param str 字符串 - * @param substr 子字符串 - * @return 是否包含子字符串 - */ - public static boolean contains(String str, String substr) { - if (str == null || substr == null) { - return false; - } - return str.contains(substr); - } - - /** - * 字符串反转 - * - * @param str 字符串 - * @return 反转后的字符串 - */ - public static String reverse(String str) { - if (str == null) { - return null; - } - return new StringBuilder(str).reverse().toString(); - } - - /** - * 替换字符串中的所有子字符串 - * - * @param str 字符串 - * @param target 目标子字符串 - * @param replacement 替换子字符串 - * @return 替换后的字符串 - */ - public static String replace(String str, String target, String replacement) { - if (str == null || target == null || replacement == null) { - return str; - } - return str.replace(target, replacement); - } - - /** - * 去除字符串中的所有空白字符 - * - * @param str 字符串 - * @return 去除空白后的字符串 - */ - public static String removeWhitespace(String str) { - if (str == null) { - return null; - } - return str.replaceAll("\\s", EMPTY_STRING); - } - - /** - * 将字符串分割为数组 - * - * @param str 字符串 - * @param delimiter 分隔符 - * @return 分割后的字符串数组 - */ - public static String[] split(String str, String delimiter) { - if (str == null || delimiter == null) { - return new String[0]; - } - return str.split(delimiter); - } - - /** - * 将字符串数组合并为一个字符串 - * - * @param array 字符串数组 - * @param delimiter 分隔符 - * @return 合并后的字符串 - */ - public static String join(String[] array, String delimiter) { - if (array == null || delimiter == null) { - return null; - } - return String.join(delimiter, array); - } - - /** - * 字符串首字母大写 - * - * @param str 字符串 - * @return 首字母大写的字符串 - */ - public static String capitalize(String str) { - if (isEmpty(str)) { - return str; - } - return str.substring(0, 1).toUpperCase() + str.substring(1); - } - - /** - * 字符串首字母小写 - * - * @param str 字符串 - * @return 首字母小写的字符串 - */ - public static String uncapitalize(String str) { - if (isEmpty(str)) { - return str; - } - return str.substring(0, 1).toLowerCase() + str.substring(1); - } - - /** - * 将字符串进行 Base64 编码 - * - * @param str 字符串 - * @return 编码后的字符串 - */ - public static String encodeBase64(String str) { - if (str == null) { - return null; - } - return java.util.Base64.getEncoder().encodeToString(str.getBytes(StandardCharsets.UTF_8)); - } - - /** - * 将 Base64 编码的字符串解码 - * - * @param base64Str Base64 编码字符串 - * @return 解码后的字符串 - */ - public static String decodeBase64(String base64Str) { - if (base64Str == null) { - return null; - } - byte[] decodedBytes = java.util.Base64.getDecoder().decode(base64Str); - return new String(decodedBytes, StandardCharsets.UTF_8); - } - - - /** - * 字符串是否以指定后缀结尾 - * - * @param str 字符串 - * @param suffix 后缀 - * @return 是否以指定后缀结尾 - */ - public static boolean endsWith(String str, String suffix) { - if (str == null || suffix == null) { - return false; - } - return str.endsWith(suffix); - } - - /** - * 字符串是否以指定前缀开头 - * - * @param str 字符串 - * @param prefix 前缀 - * @return 是否以指定前缀开头 - */ - public static boolean startsWith(String str, String prefix) { - if (str == null || prefix == null) { - return false; - } - return str.startsWith(prefix); - } - - /** - * 移除前缀 - * - * @param str 字符串 - * @param prefix 前缀 - * @return 移除前缀 - */ - public static String removePrefix(String str, String prefix) { - return (isBlank(str) || isBlank(prefix) || !str.startsWith(prefix)) ? str : str.substring(prefix.length()); - } - - /** - * 移除后缀 - * - * @param str 字符串 - * @param suffix 后缀 - * @return 移除后缀 - */ - public static String removeSuffix(String str, String suffix) { - return (isBlank(str) || isBlank(suffix) || !str.endsWith(suffix)) ? str : str.substring(0, str.length() - suffix.length()); - } - - /** - * HTML 转义 - * - * @param html html 字符串 - * @return 转义字符 - */ - public static String escapeHtml(String html) { - return StringUtils.replaceEach(html, - new String[]{"&", "\"", "<", ">", "'"}, - new String[]{"&", """, "<", ">", "'"}); - } - - /** - * uuid生成 - * - * @return uuid - */ - public static String randomUUID() { - return UUID.randomUUID().toString().replace("-", ""); - } -} diff --git a/bonus-common/bonus-common-core/src/test/java/com/bonus/common/core/utils/StringHelperTest.java b/bonus-common/bonus-common-core/src/test/java/com/bonus/common/core/utils/StringHelperTest.java deleted file mode 100644 index a257559..0000000 --- a/bonus-common/bonus-common-core/src/test/java/com/bonus/common/core/utils/StringHelperTest.java +++ /dev/null @@ -1,177 +0,0 @@ -package com.bonus.common.core.utils; - - -import org.junit.Test; - -import static org.junit.Assert.*; - -public class StringHelperTest { - - @Test - public void testIsEmpty() { - assertTrue(StringHelper.isEmpty(null)); - assertTrue(StringHelper.isEmpty("")); - assertFalse(StringHelper.isEmpty("abc")); - } - - @Test - public void testIsNotEmpty() { - assertFalse(StringHelper.isNotEmpty(null)); - assertFalse(StringHelper.isNotEmpty("")); - assertTrue(StringHelper.isNotEmpty("abc")); - } - - @Test - public void testIsBlank() { - assertTrue(StringHelper.isBlank(null)); - assertTrue(StringHelper.isBlank("")); - assertTrue(StringHelper.isBlank(" ")); - assertFalse(StringHelper.isBlank("abc")); - } - - @Test - public void testIsNotBlank() { - assertFalse(StringHelper.isNotBlank(null)); - assertFalse(StringHelper.isNotBlank("")); - assertFalse(StringHelper.isNotBlank(" ")); - assertTrue(StringHelper.isNotBlank("abc")); - } - - @Test - public void testTrim() { - assertNull(StringHelper.trim(null)); - assertEquals("abc", StringHelper.trim(" abc ")); - } - - @Test - public void testToUpperCase() { - assertNull(StringHelper.toUpperCase(null)); - assertEquals("ABC", StringHelper.toUpperCase("abc")); - } - - @Test - public void testToLowerCase() { - assertNull(StringHelper.toLowerCase(null)); - assertEquals("abc", StringHelper.toLowerCase("ABC")); - } - - @Test - public void testIsNumeric() { - assertFalse(StringHelper.isNumeric(null)); - assertFalse(StringHelper.isNumeric("abc")); - assertTrue(StringHelper.isNumeric("123")); - } - - @Test - public void testIsAlphabetic() { - assertFalse(StringHelper.isAlphabetic(null)); - assertFalse(StringHelper.isAlphabetic("123")); - assertTrue(StringHelper.isAlphabetic("abc")); - } - - @Test - public void testIsAlphanumeric() { - assertFalse(StringHelper.isAlphanumeric(null)); - assertFalse(StringHelper.isAlphanumeric("abc!")); - assertTrue(StringHelper.isAlphanumeric("abc123")); - } - - @Test - public void testContains() { - assertFalse(StringHelper.contains(null, "abc")); - assertFalse(StringHelper.contains("abc", null)); - assertTrue(StringHelper.contains("abc", "a")); - } - - @Test - public void testReverse() { - assertNull(StringHelper.reverse(null)); - assertEquals("cba", StringHelper.reverse("abc")); - } - - @Test - public void testReplace() { - assertNull(StringHelper.replace(null, "a", "b")); - assertEquals("bbc", StringHelper.replace("abc", "a", "b")); - } - - @Test - public void testRemoveWhitespace() { - assertNull(StringHelper.removeWhitespace(null)); - assertEquals("abc", StringHelper.removeWhitespace(" a b c ")); - } - - @Test - public void testSplit() { - assertArrayEquals(new String[0], StringHelper.split(null, ",")); - assertArrayEquals(new String[]{"a", "b", "c"}, StringHelper.split("a,b,c", ",")); - } - - @Test - public void testJoin() { - assertNull(StringHelper.join(null, ",")); - assertEquals("a,b,c", StringHelper.join(new String[]{"a", "b", "c"}, ",")); - } - - @Test - public void testCapitalize() { - assertNull(StringHelper.capitalize(null)); - assertEquals("Abc", StringHelper.capitalize("abc")); - } - - @Test - public void testUncapitalize() { - assertNull(StringHelper.uncapitalize(null)); - assertEquals("abc", StringHelper.uncapitalize("Abc")); - } - - @Test - public void testEncodeBase64() { - assertNull(StringHelper.encodeBase64(null)); - assertEquals("YWJj", StringHelper.encodeBase64("abc")); - } - - @Test - public void testDecodeBase64() { - assertNull(StringHelper.decodeBase64(null)); - assertEquals("abc", StringHelper.decodeBase64("YWJj")); - } - - @Test - public void testEndsWith() { - assertFalse(StringHelper.endsWith(null, "c")); - assertFalse(StringHelper.endsWith("abc", null)); - assertTrue(StringHelper.endsWith("abc", "c")); - } - - @Test - public void testStartsWith() { - assertFalse(StringHelper.startsWith(null, "a")); - assertFalse(StringHelper.startsWith("abc", null)); - assertTrue(StringHelper.startsWith("abc", "a")); - } - - @Test - public void testRemovePrefix() { - assertNull(StringHelper.removePrefix(null, "a")); - assertEquals("bc", StringHelper.removePrefix("abc", "a")); - } - - @Test - public void testRemoveSuffix() { - assertNull(StringHelper.removeSuffix(null, "c")); - assertEquals("ab", StringHelper.removeSuffix("abc", "c")); - } - - @Test - public void testEscapeHtml() { - assertNull(StringHelper.escapeHtml(null)); - assertEquals("<div>", StringHelper.escapeHtml("
")); - } - - @Test - public void testRandomUUID() { - assertNotNull(StringHelper.randomUUID()); - assertEquals(32, StringHelper.randomUUID().length()); - } -} diff --git a/bonus-common/bonus-common-core/src/test/java/com/bonus/common/core/utils/StringUtilsTest.java b/bonus-common/bonus-common-core/src/test/java/com/bonus/common/core/utils/StringUtilsTest.java new file mode 100644 index 0000000..5176a86 --- /dev/null +++ b/bonus-common/bonus-common-core/src/test/java/com/bonus/common/core/utils/StringUtilsTest.java @@ -0,0 +1,172 @@ +package com.bonus.common.core.utils; + +import org.junit.Test; + +import static org.junit.Assert.*; + +public class StringUtilsTest { + + @Test + public void testIsEmpty() { + assertTrue(StringUtils.isEmpty("")); + assertFalse(StringUtils.isEmpty("abc")); + } + + @Test + public void testIsNotEmpty() { + assertFalse(StringUtils.isNotEmpty("")); + assertTrue(StringUtils.isNotEmpty("abc")); + } + + @Test + public void testIsBlank() { + assertTrue(StringUtils.isBlank(null)); + assertTrue(StringUtils.isBlank("")); + assertTrue(StringUtils.isBlank(" ")); + assertFalse(StringUtils.isBlank("abc")); + } + + @Test + public void testIsNotBlank() { + assertFalse(StringUtils.isNotBlank(null)); + assertFalse(StringUtils.isNotBlank("")); + assertFalse(StringUtils.isNotBlank(" ")); + assertTrue(StringUtils.isNotBlank("abc")); + } + + @Test + public void testTrim() { + assertEquals("abc", StringUtils.trim(" abc ")); + } + + @Test + public void testToUpperCase() { + assertNull(StringUtils.toUpperCase(null)); + assertEquals("ABC", StringUtils.toUpperCase("abc")); + } + + @Test + public void testToLowerCase() { + assertNull(StringUtils.toLowerCase(null)); + assertEquals("abc", StringUtils.toLowerCase("ABC")); + } + + @Test + public void testIsNumeric() { + assertFalse(StringUtils.isNumeric(null)); + assertFalse(StringUtils.isNumeric("abc")); + assertTrue(StringUtils.isNumeric("123")); + } + + @Test + public void testIsAlphabetic() { + assertFalse(StringUtils.isAlphabetic(null)); + assertFalse(StringUtils.isAlphabetic("123")); + assertTrue(StringUtils.isAlphabetic("abc")); + } + + @Test + public void testIsAlphanumeric() { + assertFalse(StringUtils.isAlphanumeric(null)); + assertFalse(StringUtils.isAlphanumeric("abc!")); + assertTrue(StringUtils.isAlphanumeric("abc123")); + } + + @Test + public void testContains() { + assertFalse(StringUtils.contains(null, "abc")); + assertFalse(StringUtils.contains("abc", null)); + assertTrue(StringUtils.contains("abc", "a")); + } + + @Test + public void testReverse() { + assertNull(StringUtils.reverse(null)); + assertEquals("cba", StringUtils.reverse("abc")); + } + + @Test + public void testReplace() { + assertNull(StringUtils.replace(null, "a", "b")); + assertEquals("bbc", StringUtils.replace("abc", "a", "b")); + } + + @Test + public void testRemoveWhitespace() { + assertNull(StringUtils.removeWhitespace(null)); + assertEquals("abc", StringUtils.removeWhitespace(" a b c ")); + } + + @Test + public void testSplit() { + assertArrayEquals(new String[0], StringUtils.split(null, ",")); + assertArrayEquals(new String[]{"a", "b", "c"}, StringUtils.split("a,b,c", ",")); + } + + @Test + public void testJoin() { + assertEquals("a,b,c", StringUtils.join(new String[]{"a", "b", "c"}, ",")); + } + + @Test + public void testCapitalize() { + assertNull(StringUtils.capitalize(null)); + assertEquals("Abc", StringUtils.capitalize("abc")); + } + + @Test + public void testUncapitalize() { + assertNull(StringUtils.uncapitalize(null)); + assertEquals("abc", StringUtils.uncapitalize("Abc")); + } + + @Test + public void testEncodeBase64() { + assertNull(StringUtils.encodeBase64(null)); + assertEquals("YWJj", StringUtils.encodeBase64("abc")); + } + + @Test + public void testDecodeBase64() { + assertNull(StringUtils.decodeBase64(null)); + assertEquals("abc", StringUtils.decodeBase64("YWJj")); + } + + @Test + public void testEndsWith() { + assertFalse(StringUtils.endsWith(null, "c")); + assertFalse(StringUtils.endsWith("abc", null)); + assertTrue(StringUtils.endsWith("abc", "c")); + } + + @Test + public void testStartsWith() { + assertFalse(StringUtils.startsWith(null, "a")); + assertFalse(StringUtils.startsWith("abc", null)); + assertTrue(StringUtils.startsWith("abc", "a")); + } + + @Test + public void testRemovePrefix() { + assertNull(StringUtils.removePrefix(null, "a")); + assertEquals("bc", StringUtils.removePrefix("abc", "a")); + } + + @Test + public void testRemoveSuffix() { + assertNull(StringUtils.removeSuffix(null, "c")); + assertEquals("ab", StringUtils.removeSuffix("abc", "c")); + } + + @Test + public void testEscapeHtml() { + assertNull(StringUtils.escapeHtml(null)); + assertEquals("<div>", StringUtils.escapeHtml("
")); + } + + @Test + public void testRandomUUID() { + assertNotNull(StringUtils.randomUUID()); + assertEquals(32, StringUtils.randomUUID().length()); + } +}