From 7aa8a77fb23252360dcc29721ddc56ed8aae8de8 Mon Sep 17 00:00:00 2001 From: Yann Ann Date: Sun, 16 Oct 2022 12:49:11 +0800 Subject: [PATCH] [Migrate][Test] Migrate other UT cases from jUnit 4 to 5, change @Ignore to @Disable --- docs/docs/en/contribute/join/unit-test.md | 4 +- docs/docs/zh/contribute/join/unit-test.md | 4 +- .../plugin/task/api/model/JSONUtilsTest.java | 94 +++++++++---------- .../task/api/utils/ParameterUtilsTest.java | 32 +++---- 4 files changed, 64 insertions(+), 70 deletions(-) diff --git a/docs/docs/en/contribute/join/unit-test.md b/docs/docs/en/contribute/join/unit-test.md index 932a0bf64a..7cc02c29b3 100644 --- a/docs/docs/en/contribute/join/unit-test.md +++ b/docs/docs/en/contribute/join/unit-test.md @@ -84,9 +84,9 @@ Try not to use Thread.sleep in your test code, it makes the test unstable and ma 2: Ignore some test classes -The @Ignore annotation should be linked to the relevant issue address so that subsequent developers can track the history of why the test was ignored. +The @Disabled annotation should be linked to the relevant issue address so that subsequent developers can track the history of why the test was ignored. -For example @Ignore("see #1"). +For example @Disabled("see #1"). 3: try-catch Unit test exception diff --git a/docs/docs/zh/contribute/join/unit-test.md b/docs/docs/zh/contribute/join/unit-test.md index 5d15a9ae5d..c70cabb3f1 100644 --- a/docs/docs/zh/contribute/join/unit-test.md +++ b/docs/docs/zh/contribute/join/unit-test.md @@ -86,9 +86,9 @@ Awaitility.await().atMost(…) 2:忽略某些测试类 -@Ignore 注解应该附上相关 issue 地址,方便后续开发者追踪了解该测试被忽略的历史原因。 +@Disabled 注解应该附上相关 issue 地址,方便后续开发者追踪了解该测试被忽略的历史原因。 -如 @Ignore("see #1") +如 @Disabled("see #1") 3: try-catch 单元测试异常 diff --git a/dolphinscheduler-task-plugin/dolphinscheduler-task-api/src/test/java/org/apache/dolphinscheduler/plugin/task/api/model/JSONUtilsTest.java b/dolphinscheduler-task-plugin/dolphinscheduler-task-api/src/test/java/org/apache/dolphinscheduler/plugin/task/api/model/JSONUtilsTest.java index 0200ec5d92..b52a9a099b 100644 --- a/dolphinscheduler-task-plugin/dolphinscheduler-task-api/src/test/java/org/apache/dolphinscheduler/plugin/task/api/model/JSONUtilsTest.java +++ b/dolphinscheduler-task-plugin/dolphinscheduler-task-api/src/test/java/org/apache/dolphinscheduler/plugin/task/api/model/JSONUtilsTest.java @@ -30,8 +30,8 @@ import java.util.List; import java.util.Map; import java.util.TimeZone; -import org.junit.Assert; -import org.junit.Test; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.SerializationFeature; @@ -59,7 +59,7 @@ public class JSONUtilsTest { ArrayNode jsonNodes = arrayNode.addAll(objects); String s = JSONUtils.toJsonString(jsonNodes); - Assert.assertEquals(s, str); + Assertions.assertEquals(s, str); } @@ -74,7 +74,7 @@ public class JSONUtilsTest { JsonNode jsonNodes = JSONUtils.toJsonNode(property); String s = JSONUtils.toJsonString(jsonNodes); - Assert.assertEquals(s, str); + Assertions.assertEquals(s, str); } @@ -86,7 +86,7 @@ public class JSONUtilsTest { objectNode.put("a", "b"); objectNode.put("b", "d"); String s = JSONUtils.toJsonString(objectNode); - Assert.assertEquals(s, jsonStr); + Assertions.assertEquals(s, jsonStr); } @Test @@ -95,8 +95,8 @@ public class JSONUtilsTest { String jsonStr = "{\"id\":\"1001\",\"name\":\"Jobs\"}"; Map models = JSONUtils.toMap(jsonStr); - Assert.assertEquals("1001", models.get("id")); - Assert.assertEquals("Jobs", models.get("name")); + Assertions.assertEquals("1001", models.get("id")); + Assertions.assertEquals("Jobs", models.get("name")); } @@ -110,7 +110,7 @@ public class JSONUtilsTest { String str = "{\"direct\":\"IN\",\"prop\":\"ds\",\"type\":\"VARCHAR\",\"value\":\"sssssss\"}"; Property property1 = JSONUtils.parseObject(str, Property.class); Direct direct = property1.getDirect(); - Assert.assertEquals(Direct.IN, direct); + Assertions.assertEquals(Direct.IN, direct); } @Test @@ -120,12 +120,12 @@ public class JSONUtilsTest { List maps = JSONUtils.toList(str, LinkedHashMap.class); - Assert.assertEquals(1, maps.size()); - Assert.assertEquals("mysql200", maps.get(0).get("mysql service name")); - Assert.assertEquals("192.168.xx.xx", maps.get(0).get("mysql address")); - Assert.assertEquals("3306", maps.get(0).get("port")); - Assert.assertEquals("80", maps.get(0).get("no index of number")); - Assert.assertEquals("190", maps.get(0).get("database client connections")); + Assertions.assertEquals(1, maps.size()); + Assertions.assertEquals("mysql200", maps.get(0).get("mysql service name")); + Assertions.assertEquals("192.168.xx.xx", maps.get(0).get("mysql address")); + Assertions.assertEquals("3306", maps.get(0).get("port")); + Assertions.assertEquals("80", maps.get(0).get("no index of number")); + Assertions.assertEquals("190", maps.get(0).get("database client connections")); } public String list2String() { @@ -145,19 +145,18 @@ public class JSONUtilsTest { @Test public void testParseObject() { - Assert.assertNull(JSONUtils.parseObject("")); - Assert.assertNull(JSONUtils.parseObject("foo", String.class)); + Assertions.assertNull(JSONUtils.parseObject("")); + Assertions.assertNull(JSONUtils.parseObject("foo", String.class)); } @Test public void testNodeString() { - Assert.assertEquals("", JSONUtils.getNodeString("", "key")); - Assert.assertEquals("", JSONUtils.getNodeString("abc", "key")); - Assert.assertEquals("", JSONUtils.getNodeString("{\"bar\":\"foo\"}", "key")); - Assert.assertEquals("foo", JSONUtils.getNodeString("{\"bar\":\"foo\"}", "bar")); - Assert.assertEquals("[1,2,3]", JSONUtils.getNodeString("{\"bar\": [1,2,3]}", "bar")); - Assert.assertEquals("{\"1\":\"2\",\"2\":3}", - JSONUtils.getNodeString("{\"bar\": {\"1\":\"2\",\"2\":3}}", "bar")); + Assertions.assertEquals("", JSONUtils.getNodeString("", "key")); + Assertions.assertEquals("", JSONUtils.getNodeString("abc", "key")); + Assertions.assertEquals("", JSONUtils.getNodeString("{\"bar\":\"foo\"}", "key")); + Assertions.assertEquals("foo", JSONUtils.getNodeString("{\"bar\":\"foo\"}", "bar")); + Assertions.assertEquals("[1,2,3]", JSONUtils.getNodeString("{\"bar\": [1,2,3]}", "bar")); + Assertions.assertEquals("{\"1\":\"2\",\"2\":3}", JSONUtils.getNodeString("{\"bar\": {\"1\":\"2\",\"2\":3}}", "bar")); } @Test @@ -165,30 +164,28 @@ public class JSONUtilsTest { String str = "foo"; byte[] serializeByte = JSONUtils.toJsonByteArray(str); String deserialize = JSONUtils.parseObject(serializeByte, String.class); - Assert.assertEquals(str, deserialize); + Assertions.assertEquals(str, deserialize); str = null; serializeByte = JSONUtils.toJsonByteArray(str); deserialize = JSONUtils.parseObject(serializeByte, String.class); - Assert.assertNull(deserialize); + Assertions.assertNull(deserialize); } @Test public void testToList() { - Assert.assertEquals(new ArrayList(), - JSONUtils.toList("A1B2C3", null)); - Assert.assertEquals(new ArrayList(), - JSONUtils.toList("", null)); + Assertions.assertEquals(new ArrayList(), JSONUtils.toList("A1B2C3", null)); + Assertions.assertEquals(new ArrayList(), JSONUtils.toList("", null)); } @Test public void testCheckJsonValid() { - Assert.assertTrue(JSONUtils.checkJsonValid("3")); - Assert.assertFalse(JSONUtils.checkJsonValid("")); + Assertions.assertTrue(JSONUtils.checkJsonValid("3")); + Assertions.assertFalse(JSONUtils.checkJsonValid("")); } @Test public void testFindValue() { - Assert.assertNull(JSONUtils.findValue( + Assertions.assertNull(JSONUtils.findValue( new ArrayNode(new JsonNodeFactory(true)), null)); } @@ -197,18 +194,18 @@ public class JSONUtilsTest { Map map = new HashMap<>(); map.put("foo", "bar"); - Assert.assertTrue(map.equals(JSONUtils.toMap( + Assertions.assertTrue(map.equals(JSONUtils.toMap( "{\n" + "\"foo\": \"bar\"\n" + "}"))); - Assert.assertFalse(map.equals(JSONUtils.toMap( + Assertions.assertFalse(map.equals(JSONUtils.toMap( "{\n" + "\"bar\": \"foo\"\n" + "}"))); - Assert.assertNull(JSONUtils.toMap("3")); - Assert.assertNull(JSONUtils.toMap(null)); + Assertions.assertNull(JSONUtils.toMap("3")); + Assertions.assertNull(JSONUtils.toMap(null)); String str = "{\"resourceList\":[],\"localParams\":[],\"rawScript\":\"#!/bin/bash\\necho \\\"shell-1\\\"\"}"; Map m = JSONUtils.toMap(str); - Assert.assertNotNull(m); + Assertions.assertNotNull(m); } @Test @@ -216,13 +213,10 @@ public class JSONUtilsTest { Map map = new HashMap<>(); map.put("foo", "bar"); - Assert.assertEquals("{\"foo\":\"bar\"}", - JSONUtils.toJsonString(map)); - Assert.assertEquals(String.valueOf((Object) null), - JSONUtils.toJsonString(null)); + Assertions.assertEquals("{\"foo\":\"bar\"}", JSONUtils.toJsonString(map)); + Assertions.assertEquals(String.valueOf((Object) null), JSONUtils.toJsonString(null)); - Assert.assertEquals("{\"foo\":\"bar\"}", - JSONUtils.toJsonString(map, SerializationFeature.WRITE_NULL_MAP_VALUES)); + Assertions.assertEquals("{\"foo\":\"bar\"}", JSONUtils.toJsonString(map, SerializationFeature.WRITE_NULL_MAP_VALUES)); } @Test @@ -230,13 +224,13 @@ public class JSONUtilsTest { String str = "{\"color\":\"yellow\",\"type\":\"renault\"}"; ObjectNode node = JSONUtils.parseObject(str); - Assert.assertEquals("yellow", node.path("color").asText()); + Assertions.assertEquals("yellow", node.path("color").asText()); node.put("price", 100); - Assert.assertEquals(100, node.path("price").asInt()); + Assertions.assertEquals(100, node.path("price").asInt()); node.put("color", "red"); - Assert.assertEquals("red", node.path("color").asText()); + Assertions.assertEquals("red", node.path("color").asText()); } @Test @@ -244,7 +238,7 @@ public class JSONUtilsTest { String str = "[{\"color\":\"yellow\",\"type\":\"renault\"}]"; ArrayNode node = JSONUtils.parseArray(str); - Assert.assertEquals("yellow", node.path(0).path("color").asText()); + Assertions.assertEquals("yellow", node.path(0).path("color").asText()); } @Test @@ -256,10 +250,10 @@ public class JSONUtilsTest { String time = "2022-02-22 13:38:24"; Date date = DateUtils.stringToDate(time); String json = JSONUtils.toJsonString(date); - Assert.assertEquals("\"" + time + "\"", json); + Assertions.assertEquals("\"" + time + "\"", json); String errorFormatTime = "Tue Feb 22 03:50:00 UTC 2022"; - Assert.assertNull(DateUtils.stringToDate(errorFormatTime)); + Assertions.assertNull(DateUtils.stringToDate(errorFormatTime)); } @Test @@ -270,7 +264,7 @@ public class JSONUtilsTest { String json = "\"2022-02-22 13:38:24\""; Date date = JSONUtils.parseObject(json, Date.class); - Assert.assertEquals(DateUtils.stringToDate("2022-02-22 13:38:24"), date); + Assertions.assertEquals(DateUtils.stringToDate("2022-02-22 13:38:24"), date); } diff --git a/dolphinscheduler-task-plugin/dolphinscheduler-task-api/src/test/java/org/apache/dolphinscheduler/plugin/task/api/utils/ParameterUtilsTest.java b/dolphinscheduler-task-plugin/dolphinscheduler-task-api/src/test/java/org/apache/dolphinscheduler/plugin/task/api/utils/ParameterUtilsTest.java index 59acc82687..65d8be407d 100644 --- a/dolphinscheduler-task-plugin/dolphinscheduler-task-api/src/test/java/org/apache/dolphinscheduler/plugin/task/api/utils/ParameterUtilsTest.java +++ b/dolphinscheduler-task-plugin/dolphinscheduler-task-api/src/test/java/org/apache/dolphinscheduler/plugin/task/api/utils/ParameterUtilsTest.java @@ -28,14 +28,14 @@ import java.util.Date; import java.util.HashMap; import java.util.Map; -import org.junit.Assert; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.mockito.junit.MockitoJUnitRunner; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.junit.jupiter.MockitoExtension; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -@RunWith(MockitoJUnitRunner.class) +@ExtendWith(MockitoExtension.class) public class ParameterUtilsTest { public static final Logger logger = LoggerFactory.getLogger(ParameterUtilsTest.class); @@ -46,29 +46,29 @@ public class ParameterUtilsTest { @Test public void testConvertParameterPlaceholders() throws ParseException { // parameterString,parameterMap is null - Assert.assertNull(ParameterUtils.convertParameterPlaceholders(null, null)); + Assertions.assertNull(ParameterUtils.convertParameterPlaceholders(null, null)); // parameterString is null,parameterMap is not null Map parameterMap = new HashMap(); parameterMap.put("testParameter", "testParameter"); - Assert.assertNull(ParameterUtils.convertParameterPlaceholders(null, parameterMap)); + Assertions.assertNull(ParameterUtils.convertParameterPlaceholders(null, parameterMap)); // parameterString、parameterMap is not null String parameterString = "test_parameter"; - Assert.assertEquals(parameterString, + Assertions.assertEquals(parameterString, ParameterUtils.convertParameterPlaceholders(parameterString, parameterMap)); // replace variable ${} form parameterMap.put("testParameter2", "${testParameter}"); - Assert.assertEquals(parameterString, PlaceholderUtils.replacePlaceholders(parameterString, parameterMap, true)); + Assertions.assertEquals(parameterString, PlaceholderUtils.replacePlaceholders(parameterString, parameterMap, true)); // replace time $[...] form, eg. $[yyyyMMdd] Date cronTime = new Date(); - Assert.assertEquals(parameterString, replacePlaceholders(parameterString, cronTime, true)); + Assertions.assertEquals(parameterString, replacePlaceholders(parameterString, cronTime, true)); // replace time $[...] form, eg. $[yyyyMMdd] Date cronTimeStr = DateUtils.stringToDate("2019-02-02 00:00:00"); - Assert.assertEquals(parameterString, replacePlaceholders(parameterString, cronTimeStr, true)); + Assertions.assertEquals(parameterString, replacePlaceholders(parameterString, cronTimeStr, true)); } @Test @@ -80,7 +80,7 @@ public class ParameterUtilsTest { parameterMap.put("user", "Kris"); parameterMap.put(Constants.PARAMETER_DATETIME, "20201201123000"); parameterString = ParameterUtils.convertParameterPlaceholders(parameterString, parameterMap); - Assert.assertEquals( + Assertions.assertEquals( "Kris is userName, '$[1]' '20221201' '20181201' '20210301' '20200801' '20201215' '20201117' '20201204' '$[0]' '20201128' '143000' '113000' '123300' '122800' '$[3]'", parameterString); } @@ -90,10 +90,10 @@ public class ParameterUtilsTest { */ @Test public void testHandleEscapes() throws Exception { - Assert.assertNull(ParameterUtils.handleEscapes(null)); - Assert.assertEquals("", ParameterUtils.handleEscapes("")); - Assert.assertEquals("test Parameter", ParameterUtils.handleEscapes("test Parameter")); - Assert.assertEquals("////%test////%Parameter", ParameterUtils.handleEscapes("%test%Parameter")); + Assertions.assertNull(ParameterUtils.handleEscapes(null)); + Assertions.assertEquals("", ParameterUtils.handleEscapes("")); + Assertions.assertEquals("test Parameter", ParameterUtils.handleEscapes("test Parameter")); + Assertions.assertEquals("////%test////%Parameter", ParameterUtils.handleEscapes("%test%Parameter")); } }