]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-2379 Fix ClassCastException in DependencyUnmarshaller
authorEvgeny Mandrikov <mandrikov@gmail.com>
Wed, 20 Apr 2011 17:12:40 +0000 (21:12 +0400)
committerEvgeny Mandrikov <mandrikov@gmail.com>
Wed, 20 Apr 2011 20:39:27 +0000 (00:39 +0400)
GwtUtils.getString returns string representation of a numeric field,
so this is a contract in WSUtils.getString and JdkUtils.getString
must not cast Object to String.

sonar-ws-client/src/main/java/org/sonar/wsclient/services/WSUtils.java
sonar-ws-client/src/main/java/org/sonar/wsclient/unmarshallers/JsonUtils.java
sonar-ws-client/src/test/java/org/sonar/wsclient/unmarshallers/DependencyUnmarshallerTest.java
sonar-ws-client/src/test/java/org/sonar/wsclient/unmarshallers/JsonUtilsTest.java
sonar-ws-client/src/test/resources/dependencies/many.json
sonar-ws-client/src/test/resources/dependencies/single.json

index 05aba34a4d12bf7d4e53976d0850ca0022c20ff6..93c308ca700a09471fc6acdf27fa9427c6adfd46 100644 (file)
@@ -50,7 +50,8 @@ public abstract class WSUtils {
   public abstract Object getField(Object json, String field);
 
   /**
-   * @return String value of specified field from specified JSON object,
+   * @return value of a string field from specified JSON object,
+   *         or string representation of a numeric field,
    *         or <code>null</code> if field does not exist
    */
   public abstract String getString(Object json, String field);
index 008eddc2539b19b639c89530d54178ecd8a2c06e..7ada6ae781ccd36f7eb443be115fabc2b94d831d 100644 (file)
@@ -36,8 +36,8 @@ public final class JsonUtils {
 
   public static String getString(Map obj, String field) {
     Object value = obj.get(field);
-    if (value != null) {
-      return (String) value;
+    if (value instanceof String || value instanceof Number) {
+      return value.toString();
     }
     return null;
   }
index cef3497e56d889d8ff525d4388db982cfa147a3e..bcf9dcd78ef7adf648f78aeeef8104bfdb228129 100644 (file)
@@ -37,7 +37,9 @@ public class DependencyUnmarshallerTest extends UnmarshallerTestCase {
 
     dependency = new DependencyUnmarshaller().toModel(loadFile("/dependencies/single.json"));
     assertThat(dependency.getId(), is("1649"));
+    assertThat(dependency.getFromId(), is(33L));
     assertThat(dependency.getFromKey(), is("org.apache.shiro:shiro-core:org.apache.shiro.authc.pam"));
+    assertThat(dependency.getToId(), is(45L));
     assertThat(dependency.getToKey(), is("org.apache.shiro:shiro-core:org.apache.shiro.realm"));
     assertThat(dependency.getUsage(), is("USES"));
     assertThat(dependency.getWeight(), is(5));
index d14fc150b381c598316faf56f4dff4668f569f5b..ddcf6f9b4c63b776f417488bea162e41d2408ea1 100644 (file)
@@ -56,6 +56,13 @@ public class JsonUtilsTest extends UnmarshallerTestCase {
     assertThat(JsonUtils.getString(obj, "three"), nullValue());
   }
 
+  @Test
+  public void getNumberAsString() {
+    JSONObject obj = (JSONObject) JSONValue.parse("{\"one\": 1, \"two\": 2}");
+    assertThat(JsonUtils.getString(obj, "one"), is("1"));
+    assertThat(JsonUtils.getString(obj, "two"), is("2"));
+  }
+
   @Test
   public void getDateField() {
     JSONObject obj = (JSONObject) JSONValue.parse("{\"foo\": \"2009-12-25\", \"two\": \"2\"}");
index 75e1d00e365cfca568bc7c4b569b022478551abd..b366f300beaf2a6abf1bdb83138168cec99d71d2 100644 (file)
@@ -1,6 +1,6 @@
 [
   {
-    "id":"1649",
+    "id": 1649,
     "fi": 33,
     "ti": 45,
     "fk":"org.apache.shiro:shiro-core:org.apache.shiro.authc.pam",
@@ -9,7 +9,7 @@
     "w":5
   },
   {
-    "id":"1686",
+    "id": 1686,
     "fi": 333,
     "ti": 453,
     "fk":"org.apache.shiro:shiro-core:org.apache.shiro.realm",
@@ -18,7 +18,7 @@
     "w":3
   },
   {
-    "id":"1690",
+    "id": 1690,
     "fi": 334,
     "ti": 454,
     "fk":"org.apache.shiro:shiro-core:org.apache.shiro.realm",
@@ -27,7 +27,7 @@
     "w":2
   },
   {
-    "id":"1693",
+    "id": 1693,
     "fi": 335,
     "ti": 455,
     "fk":"org.apache.shiro:shiro-core:org.apache.shiro.realm",
@@ -36,7 +36,7 @@
     "w":3
   },
   {
-    "id":"1697",
+    "id": 1697,
     "fi": 336,
     "ti": 456,
     "fk":"org.apache.shiro:shiro-core:org.apache.shiro.realm",
@@ -45,7 +45,7 @@
     "w":16
   },
   {
-    "id":"1714",
+    "id": 1714,
     "fi": 337,
     "ti": 457,
     "fk":"org.apache.shiro:shiro-core:org.apache.shiro.realm",
@@ -54,7 +54,7 @@
     "w":6
   },
   {
-    "id":"1721",
+    "id": 1721,
     "fi": 3338,
     "ti": 4258,
     "fk":"org.apache.shiro:shiro-core:org.apache.shiro.realm",
@@ -63,7 +63,7 @@
     "w":9
   },
   {
-    "id":"1731",
+    "id": 1731,
     "fi": 343,
     "ti": 4445,
     "fk":"org.apache.shiro:shiro-core:org.apache.shiro.realm",
@@ -72,7 +72,7 @@
     "w":3
   },
   {
-    "id":"1735",
+    "id": 1735,
     "fi": 3443,
     "ti": 455,
     "fk":"org.apache.shiro:shiro-core:org.apache.shiro.realm.ldap",
@@ -81,7 +81,7 @@
     "w":1
   },
   {
-    "id":"1775",
+    "id": 1775,
     "fi": 373,
     "ti": 6645,
     "fk":"org.apache.shiro:shiro-core:org.apache.shiro.mgt",
@@ -90,7 +90,7 @@
     "w":2
   },
   {
-    "id":"1886",
+    "id": 1886,
     "fi": 339,
     "ti": 495,
     "fk":"org.apache.shiro:shiro-core:org.apache.shiro.realm.jdbc",
@@ -99,7 +99,7 @@
     "w":1
   },
   {
-    "id":"1962",
+    "id": 1962,
     "fi": 373,
     "ti": 485,
     "fk":"org.apache.shiro:shiro-core:org.apache.shiro.config",
     "w":4
   },
   {
-    "id":"1995",
+    "id": 1995,
     "fi": 363,
     "ti": 445,
     "fk":"org.apache.shiro:shiro-core:org.apache.shiro.realm.jndi",
     "w":2
   },
   {
-    "id":"2098",
+    "id": 2098,
     "fi": 333,
     "ti": 415,
     "fk":"org.apache.shiro:shiro-core:org.apache.shiro.realm.text",
     "w":1
   },
   {
-    "id":"2119",
+    "id": 2119,
     "fi": 332,
     "ti": 451,
     "fk":"org.apache.shiro:shiro-core:org.apache.shiro.authz",
index 3afb73ceeabf362242a4e1d11c0be702aa5b561a..84e856799beaa3624fcc44e0fbd2fd94a8061f89 100644 (file)
@@ -1,6 +1,6 @@
 [
   {
-    "id":"1649",
+    "id": 1649,
     "fi": 33,
     "ti": 45,
     "fk":"org.apache.shiro:shiro-core:org.apache.shiro.authc.pam",