-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Improve] add TimePeriodUtil & ResourceBundleUtil unit test (#2233)
Signed-off-by: yuluo-yx <[email protected]> Co-authored-by: tomsun28 <[email protected]>
- Loading branch information
Showing
3 changed files
with
172 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
107 changes: 107 additions & 0 deletions
107
common/src/test/java/org/apache/hertzbeat/common/util/TimePeriodUtilTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
|
||
} |