From 4cc25429d32427ab984ccd458a0634200fd2069c Mon Sep 17 00:00:00 2001 From: Paul Kenneth Kent Date: Tue, 9 Apr 2019 02:14:51 +0100 Subject: [PATCH] Add unit tests for com.alibaba.csp.sentinel.util (#651) * Add unit test for com.alibaba.csp.sentinel.util.IdUtil * Add unit tests for com.alibaba.csp.sentinel.util.StringUtil --- .../alibaba/csp/sentinel/util/IdUtilTest.java | 27 ++++++ .../csp/sentinel/util/StringUtilTest.java | 84 +++++++++++++++++++ 2 files changed, 111 insertions(+) create mode 100644 sentinel-core/src/test/java/com/alibaba/csp/sentinel/util/IdUtilTest.java create mode 100644 sentinel-core/src/test/java/com/alibaba/csp/sentinel/util/StringUtilTest.java diff --git a/sentinel-core/src/test/java/com/alibaba/csp/sentinel/util/IdUtilTest.java b/sentinel-core/src/test/java/com/alibaba/csp/sentinel/util/IdUtilTest.java new file mode 100644 index 00000000..d0909a5c --- /dev/null +++ b/sentinel-core/src/test/java/com/alibaba/csp/sentinel/util/IdUtilTest.java @@ -0,0 +1,27 @@ +/* + * Copyright 1999-2019 Alibaba Group Holding Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.alibaba.csp.sentinel.util; + +import static org.junit.Assert.assertEquals; +import org.junit.Test; + +public class IdUtilTest { + + @Test + public void truncate() { + assertEquals("(foo),(bar),(baz)", IdUtil.truncate(".(foo).,(bar).,(baz)")); + } +} diff --git a/sentinel-core/src/test/java/com/alibaba/csp/sentinel/util/StringUtilTest.java b/sentinel-core/src/test/java/com/alibaba/csp/sentinel/util/StringUtilTest.java new file mode 100644 index 00000000..392818fb --- /dev/null +++ b/sentinel-core/src/test/java/com/alibaba/csp/sentinel/util/StringUtilTest.java @@ -0,0 +1,84 @@ +/* + * Copyright 1999-2019 Alibaba Group Holding Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.alibaba.csp.sentinel.util; + +import org.junit.Assert; +import org.junit.Test; + +public class StringUtilTest { + + @Test + public void testCapitalize() { + Assert.assertNull(StringUtil.capitalize(null)); + Assert.assertEquals("Foo", StringUtil.capitalize("foo")); + } + + @Test + public void testEqualsIgnoreCase() { + Assert.assertFalse(StringUtil.equalsIgnoreCase("", "BCCC")); + Assert.assertFalse(StringUtil.equalsIgnoreCase(null, "")); + Assert.assertTrue(StringUtil.equalsIgnoreCase("", "")); + Assert.assertTrue(StringUtil.equalsIgnoreCase("BcCc", "BCCC")); + Assert.assertTrue(StringUtil.equalsIgnoreCase(null, null)); + } + + @Test + public void testEquals() { + Assert.assertFalse(StringUtil.equals(null, "")); + Assert.assertFalse(StringUtil.equals("\"", "\"#\"\"\"\"\"\"")); + Assert.assertTrue(StringUtil.equals(null, null)); + } + + @Test + public void testIsBlank() { + Assert.assertFalse(StringUtil.isBlank("!!!!")); + Assert.assertTrue(StringUtil.isBlank(null)); + Assert.assertTrue(StringUtil.isBlank("\n\n")); + Assert.assertTrue(StringUtil.isBlank("")); + } + + @Test + public void testIsEmpty() { + Assert.assertFalse(StringUtil.isEmpty("bar")); + Assert.assertTrue(StringUtil.isEmpty("")); + } + + @Test + public void testIsNotBlank() { + Assert.assertFalse(StringUtil.isNotBlank("")); + Assert.assertTrue(StringUtil.isNotBlank("\"###")); + } + + @Test + public void testIsNotEmpty() { + Assert.assertFalse(StringUtil.isNotEmpty("")); + Assert.assertTrue(StringUtil.isNotEmpty("foo")); + } + + @Test + public void testTrim() { + Assert.assertNull(StringUtil.trim(null)); + Assert.assertEquals("", StringUtil.trim("")); + Assert.assertEquals("foo", StringUtil.trim("foo ")); + } + + @Test + public void testTrimToEmpty() { + Assert.assertEquals("", StringUtil.trimToEmpty("")); + Assert.assertEquals("", StringUtil.trimToEmpty(null)); + } + +}