Browse Source

[Migrate][Test] Migrate other UT cases from jUnit 4 to 5, change @Ignore to @Disable

3.2.0-release
Yann Ann 2 years ago committed by GitHub
parent
commit
7aa8a77fb2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 4
      docs/docs/en/contribute/join/unit-test.md
  2. 4
      docs/docs/zh/contribute/join/unit-test.md
  3. 94
      dolphinscheduler-task-plugin/dolphinscheduler-task-api/src/test/java/org/apache/dolphinscheduler/plugin/task/api/model/JSONUtilsTest.java
  4. 32
      dolphinscheduler-task-plugin/dolphinscheduler-task-api/src/test/java/org/apache/dolphinscheduler/plugin/task/api/utils/ParameterUtilsTest.java

4
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 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 3: try-catch Unit test exception

4
docs/docs/zh/contribute/join/unit-test.md

@ -86,9 +86,9 @@ Awaitility.await().atMost(…)
2:忽略某些测试类 2:忽略某些测试类
@Ignore 注解应该附上相关 issue 地址,方便后续开发者追踪了解该测试被忽略的历史原因。 @Disabled 注解应该附上相关 issue 地址,方便后续开发者追踪了解该测试被忽略的历史原因。
@Ignore("see #1") @Disabled("see #1")
3: try-catch 单元测试异常 3: try-catch 单元测试异常

94
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.Map;
import java.util.TimeZone; import java.util.TimeZone;
import org.junit.Assert; import org.junit.jupiter.api.Assertions;
import org.junit.Test; import org.junit.jupiter.api.Test;
import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.SerializationFeature; import com.fasterxml.jackson.databind.SerializationFeature;
@ -59,7 +59,7 @@ public class JSONUtilsTest {
ArrayNode jsonNodes = arrayNode.addAll(objects); ArrayNode jsonNodes = arrayNode.addAll(objects);
String s = JSONUtils.toJsonString(jsonNodes); 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); JsonNode jsonNodes = JSONUtils.toJsonNode(property);
String s = JSONUtils.toJsonString(jsonNodes); 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("a", "b");
objectNode.put("b", "d"); objectNode.put("b", "d");
String s = JSONUtils.toJsonString(objectNode); String s = JSONUtils.toJsonString(objectNode);
Assert.assertEquals(s, jsonStr); Assertions.assertEquals(s, jsonStr);
} }
@Test @Test
@ -95,8 +95,8 @@ public class JSONUtilsTest {
String jsonStr = "{\"id\":\"1001\",\"name\":\"Jobs\"}"; String jsonStr = "{\"id\":\"1001\",\"name\":\"Jobs\"}";
Map<String, String> models = JSONUtils.toMap(jsonStr); Map<String, String> models = JSONUtils.toMap(jsonStr);
Assert.assertEquals("1001", models.get("id")); Assertions.assertEquals("1001", models.get("id"));
Assert.assertEquals("Jobs", models.get("name")); Assertions.assertEquals("Jobs", models.get("name"));
} }
@ -110,7 +110,7 @@ public class JSONUtilsTest {
String str = "{\"direct\":\"IN\",\"prop\":\"ds\",\"type\":\"VARCHAR\",\"value\":\"sssssss\"}"; String str = "{\"direct\":\"IN\",\"prop\":\"ds\",\"type\":\"VARCHAR\",\"value\":\"sssssss\"}";
Property property1 = JSONUtils.parseObject(str, Property.class); Property property1 = JSONUtils.parseObject(str, Property.class);
Direct direct = property1.getDirect(); Direct direct = property1.getDirect();
Assert.assertEquals(Direct.IN, direct); Assertions.assertEquals(Direct.IN, direct);
} }
@Test @Test
@ -120,12 +120,12 @@ public class JSONUtilsTest {
List<LinkedHashMap> maps = JSONUtils.toList(str, List<LinkedHashMap> maps = JSONUtils.toList(str,
LinkedHashMap.class); LinkedHashMap.class);
Assert.assertEquals(1, maps.size()); Assertions.assertEquals(1, maps.size());
Assert.assertEquals("mysql200", maps.get(0).get("mysql service name")); Assertions.assertEquals("mysql200", maps.get(0).get("mysql service name"));
Assert.assertEquals("192.168.xx.xx", maps.get(0).get("mysql address")); Assertions.assertEquals("192.168.xx.xx", maps.get(0).get("mysql address"));
Assert.assertEquals("3306", maps.get(0).get("port")); Assertions.assertEquals("3306", maps.get(0).get("port"));
Assert.assertEquals("80", maps.get(0).get("no index of number")); Assertions.assertEquals("80", maps.get(0).get("no index of number"));
Assert.assertEquals("190", maps.get(0).get("database client connections")); Assertions.assertEquals("190", maps.get(0).get("database client connections"));
} }
public String list2String() { public String list2String() {
@ -145,19 +145,18 @@ public class JSONUtilsTest {
@Test @Test
public void testParseObject() { public void testParseObject() {
Assert.assertNull(JSONUtils.parseObject("")); Assertions.assertNull(JSONUtils.parseObject(""));
Assert.assertNull(JSONUtils.parseObject("foo", String.class)); Assertions.assertNull(JSONUtils.parseObject("foo", String.class));
} }
@Test @Test
public void testNodeString() { public void testNodeString() {
Assert.assertEquals("", JSONUtils.getNodeString("", "key")); Assertions.assertEquals("", JSONUtils.getNodeString("", "key"));
Assert.assertEquals("", JSONUtils.getNodeString("abc", "key")); Assertions.assertEquals("", JSONUtils.getNodeString("abc", "key"));
Assert.assertEquals("", JSONUtils.getNodeString("{\"bar\":\"foo\"}", "key")); Assertions.assertEquals("", JSONUtils.getNodeString("{\"bar\":\"foo\"}", "key"));
Assert.assertEquals("foo", JSONUtils.getNodeString("{\"bar\":\"foo\"}", "bar")); Assertions.assertEquals("foo", JSONUtils.getNodeString("{\"bar\":\"foo\"}", "bar"));
Assert.assertEquals("[1,2,3]", JSONUtils.getNodeString("{\"bar\": [1,2,3]}", "bar")); Assertions.assertEquals("[1,2,3]", JSONUtils.getNodeString("{\"bar\": [1,2,3]}", "bar"));
Assert.assertEquals("{\"1\":\"2\",\"2\":3}", Assertions.assertEquals("{\"1\":\"2\",\"2\":3}", JSONUtils.getNodeString("{\"bar\": {\"1\":\"2\",\"2\":3}}", "bar"));
JSONUtils.getNodeString("{\"bar\": {\"1\":\"2\",\"2\":3}}", "bar"));
} }
@Test @Test
@ -165,30 +164,28 @@ public class JSONUtilsTest {
String str = "foo"; String str = "foo";
byte[] serializeByte = JSONUtils.toJsonByteArray(str); byte[] serializeByte = JSONUtils.toJsonByteArray(str);
String deserialize = JSONUtils.parseObject(serializeByte, String.class); String deserialize = JSONUtils.parseObject(serializeByte, String.class);
Assert.assertEquals(str, deserialize); Assertions.assertEquals(str, deserialize);
str = null; str = null;
serializeByte = JSONUtils.toJsonByteArray(str); serializeByte = JSONUtils.toJsonByteArray(str);
deserialize = JSONUtils.parseObject(serializeByte, String.class); deserialize = JSONUtils.parseObject(serializeByte, String.class);
Assert.assertNull(deserialize); Assertions.assertNull(deserialize);
} }
@Test @Test
public void testToList() { public void testToList() {
Assert.assertEquals(new ArrayList(), Assertions.assertEquals(new ArrayList(), JSONUtils.toList("A1B2C3", null));
JSONUtils.toList("A1B2C3", null)); Assertions.assertEquals(new ArrayList(), JSONUtils.toList("", null));
Assert.assertEquals(new ArrayList(),
JSONUtils.toList("", null));
} }
@Test @Test
public void testCheckJsonValid() { public void testCheckJsonValid() {
Assert.assertTrue(JSONUtils.checkJsonValid("3")); Assertions.assertTrue(JSONUtils.checkJsonValid("3"));
Assert.assertFalse(JSONUtils.checkJsonValid("")); Assertions.assertFalse(JSONUtils.checkJsonValid(""));
} }
@Test @Test
public void testFindValue() { public void testFindValue() {
Assert.assertNull(JSONUtils.findValue( Assertions.assertNull(JSONUtils.findValue(
new ArrayNode(new JsonNodeFactory(true)), null)); new ArrayNode(new JsonNodeFactory(true)), null));
} }
@ -197,18 +194,18 @@ public class JSONUtilsTest {
Map<String, String> map = new HashMap<>(); Map<String, String> map = new HashMap<>();
map.put("foo", "bar"); map.put("foo", "bar");
Assert.assertTrue(map.equals(JSONUtils.toMap( Assertions.assertTrue(map.equals(JSONUtils.toMap(
"{\n" + "\"foo\": \"bar\"\n" + "}"))); "{\n" + "\"foo\": \"bar\"\n" + "}")));
Assert.assertFalse(map.equals(JSONUtils.toMap( Assertions.assertFalse(map.equals(JSONUtils.toMap(
"{\n" + "\"bar\": \"foo\"\n" + "}"))); "{\n" + "\"bar\": \"foo\"\n" + "}")));
Assert.assertNull(JSONUtils.toMap("3")); Assertions.assertNull(JSONUtils.toMap("3"));
Assert.assertNull(JSONUtils.toMap(null)); Assertions.assertNull(JSONUtils.toMap(null));
String str = "{\"resourceList\":[],\"localParams\":[],\"rawScript\":\"#!/bin/bash\\necho \\\"shell-1\\\"\"}"; String str = "{\"resourceList\":[],\"localParams\":[],\"rawScript\":\"#!/bin/bash\\necho \\\"shell-1\\\"\"}";
Map<String, String> m = JSONUtils.toMap(str); Map<String, String> m = JSONUtils.toMap(str);
Assert.assertNotNull(m); Assertions.assertNotNull(m);
} }
@Test @Test
@ -216,13 +213,10 @@ public class JSONUtilsTest {
Map<String, Object> map = new HashMap<>(); Map<String, Object> map = new HashMap<>();
map.put("foo", "bar"); map.put("foo", "bar");
Assert.assertEquals("{\"foo\":\"bar\"}", Assertions.assertEquals("{\"foo\":\"bar\"}", JSONUtils.toJsonString(map));
JSONUtils.toJsonString(map)); Assertions.assertEquals(String.valueOf((Object) null), JSONUtils.toJsonString(null));
Assert.assertEquals(String.valueOf((Object) null),
JSONUtils.toJsonString(null));
Assert.assertEquals("{\"foo\":\"bar\"}", Assertions.assertEquals("{\"foo\":\"bar\"}", JSONUtils.toJsonString(map, SerializationFeature.WRITE_NULL_MAP_VALUES));
JSONUtils.toJsonString(map, SerializationFeature.WRITE_NULL_MAP_VALUES));
} }
@Test @Test
@ -230,13 +224,13 @@ public class JSONUtilsTest {
String str = "{\"color\":\"yellow\",\"type\":\"renault\"}"; String str = "{\"color\":\"yellow\",\"type\":\"renault\"}";
ObjectNode node = JSONUtils.parseObject(str); ObjectNode node = JSONUtils.parseObject(str);
Assert.assertEquals("yellow", node.path("color").asText()); Assertions.assertEquals("yellow", node.path("color").asText());
node.put("price", 100); node.put("price", 100);
Assert.assertEquals(100, node.path("price").asInt()); Assertions.assertEquals(100, node.path("price").asInt());
node.put("color", "red"); node.put("color", "red");
Assert.assertEquals("red", node.path("color").asText()); Assertions.assertEquals("red", node.path("color").asText());
} }
@Test @Test
@ -244,7 +238,7 @@ public class JSONUtilsTest {
String str = "[{\"color\":\"yellow\",\"type\":\"renault\"}]"; String str = "[{\"color\":\"yellow\",\"type\":\"renault\"}]";
ArrayNode node = JSONUtils.parseArray(str); ArrayNode node = JSONUtils.parseArray(str);
Assert.assertEquals("yellow", node.path(0).path("color").asText()); Assertions.assertEquals("yellow", node.path(0).path("color").asText());
} }
@Test @Test
@ -256,10 +250,10 @@ public class JSONUtilsTest {
String time = "2022-02-22 13:38:24"; String time = "2022-02-22 13:38:24";
Date date = DateUtils.stringToDate(time); Date date = DateUtils.stringToDate(time);
String json = JSONUtils.toJsonString(date); String json = JSONUtils.toJsonString(date);
Assert.assertEquals("\"" + time + "\"", json); Assertions.assertEquals("\"" + time + "\"", json);
String errorFormatTime = "Tue Feb 22 03:50:00 UTC 2022"; String errorFormatTime = "Tue Feb 22 03:50:00 UTC 2022";
Assert.assertNull(DateUtils.stringToDate(errorFormatTime)); Assertions.assertNull(DateUtils.stringToDate(errorFormatTime));
} }
@Test @Test
@ -270,7 +264,7 @@ public class JSONUtilsTest {
String json = "\"2022-02-22 13:38:24\""; String json = "\"2022-02-22 13:38:24\"";
Date date = JSONUtils.parseObject(json, Date.class); 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);
} }

32
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.HashMap;
import java.util.Map; import java.util.Map;
import org.junit.Assert; import org.junit.jupiter.api.Assertions;
import org.junit.Test; import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith; import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.junit.MockitoJUnitRunner; import org.mockito.junit.jupiter.MockitoExtension;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@RunWith(MockitoJUnitRunner.class) @ExtendWith(MockitoExtension.class)
public class ParameterUtilsTest { public class ParameterUtilsTest {
public static final Logger logger = LoggerFactory.getLogger(ParameterUtilsTest.class); public static final Logger logger = LoggerFactory.getLogger(ParameterUtilsTest.class);
@ -46,29 +46,29 @@ public class ParameterUtilsTest {
@Test @Test
public void testConvertParameterPlaceholders() throws ParseException { public void testConvertParameterPlaceholders() throws ParseException {
// parameterString,parameterMap is null // parameterString,parameterMap is null
Assert.assertNull(ParameterUtils.convertParameterPlaceholders(null, null)); Assertions.assertNull(ParameterUtils.convertParameterPlaceholders(null, null));
// parameterString is null,parameterMap is not null // parameterString is null,parameterMap is not null
Map<String, String> parameterMap = new HashMap<String, String>(); Map<String, String> parameterMap = new HashMap<String, String>();
parameterMap.put("testParameter", "testParameter"); parameterMap.put("testParameter", "testParameter");
Assert.assertNull(ParameterUtils.convertParameterPlaceholders(null, parameterMap)); Assertions.assertNull(ParameterUtils.convertParameterPlaceholders(null, parameterMap));
// parameterString、parameterMap is not null // parameterString、parameterMap is not null
String parameterString = "test_parameter"; String parameterString = "test_parameter";
Assert.assertEquals(parameterString, Assertions.assertEquals(parameterString,
ParameterUtils.convertParameterPlaceholders(parameterString, parameterMap)); ParameterUtils.convertParameterPlaceholders(parameterString, parameterMap));
// replace variable ${} form // replace variable ${} form
parameterMap.put("testParameter2", "${testParameter}"); 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] // replace time $[...] form, eg. $[yyyyMMdd]
Date cronTime = new Date(); Date cronTime = new Date();
Assert.assertEquals(parameterString, replacePlaceholders(parameterString, cronTime, true)); Assertions.assertEquals(parameterString, replacePlaceholders(parameterString, cronTime, true));
// replace time $[...] form, eg. $[yyyyMMdd] // replace time $[...] form, eg. $[yyyyMMdd]
Date cronTimeStr = DateUtils.stringToDate("2019-02-02 00:00:00"); 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 @Test
@ -80,7 +80,7 @@ public class ParameterUtilsTest {
parameterMap.put("user", "Kris"); parameterMap.put("user", "Kris");
parameterMap.put(Constants.PARAMETER_DATETIME, "20201201123000"); parameterMap.put(Constants.PARAMETER_DATETIME, "20201201123000");
parameterString = ParameterUtils.convertParameterPlaceholders(parameterString, parameterMap); 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]'", "Kris is userName, '$[1]' '20221201' '20181201' '20210301' '20200801' '20201215' '20201117' '20201204' '$[0]' '20201128' '143000' '113000' '123300' '122800' '$[3]'",
parameterString); parameterString);
} }
@ -90,10 +90,10 @@ public class ParameterUtilsTest {
*/ */
@Test @Test
public void testHandleEscapes() throws Exception { public void testHandleEscapes() throws Exception {
Assert.assertNull(ParameterUtils.handleEscapes(null)); Assertions.assertNull(ParameterUtils.handleEscapes(null));
Assert.assertEquals("", ParameterUtils.handleEscapes("")); Assertions.assertEquals("", ParameterUtils.handleEscapes(""));
Assert.assertEquals("test Parameter", ParameterUtils.handleEscapes("test Parameter")); Assertions.assertEquals("test Parameter", ParameterUtils.handleEscapes("test Parameter"));
Assert.assertEquals("////%test////%Parameter", ParameterUtils.handleEscapes("%test%Parameter")); Assertions.assertEquals("////%test////%Parameter", ParameterUtils.handleEscapes("%test%Parameter"));
} }
} }

Loading…
Cancel
Save