This is an automated email from the ASF dual-hosted git repository. xingfudeshi pushed a commit to branch 2.x in repository https://gitbox.apache.org/repos/asf/incubator-seata.git
The following commit(s) were added to refs/heads/2.x by this push: new ea9a8a2ffa test: Refactored tests in DurationUtilTest to simplify and use parameterized unit testing (#7167) ea9a8a2ffa is described below commit ea9a8a2ffa6a387c8a112c21714b40380a62a06d Author: Monil <16ucs...@lnmiit.ac.in> AuthorDate: Wed Feb 19 00:56:12 2025 -0800 test: Refactored tests in DurationUtilTest to simplify and use parameterized unit testing (#7167) --- changes/en-us/2.x.md | 1 + changes/zh-cn/2.x.md | 1 + .../apache/seata/common/util/DurationUtilTest.java | 98 +++++++++++++--------- 3 files changed, 59 insertions(+), 41 deletions(-) diff --git a/changes/en-us/2.x.md b/changes/en-us/2.x.md index 4edd73b27c..c9d01ae10c 100644 --- a/changes/en-us/2.x.md +++ b/changes/en-us/2.x.md @@ -53,6 +53,7 @@ Add changes here for all PR submitted to the 2.x branch. - [[#7092](https://github.com/apache/incubator-seata/pull/7092)] fix the issue of NacosMockTest failing to run - [[#7098](https://github.com/apache/incubator-seata/pull/7098)] Add unit tests for the `seata-common` module - [[#7160](https://github.com/apache/incubator-seata/pull/7160)] Refactored tests in `LowerCaseLinkHashMapTest` to use parameterized unit testing +- [[#7167](https://github.com/apache/incubator-seata/pull/7167)] Refactored tests in `DurationUtilTest` to simplify and use parameterized unit testing ### refactor: diff --git a/changes/zh-cn/2.x.md b/changes/zh-cn/2.x.md index 40b6bdcfc4..8c399000ca 100644 --- a/changes/zh-cn/2.x.md +++ b/changes/zh-cn/2.x.md @@ -53,6 +53,7 @@ - [[#7092](https://github.com/apache/incubator-seata/pull/7092)] 修复NacosMockTest测试方法并行导致测试结果被干扰失败的问题 - [[#7098](https://github.com/apache/incubator-seata/pull/7098)] 增加 `seata-common` 模块的测试用例 - [[#7160](https://github.com/apache/incubator-seata/pull/7160)] 在 LowerCaseLinkHashMapTest 中重构测试,以使用参数化单元测试 +- [[#7167](https://github.com/apache/incubator-seata/pull/7167)] 重构了 DurationUtilTest 中的测试,以简化并使用参数化单元测试 ### refactor: diff --git a/common/src/test/java/org/apache/seata/common/util/DurationUtilTest.java b/common/src/test/java/org/apache/seata/common/util/DurationUtilTest.java index 313b83d918..38d8562348 100644 --- a/common/src/test/java/org/apache/seata/common/util/DurationUtilTest.java +++ b/common/src/test/java/org/apache/seata/common/util/DurationUtilTest.java @@ -17,56 +17,72 @@ package org.apache.seata.common.util; import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; -public class DurationUtilTest { +import java.util.stream.Stream; - @Test - public void testParse() { - Assertions.assertEquals(-1L, DurationUtil.parse("").getSeconds()); - Assertions.assertEquals(0L, DurationUtil.parse("8").getSeconds()); - Assertions.assertEquals(8L, DurationUtil.parse("8").toMillis()); - Assertions.assertEquals(0L, DurationUtil.parse("8ms").getSeconds()); - Assertions.assertEquals(8L, DurationUtil.parse("8ms").toMillis()); - Assertions.assertEquals(8L, DurationUtil.parse("8s").getSeconds()); - Assertions.assertEquals(480L, DurationUtil.parse("8m").getSeconds()); - Assertions.assertEquals(28800L, DurationUtil.parse("8h").getSeconds()); - Assertions.assertEquals(691200L, DurationUtil.parse("8d").getSeconds()); +public class DurationUtilTest { - Assertions.assertEquals(172800L,DurationUtil.parse("P2D").getSeconds()); - Assertions.assertEquals(20L,DurationUtil.parse("PT20.345S").getSeconds()); - Assertions.assertEquals(20345L,DurationUtil.parse("PT20.345S").toMillis()); - Assertions.assertEquals(900L,DurationUtil.parse("PT15M").getSeconds()); - Assertions.assertEquals(36000L,DurationUtil.parse("PT10H").getSeconds()); - Assertions.assertEquals(8L,DurationUtil.parse("PT8S").getSeconds()); - Assertions.assertEquals(86460L,DurationUtil.parse("P1DT1M").getSeconds()); - Assertions.assertEquals(183840L,DurationUtil.parse("P2DT3H4M").getSeconds()); - Assertions.assertEquals(-21420L,DurationUtil.parse("PT-6H3M").getSeconds()); - Assertions.assertEquals(-21780L,DurationUtil.parse("-PT6H3M").getSeconds()); - Assertions.assertEquals(21420L,DurationUtil.parse("-PT-6H+3M").getSeconds()); + private static Stream<Arguments> provideValueSetsTestParseGetSeconds() { + return Stream.of( + Arguments.of(-1L, ""), + Arguments.of(0L, "8"), + Arguments.of(0L, "8ms"), + Arguments.of(8L, "8s"), + Arguments.of(480L, "8m"), + Arguments.of(28800L, "8h"), + Arguments.of(691200L, "8d"), + Arguments.of(172800L, "P2D"), + Arguments.of(20L, "PT20.345S"), + Arguments.of(900L, "PT15M"), + Arguments.of(36000L, "PT10H"), + Arguments.of(8L, "PT8S"), + Arguments.of(86460L, "P1DT1M"), + Arguments.of(183840L, "P2DT3H4M"), + Arguments.of(-21420L, "PT-6H3M"), + Arguments.of(-21780L, "-PT6H3M"), + Arguments.of(21420L, "-PT-6H+3M") + ); } - @Test - public void testParseThrowException() { - Assertions.assertThrows(UnsupportedOperationException.class, - () -> DurationUtil.parse("a")); - - Assertions.assertThrows(UnsupportedOperationException.class, - () -> DurationUtil.parse("as")); - - Assertions.assertThrows(UnsupportedOperationException.class, - () -> DurationUtil.parse("d")); + @ParameterizedTest + @MethodSource("provideValueSetsTestParseGetSeconds") + public void testParseGetSeconds(long expected, String str) { + Assertions.assertEquals(expected,DurationUtil.parse(str).getSeconds()); + } - Assertions.assertThrows(UnsupportedOperationException.class, - () -> DurationUtil.parse("h")); + private static Stream<Arguments> provideValueSetsTestParseToMillis() { + return Stream.of( + Arguments.of(8L, "8"), + Arguments.of(8L, "8ms"), + Arguments.of(20345L, "PT20.345S") + ); + } - Assertions.assertThrows(UnsupportedOperationException.class, - () -> DurationUtil.parse("m")); + @ParameterizedTest + @MethodSource("provideValueSetsTestParseToMillis") + public void testParseToMillis(long expected, String str) { + Assertions.assertEquals(expected, DurationUtil.parse(str).toMillis()); + } - Assertions.assertThrows(UnsupportedOperationException.class, - () -> DurationUtil.parse("s")); + private static Stream<Arguments> provideValueSetsTestParseThrowException() { + return Stream.of( + Arguments.of("a"), + Arguments.of("as"), + Arguments.of("d"), + Arguments.of("h"), + Arguments.of("m"), + Arguments.of("s"), + Arguments.of("ms") + ); + } + @ParameterizedTest + @MethodSource("provideValueSetsTestParseThrowException") + public void testParseThrowException(String str) { Assertions.assertThrows(UnsupportedOperationException.class, - () -> DurationUtil.parse("ms")); + () -> DurationUtil.parse(str)); } } --------------------------------------------------------------------- To unsubscribe, e-mail: notifications-unsubscr...@seata.apache.org For additional commands, e-mail: notifications-h...@seata.apache.org