Skip to content

Commit

Permalink
[Improve] add TimePeriodUtil & ResourceBundleUtil unit test (#2233)
Browse files Browse the repository at this point in the history
Signed-off-by: yuluo-yx <[email protected]>
Co-authored-by: tomsun28 <[email protected]>
  • Loading branch information
yuluo-yx and tomsun28 authored Jul 7, 2024
1 parent a618ff0 commit 78621f2
Show file tree
Hide file tree
Showing 3 changed files with 172 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,12 @@
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() {
Expand All @@ -35,6 +37,12 @@ private 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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

}
Original file line number Diff line number Diff line change
@@ -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());
}

}

0 comments on commit 78621f2

Please sign in to comment.