This is an automated email from the ASF dual-hosted git repository.
gongchao pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/hertzbeat.git
The following commit(s) were added to refs/heads/master by this push:
new f038ec532 [Improve] add CommonUtil unit test (#2346)
f038ec532 is described below
commit f038ec5326d8d203d5d337b89875a043fb6ad65f
Author: kangli <[email protected]>
AuthorDate: Wed Jul 24 10:15:05 2024 +0800
[Improve] add CommonUtil unit test (#2346)
Co-authored-by: tomsun28 <[email protected]>
---
.../hertzbeat/common/util/CommonUtilTest.java | 95 ++++++++++++++++++++++
1 file changed, 95 insertions(+)
diff --git
a/common/src/test/java/org/apache/hertzbeat/common/util/CommonUtilTest.java
b/common/src/test/java/org/apache/hertzbeat/common/util/CommonUtilTest.java
index f4de9b0b1..c99e77c97 100644
--- a/common/src/test/java/org/apache/hertzbeat/common/util/CommonUtilTest.java
+++ b/common/src/test/java/org/apache/hertzbeat/common/util/CommonUtilTest.java
@@ -19,7 +19,12 @@ package org.apache.hertzbeat.common.util;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertNotEquals;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
+import java.util.HashMap;
+import java.util.Map;
import org.junit.jupiter.api.Test;
/**
@@ -27,6 +32,40 @@ import org.junit.jupiter.api.Test;
*/
class CommonUtilTest {
+ @Test
+ void testParseStrInteger() {
+ assertEquals(10, CommonUtil.parseStrInteger("10"));
+ assertEquals(-10, CommonUtil.parseStrInteger("-10"));
+ assertEquals(+10, CommonUtil.parseStrInteger("+10"));
+ assertNotEquals(10_000, CommonUtil.parseStrInteger("10_000"));
+ assertNotEquals(16, CommonUtil.parseStrInteger("0x10"));
+ }
+
+ @Test
+ void testParseStrDouble() {
+ assertEquals(10.125, CommonUtil.parseStrDouble("10.125"));
+ assertEquals(-10.125, CommonUtil.parseStrDouble("-10.125"));
+ assertEquals(100, CommonUtil.parseStrDouble("100d"));
+ assertEquals(100000, CommonUtil.parseStrDouble("100E3"));
+ assertEquals(0.1, CommonUtil.parseStrDouble("100E-3"));
+ assertNotEquals(10_000.125, CommonUtil.parseStrDouble("10_000.125"));
+ }
+
+ @Test
+ void testParseIsNumeric() {
+ assertTrue(CommonUtil.isNumeric("1234"));
+ assertTrue(CommonUtil.isNumeric("6.954"));
+ assertFalse(CommonUtil.isNumeric("296.347%"));
+ assertFalse(CommonUtil.isNumeric("445_126"));
+ }
+
+ @Test
+ void testParseTimeStrToSecond() {
+ assertEquals(36000, CommonUtil.parseTimeStrToSecond("10:00"));
+ assertEquals(43800, CommonUtil.parseTimeStrToSecond("12:10:00"));
+ assertNotEquals(43800, CommonUtil.parseTimeStrToSecond("2024-07-23
12:10:00"));
+ }
+
@Test
void testParseDoubleStr() {
assertEquals("9.3454", CommonUtil.parseDoubleStr("9.345435345", null));
@@ -54,4 +93,60 @@ class CommonUtilTest {
assertFalse(CommonUtil.validatePhoneNum("46234554432"));
}
+ @Test
+ void testGetMessageFromThrowable() {
+ assertEquals("throwable is null, unknown error.",
CommonUtil.getMessageFromThrowable(null));
+ assertEquals("throwable cause message",
CommonUtil.getMessageFromThrowable(new Throwable(new Throwable("throwable cause
message"))));
+ assertEquals("throwable message",
CommonUtil.getMessageFromThrowable(new Throwable("throwable message")));
+ assertEquals("throwable localizedMessage",
CommonUtil.getMessageFromThrowable(new Throwable() {
+ @Override
+ public String getLocalizedMessage() {
+ return "throwable localizedMessage";
+ }
+ }));
+ assertEquals("throwable toString",
CommonUtil.getMessageFromThrowable(new Throwable() {
+ @Override
+ public String getMessage() {
+ return null;
+ }
+
+ @Override
+ public String toString() {
+ return "throwable toString";
+ }
+ }));
+ assertEquals("unknown error.", CommonUtil.getMessageFromThrowable(new
Throwable() {
+ @Override
+ public String getMessage() {
+ return null;
+ }
+
+ @Override
+ public String toString() {
+ return null;
+ }
+ }));
+ }
+
+ @Test
+ void testRemoveBlankLine() {
+ assertEquals("line1\nline2\nline3",
CommonUtil.removeBlankLine("line1\nline2\nline3"));
+ assertEquals("line1\nline3\nline4\nline6",
CommonUtil.removeBlankLine("line1\n\nline3\nline4\n\n\nline6"));
+ assertEquals("", CommonUtil.removeBlankLine(""));
+ assertEquals("", CommonUtil.removeBlankLine("\n\n\n\n"));
+ }
+
+ @Test
+ void testGetLangMappingValueFromI18nMap() {
+ Map<String, String> i18nMap = new HashMap<>();
+ i18nMap.put("zh-CN", "中文");
+ i18nMap.put("ja", null);
+ i18nMap.put("en-US", "English");
+ assertEquals("中文", CommonUtil.getLangMappingValueFromI18nMap("zh-CN",
i18nMap));
+ assertEquals("English",
CommonUtil.getLangMappingValueFromI18nMap("en-US", i18nMap));
+ assertNull(CommonUtil.getLangMappingValueFromI18nMap("zh", new
HashMap<>()));
+ assertNotNull(CommonUtil.getLangMappingValueFromI18nMap("ja",
i18nMap));
+ assertNotNull(CommonUtil.getLangMappingValueFromI18nMap("zh-TW",
i18nMap));
+ }
+
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]