aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-ws-client
diff options
context:
space:
mode:
authorEvgeny Mandrikov <mandrikov@gmail.com>2011-04-20 21:12:40 +0400
committerEvgeny Mandrikov <mandrikov@gmail.com>2011-04-21 00:39:27 +0400
commitfdf6f4dcc2a4ee620dde9cdb5e2909ec4bd8c7f9 (patch)
tree66e60139be70a9d5903ae44b8806664e87769158 /sonar-ws-client
parent07b20316ee247a1694eb9191f985cb2de2d08b49 (diff)
downloadsonarqube-fdf6f4dcc2a4ee620dde9cdb5e2909ec4bd8c7f9.tar.gz
sonarqube-fdf6f4dcc2a4ee620dde9cdb5e2909ec4bd8c7f9.zip
SONAR-2379 Fix ClassCastException in DependencyUnmarshaller
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.
Diffstat (limited to 'sonar-ws-client')
-rw-r--r--sonar-ws-client/src/main/java/org/sonar/wsclient/services/WSUtils.java3
-rw-r--r--sonar-ws-client/src/main/java/org/sonar/wsclient/unmarshallers/JsonUtils.java4
-rw-r--r--sonar-ws-client/src/test/java/org/sonar/wsclient/unmarshallers/DependencyUnmarshallerTest.java2
-rw-r--r--sonar-ws-client/src/test/java/org/sonar/wsclient/unmarshallers/JsonUtilsTest.java7
-rw-r--r--sonar-ws-client/src/test/resources/dependencies/many.json30
-rw-r--r--sonar-ws-client/src/test/resources/dependencies/single.json2
6 files changed, 29 insertions, 19 deletions
diff --git a/sonar-ws-client/src/main/java/org/sonar/wsclient/services/WSUtils.java b/sonar-ws-client/src/main/java/org/sonar/wsclient/services/WSUtils.java
index 05aba34a4d1..93c308ca700 100644
--- a/sonar-ws-client/src/main/java/org/sonar/wsclient/services/WSUtils.java
+++ b/sonar-ws-client/src/main/java/org/sonar/wsclient/services/WSUtils.java
@@ -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);
diff --git a/sonar-ws-client/src/main/java/org/sonar/wsclient/unmarshallers/JsonUtils.java b/sonar-ws-client/src/main/java/org/sonar/wsclient/unmarshallers/JsonUtils.java
index 008eddc2539..7ada6ae781c 100644
--- a/sonar-ws-client/src/main/java/org/sonar/wsclient/unmarshallers/JsonUtils.java
+++ b/sonar-ws-client/src/main/java/org/sonar/wsclient/unmarshallers/JsonUtils.java
@@ -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;
}
diff --git a/sonar-ws-client/src/test/java/org/sonar/wsclient/unmarshallers/DependencyUnmarshallerTest.java b/sonar-ws-client/src/test/java/org/sonar/wsclient/unmarshallers/DependencyUnmarshallerTest.java
index cef3497e56d..bcf9dcd78ef 100644
--- a/sonar-ws-client/src/test/java/org/sonar/wsclient/unmarshallers/DependencyUnmarshallerTest.java
+++ b/sonar-ws-client/src/test/java/org/sonar/wsclient/unmarshallers/DependencyUnmarshallerTest.java
@@ -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));
diff --git a/sonar-ws-client/src/test/java/org/sonar/wsclient/unmarshallers/JsonUtilsTest.java b/sonar-ws-client/src/test/java/org/sonar/wsclient/unmarshallers/JsonUtilsTest.java
index d14fc150b38..ddcf6f9b4c6 100644
--- a/sonar-ws-client/src/test/java/org/sonar/wsclient/unmarshallers/JsonUtilsTest.java
+++ b/sonar-ws-client/src/test/java/org/sonar/wsclient/unmarshallers/JsonUtilsTest.java
@@ -57,6 +57,13 @@ public class JsonUtilsTest extends UnmarshallerTestCase {
}
@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\"}");
Date date = JsonUtils.getDate(obj, "foo");
diff --git a/sonar-ws-client/src/test/resources/dependencies/many.json b/sonar-ws-client/src/test/resources/dependencies/many.json
index 75e1d00e365..b366f300bea 100644
--- a/sonar-ws-client/src/test/resources/dependencies/many.json
+++ b/sonar-ws-client/src/test/resources/dependencies/many.json
@@ -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",
@@ -108,7 +108,7 @@
"w":4
},
{
- "id":"1995",
+ "id": 1995,
"fi": 363,
"ti": 445,
"fk":"org.apache.shiro:shiro-core:org.apache.shiro.realm.jndi",
@@ -117,7 +117,7 @@
"w":2
},
{
- "id":"2098",
+ "id": 2098,
"fi": 333,
"ti": 415,
"fk":"org.apache.shiro:shiro-core:org.apache.shiro.realm.text",
@@ -126,7 +126,7 @@
"w":1
},
{
- "id":"2119",
+ "id": 2119,
"fi": 332,
"ti": 451,
"fk":"org.apache.shiro:shiro-core:org.apache.shiro.authz",
diff --git a/sonar-ws-client/src/test/resources/dependencies/single.json b/sonar-ws-client/src/test/resources/dependencies/single.json
index 3afb73ceeab..84e856799be 100644
--- a/sonar-ws-client/src/test/resources/dependencies/single.json
+++ b/sonar-ws-client/src/test/resources/dependencies/single.json
@@ -1,6 +1,6 @@
[
{
- "id":"1649",
+ "id": 1649,
"fi": 33,
"ti": 45,
"fk":"org.apache.shiro:shiro-core:org.apache.shiro.authc.pam",