From: Evgeny Mandrikov Date: Wed, 26 Jan 2011 16:21:54 +0000 (+0300) Subject: SONAR-2046: Reuse unmarshallers from sonar-ws-client for sonar-gwt-api X-Git-Tag: 2.6~99 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=815899837ca07904fe9dc9223b4a4e8ac486cb59;p=sonarqube.git 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. --- diff --git a/sonar-gwt-api/pom.xml b/sonar-gwt-api/pom.xml index 7f60b4adf39..1b0e10f2544 100644 --- a/sonar-gwt-api/pom.xml +++ b/sonar-gwt-api/pom.xml @@ -66,7 +66,7 @@ jar true ${project.build.directory}/classes - **/services/*.java + **/services/*.java,**/unmarshallers/*.java true diff --git a/sonar-gwt-api/src/main/java/org/sonar/gwt/ui/Page.java b/sonar-gwt-api/src/main/java/org/sonar/gwt/ui/Page.java index 94b2785f6e9..ccd7ad4ed04 100644 --- a/sonar-gwt-api/src/main/java/org/sonar/gwt/ui/Page.java +++ b/sonar-gwt-api/src/main/java/org/sonar/gwt/ui/Page.java @@ -22,13 +22,18 @@ package org.sonar.gwt.ui; import com.google.gwt.core.client.EntryPoint; import com.google.gwt.core.client.GWT; import com.google.gwt.core.client.JavaScriptObject; +import com.google.gwt.json.client.JSONObject; import com.google.gwt.user.client.ui.RootPanel; import com.google.gwt.user.client.ui.Widget; -import org.sonar.wsclient.gwt.unmarshallers.ResourceUnmarshaller; +import org.sonar.wsclient.gwt.GwtUtils; import org.sonar.wsclient.services.Resource; +import org.sonar.wsclient.services.WSUtils; +import org.sonar.wsclient.unmarshallers.ResourceUnmarshaller; public abstract class Page implements EntryPoint { + private static final ResourceUnmarshaller RESOURCE_UNMARSHALLER = new ResourceUnmarshaller(); + private Widget currentWidget = null; private Integer currentResourceId = null; @@ -40,7 +45,7 @@ public abstract class Page implements EntryPoint { private void load() { Widget widget = doOnModuleLoad(); - if (widget!=null) { + if (widget != null) { getRootPanel().add(widget); } } @@ -52,7 +57,11 @@ public abstract class Page implements EntryPoint { public final void onResourceLoad() { JavaScriptObject json = loadResource(); if (json != null) { - Resource resource = ResourceUnmarshaller.getInstance().toModel(json); + if (WSUtils.getINSTANCE() == null) { + WSUtils.setInstance(new GwtUtils()); // TODO dirty hack to initialize WSUtils + } + String jsonStr = (new JSONObject(json)).toString(); + Resource resource = RESOURCE_UNMARSHALLER.toModel(jsonStr); RootPanel container = getRootPanel(); container.clear(); @@ -82,11 +91,10 @@ public abstract class Page implements EntryPoint { } private native JavaScriptObject loadResource()/*-{ - return $wnd.config['resource']; - }-*/; + return $wnd.config['resource']; + }-*/; private native void export(String gwtId, Page page)/*-{ - $wnd.modules[gwtId]=function() {page.@org.sonar.gwt.ui.Page::onResourceLoad()()}; - }-*/; + $wnd.modules[gwtId]=function() {page.@org.sonar.gwt.ui.Page::onResourceLoad()()}; + }-*/; } - diff --git a/sonar-gwt-api/src/main/java/org/sonar/wsclient/gwt/GwtUtils.java b/sonar-gwt-api/src/main/java/org/sonar/wsclient/gwt/GwtUtils.java index 8fc7df08fb6..ddeeb92d990 100644 --- a/sonar-gwt-api/src/main/java/org/sonar/wsclient/gwt/GwtUtils.java +++ b/sonar-gwt-api/src/main/java/org/sonar/wsclient/gwt/GwtUtils.java @@ -20,9 +20,14 @@ package org.sonar.wsclient.gwt; import com.google.gwt.i18n.client.DateTimeFormat; +import com.google.gwt.json.client.JSONObject; +import com.google.gwt.json.client.JSONParser; +import com.google.gwt.json.client.JSONValue; +import org.sonar.gwt.JsonUtils; import org.sonar.wsclient.services.WSUtils; import java.util.Date; +import java.util.Set; public class GwtUtils extends WSUtils { @Override @@ -34,4 +39,64 @@ public class GwtUtils extends WSUtils { public String encodeUrl(String url) { return com.google.gwt.http.client.URL.encode(url); } + + @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) { + Double d = JsonUtils.getDouble((JSONObject) json, field); + if (d != null) { + return d.longValue(); + } + return null; + } + + @Override + public Date getDateTime(Object json, String field) { + return JsonUtils.getDate((JSONObject) json, field); + } + + @Override + public int getArraySize(Object array) { + return JsonUtils.getArraySize((JSONValue) array); + } + + @Override + public Object getArrayElement(Object array, int i) { + return JsonUtils.getArray((JSONValue) array, i); + } + + @Override + public Object parse(String jsonStr) { + return JSONParser.parse(jsonStr); + } + + @Override + public Set getFields(Object json) { + return ((JSONObject) json).keySet(); + } + } diff --git a/sonar-gwt-api/src/main/java/org/sonar/wsclient/gwt/Sonar.java b/sonar-gwt-api/src/main/java/org/sonar/wsclient/gwt/Sonar.java index 33f404dd551..916b086d01a 100644 --- a/sonar-gwt-api/src/main/java/org/sonar/wsclient/gwt/Sonar.java +++ b/sonar-gwt-api/src/main/java/org/sonar/wsclient/gwt/Sonar.java @@ -21,18 +21,19 @@ package org.sonar.wsclient.gwt; import com.google.gwt.core.client.JavaScriptObject; import com.google.gwt.i18n.client.Dictionary; +import com.google.gwt.json.client.JSONObject; import org.sonar.gwt.JsonUtils; -import org.sonar.wsclient.gwt.unmarshallers.Unmarshaller; -import org.sonar.wsclient.gwt.unmarshallers.Unmarshallers; import org.sonar.wsclient.services.Model; import org.sonar.wsclient.services.Query; import org.sonar.wsclient.services.WSUtils; +import org.sonar.wsclient.unmarshallers.Unmarshaller; +import org.sonar.wsclient.unmarshallers.Unmarshallers; public class Sonar { static { WSUtils.setInstance(new GwtUtils()); } - + private static Sonar instance = null; private final String host; @@ -56,7 +57,8 @@ public class Sonar { JsonUtils.requestJson(getUrl(query), new JsonUtils.JSONHandler() { public void onResponse(JavaScriptObject obj) { Unmarshaller unmarshaller = Unmarshallers.forModel(query.getModelClass()); - callback.onResponse(unmarshaller.toModel(obj), obj); + String json = (new JSONObject(obj)).toString(); + callback.onResponse(unmarshaller.toModel(json), obj); } public void onTimeout() { @@ -73,7 +75,8 @@ public class Sonar { JsonUtils.requestJson(getUrl(query), new JsonUtils.JSONHandler() { public void onResponse(JavaScriptObject obj) { Unmarshaller unmarshaller = Unmarshallers.forModel(query.getModelClass()); - callback.onResponse(unmarshaller.toModels(obj), obj); + String json = (new JSONObject(obj)).toString(); + callback.onResponse(unmarshaller.toModels(json), obj); } public void onTimeout() { @@ -89,4 +92,4 @@ public class Sonar { private String getUrl(Query query) { return host + query.getUrl(); } -} \ No newline at end of file +} diff --git a/sonar-gwt-api/src/main/java/org/sonar/wsclient/gwt/unmarshallers/AbstractUnmarshaller.java b/sonar-gwt-api/src/main/java/org/sonar/wsclient/gwt/unmarshallers/AbstractUnmarshaller.java deleted file mode 100644 index a7a6f835a85..00000000000 --- a/sonar-gwt-api/src/main/java/org/sonar/wsclient/gwt/unmarshallers/AbstractUnmarshaller.java +++ /dev/null @@ -1,53 +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.gwt.unmarshallers; - -import com.google.gwt.core.client.JavaScriptObject; -import com.google.gwt.json.client.JSONArray; -import com.google.gwt.json.client.JSONObject; -import org.sonar.wsclient.services.Model; - -import java.util.ArrayList; -import java.util.List; - -public abstract class AbstractUnmarshaller implements Unmarshaller { - public final MODEL toModel(JavaScriptObject json) { - JSONArray array = new JSONArray(json); - if (array.size() >= 1) { - JSONObject elt = array.get(0).isObject(); - return parse(elt); - } - return null; - } - - public final List toModels(JavaScriptObject json) { - List result = new ArrayList(); - JSONArray array = new JSONArray(json); - for (int i = 0; i < array.size(); i++) { - JSONObject elt = array.get(i).isObject(); - if (elt != null) { - result.add(parse(elt)); - } - } - return result; - } - - protected abstract MODEL parse(JSONObject json); -} diff --git a/sonar-gwt-api/src/main/java/org/sonar/wsclient/gwt/unmarshallers/DependencyTreeUnmarshaller.java b/sonar-gwt-api/src/main/java/org/sonar/wsclient/gwt/unmarshallers/DependencyTreeUnmarshaller.java deleted file mode 100644 index c7c3bb3d29e..00000000000 --- a/sonar-gwt-api/src/main/java/org/sonar/wsclient/gwt/unmarshallers/DependencyTreeUnmarshaller.java +++ /dev/null @@ -1,55 +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.gwt.unmarshallers; - -import com.google.gwt.json.client.JSONArray; -import com.google.gwt.json.client.JSONObject; -import org.sonar.gwt.JsonUtils; -import org.sonar.wsclient.services.DependencyTree; - -import java.util.ArrayList; -import java.util.List; - -public class DependencyTreeUnmarshaller extends AbstractUnmarshaller { - - protected DependencyTree parse(JSONObject json) { - 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")); - - List to = new ArrayList(); - tree.setTo(to); - - JSONArray toJson = (JSONArray) json.get("to"); - if (toJson != null) { - for (int index = 0; index < toJson.size(); index++) { - to.add(parse((JSONObject) toJson.get(index))); - } - } - return tree; - } -} diff --git a/sonar-gwt-api/src/main/java/org/sonar/wsclient/gwt/unmarshallers/DependencyUnmarshaller.java b/sonar-gwt-api/src/main/java/org/sonar/wsclient/gwt/unmarshallers/DependencyUnmarshaller.java deleted file mode 100644 index 76dd2397eba..00000000000 --- a/sonar-gwt-api/src/main/java/org/sonar/wsclient/gwt/unmarshallers/DependencyUnmarshaller.java +++ /dev/null @@ -1,42 +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.gwt.unmarshallers; - -import com.google.gwt.json.client.JSONObject; -import org.sonar.gwt.JsonUtils; -import org.sonar.wsclient.services.Dependency; - -public class DependencyUnmarshaller extends AbstractUnmarshaller { - - protected Dependency parse(JSONObject json) { - return new Dependency() - .setId(JsonUtils.getString(json, "id")) - .setFromId(JsonUtils.getDouble(json, "fi").longValue()) - .setToId(JsonUtils.getDouble(json, "ti").longValue()) - .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")); - } -} diff --git a/sonar-gwt-api/src/main/java/org/sonar/wsclient/gwt/unmarshallers/EventUnmarshaller.java b/sonar-gwt-api/src/main/java/org/sonar/wsclient/gwt/unmarshallers/EventUnmarshaller.java deleted file mode 100644 index bd7ca08aa75..00000000000 --- a/sonar-gwt-api/src/main/java/org/sonar/wsclient/gwt/unmarshallers/EventUnmarshaller.java +++ /dev/null @@ -1,38 +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.gwt.unmarshallers; - -import com.google.gwt.json.client.JSONObject; -import org.sonar.gwt.JsonUtils; -import org.sonar.wsclient.services.Event; - -public class EventUnmarshaller extends AbstractUnmarshaller { - - protected Event parse(JSONObject json) { - 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.getDate(json, "dt")) - .setDescription(JsonUtils.getString(json, "ds")) - .setData(JsonUtils.getString(json, "data")); - } -} diff --git a/sonar-gwt-api/src/main/java/org/sonar/wsclient/gwt/unmarshallers/MetricUnmarshaller.java b/sonar-gwt-api/src/main/java/org/sonar/wsclient/gwt/unmarshallers/MetricUnmarshaller.java deleted file mode 100644 index 283f5fe19a9..00000000000 --- a/sonar-gwt-api/src/main/java/org/sonar/wsclient/gwt/unmarshallers/MetricUnmarshaller.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.gwt.unmarshallers; - -import com.google.gwt.json.client.JSONObject; -import org.sonar.gwt.JsonUtils; -import org.sonar.wsclient.services.Metric; - -public class MetricUnmarshaller extends AbstractUnmarshaller { - - protected Metric parse(JSONObject json) { - 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")) - .setHidden(JsonUtils.getBoolean(json, "hidden")) - .setUserManaged(JsonUtils.getBoolean(json, "user_managed")); - } -} diff --git a/sonar-gwt-api/src/main/java/org/sonar/wsclient/gwt/unmarshallers/PropertyUnmarshaller.java b/sonar-gwt-api/src/main/java/org/sonar/wsclient/gwt/unmarshallers/PropertyUnmarshaller.java deleted file mode 100644 index 3ff7931a1c8..00000000000 --- a/sonar-gwt-api/src/main/java/org/sonar/wsclient/gwt/unmarshallers/PropertyUnmarshaller.java +++ /dev/null @@ -1,33 +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.gwt.unmarshallers; - -import com.google.gwt.json.client.JSONObject; -import org.sonar.gwt.JsonUtils; -import org.sonar.wsclient.services.Property; - -public class PropertyUnmarshaller extends AbstractUnmarshaller { - - protected Property parse(JSONObject json) { - return new Property() - .setKey(JsonUtils.getString(json, "key")) - .setValue(JsonUtils.getString(json, "value")); - } -} diff --git a/sonar-gwt-api/src/main/java/org/sonar/wsclient/gwt/unmarshallers/ResourceUnmarshaller.java b/sonar-gwt-api/src/main/java/org/sonar/wsclient/gwt/unmarshallers/ResourceUnmarshaller.java deleted file mode 100644 index 3451bf16ed5..00000000000 --- a/sonar-gwt-api/src/main/java/org/sonar/wsclient/gwt/unmarshallers/ResourceUnmarshaller.java +++ /dev/null @@ -1,100 +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.gwt.unmarshallers; - -import com.google.gwt.json.client.JSONObject; -import com.google.gwt.json.client.JSONValue; -import org.sonar.gwt.JsonUtils; -import org.sonar.wsclient.services.Measure; -import org.sonar.wsclient.services.Resource; - -import java.util.ArrayList; -import java.util.List; - -public class ResourceUnmarshaller extends AbstractUnmarshaller { - - private static final ResourceUnmarshaller INSTANCE = new ResourceUnmarshaller(); - - private ResourceUnmarshaller() { - } - - public static ResourceUnmarshaller getInstance() { - return INSTANCE; - } - - protected Resource parse(JSONObject 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")) - .setVersion(JsonUtils.getString(json, "version")) - .setDate(JsonUtils.getDate(json, "date")) - .setCopy(JsonUtils.getInteger(json, "copy")); - } - - private void parseMeasures(JSONObject json, Resource resource) { - JSONValue measuresJson = json.get("msr"); - if (measuresJson != null) { - resource.setMeasures(parseMeasures(measuresJson)); - } - } - - private List parseMeasures(JSONValue measuresJson) { - List projectMeasures = new ArrayList(); - int len = JsonUtils.getArraySize(measuresJson); - for (int i = 0; i < len; i++) { - JSONObject measureJson = JsonUtils.getArray(measuresJson, i); - if (measureJson != null) { - Measure measure = parseMeasure(measureJson); - projectMeasures.add(measure); - } - } - return projectMeasures; - } - - private Measure parseMeasure(JSONObject json) { - 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")) - .setData(JsonUtils.getString(json, "data")) - .setTrend(JsonUtils.getInteger(json, "trend")) - .setVar(JsonUtils.getInteger(json, "var")) - .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")); - return measure; - } -} diff --git a/sonar-gwt-api/src/main/java/org/sonar/wsclient/gwt/unmarshallers/ServerUnmarshaller.java b/sonar-gwt-api/src/main/java/org/sonar/wsclient/gwt/unmarshallers/ServerUnmarshaller.java deleted file mode 100644 index ad87a8133e6..00000000000 --- a/sonar-gwt-api/src/main/java/org/sonar/wsclient/gwt/unmarshallers/ServerUnmarshaller.java +++ /dev/null @@ -1,49 +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.gwt.unmarshallers; - -import com.google.gwt.core.client.JavaScriptObject; -import com.google.gwt.json.client.JSONObject; -import org.sonar.gwt.JsonUtils; -import org.sonar.wsclient.services.Server; - -import java.util.Arrays; -import java.util.List; - -public class ServerUnmarshaller implements Unmarshaller { - - public Server toModel(JavaScriptObject json) { - JSONObject map = new JSONObject(json); - Server server = new Server() - .setId(JsonUtils.getString(map, "id")) - .setVersion(JsonUtils.getString(map, "version")); - server.setStatusMessage(JsonUtils.getString(map, "status_msg")); - String status = JsonUtils.getString(map, "status"); - if (status != null) { - server.setStatus(Server.Status.valueOf(status)); - } - return server; - } - - public List toModels(JavaScriptObject json) { - return Arrays.asList(toModel(json)); - } -} - diff --git a/sonar-gwt-api/src/main/java/org/sonar/wsclient/gwt/unmarshallers/SourceUnmarshaller.java b/sonar-gwt-api/src/main/java/org/sonar/wsclient/gwt/unmarshallers/SourceUnmarshaller.java deleted file mode 100644 index bf2c3ef2f38..00000000000 --- a/sonar-gwt-api/src/main/java/org/sonar/wsclient/gwt/unmarshallers/SourceUnmarshaller.java +++ /dev/null @@ -1,42 +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.gwt.unmarshallers; - -import com.google.gwt.json.client.JSONObject; -import com.google.gwt.json.client.JSONValue; -import org.sonar.wsclient.services.Source; - -public class SourceUnmarshaller extends AbstractUnmarshaller { - - protected Source parse(JSONObject json) { - Source source = new Source(); - - for (String key : json.keySet()) { - JSONValue val = json.get(key); - if (val.isString() != null) { - source.addLine(Integer.parseInt(key), val.isString().stringValue()); - } else { - source.addLine(Integer.parseInt(key), ""); - } - } - - return source; - } -} diff --git a/sonar-gwt-api/src/main/java/org/sonar/wsclient/gwt/unmarshallers/TimeMachineUnmarshaller.java b/sonar-gwt-api/src/main/java/org/sonar/wsclient/gwt/unmarshallers/TimeMachineUnmarshaller.java deleted file mode 100644 index 562e986c70e..00000000000 --- a/sonar-gwt-api/src/main/java/org/sonar/wsclient/gwt/unmarshallers/TimeMachineUnmarshaller.java +++ /dev/null @@ -1,45 +0,0 @@ -package org.sonar.wsclient.gwt.unmarshallers; - -import com.google.gwt.json.client.JSONArray; -import com.google.gwt.json.client.JSONObject; -import org.sonar.gwt.JsonUtils; -import org.sonar.wsclient.services.TimeMachine; -import org.sonar.wsclient.services.TimeMachineCell; -import org.sonar.wsclient.services.TimeMachineColumn; - -public class TimeMachineUnmarshaller extends AbstractUnmarshaller { - - protected TimeMachine parse(JSONObject json) { - JSONArray cols = json.get("cols").isArray(); - JSONArray cells = json.get("cells").isArray(); - return new TimeMachine(toColumns(cols), toCells(cells)); - } - - private TimeMachineColumn[] toColumns(JSONArray cols) { - int size = cols.size(); - TimeMachineColumn[] result = new TimeMachineColumn[size]; - for (int index = 0; index < JsonUtils.getArraySize(cols); index++) { - JSONObject elem = JsonUtils.getArray(cols, index); - result[index] = new TimeMachineColumn(index, JsonUtils.getString(elem, "metric"), null, null); - } - return result; - } - - private TimeMachineCell[] toCells(JSONArray cells) { - int size = JsonUtils.getArraySize(cells); - TimeMachineCell[] result = new TimeMachineCell[size]; - for (int i = 0; i < size; i++) { - JSONObject cellJson = JsonUtils.getArray(cells, i); - JSONArray valuesJson = cellJson.get("v").isArray(); - - Object[] resultValues = new Object[JsonUtils.getArraySize(valuesJson)]; - for (int indexValue = 0; indexValue < JsonUtils.getArraySize(valuesJson); indexValue++) { - Object value = valuesJson.get(indexValue); - resultValues[indexValue] = value; - } - result[i] = new TimeMachineCell(JsonUtils.getDate(cellJson, "d"), resultValues); - } - return result; - } - -} diff --git a/sonar-gwt-api/src/main/java/org/sonar/wsclient/gwt/unmarshallers/Unmarshaller.java b/sonar-gwt-api/src/main/java/org/sonar/wsclient/gwt/unmarshallers/Unmarshaller.java deleted file mode 100644 index 39c5628c302..00000000000 --- a/sonar-gwt-api/src/main/java/org/sonar/wsclient/gwt/unmarshallers/Unmarshaller.java +++ /dev/null @@ -1,33 +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.gwt.unmarshallers; - -import com.google.gwt.core.client.JavaScriptObject; -import org.sonar.wsclient.services.Model; - -import java.util.List; - -public interface Unmarshaller { - - MODEL toModel(JavaScriptObject json); - - List toModels(JavaScriptObject json); - -} diff --git a/sonar-gwt-api/src/main/java/org/sonar/wsclient/gwt/unmarshallers/Unmarshallers.java b/sonar-gwt-api/src/main/java/org/sonar/wsclient/gwt/unmarshallers/Unmarshallers.java deleted file mode 100644 index d28a90ccee8..00000000000 --- a/sonar-gwt-api/src/main/java/org/sonar/wsclient/gwt/unmarshallers/Unmarshallers.java +++ /dev/null @@ -1,55 +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.gwt.unmarshallers; - -import org.sonar.wsclient.services.*; - -import java.util.HashMap; -import java.util.Map; - -public final class Unmarshallers { - - private Unmarshallers() { - } - - private static Map unmarshallers; - - static { - unmarshallers = new HashMap(); - unmarshallers.put(Metric.class, new MetricUnmarshaller()); - unmarshallers.put(Dependency.class, new DependencyUnmarshaller()); - unmarshallers.put(Resource.class, ResourceUnmarshaller.getInstance()); - unmarshallers.put(Property.class, new PropertyUnmarshaller()); - unmarshallers.put(Source.class, new SourceUnmarshaller()); - unmarshallers.put(Violation.class, new ViolationUnmarshaller()); - unmarshallers.put(Server.class, new ServerUnmarshaller()); - unmarshallers.put(DependencyTree.class, new DependencyTreeUnmarshaller()); - unmarshallers.put(Event.class, new EventUnmarshaller()); - // TODO - // FavouriteUnmarshaller - // PluginUnmarshaller - // RuleUnmarshaller - unmarshallers.put(TimeMachine.class, new TimeMachineUnmarshaller()); - } - - public static Unmarshaller forModel(Class modelClass) { - return unmarshallers.get(modelClass); - } -} diff --git a/sonar-gwt-api/src/main/java/org/sonar/wsclient/gwt/unmarshallers/ViolationUnmarshaller.java b/sonar-gwt-api/src/main/java/org/sonar/wsclient/gwt/unmarshallers/ViolationUnmarshaller.java deleted file mode 100644 index bf0194820e3..00000000000 --- a/sonar-gwt-api/src/main/java/org/sonar/wsclient/gwt/unmarshallers/ViolationUnmarshaller.java +++ /dev/null @@ -1,50 +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.gwt.unmarshallers; - -import com.google.gwt.json.client.JSONObject; -import org.sonar.gwt.JsonUtils; -import org.sonar.wsclient.services.Violation; - -public class ViolationUnmarshaller extends AbstractUnmarshaller { - - protected Violation parse(JSONObject json) { - 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.getDate(json, "createdAt")); - - JSONObject rule = (JSONObject) json.get("rule"); - if (rule != null) { - violation.setRuleKey(JsonUtils.getString(rule, "key")); - violation.setRuleName(JsonUtils.getString(rule, "name")); - } - - JSONObject resource = (JSONObject) json.get("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")); - } - return violation; - } -} 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 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 null if field does not exist + */ + public abstract Object getField(Object json, String field); + + /** + * @return String value of specified field from specified JSON object, + * or null if field does not exist + */ + public abstract String getString(Object json, String field); + + /** + * @return Boolean value of specified field from specified JSON object, + * or null if field does not exist + */ + public abstract Boolean getBoolean(Object json, String field); + + /** + * @return Integer value of specified field from specified JSON object, + * or null if field does not exist + */ + public abstract Integer getInteger(Object json, String field); + + /** + * @return Double value of specified field from specified JSON object, + * or null if field does not exist + */ + public abstract Double getDouble(Object json, String field); + + /** + * @return Long value of specified field from specified JSON object, + * or null if field does not exist + */ + public abstract Long getLong(Object json, String field); + + /** + * @return Date value of specified field from specified JSON object, + * or null 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 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 implements Unmarshaller { 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 implements Unmar } public final List toModels(String json) { + WSUtils utils = WSUtils.getINSTANCE(); List result = new ArrayList(); - 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 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 { @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 to = new ArrayList(); 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 { @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 { @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 { @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 { @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 { @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 { @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 { @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 parseMeasures(JSONArray measuresJson) { + private List parseMeasures(Object measuresJson) { + WSUtils utils = WSUtils.getINSTANCE(); List projectMeasures = new ArrayList(); - 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 { 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 { @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 parseParams(JSONArray paramsJson) { + private List parseParams(Object paramsJson) { + WSUtils utils = WSUtils.getINSTANCE(); + List ruleParams = new ArrayList(); - 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 { 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 { 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 { @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 entry = (Map.Entry) 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 { - 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 { @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 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 trees = new DependencyTreeUnmarshaller().toModels(WSTestUtils.loadFile("/dependency_tree/many_depths.json")); + List trees = new DependencyTreeUnmarshaller().toModels(loadFile("/dependency_tree/many_depths.json")); assertThat(trees.size(), is(1)); List 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 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 events = new EventUnmarshaller().toModels(WSTestUtils.loadFile("/events/events.json")); + List 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 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 plugins = new PluginUnmarshaller().toModels(WSTestUtils.loadFile("/plugins/plugins.json")); + WSUtils.setInstance(new JdkUtils()); + + List 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 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 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 resources = new ResourceUnmarshaller().toModels(WSTestUtils.loadFile("/resources/many-resources.json")); + List 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 resources = new ResourceUnmarshaller().toModels(WSTestUtils.loadFile("/resources/many-resources-with-measures.json")); + List 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 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 violations = new ViolationUnmarshaller().toModels(WSTestUtils.loadFile("/violations/violations.json")); + List 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() { - } - -}