From 2559e163ca085478538107de576ae81d72ad59c5 Mon Sep 17 00:00:00 2001 From: feijermu Date: Tue, 3 Mar 2020 17:57:41 +0800 Subject: [PATCH] Add test cases for StringUtil. (#10074) Motivation: StringUtil needs more test cases. Modification: Add several test cases for StringUtil. Result: Improve test coverage slightly. --- .../netty/util/internal/StringUtilTest.java | 42 +++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/common/src/test/java/io/netty/util/internal/StringUtilTest.java b/common/src/test/java/io/netty/util/internal/StringUtilTest.java index 7dd06e7bac..c9f6b282df 100644 --- a/common/src/test/java/io/netty/util/internal/StringUtilTest.java +++ b/common/src/test/java/io/netty/util/internal/StringUtilTest.java @@ -22,6 +22,9 @@ import java.util.Collections; import static io.netty.util.internal.StringUtil.NEWLINE; import static io.netty.util.internal.StringUtil.commonSuffixOfLength; +import static io.netty.util.internal.StringUtil.indexOfWhiteSpace; +import static io.netty.util.internal.StringUtil.indexOfNonWhiteSpace; +import static io.netty.util.internal.StringUtil.isNullOrEmpty; import static io.netty.util.internal.StringUtil.simpleClassName; import static io.netty.util.internal.StringUtil.substringAfter; import static io.netty.util.internal.StringUtil.toHexString; @@ -549,4 +552,43 @@ public class StringUtilTest { StringUtil.join(",", Arrays.asList("a", "b", "c", null, "d")).toString()); } + @Test + public void testIsNullOrEmpty() { + assertTrue(isNullOrEmpty(null)); + assertTrue(isNullOrEmpty("")); + assertTrue(isNullOrEmpty(StringUtil.EMPTY_STRING)); + assertFalse(isNullOrEmpty(" ")); + assertFalse(isNullOrEmpty("\t")); + assertFalse(isNullOrEmpty("\n")); + assertFalse(isNullOrEmpty("foo")); + assertFalse(isNullOrEmpty(NEWLINE)); + } + + @Test + public void testIndexOfWhiteSpace() { + assertEquals(-1, indexOfWhiteSpace("", 0)); + assertEquals(0, indexOfWhiteSpace(" ", 0)); + assertEquals(-1, indexOfWhiteSpace(" ", 1)); + assertEquals(0, indexOfWhiteSpace("\n", 0)); + assertEquals(-1, indexOfWhiteSpace("\n", 1)); + assertEquals(0, indexOfWhiteSpace("\t", 0)); + assertEquals(-1, indexOfWhiteSpace("\t", 1)); + assertEquals(3, indexOfWhiteSpace("foo\r\nbar", 1)); + assertEquals(-1, indexOfWhiteSpace("foo\r\nbar", 10)); + assertEquals(7, indexOfWhiteSpace("foo\tbar\r\n", 6)); + assertEquals(-1, indexOfWhiteSpace("foo\tbar\r\n", Integer.MAX_VALUE)); + } + + @Test + public void testIndexOfNonWhiteSpace() { + assertEquals(-1, indexOfNonWhiteSpace("", 0)); + assertEquals(-1, indexOfNonWhiteSpace(" ", 0)); + assertEquals(-1, indexOfNonWhiteSpace(" \t", 0)); + assertEquals(-1, indexOfNonWhiteSpace(" \t\r\n", 0)); + assertEquals(2, indexOfNonWhiteSpace(" \tfoo\r\n", 0)); + assertEquals(2, indexOfNonWhiteSpace(" \tfoo\r\n", 1)); + assertEquals(4, indexOfNonWhiteSpace(" \tfoo\r\n", 4)); + assertEquals(-1, indexOfNonWhiteSpace(" \tfoo\r\n", 10)); + assertEquals(-1, indexOfNonWhiteSpace(" \tfoo\r\n", Integer.MAX_VALUE)); + } }