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 78621f2f1 [Improve] add TimePeriodUtil & ResourceBundleUtil unit test 
(#2233)
78621f2f1 is described below

commit 78621f2f141bff56d5437e67504bd64f2ed2628d
Author: YuLuo <[email protected]>
AuthorDate: Sun Jul 7 20:50:09 2024 +0800

    [Improve] add TimePeriodUtil & ResourceBundleUtil unit test (#2233)
    
    Signed-off-by: yuluo-yx <[email protected]>
    Co-authored-by: tomsun28 <[email protected]>
---
 .../hertzbeat/common/util/TimePeriodUtil.java      |   8 ++
 .../common/util/ResourceBundleUtilTest.java        |  58 ++++++++++-
 .../hertzbeat/common/util/TimePeriodUtilTest.java  | 107 +++++++++++++++++++++
 3 files changed, 172 insertions(+), 1 deletion(-)

diff --git 
a/common/src/main/java/org/apache/hertzbeat/common/util/TimePeriodUtil.java 
b/common/src/main/java/org/apache/hertzbeat/common/util/TimePeriodUtil.java
index 5e4cf7704..c48d73e1d 100644
--- a/common/src/main/java/org/apache/hertzbeat/common/util/TimePeriodUtil.java
+++ b/common/src/main/java/org/apache/hertzbeat/common/util/TimePeriodUtil.java
@@ -20,10 +20,12 @@ package org.apache.hertzbeat.common.util;
 import java.time.Duration;
 import java.time.Period;
 import java.time.temporal.TemporalAmount;
+import lombok.extern.slf4j.Slf4j;
 
 /**
  * time util
  */
+@Slf4j
 public final class TimePeriodUtil {
 
     private TimePeriodUtil() {
@@ -35,6 +37,12 @@ public final class TimePeriodUtil {
      * @return TemporalAmount
      */
     public static TemporalAmount parseTokenTime(String tokenTime) {
+
+        if (tokenTime == null || tokenTime.length() < 2) {
+            log.error("tokenTime is invalid");
+            return null;
+        }
+
         if (Character.isUpperCase(tokenTime.charAt(tokenTime.length() - 1))) {
             return Period.parse("P" + tokenTime);
         } else {
diff --git 
a/common/src/test/java/org/apache/hertzbeat/common/util/ResourceBundleUtilTest.java
 
b/common/src/test/java/org/apache/hertzbeat/common/util/ResourceBundleUtilTest.java
index f453a87cb..a9b8fb4b5 100644
--- 
a/common/src/test/java/org/apache/hertzbeat/common/util/ResourceBundleUtilTest.java
+++ 
b/common/src/test/java/org/apache/hertzbeat/common/util/ResourceBundleUtilTest.java
@@ -17,19 +17,75 @@
 
 package org.apache.hertzbeat.common.util;
 
+import org.junit.jupiter.api.extension.ExtendWith;
+import org.mockito.MockedStatic;
+import org.mockito.Mockito;
+import org.mockito.junit.jupiter.MockitoExtension;
 import org.junit.jupiter.api.BeforeEach;
 import org.junit.jupiter.api.Test;
+import java.util.Locale;
+import java.util.MissingResourceException;
+import java.util.ResourceBundle;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.mockito.Mockito.mockStatic;
 
 /**
  * Test case for {@link ResourceBundleUtil}
  */
+@ExtendWith(MockitoExtension.class)
 class ResourceBundleUtilTest {
 
+    private static final String BUNDLE_NAME = "TestBundle";
+
     @BeforeEach
     void setUp() {
+        Locale.setDefault(Locale.US);
     }
 
     @Test
-    void getBundle() {
+    void testGetBundleWithValidBundleName() {
+        try (MockedStatic<ResourceBundle> mockedResourceBundle = 
mockStatic(ResourceBundle.class)) {
+            ResourceBundle mockBundle = Mockito.mock(ResourceBundle.class);
+
+            mockedResourceBundle.when(
+                    () -> ResourceBundle.getBundle(
+                        Mockito.eq(BUNDLE_NAME),
+                        Mockito.any(ResourceBundle.Control.class)
+                    )
+            ).thenReturn(mockBundle);
+
+            ResourceBundle bundle = ResourceBundleUtil.getBundle(BUNDLE_NAME);
+
+            assertNotNull(bundle);
+            assertEquals(mockBundle, bundle);
+        }
     }
+
+    @Test
+    void testGetBundleByInvalidBundleName() {
+        try (MockedStatic<ResourceBundle> mockedResourceBundle = 
mockStatic(ResourceBundle.class)) {
+            mockedResourceBundle.when(
+                    () -> ResourceBundle.getBundle(
+                        Mockito.eq(BUNDLE_NAME),
+                        Mockito.any(ResourceBundle.Control.class)
+                    )
+            ).thenThrow(new MissingResourceException("Missing bundle", 
"ResourceBundle", BUNDLE_NAME));
+
+            ResourceBundle mockDefaultBundle = 
Mockito.mock(ResourceBundle.class);
+
+            mockedResourceBundle.when(() -> ResourceBundle.getBundle(
+                    Mockito.eq(BUNDLE_NAME),
+                    Mockito.eq(Locale.US),
+                    Mockito.any(ResourceBundle.Control.class))
+            ).thenReturn(mockDefaultBundle);
+
+            ResourceBundle bundle = ResourceBundleUtil.getBundle(BUNDLE_NAME);
+
+            assertNotNull(bundle);
+            assertEquals(mockDefaultBundle, bundle);
+        }
+    }
+
 }
diff --git 
a/common/src/test/java/org/apache/hertzbeat/common/util/TimePeriodUtilTest.java 
b/common/src/test/java/org/apache/hertzbeat/common/util/TimePeriodUtilTest.java
new file mode 100644
index 000000000..59a7492b8
--- /dev/null
+++ 
b/common/src/test/java/org/apache/hertzbeat/common/util/TimePeriodUtilTest.java
@@ -0,0 +1,107 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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 org.apache.hertzbeat.common.util;
+
+import java.time.Duration;
+import java.time.Period;
+import java.time.format.DateTimeParseException;
+import java.time.temporal.TemporalAmount;
+
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertNull;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+/**
+ * Test case for {@link SnowFlakeIdGenerator}
+ */
+
+class TimePeriodUtilTest {
+
+       @Test
+       void testParseTokenTime() {
+
+               // Years
+               TemporalAmount result = TimePeriodUtil.parseTokenTime("1Y");
+               assertTrue(result instanceof Period);
+               assertEquals(Period.ofYears(1), result);
+
+               // Month
+               result = TimePeriodUtil.parseTokenTime("5M");
+               assertTrue(result instanceof Period);
+               assertEquals(Period.ofMonths(5), result);
+
+               // Day
+               result = TimePeriodUtil.parseTokenTime("3D");
+               assertTrue(result instanceof Period);
+               assertEquals(Period.ofDays(3), result);
+
+               // Week
+               result = TimePeriodUtil.parseTokenTime("3W");
+               assertTrue(result instanceof Period);
+               assertEquals(Period.ofWeeks(3), result);
+       }
+
+       @Test
+       void testParseTokenTimeDuration() {
+
+               // Minute
+               TemporalAmount result = TimePeriodUtil.parseTokenTime("30m");
+               assertTrue(result instanceof Duration);
+               assertEquals(Duration.ofMinutes(30), result);
+
+               // Hour
+               result = TimePeriodUtil.parseTokenTime("2h");
+               assertTrue(result instanceof Duration);
+               assertEquals(Duration.ofHours(2), result);
+       }
+
+       @Test
+       void testParseTokenTimeLowerCaseMinute() {
+               // Lowercase Minute
+               TemporalAmount result = TimePeriodUtil.parseTokenTime("1m");
+               assertTrue(result instanceof Duration);
+               assertEquals(Duration.ofMinutes(1), result);
+       }
+
+       @Test
+       void testParseTokenTimeInvalidInput() {
+
+               // null input
+               TemporalAmount result = TimePeriodUtil.parseTokenTime(null);
+               assertNull(result);
+
+               // empty string
+               result = TimePeriodUtil.parseTokenTime("");
+               assertNull(result);
+
+               // string with length < 2
+               result = TimePeriodUtil.parseTokenTime("1");
+               assertNull(result);
+
+               // invalid format (non-numeric)
+               Exception exception = 
assertThrows(DateTimeParseException.class, () -> {
+                       TimePeriodUtil.parseTokenTime("abc");
+               });
+               assertNotNull(exception.getMessage());
+       }
+
+}


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to