Browse Source

Adapting partial code(file name start with J) to the sonar cloud rule (#2165)

* Adapting partial code(file name start with J) to the sonar cloud rule

* fix unit failure
pull/3/MERGE
gabry.wu 5 years ago committed by gaojun2048
parent
commit
bdfb9d6a50
  1. 7
      dolphinscheduler-alert/src/main/java/org/apache/dolphinscheduler/alert/utils/JSONUtils.java
  2. 27
      dolphinscheduler-alert/src/test/java/org/apache/dolphinscheduler/alert/utils/JSONUtilsTest.java
  3. 10
      dolphinscheduler-common/src/main/java/org/apache/dolphinscheduler/common/utils/JSONUtils.java
  4. 18
      dolphinscheduler-common/src/test/java/org/apache/dolphinscheduler/common/utils/JSONUtilsTest.java

7
dolphinscheduler-alert/src/main/java/org/apache/dolphinscheduler/alert/utils/JSONUtils.java

@ -22,6 +22,7 @@ import org.apache.dolphinscheduler.common.utils.StringUtils;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import java.util.Collections;
import java.util.List; import java.util.List;
/** /**
@ -50,11 +51,11 @@ public class JSONUtils {
* @param json the json * @param json the json
* @param clazz c * @param clazz c
* @param <T> the generic clazz * @param <T> the generic clazz
* @return the result list * @return the result list or empty list
*/ */
public static <T> List<T> toList(String json, Class<T> clazz) { public static <T> List<T> toList(String json, Class<T> clazz) {
if (StringUtils.isEmpty(json)) { if (StringUtils.isEmpty(json)) {
return null; return Collections.emptyList();
} }
try { try {
return JSON.parseArray(json, clazz); return JSON.parseArray(json, clazz);
@ -62,7 +63,7 @@ public class JSONUtils {
logger.error("JSONArray.parseArray exception!",e); logger.error("JSONArray.parseArray exception!",e);
} }
return null; return Collections.emptyList();
} }
} }

27
dolphinscheduler-alert/src/test/java/org/apache/dolphinscheduler/alert/utils/JSONUtilsTest.java

@ -26,8 +26,7 @@ import java.util.ArrayList;
import java.util.LinkedHashMap; import java.util.LinkedHashMap;
import java.util.List; import java.util.List;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.*;
import static org.junit.Assert.assertNull;
public class JSONUtilsTest { public class JSONUtilsTest {
@ -73,7 +72,7 @@ public class JSONUtilsTest {
result = JSONUtils.toJsonString(null); result = JSONUtils.toJsonString(null);
logger.info(result); logger.info(result);
assertEquals(result,"null"); assertEquals("null", result);
} }
@ -86,25 +85,27 @@ public class JSONUtilsTest {
//Invoke toList //Invoke toList
List<LinkedHashMap> result = JSONUtils.toList(expected ,LinkedHashMap.class); List<LinkedHashMap> result = JSONUtils.toList(expected ,LinkedHashMap.class);
//Equal list size=1 //Equal list size=1
assertEquals(result.size(),1); assertEquals(1,result.size());
//Transform entity to LinkedHashMap<String, Object> //Transform entity to LinkedHashMap<String, Object>
LinkedHashMap<String, Object> entity = result.get(0); LinkedHashMap<String, Object> entity = result.get(0);
//Equal expected values //Equal expected values
assertEquals(entity.get("mysql service name"),"mysql200"); assertEquals("mysql200",entity.get("mysql service name"));
assertEquals(entity.get("mysql address"),"192.168.xx.xx"); assertEquals("192.168.xx.xx", entity.get("mysql address"));
assertEquals(entity.get("port"),"3306"); assertEquals("3306", entity.get("port"));
assertEquals(entity.get("no index of number"),"80"); assertEquals("80", entity.get("no index of number"));
assertEquals(entity.get("database client connections"),"190"); assertEquals("190", entity.get("database client connections"));
//If param is null, then return null //If param is null, then return empty list
result = JSONUtils.toList(null ,LinkedHashMap.class); result = JSONUtils.toList(null ,LinkedHashMap.class);
assertNull(result); assertNotNull(result);
assertTrue(result.isEmpty());
//If param is incorrect, then return null and log error message //If param is incorrect, then return empty list and log error message
result = JSONUtils.toList("}{" ,LinkedHashMap.class); result = JSONUtils.toList("}{" ,LinkedHashMap.class);
assertNull(result); assertNotNull(result);
assertTrue(result.isEmpty());
} }

10
dolphinscheduler-common/src/main/java/org/apache/dolphinscheduler/common/utils/JSONUtils.java

@ -42,12 +42,6 @@ public class JSONUtils {
*/ */
private static final ObjectMapper objectMapper = new ObjectMapper(); private static final ObjectMapper objectMapper = new ObjectMapper();
/**
* init
*/
private static final JSONUtils instance = new JSONUtils();
private JSONUtils() { private JSONUtils() {
//Feature that determines whether encountering of unknown properties, false means not analyzer unknown properties //Feature that determines whether encountering of unknown properties, false means not analyzer unknown properties
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false).setTimeZone(TimeZone.getDefault()); objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false).setTimeZone(TimeZone.getDefault());
@ -221,7 +215,7 @@ public class JSONUtils {
try{ try{
return JSON.toJSONString(object,false); return JSON.toJSONString(object,false);
} catch (Exception e) { } catch (Exception e) {
throw new RuntimeException("Json deserialization exception.", e); throw new RuntimeException("Object json deserialization exception.", e);
} }
} }
@ -229,7 +223,7 @@ public class JSONUtils {
try{ try{
return JSON.parseObject(text); return JSON.parseObject(text);
} catch (Exception e) { } catch (Exception e) {
throw new RuntimeException("Json deserialization exception.", e); throw new RuntimeException("String json deserialization exception.", e);
} }
} }

18
dolphinscheduler-common/src/test/java/org/apache/dolphinscheduler/common/utils/JSONUtilsTest.java

@ -40,8 +40,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(models.get("id"), "1001"); Assert.assertEquals("1001", models.get("id"));
Assert.assertEquals(models.get("name"), "Jobs"); Assert.assertEquals("Jobs", models.get("name"));
} }
@ -55,7 +55,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 = JSON.parseObject(str, Property.class); Property property1 = JSON.parseObject(str, Property.class);
Direct direct = property1.getDirect(); Direct direct = property1.getDirect();
Assert.assertEquals(direct , Direct.IN); Assert.assertEquals(Direct.IN, direct);
} }
@ -66,12 +66,12 @@ public class JSONUtilsTest {
List<LinkedHashMap> maps = JSONUtils.toList(str, List<LinkedHashMap> maps = JSONUtils.toList(str,
LinkedHashMap.class); LinkedHashMap.class);
Assert.assertEquals(maps.size(), 1); Assert.assertEquals(1, maps.size());
Assert.assertEquals(maps.get(0).get("mysql service name"), "mysql200"); Assert.assertEquals("mysql200", maps.get(0).get("mysql service name"));
Assert.assertEquals(maps.get(0).get("mysql address"), "192.168.xx.xx"); Assert.assertEquals("192.168.xx.xx", maps.get(0).get("mysql address"));
Assert.assertEquals(maps.get(0).get("port"), "3306"); Assert.assertEquals("3306", maps.get(0).get("port"));
Assert.assertEquals(maps.get(0).get("no index of number"), "80"); Assert.assertEquals("80", maps.get(0).get("no index of number"));
Assert.assertEquals(maps.get(0).get("database client connections"), "190"); Assert.assertEquals("190", maps.get(0).get("database client connections"));
} }
public String list2String(){ public String list2String(){

Loading…
Cancel
Save