瀏覽代碼

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
gabry.wu 5 年之前
父節點
當前提交
a076ae4c36

+ 4 - 3
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.LoggerFactory;
 
+import java.util.Collections;
 import java.util.List;
 
 /**
@@ -50,11 +51,11 @@ public class JSONUtils {
    * @param json the json
    * @param clazz c
    * @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) {
     if (StringUtils.isEmpty(json)) {
-      return null;
+      return Collections.emptyList();
     }
     try {
       return JSON.parseArray(json, clazz);
@@ -62,7 +63,7 @@ public class JSONUtils {
       logger.error("JSONArray.parseArray exception!",e);
     }
 
-    return null;
+    return Collections.emptyList();
   }
 
 }

+ 14 - 13
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.List;
 
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertNull;
+import static org.junit.Assert.*;
 
 public class JSONUtilsTest {
 
@@ -73,7 +72,7 @@ public class JSONUtilsTest {
         result = JSONUtils.toJsonString(null);
         logger.info(result);
 
-        assertEquals(result,"null");
+        assertEquals("null", result);
 
     }
 
@@ -86,25 +85,27 @@ public class JSONUtilsTest {
         //Invoke toList
         List<LinkedHashMap> result = JSONUtils.toList(expected ,LinkedHashMap.class);
         //Equal list size=1
-        assertEquals(result.size(),1);
+        assertEquals(1,result.size());
 
         //Transform entity to LinkedHashMap<String, Object>
         LinkedHashMap<String, Object> entity = result.get(0);
 
         //Equal expected values
-        assertEquals(entity.get("mysql service name"),"mysql200");
-        assertEquals(entity.get("mysql address"),"192.168.xx.xx");
-        assertEquals(entity.get("port"),"3306");
-        assertEquals(entity.get("no index of number"),"80");
-        assertEquals(entity.get("database client connections"),"190");
+        assertEquals("mysql200",entity.get("mysql service name"));
+        assertEquals("192.168.xx.xx", entity.get("mysql address"));
+        assertEquals("3306", entity.get("port"));
+        assertEquals("80", entity.get("no index of number"));
+        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);
-        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);
-        assertNull(result);
+        assertNotNull(result);
+        assertTrue(result.isEmpty());
 
     }
 

+ 2 - 8
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();
 
-  /**
-   * init
-   */
-  private static final JSONUtils instance = new JSONUtils();
-
-
   private JSONUtils() {
     //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());
@@ -221,7 +215,7 @@ public class JSONUtils {
     try{
       return JSON.toJSONString(object,false);
     } 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{
       return JSON.parseObject(text);
     } catch (Exception e) {
-      throw new RuntimeException("Json deserialization exception.", e);
+      throw new RuntimeException("String json deserialization exception.", e);
     }
   }
 

+ 9 - 9
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\"}";
 
         Map<String,String> models = JSONUtils.toMap(jsonStr);
-        Assert.assertEquals(models.get("id"), "1001");
-        Assert.assertEquals(models.get("name"), "Jobs");
+        Assert.assertEquals("1001", models.get("id"));
+        Assert.assertEquals("Jobs", models.get("name"));
 
     }
 
@@ -55,7 +55,7 @@ public class JSONUtilsTest {
         String str = "{\"direct\":\"IN\",\"prop\":\"ds\",\"type\":\"VARCHAR\",\"value\":\"sssssss\"}";
         Property property1 = JSON.parseObject(str, Property.class);
         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,
                 LinkedHashMap.class);
 
-        Assert.assertEquals(maps.size(), 1);
-        Assert.assertEquals(maps.get(0).get("mysql service name"), "mysql200");
-        Assert.assertEquals(maps.get(0).get("mysql address"), "192.168.xx.xx");
-        Assert.assertEquals(maps.get(0).get("port"), "3306");
-        Assert.assertEquals(maps.get(0).get("no index of number"), "80");
-        Assert.assertEquals(maps.get(0).get("database client connections"), "190");
+        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"));
     }
 
     public String list2String(){