aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-ws-client/src
diff options
context:
space:
mode:
authorEvgeny Mandrikov <mandrikov@gmail.com>2011-01-26 19:21:54 +0300
committerEvgeny Mandrikov <mandrikov@gmail.com>2011-01-27 17:11:11 +0300
commit815899837ca07904fe9dc9223b4a4e8ac486cb59 (patch)
treeb92834ad5dd92bdd9eb8504e8bd649e81e559d41 /sonar-ws-client/src
parent40e0e78a54535e1a5009a626fb43dccad9230328 (diff)
downloadsonarqube-815899837ca07904fe9dc9223b4a4e8ac486cb59.tar.gz
sonarqube-815899837ca07904fe9dc9223b4a4e8ac486cb59.zip
SONAR-2046: Reuse unmarshallers from sonar-ws-client for sonar-gwt-api
* WSUtils has two implementations - one for GWT and another for Java, so can be used by unmarshallers in both cases. But this not very good, because code is not type-safe. In fact it would be better to use emulation of types from org.json.simple for GWT, but this solution is more straightforward.
Diffstat (limited to 'sonar-ws-client/src')
-rw-r--r--sonar-ws-client/src/main/java/org/sonar/wsclient/JdkUtils.java59
-rw-r--r--sonar-ws-client/src/main/java/org/sonar/wsclient/services/WSUtils.java69
-rw-r--r--sonar-ws-client/src/main/java/org/sonar/wsclient/unmarshallers/AbstractUnmarshaller.java22
-rw-r--r--sonar-ws-client/src/main/java/org/sonar/wsclient/unmarshallers/DependencyTreeUnmarshaller.java30
-rw-r--r--sonar-ws-client/src/main/java/org/sonar/wsclient/unmarshallers/DependencyUnmarshaller.java29
-rw-r--r--sonar-ws-client/src/main/java/org/sonar/wsclient/unmarshallers/EventUnmarshaller.java19
-rw-r--r--sonar-ws-client/src/main/java/org/sonar/wsclient/unmarshallers/FavouriteUnmarshaller.java17
-rw-r--r--sonar-ws-client/src/main/java/org/sonar/wsclient/unmarshallers/MetricUnmarshaller.java21
-rw-r--r--sonar-ws-client/src/main/java/org/sonar/wsclient/unmarshallers/PluginUnmarshaller.java11
-rw-r--r--sonar-ws-client/src/main/java/org/sonar/wsclient/unmarshallers/PropertyUnmarshaller.java11
-rw-r--r--sonar-ws-client/src/main/java/org/sonar/wsclient/unmarshallers/ResourceUnmarshaller.java110
-rw-r--r--sonar-ws-client/src/main/java/org/sonar/wsclient/unmarshallers/RuleUnmarshaller.java45
-rw-r--r--sonar-ws-client/src/main/java/org/sonar/wsclient/unmarshallers/ServerUnmarshaller.java14
-rw-r--r--sonar-ws-client/src/main/java/org/sonar/wsclient/unmarshallers/SourceUnmarshaller.java12
-rw-r--r--sonar-ws-client/src/main/java/org/sonar/wsclient/unmarshallers/TimeMachineUnmarshaller.java36
-rw-r--r--sonar-ws-client/src/main/java/org/sonar/wsclient/unmarshallers/ViolationUnmarshaller.java30
-rw-r--r--sonar-ws-client/src/test/java/org/sonar/wsclient/unmarshallers/DependencyTreeUnmarshallerTest.java6
-rw-r--r--sonar-ws-client/src/test/java/org/sonar/wsclient/unmarshallers/DependencyUnmarshallerTest.java6
-rw-r--r--sonar-ws-client/src/test/java/org/sonar/wsclient/unmarshallers/EventUnmarshallerTest.java4
-rw-r--r--sonar-ws-client/src/test/java/org/sonar/wsclient/unmarshallers/JsonUtilsTest.java2
-rw-r--r--sonar-ws-client/src/test/java/org/sonar/wsclient/unmarshallers/MetricUnmarshallerTest.java8
-rw-r--r--sonar-ws-client/src/test/java/org/sonar/wsclient/unmarshallers/PluginUnmarshallerTest.java8
-rw-r--r--sonar-ws-client/src/test/java/org/sonar/wsclient/unmarshallers/PropertyUnmarshallerTest.java8
-rw-r--r--sonar-ws-client/src/test/java/org/sonar/wsclient/unmarshallers/ResourceUnmarshallerTest.java12
-rw-r--r--sonar-ws-client/src/test/java/org/sonar/wsclient/unmarshallers/RuleUnmarshallerTest.java4
-rw-r--r--sonar-ws-client/src/test/java/org/sonar/wsclient/unmarshallers/ServerUnmarshallerTest.java6
-rw-r--r--sonar-ws-client/src/test/java/org/sonar/wsclient/unmarshallers/SourceUnmarshallerTest.java6
-rw-r--r--sonar-ws-client/src/test/java/org/sonar/wsclient/unmarshallers/TimeMachineUnmarshallerTest.java8
-rw-r--r--sonar-ws-client/src/test/java/org/sonar/wsclient/unmarshallers/UnmarshallerTestCase.java25
-rw-r--r--sonar-ws-client/src/test/java/org/sonar/wsclient/unmarshallers/ViolationUnmarshallerTest.java6
-rw-r--r--sonar-ws-client/src/test/java/org/sonar/wsclient/unmarshallers/WSTestUtils.java39
31 files changed, 410 insertions, 273 deletions
diff --git a/sonar-ws-client/src/main/java/org/sonar/wsclient/JdkUtils.java b/sonar-ws-client/src/main/java/org/sonar/wsclient/JdkUtils.java
index 464fe78cb31..d1f21e42646 100644
--- a/sonar-ws-client/src/main/java/org/sonar/wsclient/JdkUtils.java
+++ b/sonar-ws-client/src/main/java/org/sonar/wsclient/JdkUtils.java
@@ -19,12 +19,17 @@
*/
package org.sonar.wsclient;
+import org.json.simple.JSONObject;
+import org.json.simple.JSONValue;
import org.sonar.wsclient.services.WSUtils;
+import org.sonar.wsclient.unmarshallers.JsonUtils;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
+import java.util.ArrayList;
import java.util.Date;
+import java.util.Set;
public final class JdkUtils extends WSUtils {
@@ -42,4 +47,58 @@ public final class JdkUtils extends WSUtils {
}
}
+ @Override
+ public Object getField(Object json, String field) {
+ return ((JSONObject) json).get(field);
+ }
+
+ @Override
+ public String getString(Object json, String field) {
+ return JsonUtils.getString((JSONObject) json, field);
+ }
+
+ @Override
+ public Boolean getBoolean(Object json, String field) {
+ return JsonUtils.getBoolean((JSONObject) json, field);
+ }
+
+ @Override
+ public Integer getInteger(Object json, String field) {
+ return JsonUtils.getInteger((JSONObject) json, field);
+ }
+
+ @Override
+ public Double getDouble(Object json, String field) {
+ return JsonUtils.getDouble((JSONObject) json, field);
+ }
+
+ @Override
+ public Long getLong(Object json, String field) {
+ return JsonUtils.getLong((JSONObject) json, field);
+ }
+
+ @Override
+ public Date getDateTime(Object json, String field) {
+ return JsonUtils.getDateTime((JSONObject) json, field);
+ }
+
+ @Override
+ public int getArraySize(Object array) {
+ return ((ArrayList) array).size();
+ }
+
+ @Override
+ public Object getArrayElement(Object array, int i) {
+ return ((ArrayList) array).get(i);
+ }
+
+ @Override
+ public Object parse(String jsonStr) {
+ return JSONValue.parse(jsonStr);
+ }
+
+ @Override
+ public Set<String> getFields(Object json) {
+ return ((JSONObject) json).keySet();
+ }
}
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 ab4cd8d1028..8119bacee8c 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
@@ -20,7 +20,13 @@
package org.sonar.wsclient.services;
import java.util.Date;
+import java.util.Set;
+/**
+ * Compatibility layer between GWT and plain Java.
+ * Well, this is bad, because code is not type-safe, so all unmarshallers also,
+ * but this allows to remove duplications between sonar-gwt-api and sonar-ws-client.
+ */
public abstract class WSUtils {
private static WSUtils INSTANCE = null;
@@ -36,4 +42,67 @@ public abstract class WSUtils {
public abstract String format(Date date, String format);
public abstract String encodeUrl(String url);
+
+ /**
+ * @return value of specified field from specified JSON object,
+ * or <code>null</code> if field does not exist
+ */
+ public abstract Object getField(Object json, String field);
+
+ /**
+ * @return String value of specified field from specified JSON object,
+ * or <code>null</code> if field does not exist
+ */
+ public abstract String getString(Object json, String field);
+
+ /**
+ * @return Boolean value of specified field from specified JSON object,
+ * or <code>null</code> if field does not exist
+ */
+ public abstract Boolean getBoolean(Object json, String field);
+
+ /**
+ * @return Integer value of specified field from specified JSON object,
+ * or <code>null</code> if field does not exist
+ */
+ public abstract Integer getInteger(Object json, String field);
+
+ /**
+ * @return Double value of specified field from specified JSON object,
+ * or <code>null</code> if field does not exist
+ */
+ public abstract Double getDouble(Object json, String field);
+
+ /**
+ * @return Long value of specified field from specified JSON object,
+ * or <code>null</code> if field does not exist
+ */
+ public abstract Long getLong(Object json, String field);
+
+ /**
+ * @return Date value of specified field from specified JSON object,
+ * or <code>null</code> if field does not exist
+ */
+ public abstract Date getDateTime(Object json, String field);
+
+ /**
+ * @return size of specified JSON array
+ */
+ public abstract int getArraySize(Object array);
+
+ /**
+ * @return element from specified JSON array
+ */
+ public abstract Object getArrayElement(Object array, int i);
+
+ /**
+ * @return JSON object
+ */
+ public abstract Object parse(String jsonStr);
+
+ /**
+ * @return field names in specified JSON object
+ */
+ public abstract Set<String> getFields(Object json);
+
}
diff --git a/sonar-ws-client/src/main/java/org/sonar/wsclient/unmarshallers/AbstractUnmarshaller.java b/sonar-ws-client/src/main/java/org/sonar/wsclient/unmarshallers/AbstractUnmarshaller.java
index d906f4ad9d8..13ddb93409d 100644
--- a/sonar-ws-client/src/main/java/org/sonar/wsclient/unmarshallers/AbstractUnmarshaller.java
+++ b/sonar-ws-client/src/main/java/org/sonar/wsclient/unmarshallers/AbstractUnmarshaller.java
@@ -19,10 +19,8 @@
*/
package org.sonar.wsclient.unmarshallers;
-import org.json.simple.JSONArray;
-import org.json.simple.JSONObject;
-import org.json.simple.JSONValue;
import org.sonar.wsclient.services.Model;
+import org.sonar.wsclient.services.WSUtils;
import java.util.ArrayList;
import java.util.List;
@@ -30,10 +28,11 @@ import java.util.List;
public abstract class AbstractUnmarshaller<MODEL extends Model> implements Unmarshaller<MODEL> {
public final MODEL toModel(String json) {
- MODEL result = null;
- JSONArray array = (JSONArray) JSONValue.parse(json);
- if (array.size() >= 1) {
- JSONObject elt = (JSONObject) array.get(0);
+ WSUtils utils = WSUtils.getINSTANCE();
+ MODEL result = null;
+ Object array = utils.parse(json);
+ if (utils.getArraySize(array) >= 1) {
+ Object elt = utils.getArrayElement(array, 0);
if (elt != null) {
result = parse(elt);
}
@@ -43,10 +42,11 @@ public abstract class AbstractUnmarshaller<MODEL extends Model> implements Unmar
}
public final List<MODEL> toModels(String json) {
+ WSUtils utils = WSUtils.getINSTANCE();
List<MODEL> result = new ArrayList<MODEL>();
- JSONArray array = (JSONArray) JSONValue.parse(json);
- for (Object anArray : array) {
- JSONObject elt = (JSONObject) anArray;
+ Object array = utils.parse(json);
+ for (int i = 0; i < utils.getArraySize(array); i++) {
+ Object elt = utils.getArrayElement(array, i);
if (elt != null) {
result.add(parse(elt));
}
@@ -54,5 +54,5 @@ public abstract class AbstractUnmarshaller<MODEL extends Model> implements Unmar
return result;
}
- protected abstract MODEL parse(JSONObject elt);
+ protected abstract MODEL parse(Object elt);
}
diff --git a/sonar-ws-client/src/main/java/org/sonar/wsclient/unmarshallers/DependencyTreeUnmarshaller.java b/sonar-ws-client/src/main/java/org/sonar/wsclient/unmarshallers/DependencyTreeUnmarshaller.java
index f326ba02176..f91b8af085d 100644
--- a/sonar-ws-client/src/main/java/org/sonar/wsclient/unmarshallers/DependencyTreeUnmarshaller.java
+++ b/sonar-ws-client/src/main/java/org/sonar/wsclient/unmarshallers/DependencyTreeUnmarshaller.java
@@ -19,34 +19,34 @@
*/
package org.sonar.wsclient.unmarshallers;
-import org.json.simple.JSONArray;
-import org.json.simple.JSONObject;
import org.sonar.wsclient.services.DependencyTree;
+import org.sonar.wsclient.services.WSUtils;
import java.util.ArrayList;
import java.util.List;
public class DependencyTreeUnmarshaller extends AbstractUnmarshaller<DependencyTree> {
@Override
- protected DependencyTree parse(JSONObject json) {
+ protected DependencyTree parse(Object json) {
+ WSUtils utils = WSUtils.getINSTANCE();
DependencyTree tree = new DependencyTree()
- .setDepId(JsonUtils.getString(json, "did"))
- .setResourceId(JsonUtils.getString(json, "rid"))
- .setResourceKey(JsonUtils.getString(json, "k"))
- .setResourceName(JsonUtils.getString(json, "n"))
- .setResourceScope(JsonUtils.getString(json, "s"))
- .setResourceQualifier(JsonUtils.getString(json, "q"))
- .setResourceVersion(JsonUtils.getString(json, "v"))
- .setUsage(JsonUtils.getString(json, "u"))
- .setWeight(JsonUtils.getInteger(json, "w"));
+ .setDepId(utils.getString(json, "did"))
+ .setResourceId(utils.getString(json, "rid"))
+ .setResourceKey(utils.getString(json, "k"))
+ .setResourceName(utils.getString(json, "n"))
+ .setResourceScope(utils.getString(json, "s"))
+ .setResourceQualifier(utils.getString(json, "q"))
+ .setResourceVersion(utils.getString(json, "v"))
+ .setUsage(utils.getString(json, "u"))
+ .setWeight(utils.getInteger(json, "w"));
List<DependencyTree> to = new ArrayList<DependencyTree>();
tree.setTo(to);
- JSONArray toJson = (JSONArray) json.get("to");
+ Object toJson = utils.getField(json, "to");
if (toJson != null) {
- for (Object aToJson : toJson) {
- to.add(parse((JSONObject) aToJson));
+ for (int i = 0; i < utils.getArraySize(toJson); i++) {
+ to.add(parse(utils.getArrayElement(toJson, i)));
}
}
return tree;
diff --git a/sonar-ws-client/src/main/java/org/sonar/wsclient/unmarshallers/DependencyUnmarshaller.java b/sonar-ws-client/src/main/java/org/sonar/wsclient/unmarshallers/DependencyUnmarshaller.java
index 803753aede7..bf8b19db704 100644
--- a/sonar-ws-client/src/main/java/org/sonar/wsclient/unmarshallers/DependencyUnmarshaller.java
+++ b/sonar-ws-client/src/main/java/org/sonar/wsclient/unmarshallers/DependencyUnmarshaller.java
@@ -19,24 +19,25 @@
*/
package org.sonar.wsclient.unmarshallers;
-import org.json.simple.JSONObject;
import org.sonar.wsclient.services.Dependency;
+import org.sonar.wsclient.services.WSUtils;
public class DependencyUnmarshaller extends AbstractUnmarshaller<Dependency> {
@Override
- protected Dependency parse(JSONObject json) {
+ protected Dependency parse(Object json) {
+ WSUtils utils = WSUtils.getINSTANCE();
return new Dependency()
- .setId(JsonUtils.getString(json, "id"))
- .setFromId(JsonUtils.getLong(json, "fi"))
- .setToId(JsonUtils.getLong(json, "ti"))
- .setFromKey(JsonUtils.getString(json, "fk"))
- .setToKey(JsonUtils.getString(json, "tk"))
- .setUsage(JsonUtils.getString(json, "u"))
- .setWeight(JsonUtils.getInteger(json, "w"))
- .setFromName(JsonUtils.getString(json, "fn"))
- .setFromQualifier(JsonUtils.getString(json, "fq"))
- .setToName(JsonUtils.getString(json, "tn"))
- .setToQualifier(JsonUtils.getString(json, "tq"));
+ .setId(utils.getString(json, "id"))
+ .setFromId(utils.getLong(json, "fi"))
+ .setToId(utils.getLong(json, "ti"))
+ .setFromKey(utils.getString(json, "fk"))
+ .setToKey(utils.getString(json, "tk"))
+ .setUsage(utils.getString(json, "u"))
+ .setWeight(utils.getInteger(json, "w"))
+ .setFromName(utils.getString(json, "fn"))
+ .setFromQualifier(utils.getString(json, "fq"))
+ .setToName(utils.getString(json, "tn"))
+ .setToQualifier(utils.getString(json, "tq"));
}
-} \ No newline at end of file
+}
diff --git a/sonar-ws-client/src/main/java/org/sonar/wsclient/unmarshallers/EventUnmarshaller.java b/sonar-ws-client/src/main/java/org/sonar/wsclient/unmarshallers/EventUnmarshaller.java
index 2f7878e55ef..adff1c0f2db 100644
--- a/sonar-ws-client/src/main/java/org/sonar/wsclient/unmarshallers/EventUnmarshaller.java
+++ b/sonar-ws-client/src/main/java/org/sonar/wsclient/unmarshallers/EventUnmarshaller.java
@@ -19,20 +19,21 @@
*/
package org.sonar.wsclient.unmarshallers;
-import org.json.simple.JSONObject;
import org.sonar.wsclient.services.Event;
+import org.sonar.wsclient.services.WSUtils;
public class EventUnmarshaller extends AbstractUnmarshaller<Event> {
@Override
- protected Event parse(JSONObject json) {
+ protected Event parse(Object json) {
+ WSUtils utils = WSUtils.getINSTANCE();
return new Event()
- .setId(JsonUtils.getString(json, "id"))
- .setResourceKey(JsonUtils.getString(json, "rk"))
- .setName(JsonUtils.getString(json, "n"))
- .setCategory(JsonUtils.getString(json, "c"))
- .setDate(JsonUtils.getDateTime(json, "dt"))
- .setDescription(JsonUtils.getString(json, "ds"))
- .setData(JsonUtils.getString(json, "data"));
+ .setId(utils.getString(json, "id"))
+ .setResourceKey(utils.getString(json, "rk"))
+ .setName(utils.getString(json, "n"))
+ .setCategory(utils.getString(json, "c"))
+ .setDate(utils.getDateTime(json, "dt"))
+ .setDescription(utils.getString(json, "ds"))
+ .setData(utils.getString(json, "data"));
}
}
diff --git a/sonar-ws-client/src/main/java/org/sonar/wsclient/unmarshallers/FavouriteUnmarshaller.java b/sonar-ws-client/src/main/java/org/sonar/wsclient/unmarshallers/FavouriteUnmarshaller.java
index ab93ef28082..1d40609208e 100644
--- a/sonar-ws-client/src/main/java/org/sonar/wsclient/unmarshallers/FavouriteUnmarshaller.java
+++ b/sonar-ws-client/src/main/java/org/sonar/wsclient/unmarshallers/FavouriteUnmarshaller.java
@@ -19,19 +19,20 @@
*/
package org.sonar.wsclient.unmarshallers;
-import org.json.simple.JSONObject;
import org.sonar.wsclient.services.Favourite;
+import org.sonar.wsclient.services.WSUtils;
public class FavouriteUnmarshaller extends AbstractUnmarshaller<Favourite> {
@Override
- protected Favourite parse(JSONObject json) {
+ protected Favourite parse(Object json) {
+ WSUtils utils = WSUtils.getINSTANCE();
return new Favourite()
- .setId(JsonUtils.getInteger(json, "id"))
- .setKey(JsonUtils.getString(json, "key"))
- .setName(JsonUtils.getString(json, "name"))
- .setScope(JsonUtils.getString(json, "scope"))
- .setQualifier(JsonUtils.getString(json, "qualifier"))
- .setLanguage(JsonUtils.getString(json, "lang"));
+ .setId(utils.getInteger(json, "id"))
+ .setKey(utils.getString(json, "key"))
+ .setName(utils.getString(json, "name"))
+ .setScope(utils.getString(json, "scope"))
+ .setQualifier(utils.getString(json, "qualifier"))
+ .setLanguage(utils.getString(json, "lang"));
}
}
diff --git a/sonar-ws-client/src/main/java/org/sonar/wsclient/unmarshallers/MetricUnmarshaller.java b/sonar-ws-client/src/main/java/org/sonar/wsclient/unmarshallers/MetricUnmarshaller.java
index e63bcd8c08b..123e94583c1 100644
--- a/sonar-ws-client/src/main/java/org/sonar/wsclient/unmarshallers/MetricUnmarshaller.java
+++ b/sonar-ws-client/src/main/java/org/sonar/wsclient/unmarshallers/MetricUnmarshaller.java
@@ -19,21 +19,22 @@
*/
package org.sonar.wsclient.unmarshallers;
-import org.json.simple.JSONObject;
import org.sonar.wsclient.services.Metric;
+import org.sonar.wsclient.services.WSUtils;
public class MetricUnmarshaller extends AbstractUnmarshaller<Metric> {
@Override
- protected Metric parse(JSONObject json) {
+ protected Metric parse(Object json) {
+ WSUtils utils = WSUtils.getINSTANCE();
return new Metric()
- .setKey(JsonUtils.getString(json, "key"))
- .setName(JsonUtils.getString(json, "name"))
- .setDomain(JsonUtils.getString(json, "domain"))
- .setDescription(JsonUtils.getString(json, "description"))
- .setDirection(JsonUtils.getInteger(json, "direction"))
- .setType(JsonUtils.getString(json, "val_type"))
- .setUserManaged(JsonUtils.getBoolean(json, "user_managed"))
- .setHidden(JsonUtils.getBoolean(json, "hidden"));
+ .setKey(utils.getString(json, "key"))
+ .setName(utils.getString(json, "name"))
+ .setDomain(utils.getString(json, "domain"))
+ .setDescription(utils.getString(json, "description"))
+ .setDirection(utils.getInteger(json, "direction"))
+ .setType(utils.getString(json, "val_type"))
+ .setUserManaged(utils.getBoolean(json, "user_managed"))
+ .setHidden(utils.getBoolean(json, "hidden"));
}
}
diff --git a/sonar-ws-client/src/main/java/org/sonar/wsclient/unmarshallers/PluginUnmarshaller.java b/sonar-ws-client/src/main/java/org/sonar/wsclient/unmarshallers/PluginUnmarshaller.java
index 29d785f485e..dbcfafa6faa 100644
--- a/sonar-ws-client/src/main/java/org/sonar/wsclient/unmarshallers/PluginUnmarshaller.java
+++ b/sonar-ws-client/src/main/java/org/sonar/wsclient/unmarshallers/PluginUnmarshaller.java
@@ -1,7 +1,7 @@
package org.sonar.wsclient.unmarshallers;
-import org.json.simple.JSONObject;
import org.sonar.wsclient.services.Plugin;
+import org.sonar.wsclient.services.WSUtils;
/**
* @since 2.4
@@ -9,11 +9,12 @@ import org.sonar.wsclient.services.Plugin;
public class PluginUnmarshaller extends AbstractUnmarshaller<Plugin> {
@Override
- protected Plugin parse(JSONObject json) {
+ protected Plugin parse(Object json) {
+ WSUtils utils = WSUtils.getINSTANCE();
return new Plugin()
- .setKey(JsonUtils.getString(json, "key"))
- .setName(JsonUtils.getString(json, "name"))
- .setVersion(JsonUtils.getString(json, "version"));
+ .setKey(utils.getString(json, "key"))
+ .setName(utils.getString(json, "name"))
+ .setVersion(utils.getString(json, "version"));
}
}
diff --git a/sonar-ws-client/src/main/java/org/sonar/wsclient/unmarshallers/PropertyUnmarshaller.java b/sonar-ws-client/src/main/java/org/sonar/wsclient/unmarshallers/PropertyUnmarshaller.java
index 9df96d1433d..5efea1ca841 100644
--- a/sonar-ws-client/src/main/java/org/sonar/wsclient/unmarshallers/PropertyUnmarshaller.java
+++ b/sonar-ws-client/src/main/java/org/sonar/wsclient/unmarshallers/PropertyUnmarshaller.java
@@ -19,15 +19,16 @@
*/
package org.sonar.wsclient.unmarshallers;
-import org.json.simple.JSONObject;
import org.sonar.wsclient.services.Property;
+import org.sonar.wsclient.services.WSUtils;
public class PropertyUnmarshaller extends AbstractUnmarshaller<Property> {
@Override
- protected Property parse(JSONObject json) {
+ protected Property parse(Object json) {
+ WSUtils utils = WSUtils.getINSTANCE();
return new Property()
- .setKey(JsonUtils.getString(json, "key"))
- .setValue(JsonUtils.getString(json, "value"));
+ .setKey(utils.getString(json, "key"))
+ .setValue(utils.getString(json, "value"));
}
-} \ No newline at end of file
+}
diff --git a/sonar-ws-client/src/main/java/org/sonar/wsclient/unmarshallers/ResourceUnmarshaller.java b/sonar-ws-client/src/main/java/org/sonar/wsclient/unmarshallers/ResourceUnmarshaller.java
index fba424d4bbe..bdb9db20e17 100644
--- a/sonar-ws-client/src/main/java/org/sonar/wsclient/unmarshallers/ResourceUnmarshaller.java
+++ b/sonar-ws-client/src/main/java/org/sonar/wsclient/unmarshallers/ResourceUnmarshaller.java
@@ -19,10 +19,9 @@
*/
package org.sonar.wsclient.unmarshallers;
-import org.json.simple.JSONArray;
-import org.json.simple.JSONObject;
import org.sonar.wsclient.services.Measure;
import org.sonar.wsclient.services.Resource;
+import org.sonar.wsclient.services.WSUtils;
import java.util.ArrayList;
import java.util.List;
@@ -30,53 +29,56 @@ import java.util.List;
public class ResourceUnmarshaller extends AbstractUnmarshaller<Resource> {
@Override
- protected Resource parse(JSONObject json) {
+ protected Resource parse(Object json) {
Resource resource = new Resource();
parseResourceFields(json, resource);
parseMeasures(json, resource);
return resource;
}
- private void parseResourceFields(JSONObject json, Resource resource) {
- resource.setId(JsonUtils.getInteger(json, "id"))
- .setKey(JsonUtils.getString(json, "key"))
- .setName(JsonUtils.getString(json, "name"))
- .setLongName(JsonUtils.getString(json, "lname"))
- .setScope(JsonUtils.getString(json, "scope"))
- .setQualifier(JsonUtils.getString(json, "qualifier"))
- .setLanguage(JsonUtils.getString(json, "lang"))
- .setDescription(JsonUtils.getString(json, "description"))
- .setDate(JsonUtils.getDateTime(json, "date"))
- .setVersion(JsonUtils.getString(json, "version"))
- .setPeriod1Mode(JsonUtils.getString(json, "p1"))
- .setPeriod1Param(JsonUtils.getString(json, "p1p"))
- .setPeriod1Date(JsonUtils.getDateTime(json, "p1d"))
- .setPeriod2Mode(JsonUtils.getString(json, "p2"))
- .setPeriod2Param(JsonUtils.getString(json, "p2p"))
- .setPeriod2Date(JsonUtils.getDateTime(json, "p2d"))
- .setPeriod3Mode(JsonUtils.getString(json, "p3"))
- .setPeriod3Param(JsonUtils.getString(json, "p3p"))
- .setPeriod3Date(JsonUtils.getDateTime(json, "p3d"))
- .setPeriod4Mode(JsonUtils.getString(json, "p4"))
- .setPeriod4Param(JsonUtils.getString(json, "p4p"))
- .setPeriod4Date(JsonUtils.getDateTime(json, "p4d"))
- .setPeriod5Mode(JsonUtils.getString(json, "p5"))
- .setPeriod5Param(JsonUtils.getString(json, "p5p"))
- .setPeriod5Date(JsonUtils.getDateTime(json, "p5d"));
+ private void parseResourceFields(Object json, Resource resource) {
+ WSUtils utils = WSUtils.getINSTANCE();
+ resource.setId(utils.getInteger(json, "id"))
+ .setKey(utils.getString(json, "key"))
+ .setName(utils.getString(json, "name"))
+ .setLongName(utils.getString(json, "lname"))
+ .setScope(utils.getString(json, "scope"))
+ .setQualifier(utils.getString(json, "qualifier"))
+ .setLanguage(utils.getString(json, "lang"))
+ .setDescription(utils.getString(json, "description"))
+ .setDate(utils.getDateTime(json, "date"))
+ .setVersion(utils.getString(json, "version"))
+ .setPeriod1Mode(utils.getString(json, "p1"))
+ .setPeriod1Param(utils.getString(json, "p1p"))
+ .setPeriod1Date(utils.getDateTime(json, "p1d"))
+ .setPeriod2Mode(utils.getString(json, "p2"))
+ .setPeriod2Param(utils.getString(json, "p2p"))
+ .setPeriod2Date(utils.getDateTime(json, "p2d"))
+ .setPeriod3Mode(utils.getString(json, "p3"))
+ .setPeriod3Param(utils.getString(json, "p3p"))
+ .setPeriod3Date(utils.getDateTime(json, "p3d"))
+ .setPeriod4Mode(utils.getString(json, "p4"))
+ .setPeriod4Param(utils.getString(json, "p4p"))
+ .setPeriod4Date(utils.getDateTime(json, "p4d"))
+ .setPeriod5Mode(utils.getString(json, "p5"))
+ .setPeriod5Param(utils.getString(json, "p5p"))
+ .setPeriod5Date(utils.getDateTime(json, "p5d"));
}
- private void parseMeasures(JSONObject json, Resource resource) {
- JSONArray measuresJson = JsonUtils.getArray(json, "msr");
+ private void parseMeasures(Object json, Resource resource) {
+ WSUtils utils = WSUtils.getINSTANCE();
+ Object measuresJson = utils.getField(json, "msr");
if (measuresJson != null) {
resource.setMeasures(parseMeasures(measuresJson));
}
}
- private List<Measure> parseMeasures(JSONArray measuresJson) {
+ private List<Measure> parseMeasures(Object measuresJson) {
+ WSUtils utils = WSUtils.getINSTANCE();
List<Measure> projectMeasures = new ArrayList<Measure>();
- int len = measuresJson.size();
+ int len = utils.getArraySize(measuresJson);
for (int i = 0; i < len; i++) {
- JSONObject measureJson = (JSONObject) measuresJson.get(i);
+ Object measureJson = utils.getArrayElement(measuresJson, i);
if (measureJson != null) {
Measure measure = parseMeasure(measureJson);
projectMeasures.add(measure);
@@ -85,27 +87,29 @@ public class ResourceUnmarshaller extends AbstractUnmarshaller<Resource> {
return projectMeasures;
}
- private Measure parseMeasure(JSONObject json) {
+ private Measure parseMeasure(Object json) {
+ WSUtils utils = WSUtils.getINSTANCE();
+
Measure measure = new Measure();
measure
- .setMetricKey(JsonUtils.getString(json, "key"))
- .setMetricName(JsonUtils.getString(json, "name"))
- .setValue(JsonUtils.getDouble(json, "val"))
- .setFormattedValue(JsonUtils.getString(json, "frmt_val"))
- .setTrend(JsonUtils.getInteger(json, "trend"))
- .setVar(JsonUtils.getInteger(json, "var"))
- .setData(JsonUtils.getString(json, "data"))
- .setRuleKey(JsonUtils.getString(json, "rule_key"))
- .setRuleName(JsonUtils.getString(json, "rule_name"))
- .setRuleCategory(JsonUtils.getString(json, "rule_category"))
- .setRuleSeverity(JsonUtils.getString(json, "rule_priority"))
- .setCharacteristicKey(JsonUtils.getString(json, "ctic_key"))
- .setCharacteristicName(JsonUtils.getString(json, "ctic_name"))
- .setVariation1(JsonUtils.getDouble(json, "var1"))
- .setVariation2(JsonUtils.getDouble(json, "var2"))
- .setVariation3(JsonUtils.getDouble(json, "var3"))
- .setVariation4(JsonUtils.getDouble(json, "var4"))
- .setVariation5(JsonUtils.getDouble(json, "var5"));
+ .setMetricKey(utils.getString(json, "key"))
+ .setMetricName(utils.getString(json, "name"))
+ .setValue(utils.getDouble(json, "val"))
+ .setFormattedValue(utils.getString(json, "frmt_val"))
+ .setTrend(utils.getInteger(json, "trend"))
+ .setVar(utils.getInteger(json, "var"))
+ .setData(utils.getString(json, "data"))
+ .setRuleKey(utils.getString(json, "rule_key"))
+ .setRuleName(utils.getString(json, "rule_name"))
+ .setRuleCategory(utils.getString(json, "rule_category"))
+ .setRuleSeverity(utils.getString(json, "rule_priority"))
+ .setCharacteristicKey(utils.getString(json, "ctic_key"))
+ .setCharacteristicName(utils.getString(json, "ctic_name"))
+ .setVariation1(utils.getDouble(json, "var1"))
+ .setVariation2(utils.getDouble(json, "var2"))
+ .setVariation3(utils.getDouble(json, "var3"))
+ .setVariation4(utils.getDouble(json, "var4"))
+ .setVariation5(utils.getDouble(json, "var5"));
return measure;
}
}
diff --git a/sonar-ws-client/src/main/java/org/sonar/wsclient/unmarshallers/RuleUnmarshaller.java b/sonar-ws-client/src/main/java/org/sonar/wsclient/unmarshallers/RuleUnmarshaller.java
index 041d3afd573..9cb3b12f56e 100644
--- a/sonar-ws-client/src/main/java/org/sonar/wsclient/unmarshallers/RuleUnmarshaller.java
+++ b/sonar-ws-client/src/main/java/org/sonar/wsclient/unmarshallers/RuleUnmarshaller.java
@@ -19,10 +19,9 @@
*/
package org.sonar.wsclient.unmarshallers;
-import org.json.simple.JSONArray;
-import org.json.simple.JSONObject;
import org.sonar.wsclient.services.Rule;
import org.sonar.wsclient.services.RuleParam;
+import org.sonar.wsclient.services.WSUtils;
import java.util.ArrayList;
import java.util.List;
@@ -33,34 +32,40 @@ import java.util.List;
public class RuleUnmarshaller extends AbstractUnmarshaller<Rule> {
@Override
- protected Rule parse(JSONObject json) {
+ protected Rule parse(Object json) {
Rule rule = new Rule();
parseRuleFields(json, rule);
parseParams(json, rule);
return rule;
}
- private void parseRuleFields(JSONObject json, Rule rule) {
- rule.setTitle(JsonUtils.getString(json, "title"))
- .setKey(JsonUtils.getString(json, "key"))
- .setRepository(JsonUtils.getString(json, "plugin"))
- .setDescription(JsonUtils.getString(json, "description"))
- .setSeverity(JsonUtils.getString(json, "priority"))
- .setActive("ACTIVE".equals(JsonUtils.getString(json, "status")));
+ private void parseRuleFields(Object json, Rule rule) {
+ WSUtils utils = WSUtils.getINSTANCE();
+
+ rule.setTitle(utils.getString(json, "title"))
+ .setKey(utils.getString(json, "key"))
+ .setRepository(utils.getString(json, "plugin"))
+ .setDescription(utils.getString(json, "description"))
+ .setSeverity(utils.getString(json, "priority"))
+ .setActive("ACTIVE".equals(utils.getString(json, "status")));
}
- private void parseParams(JSONObject json, Rule rule) {
- JSONArray paramsJson = (JSONArray) json.get("params");
+ private void parseParams(Object json, Rule rule) {
+ WSUtils utils = WSUtils.getINSTANCE();
+
+ Object paramsJson = utils.getField(json, "params");
if (paramsJson != null) {
rule.setParams(parseParams(paramsJson));
}
}
- private List<RuleParam> parseParams(JSONArray paramsJson) {
+ private List<RuleParam> parseParams(Object paramsJson) {
+ WSUtils utils = WSUtils.getINSTANCE();
+
List<RuleParam> ruleParams = new ArrayList<RuleParam>();
- int len = paramsJson.size();
+ int len = utils.getArraySize(paramsJson);
for (int i = 0; i < len; i++) {
- JSONObject paramJson = (JSONObject) paramsJson.get(i);
+ Object paramJson = utils.getArrayElement(paramsJson, i);
if (paramJson != null) {
RuleParam param = parseParam(paramJson);
ruleParams.add(param);
@@ -69,11 +74,13 @@ public class RuleUnmarshaller extends AbstractUnmarshaller<Rule> {
return ruleParams;
}
- private RuleParam parseParam(JSONObject json) {
+ private RuleParam parseParam(Object json) {
+ WSUtils utils = WSUtils.getINSTANCE();
+
RuleParam param = new RuleParam();
- param.setName(JsonUtils.getString(json, "name"))
- .setDescription(JsonUtils.getString(json, "description"))
- .setValue(JsonUtils.getString(json, "value"));
+ param.setName(utils.getString(json, "name"))
+ .setDescription(utils.getString(json, "description"))
+ .setValue(utils.getString(json, "value"));
return param;
}
diff --git a/sonar-ws-client/src/main/java/org/sonar/wsclient/unmarshallers/ServerUnmarshaller.java b/sonar-ws-client/src/main/java/org/sonar/wsclient/unmarshallers/ServerUnmarshaller.java
index a17a36189d4..829b174dc47 100644
--- a/sonar-ws-client/src/main/java/org/sonar/wsclient/unmarshallers/ServerUnmarshaller.java
+++ b/sonar-ws-client/src/main/java/org/sonar/wsclient/unmarshallers/ServerUnmarshaller.java
@@ -19,9 +19,8 @@
*/
package org.sonar.wsclient.unmarshallers;
-import org.json.simple.JSONObject;
-import org.json.simple.JSONValue;
import org.sonar.wsclient.services.Server;
+import org.sonar.wsclient.services.WSUtils;
import java.util.List;
@@ -30,12 +29,13 @@ import java.util.List;
*/
public class ServerUnmarshaller implements Unmarshaller<Server> {
public Server toModel(String json) {
- JSONObject map = (JSONObject) JSONValue.parse(json);
+ WSUtils utils = WSUtils.getINSTANCE();
+ Object map = utils.parse(json);
Server server = new Server()
- .setId(JsonUtils.getString(map, "id"))
- .setVersion(JsonUtils.getString(map, "version"))
- .setStatusMessage(JsonUtils.getString(map, "status_msg"));
- String status = JsonUtils.getString(map, "status");
+ .setId(utils.getString(map, "id"))
+ .setVersion(utils.getString(map, "version"))
+ .setStatusMessage(utils.getString(map, "status_msg"));
+ String status = utils.getString(map, "status");
if (status != null) {
server.setStatus(Server.Status.valueOf(status));
}
diff --git a/sonar-ws-client/src/main/java/org/sonar/wsclient/unmarshallers/SourceUnmarshaller.java b/sonar-ws-client/src/main/java/org/sonar/wsclient/unmarshallers/SourceUnmarshaller.java
index cbda6b4c5d0..16eb4aff98d 100644
--- a/sonar-ws-client/src/main/java/org/sonar/wsclient/unmarshallers/SourceUnmarshaller.java
+++ b/sonar-ws-client/src/main/java/org/sonar/wsclient/unmarshallers/SourceUnmarshaller.java
@@ -19,20 +19,18 @@
*/
package org.sonar.wsclient.unmarshallers;
-import org.json.simple.JSONObject;
import org.sonar.wsclient.services.Source;
-
-import java.util.Map;
+import org.sonar.wsclient.services.WSUtils;
public class SourceUnmarshaller extends AbstractUnmarshaller<Source> {
@Override
- protected Source parse(JSONObject json) {
+ protected Source parse(Object json) {
+ WSUtils utils = WSUtils.getINSTANCE();
Source source = new Source();
- for (Object o : json.entrySet()) {
- Map.Entry<String, String> entry = (Map.Entry<String, String>) o;
- source.addLine(Integer.parseInt(entry.getKey()), entry.getValue());
+ for (String field : utils.getFields(json)) {
+ source.addLine(Integer.parseInt(field), utils.getString(json, field));
}
return source;
diff --git a/sonar-ws-client/src/main/java/org/sonar/wsclient/unmarshallers/TimeMachineUnmarshaller.java b/sonar-ws-client/src/main/java/org/sonar/wsclient/unmarshallers/TimeMachineUnmarshaller.java
index b59a364bc31..08a0ac26d87 100644
--- a/sonar-ws-client/src/main/java/org/sonar/wsclient/unmarshallers/TimeMachineUnmarshaller.java
+++ b/sonar-ws-client/src/main/java/org/sonar/wsclient/unmarshallers/TimeMachineUnmarshaller.java
@@ -19,43 +19,45 @@
*/
package org.sonar.wsclient.unmarshallers;
-import org.json.simple.JSONArray;
-import org.json.simple.JSONObject;
import org.sonar.wsclient.services.TimeMachine;
import org.sonar.wsclient.services.TimeMachineCell;
import org.sonar.wsclient.services.TimeMachineColumn;
+import org.sonar.wsclient.services.WSUtils;
public class TimeMachineUnmarshaller extends AbstractUnmarshaller<TimeMachine> {
- protected TimeMachine parse(JSONObject json) {
- JSONArray cols = JsonUtils.getArray(json, "cols");
- JSONArray cells = JsonUtils.getArray(json, "cells");
+ protected TimeMachine parse(Object json) {
+ WSUtils utils = WSUtils.getINSTANCE();
+ Object cols = utils.getField(json, "cols");
+ Object cells = utils.getField(json, "cells");
return new TimeMachine(toColumns(cols), toCells(cells));
}
- private TimeMachineColumn[] toColumns(JSONArray cols) {
- int size = cols.size();
+ private TimeMachineColumn[] toColumns(Object cols) {
+ WSUtils utils = WSUtils.getINSTANCE();
+ int size = utils.getArraySize(cols);
TimeMachineColumn[] result = new TimeMachineColumn[size];
for (int index = 0; index < size; index++) {
- JSONObject colJson = (JSONObject) cols.get(index);
- result[index] = new TimeMachineColumn(index, JsonUtils.getString(colJson, "metric"), null, null);
+ Object colJson = utils.getArrayElement(cols, index);
+ result[index] = new TimeMachineColumn(index, utils.getString(colJson, "metric"), null, null);
}
return result;
}
- private TimeMachineCell[] toCells(JSONArray cells) {
- int size = cells.size();
+ private TimeMachineCell[] toCells(Object cells) {
+ WSUtils utils = WSUtils.getINSTANCE();
+ int size = utils.getArraySize(cells);
TimeMachineCell[] result = new TimeMachineCell[size];
for (int i = 0; i < size; i++) {
- JSONObject cellJson = (JSONObject) cells.get(i);
- JSONArray valuesJson = JsonUtils.getArray(cellJson, "v");
+ Object cellJson = utils.getArrayElement(cells, i);
+ Object valuesJson = utils.getField(cellJson, "v");
- Object[] resultValues = new Object[valuesJson.size()];
- for (int indexValue = 0; indexValue < valuesJson.size(); indexValue++) {
- Object value = valuesJson.get(indexValue);
+ Object[] resultValues = new Object[utils.getArraySize(valuesJson)];
+ for (int indexValue = 0; indexValue < utils.getArraySize(valuesJson); indexValue++) {
+ Object value = utils.getArrayElement(valuesJson, indexValue);
resultValues[indexValue] = value;
}
- result[i] = new TimeMachineCell(JsonUtils.getDateTime(cellJson, "d"), resultValues);
+ result[i] = new TimeMachineCell(utils.getDateTime(cellJson, "d"), resultValues);
}
return result;
}
diff --git a/sonar-ws-client/src/main/java/org/sonar/wsclient/unmarshallers/ViolationUnmarshaller.java b/sonar-ws-client/src/main/java/org/sonar/wsclient/unmarshallers/ViolationUnmarshaller.java
index 258e5938570..18c297b22cb 100644
--- a/sonar-ws-client/src/main/java/org/sonar/wsclient/unmarshallers/ViolationUnmarshaller.java
+++ b/sonar-ws-client/src/main/java/org/sonar/wsclient/unmarshallers/ViolationUnmarshaller.java
@@ -19,31 +19,33 @@
*/
package org.sonar.wsclient.unmarshallers;
-import org.json.simple.JSONObject;
import org.sonar.wsclient.services.Violation;
+import org.sonar.wsclient.services.WSUtils;
public class ViolationUnmarshaller extends AbstractUnmarshaller<Violation> {
@Override
- protected Violation parse(JSONObject json) {
+ protected Violation parse(Object json) {
+ WSUtils utils = WSUtils.getINSTANCE();
+
Violation violation = new Violation();
- violation.setMessage(JsonUtils.getString(json, "message"));
- violation.setLine(JsonUtils.getInteger(json, "line"));
- violation.setSeverity(JsonUtils.getString(json, "priority"));
- violation.setCreatedAt(JsonUtils.getDateTime(json, "createdAt"));
+ violation.setMessage(utils.getString(json, "message"));
+ violation.setLine(utils.getInteger(json, "line"));
+ violation.setSeverity(utils.getString(json, "priority"));
+ violation.setCreatedAt(utils.getDateTime(json, "createdAt"));
- JSONObject rule = (JSONObject) json.get("rule");
+ Object rule = utils.getField(json, "rule");
if (rule != null) {
- violation.setRuleKey(JsonUtils.getString(rule, "key"));
- violation.setRuleName(JsonUtils.getString(rule, "name"));
+ violation.setRuleKey(utils.getString(rule, "key"));
+ violation.setRuleName(utils.getString(rule, "name"));
}
- JSONObject resource = (JSONObject) json.get("resource");
+ Object resource = utils.getField(json, "resource");
if (resource != null) {
- violation.setResourceKey(JsonUtils.getString(resource, "key"));
- violation.setResourceName(JsonUtils.getString(resource, "name"));
- violation.setResourceQualifier(JsonUtils.getString(resource, "qualifier"));
- violation.setResourceScope(JsonUtils.getString(resource, "scope"));
+ violation.setResourceKey(utils.getString(resource, "key"));
+ violation.setResourceName(utils.getString(resource, "name"));
+ violation.setResourceQualifier(utils.getString(resource, "qualifier"));
+ violation.setResourceScope(utils.getString(resource, "scope"));
}
return violation;
}
diff --git a/sonar-ws-client/src/test/java/org/sonar/wsclient/unmarshallers/DependencyTreeUnmarshallerTest.java b/sonar-ws-client/src/test/java/org/sonar/wsclient/unmarshallers/DependencyTreeUnmarshallerTest.java
index 4d5e412330e..b96defeba12 100644
--- a/sonar-ws-client/src/test/java/org/sonar/wsclient/unmarshallers/DependencyTreeUnmarshallerTest.java
+++ b/sonar-ws-client/src/test/java/org/sonar/wsclient/unmarshallers/DependencyTreeUnmarshallerTest.java
@@ -27,13 +27,13 @@ import java.util.List;
import static org.hamcrest.core.Is.is;
import static org.junit.Assert.assertThat;
-public class DependencyTreeUnmarshallerTest {
+public class DependencyTreeUnmarshallerTest extends UnmarshallerTestCase {
@Test
public void singleDepthOfDependencies() {
List<DependencyTree> trees = new DependencyTreeUnmarshaller().toModels("[]");
assertThat(trees.size(), is(0));
- trees = new DependencyTreeUnmarshaller().toModels(WSTestUtils.loadFile("/dependency_tree/single_depth.json"));
+ trees = new DependencyTreeUnmarshaller().toModels(loadFile("/dependency_tree/single_depth.json"));
assertThat(trees.size(), is(2));
assertThat(trees.get(0).getDepId(), is("12345"));
assertThat(trees.get(0).getResourceId(), is("2000"));
@@ -48,7 +48,7 @@ public class DependencyTreeUnmarshallerTest {
@Test
public void manyDepthsOfDependencies() {
- List<DependencyTree> trees = new DependencyTreeUnmarshaller().toModels(WSTestUtils.loadFile("/dependency_tree/many_depths.json"));
+ List<DependencyTree> trees = new DependencyTreeUnmarshaller().toModels(loadFile("/dependency_tree/many_depths.json"));
assertThat(trees.size(), is(1));
List<DependencyTree> secondLevelTrees = trees.get(0).getTo();
assertThat(secondLevelTrees.size(), is(2));
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 d5c999d57ff..1ad4d090a2a 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
@@ -28,14 +28,14 @@ import static org.hamcrest.CoreMatchers.nullValue;
import static org.hamcrest.core.Is.is;
import static org.junit.Assert.assertThat;
-public class DependencyUnmarshallerTest {
+public class DependencyUnmarshallerTest extends UnmarshallerTestCase {
@Test
public void toModel() {
Dependency dependency = new DependencyUnmarshaller().toModel("[]");
assertThat(dependency, nullValue());
- dependency = new DependencyUnmarshaller().toModel(WSTestUtils.loadFile("/dependencies/single.json"));
+ dependency = new DependencyUnmarshaller().toModel(loadFile("/dependencies/single.json"));
assertThat(dependency.getId(), is("1649"));
assertThat(dependency.getFromKey(), is("org.apache.shiro:shiro-core:org.apache.shiro.authc.pam"));
assertThat(dependency.getToKey(), is("org.apache.shiro:shiro-core:org.apache.shiro.realm"));
@@ -52,7 +52,7 @@ public class DependencyUnmarshallerTest {
Collection<Dependency> dependencies = new DependencyUnmarshaller().toModels("[]");
assertThat(dependencies.size(), is(0));
- dependencies = new DependencyUnmarshaller().toModels(WSTestUtils.loadFile("/dependencies/many.json"));
+ dependencies = new DependencyUnmarshaller().toModels(loadFile("/dependencies/many.json"));
assertThat(dependencies.size(), is(15));
}
diff --git a/sonar-ws-client/src/test/java/org/sonar/wsclient/unmarshallers/EventUnmarshallerTest.java b/sonar-ws-client/src/test/java/org/sonar/wsclient/unmarshallers/EventUnmarshallerTest.java
index 1577760bb23..b5c19029f0b 100644
--- a/sonar-ws-client/src/test/java/org/sonar/wsclient/unmarshallers/EventUnmarshallerTest.java
+++ b/sonar-ws-client/src/test/java/org/sonar/wsclient/unmarshallers/EventUnmarshallerTest.java
@@ -29,11 +29,11 @@ import java.util.List;
import static org.hamcrest.core.Is.is;
import static org.junit.Assert.assertThat;
-public class EventUnmarshallerTest {
+public class EventUnmarshallerTest extends UnmarshallerTestCase {
@Test
public void toModel() throws Exception {
- List<Event> events = new EventUnmarshaller().toModels(WSTestUtils.loadFile("/events/events.json"));
+ List<Event> events = new EventUnmarshaller().toModels(loadFile("/events/events.json"));
Event event = events.get(0);
assertThat(event.getId(), is("10"));
assertThat(event.getName(), is("foo"));
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 dde72bf07a1..ef2e1c3aaf0 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
@@ -30,7 +30,7 @@ import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.nullValue;
import static org.junit.Assert.assertThat;
-public class JsonUtilsTest {
+public class JsonUtilsTest extends UnmarshallerTestCase {
@Test
public void getIntFields() {
diff --git a/sonar-ws-client/src/test/java/org/sonar/wsclient/unmarshallers/MetricUnmarshallerTest.java b/sonar-ws-client/src/test/java/org/sonar/wsclient/unmarshallers/MetricUnmarshallerTest.java
index 1ac9d9a83b1..0eb4ff65fa7 100644
--- a/sonar-ws-client/src/test/java/org/sonar/wsclient/unmarshallers/MetricUnmarshallerTest.java
+++ b/sonar-ws-client/src/test/java/org/sonar/wsclient/unmarshallers/MetricUnmarshallerTest.java
@@ -29,14 +29,14 @@ import static org.hamcrest.core.Is.is;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
-public class MetricUnmarshallerTest {
+public class MetricUnmarshallerTest extends UnmarshallerTestCase {
@Test
public void toModel() {
Metric metric = new MetricUnmarshaller().toModel("[]");
assertThat(metric, nullValue());
- metric = new MetricUnmarshaller().toModel(WSTestUtils.loadFile("/metrics/one_metric.json"));
+ metric = new MetricUnmarshaller().toModel(loadFile("/metrics/one_metric.json"));
assertThat(metric.getKey(), is("ncloc"));
assertThat(metric.getName(), is("Lines of code"));
assertThat(metric.getDescription(), is("Non Commenting Lines of Code"));
@@ -49,10 +49,10 @@ public class MetricUnmarshallerTest {
Collection<Metric> metrics = new MetricUnmarshaller().toModels("[]");
assertThat(metrics.size(), is(0));
- metrics = new MetricUnmarshaller().toModels(WSTestUtils.loadFile("/metrics/one_metric.json"));
+ metrics = new MetricUnmarshaller().toModels(loadFile("/metrics/one_metric.json"));
assertThat(metrics.size(), is(1));
- metrics = new MetricUnmarshaller().toModels(WSTestUtils.loadFile("/metrics/many_metrics.json"));
+ metrics = new MetricUnmarshaller().toModels(loadFile("/metrics/many_metrics.json"));
assertThat(metrics.size(), is(10));
}
diff --git a/sonar-ws-client/src/test/java/org/sonar/wsclient/unmarshallers/PluginUnmarshallerTest.java b/sonar-ws-client/src/test/java/org/sonar/wsclient/unmarshallers/PluginUnmarshallerTest.java
index 6b709a3a594..12423bf5ef7 100644
--- a/sonar-ws-client/src/test/java/org/sonar/wsclient/unmarshallers/PluginUnmarshallerTest.java
+++ b/sonar-ws-client/src/test/java/org/sonar/wsclient/unmarshallers/PluginUnmarshallerTest.java
@@ -1,18 +1,22 @@
package org.sonar.wsclient.unmarshallers;
import org.junit.Test;
+import org.sonar.wsclient.JdkUtils;
import org.sonar.wsclient.services.Plugin;
+import org.sonar.wsclient.services.WSUtils;
import java.util.List;
import static org.hamcrest.core.Is.is;
import static org.junit.Assert.assertThat;
-public class PluginUnmarshallerTest {
+public class PluginUnmarshallerTest extends UnmarshallerTestCase {
@Test
public void toModel() throws Exception {
- List<Plugin> plugins = new PluginUnmarshaller().toModels(WSTestUtils.loadFile("/plugins/plugins.json"));
+ WSUtils.setInstance(new JdkUtils());
+
+ List<Plugin> plugins = new PluginUnmarshaller().toModels(loadFile("/plugins/plugins.json"));
Plugin plugin = plugins.get(0);
assertThat(plugin.getKey(), is("foo"));
assertThat(plugin.getName(), is("Foo"));
diff --git a/sonar-ws-client/src/test/java/org/sonar/wsclient/unmarshallers/PropertyUnmarshallerTest.java b/sonar-ws-client/src/test/java/org/sonar/wsclient/unmarshallers/PropertyUnmarshallerTest.java
index ea919cb18f6..9ffb0de4b8c 100644
--- a/sonar-ws-client/src/test/java/org/sonar/wsclient/unmarshallers/PropertyUnmarshallerTest.java
+++ b/sonar-ws-client/src/test/java/org/sonar/wsclient/unmarshallers/PropertyUnmarshallerTest.java
@@ -28,13 +28,13 @@ import static org.hamcrest.CoreMatchers.nullValue;
import static org.hamcrest.core.Is.is;
import static org.junit.Assert.assertThat;
-public class PropertyUnmarshallerTest {
+public class PropertyUnmarshallerTest extends UnmarshallerTestCase {
@Test
public void toModel() {
Property property = new PropertyUnmarshaller().toModel("[]");
assertThat(property, nullValue());
- property = new PropertyUnmarshaller().toModel(WSTestUtils.loadFile("/properties/single.json"));
+ property = new PropertyUnmarshaller().toModel(loadFile("/properties/single.json"));
assertThat(property.getKey(), is("myprop"));
assertThat(property.getValue(), is("myvalue"));
}
@@ -44,10 +44,10 @@ public class PropertyUnmarshallerTest {
Collection<Property> properties = new PropertyUnmarshaller().toModels("[]");
assertThat(properties.size(), is(0));
- properties = new PropertyUnmarshaller().toModels(WSTestUtils.loadFile("/properties/single.json"));
+ properties = new PropertyUnmarshaller().toModels(loadFile("/properties/single.json"));
assertThat(properties.size(), is(1));
- properties = new PropertyUnmarshaller().toModels(WSTestUtils.loadFile("/properties/many.json"));
+ properties = new PropertyUnmarshaller().toModels(loadFile("/properties/many.json"));
assertThat(properties.size(), is(3));
}
}
diff --git a/sonar-ws-client/src/test/java/org/sonar/wsclient/unmarshallers/ResourceUnmarshallerTest.java b/sonar-ws-client/src/test/java/org/sonar/wsclient/unmarshallers/ResourceUnmarshallerTest.java
index 98df780a512..acfda91f3e5 100644
--- a/sonar-ws-client/src/test/java/org/sonar/wsclient/unmarshallers/ResourceUnmarshallerTest.java
+++ b/sonar-ws-client/src/test/java/org/sonar/wsclient/unmarshallers/ResourceUnmarshallerTest.java
@@ -29,11 +29,11 @@ import static org.hamcrest.core.Is.is;
import static org.hamcrest.core.IsNot.not;
import static org.junit.Assert.assertThat;
-public class ResourceUnmarshallerTest {
+public class ResourceUnmarshallerTest extends UnmarshallerTestCase {
@Test
public void singleResource() {
- String json = WSTestUtils.loadFile("/resources/single-resource.json");
+ String json = loadFile("/resources/single-resource.json");
assertSonar(new ResourceUnmarshaller().toModel(json));
List<Resource> resources = new ResourceUnmarshaller().toModels(json);
@@ -43,7 +43,7 @@ public class ResourceUnmarshallerTest {
@Test
public void singleResourceWithMeasures() {
- Resource resource = new ResourceUnmarshaller().toModel(WSTestUtils.loadFile("/resources/single-resource-with-measures.json"));
+ Resource resource = new ResourceUnmarshaller().toModel(loadFile("/resources/single-resource-with-measures.json"));
assertSonar(resource);
assertThat(resource.getMeasures().size(), is(2));
@@ -54,7 +54,7 @@ public class ResourceUnmarshallerTest {
@Test
public void singleResourceWithTrends() {
- Resource resource = new ResourceUnmarshaller().toModel(WSTestUtils.loadFile("/resources/single-resource-with-trends.json"));
+ Resource resource = new ResourceUnmarshaller().toModel(loadFile("/resources/single-resource-with-trends.json"));
assertSonar(resource);
assertThat(resource.getMeasures().size(), is(2));
@@ -68,7 +68,7 @@ public class ResourceUnmarshallerTest {
@Test
public void manyResources() {
- List<Resource> resources = new ResourceUnmarshaller().toModels(WSTestUtils.loadFile("/resources/many-resources.json"));
+ List<Resource> resources = new ResourceUnmarshaller().toModels(loadFile("/resources/many-resources.json"));
assertThat(resources.size(), is(19));
for (Resource resource : resources) {
@@ -80,7 +80,7 @@ public class ResourceUnmarshallerTest {
@Test
public void manyResourcesWithMeasures() {
- List<Resource> resources = new ResourceUnmarshaller().toModels(WSTestUtils.loadFile("/resources/many-resources-with-measures.json"));
+ List<Resource> resources = new ResourceUnmarshaller().toModels(loadFile("/resources/many-resources-with-measures.json"));
assertThat(resources.size(), is(17));
for (Resource resource : resources) {
diff --git a/sonar-ws-client/src/test/java/org/sonar/wsclient/unmarshallers/RuleUnmarshallerTest.java b/sonar-ws-client/src/test/java/org/sonar/wsclient/unmarshallers/RuleUnmarshallerTest.java
index 71484bbe97e..6f75b766871 100644
--- a/sonar-ws-client/src/test/java/org/sonar/wsclient/unmarshallers/RuleUnmarshallerTest.java
+++ b/sonar-ws-client/src/test/java/org/sonar/wsclient/unmarshallers/RuleUnmarshallerTest.java
@@ -28,7 +28,7 @@ import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.nullValue;
import static org.junit.Assert.assertThat;
-public class RuleUnmarshallerTest {
+public class RuleUnmarshallerTest extends UnmarshallerTestCase {
@Test
public void toModels() {
@@ -38,7 +38,7 @@ public class RuleUnmarshallerTest {
List<Rule> rules = new RuleUnmarshaller().toModels("[]");
assertThat(rules.size(), is(0));
- rules = new RuleUnmarshaller().toModels(WSTestUtils.loadFile("/rules/rules.json"));
+ rules = new RuleUnmarshaller().toModels(loadFile("/rules/rules.json"));
assertThat(rules.size(), is(6));
rule = rules.get(0);
diff --git a/sonar-ws-client/src/test/java/org/sonar/wsclient/unmarshallers/ServerUnmarshallerTest.java b/sonar-ws-client/src/test/java/org/sonar/wsclient/unmarshallers/ServerUnmarshallerTest.java
index 8e966d1a7c1..c6936bc56a5 100644
--- a/sonar-ws-client/src/test/java/org/sonar/wsclient/unmarshallers/ServerUnmarshallerTest.java
+++ b/sonar-ws-client/src/test/java/org/sonar/wsclient/unmarshallers/ServerUnmarshallerTest.java
@@ -26,11 +26,11 @@ import static org.hamcrest.CoreMatchers.nullValue;
import static org.hamcrest.core.Is.is;
import static org.junit.Assert.assertThat;
-public class ServerUnmarshallerTest {
+public class ServerUnmarshallerTest extends UnmarshallerTestCase {
@Test
public void testToModel() {
- Server server = new ServerUnmarshaller().toModel(WSTestUtils.loadFile("/server/server.json"));
+ Server server = new ServerUnmarshaller().toModel(loadFile("/server/server.json"));
assertThat(server.getId(), is("123456789"));
assertThat(server.getVersion(), is("2.0-SNAPSHOT"));
assertThat(server.getStatus(), is(Server.Status.UP));
@@ -39,7 +39,7 @@ public class ServerUnmarshallerTest {
@Test
public void shouldNotFailIfStatusIsMissing() {
- Server server = new ServerUnmarshaller().toModel(WSTestUtils.loadFile("/server/status_missing.json"));
+ Server server = new ServerUnmarshaller().toModel(loadFile("/server/status_missing.json"));
assertThat(server.getId(), is("123456789"));
assertThat(server.getVersion(), is("2.0-SNAPSHOT"));
assertThat(server.getStatus(), nullValue());
diff --git a/sonar-ws-client/src/test/java/org/sonar/wsclient/unmarshallers/SourceUnmarshallerTest.java b/sonar-ws-client/src/test/java/org/sonar/wsclient/unmarshallers/SourceUnmarshallerTest.java
index 31061fa2bbe..b97d71c216f 100644
--- a/sonar-ws-client/src/test/java/org/sonar/wsclient/unmarshallers/SourceUnmarshallerTest.java
+++ b/sonar-ws-client/src/test/java/org/sonar/wsclient/unmarshallers/SourceUnmarshallerTest.java
@@ -26,21 +26,21 @@ import static org.hamcrest.CoreMatchers.nullValue;
import static org.hamcrest.core.Is.is;
import static org.junit.Assert.assertThat;
-public class SourceUnmarshallerTest {
+public class SourceUnmarshallerTest extends UnmarshallerTestCase {
@Test
public void toModel() {
Source source = new SourceUnmarshaller().toModel("[]");
assertThat(source, nullValue());
- source = new SourceUnmarshaller().toModel(WSTestUtils.loadFile("/sources/source.json"));
+ source = new SourceUnmarshaller().toModel(loadFile("/sources/source.json"));
assertThat(source.getLines().size(), is(236));
assertThat(source.getLine(3), is(" * Copyright (C) 2009 SonarSource SA"));
}
@Test
public void fromLineToLine() {
- Source source = new SourceUnmarshaller().toModel(WSTestUtils.loadFile("/sources/from_line_to_line.json"));
+ Source source = new SourceUnmarshaller().toModel(loadFile("/sources/from_line_to_line.json"));
assertThat(source.getLines().size(), is(15));
assertThat(source.getLine(1), nullValue());
assertThat(source.getLine(3), is(" * Copyright (C) 2009 SonarSource SA"));
diff --git a/sonar-ws-client/src/test/java/org/sonar/wsclient/unmarshallers/TimeMachineUnmarshallerTest.java b/sonar-ws-client/src/test/java/org/sonar/wsclient/unmarshallers/TimeMachineUnmarshallerTest.java
index 731d58e24f7..bad185a6b79 100644
--- a/sonar-ws-client/src/test/java/org/sonar/wsclient/unmarshallers/TimeMachineUnmarshallerTest.java
+++ b/sonar-ws-client/src/test/java/org/sonar/wsclient/unmarshallers/TimeMachineUnmarshallerTest.java
@@ -26,11 +26,11 @@ import static org.hamcrest.CoreMatchers.nullValue;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat;
-public class TimeMachineUnmarshallerTest {
+public class TimeMachineUnmarshallerTest extends UnmarshallerTestCase {
@Test
public void testToModel() throws Exception {
- TimeMachine timeMachine = new TimeMachineUnmarshaller().toModel(WSTestUtils.loadFile("/timemachine/timemachine.json"));
+ TimeMachine timeMachine = new TimeMachineUnmarshaller().toModel(loadFile("/timemachine/timemachine.json"));
// columns
assertThat(timeMachine.getColumns().length, is(2));
@@ -52,7 +52,7 @@ public class TimeMachineUnmarshallerTest {
@Test
public void shouldAcceptNullValues() throws Exception {
- TimeMachine timeMachine = new TimeMachineUnmarshaller().toModel(WSTestUtils.loadFile("/timemachine/null-values.json"));
+ TimeMachine timeMachine = new TimeMachineUnmarshaller().toModel(loadFile("/timemachine/null-values.json"));
assertThat(timeMachine.getCells()[0].getValues().length, is(2));
assertThat(timeMachine.getCells()[0].getValues()[0], nullValue());
@@ -64,7 +64,7 @@ public class TimeMachineUnmarshallerTest {
@Test
public void shouldCastValues() throws Exception {
- TimeMachine timeMachine = new TimeMachineUnmarshaller().toModel(WSTestUtils.loadFile("/timemachine/typed-values.json"));
+ TimeMachine timeMachine = new TimeMachineUnmarshaller().toModel(loadFile("/timemachine/typed-values.json"));
assertThat(timeMachine.getCells()[0].getValues().length, is(2));
assertThat((String) timeMachine.getCells()[0].getValues()[0], is("Sonar way"));
diff --git a/sonar-ws-client/src/test/java/org/sonar/wsclient/unmarshallers/UnmarshallerTestCase.java b/sonar-ws-client/src/test/java/org/sonar/wsclient/unmarshallers/UnmarshallerTestCase.java
new file mode 100644
index 00000000000..82792d381d3
--- /dev/null
+++ b/sonar-ws-client/src/test/java/org/sonar/wsclient/unmarshallers/UnmarshallerTestCase.java
@@ -0,0 +1,25 @@
+package org.sonar.wsclient.unmarshallers;
+
+import org.apache.commons.io.IOUtils;
+import org.junit.BeforeClass;
+import org.sonar.wsclient.JdkUtils;
+import org.sonar.wsclient.services.WSUtils;
+
+import java.io.IOException;
+
+public abstract class UnmarshallerTestCase {
+
+ @BeforeClass
+ public static void setupWsUtils() {
+ WSUtils.setInstance(new JdkUtils());
+ }
+
+ public static String loadFile(String path) {
+ try {
+ return IOUtils.toString(UnmarshallerTestCase.class.getResourceAsStream(path));
+ } catch (IOException e) {
+ throw new RuntimeException(e);
+ }
+ }
+
+}
diff --git a/sonar-ws-client/src/test/java/org/sonar/wsclient/unmarshallers/ViolationUnmarshallerTest.java b/sonar-ws-client/src/test/java/org/sonar/wsclient/unmarshallers/ViolationUnmarshallerTest.java
index 22e256dc73b..fd83fcca74b 100644
--- a/sonar-ws-client/src/test/java/org/sonar/wsclient/unmarshallers/ViolationUnmarshallerTest.java
+++ b/sonar-ws-client/src/test/java/org/sonar/wsclient/unmarshallers/ViolationUnmarshallerTest.java
@@ -30,14 +30,14 @@ import static org.hamcrest.Matchers.notNullValue;
import static org.hamcrest.core.IsNot.not;
import static org.junit.Assert.assertThat;
-public class ViolationUnmarshallerTest {
+public class ViolationUnmarshallerTest extends UnmarshallerTestCase {
@Test
public void toModels() {
Violation violation = new ViolationUnmarshaller().toModel("[]");
assertThat(violation, nullValue());
- List<Violation> violations = new ViolationUnmarshaller().toModels(WSTestUtils.loadFile("/violations/violations.json"));
+ List<Violation> violations = new ViolationUnmarshaller().toModels(loadFile("/violations/violations.json"));
assertThat(violations.size(), is(2));
violation = violations.get(0);
@@ -56,7 +56,7 @@ public class ViolationUnmarshallerTest {
@Test
public void violationWithoutLineNumber() {
- Violation violation = new ViolationUnmarshaller().toModel(WSTestUtils.loadFile("/violations/violation-without-optional-fields.json"));
+ Violation violation = new ViolationUnmarshaller().toModel(loadFile("/violations/violation-without-optional-fields.json"));
assertThat(violation.getMessage(), not(nullValue()));
assertThat(violation.getLine(), nullValue());
assertThat(violation.getCreatedAt(), nullValue());
diff --git a/sonar-ws-client/src/test/java/org/sonar/wsclient/unmarshallers/WSTestUtils.java b/sonar-ws-client/src/test/java/org/sonar/wsclient/unmarshallers/WSTestUtils.java
deleted file mode 100644
index a6c8981137d..00000000000
--- a/sonar-ws-client/src/test/java/org/sonar/wsclient/unmarshallers/WSTestUtils.java
+++ /dev/null
@@ -1,39 +0,0 @@
-/*
- * Sonar, open source software quality management tool.
- * Copyright (C) 2009 SonarSource SA
- * mailto:contact AT sonarsource DOT com
- *
- * Sonar is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 3 of the License, or (at your option) any later version.
- *
- * Sonar is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with Sonar; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
- */
-package org.sonar.wsclient.unmarshallers;
-
-import org.apache.commons.io.IOUtils;
-
-import java.io.IOException;
-
-public final class WSTestUtils {
-
- public static String loadFile(String path) {
- try {
- return IOUtils.toString(WSTestUtils.class.getResourceAsStream(path));
- } catch (IOException e) {
- throw new RuntimeException(e);
- }
- }
-
- private WSTestUtils() {
- }
-
-}