<type>jar</type>
<overWrite>true</overWrite>
<outputDirectory>${project.build.directory}/classes</outputDirectory>
- <includes>**/services/*.java</includes>
+ <includes>**/services/*.java,**/unmarshallers/*.java</includes>
</artifactItem>
</artifactItems>
<overWriteReleases>true</overWriteReleases>
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;
private void load() {
Widget widget = doOnModuleLoad();
- if (widget!=null) {
+ if (widget != null) {
getRootPanel().add(widget);
}
}
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();
}
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()()};
+ }-*/;
}
-
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
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<String> getFields(Object json) {
+ return ((JSONObject) json).keySet();
+ }
+
}
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;
JsonUtils.requestJson(getUrl(query), new JsonUtils.JSONHandler() {
public void onResponse(JavaScriptObject obj) {
Unmarshaller<MODEL> 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() {
JsonUtils.requestJson(getUrl(query), new JsonUtils.JSONHandler() {
public void onResponse(JavaScriptObject obj) {
Unmarshaller<MODEL> 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() {
private String getUrl(Query query) {
return host + query.getUrl();
}
-}
\ No newline at end of file
+}
+++ /dev/null
-/*
- * 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<MODEL extends Model> implements Unmarshaller<MODEL> {
- 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<MODEL> toModels(JavaScriptObject json) {
- List<MODEL> result = new ArrayList<MODEL>();
- 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);
-}
+++ /dev/null
-/*
- * 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<DependencyTree> {
-
- 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<DependencyTree> to = new ArrayList<DependencyTree>();
- 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;
- }
-}
+++ /dev/null
-/*
- * 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<Dependency> {
-
- 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"));
- }
-}
+++ /dev/null
-/*
- * 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<Event> {
-
- 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"));
- }
-}
+++ /dev/null
-/*
- * 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<Metric> {
-
- 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"));
- }
-}
+++ /dev/null
-/*
- * 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<Property> {
-
- protected Property parse(JSONObject json) {
- return new Property()
- .setKey(JsonUtils.getString(json, "key"))
- .setValue(JsonUtils.getString(json, "value"));
- }
-}
+++ /dev/null
-/*
- * 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<Resource> {
-
- 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<Measure> parseMeasures(JSONValue measuresJson) {
- List<Measure> projectMeasures = new ArrayList<Measure>();
- 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;
- }
-}
+++ /dev/null
-/*
- * 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<Server> {
-
- 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<Server> toModels(JavaScriptObject json) {
- return Arrays.asList(toModel(json));
- }
-}
-
+++ /dev/null
-/*
- * 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<Source> {
-
- 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;
- }
-}
+++ /dev/null
-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<TimeMachine> {
-
- 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;
- }
-
-}
+++ /dev/null
-/*
- * 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 extends Model> {
-
- MODEL toModel(JavaScriptObject json);
-
- List<MODEL> toModels(JavaScriptObject json);
-
-}
+++ /dev/null
-/*
- * 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<Class, Unmarshaller> unmarshallers;
-
- static {
- unmarshallers = new HashMap<Class, Unmarshaller>();
- 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);
- }
-}
+++ /dev/null
-/*
- * 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<Violation> {
-
- 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;
- }
-}
*/
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 {
}
}
+ @Override
+ public Object getField(Object json, String field) {
+ return ((JSONObject) json).get(field);
+ }
+
+ @Override
+ public String getString(Object json, String field) {
+ return JsonUtils.getString((JSONObject) json, field);
+ }
+
+ @Override
+ public Boolean getBoolean(Object json, String field) {
+ return JsonUtils.getBoolean((JSONObject) json, field);
+ }
+
+ @Override
+ public Integer getInteger(Object json, String field) {
+ return JsonUtils.getInteger((JSONObject) json, field);
+ }
+
+ @Override
+ public Double getDouble(Object json, String field) {
+ return JsonUtils.getDouble((JSONObject) json, field);
+ }
+
+ @Override
+ public Long getLong(Object json, String field) {
+ return JsonUtils.getLong((JSONObject) json, field);
+ }
+
+ @Override
+ public Date getDateTime(Object json, String field) {
+ return JsonUtils.getDateTime((JSONObject) json, field);
+ }
+
+ @Override
+ public int getArraySize(Object array) {
+ return ((ArrayList) array).size();
+ }
+
+ @Override
+ public Object getArrayElement(Object array, int i) {
+ return ((ArrayList) array).get(i);
+ }
+
+ @Override
+ public Object parse(String jsonStr) {
+ return JSONValue.parse(jsonStr);
+ }
+
+ @Override
+ public Set<String> getFields(Object json) {
+ return ((JSONObject) json).keySet();
+ }
}
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;
public abstract String format(Date date, String format);
public abstract String encodeUrl(String url);
+
+ /**
+ * @return value of specified field from specified JSON object,
+ * or <code>null</code> if field does not exist
+ */
+ public abstract Object getField(Object json, String field);
+
+ /**
+ * @return String value of specified field from specified JSON object,
+ * or <code>null</code> if field does not exist
+ */
+ public abstract String getString(Object json, String field);
+
+ /**
+ * @return Boolean value of specified field from specified JSON object,
+ * or <code>null</code> if field does not exist
+ */
+ public abstract Boolean getBoolean(Object json, String field);
+
+ /**
+ * @return Integer value of specified field from specified JSON object,
+ * or <code>null</code> if field does not exist
+ */
+ public abstract Integer getInteger(Object json, String field);
+
+ /**
+ * @return Double value of specified field from specified JSON object,
+ * or <code>null</code> if field does not exist
+ */
+ public abstract Double getDouble(Object json, String field);
+
+ /**
+ * @return Long value of specified field from specified JSON object,
+ * or <code>null</code> if field does not exist
+ */
+ public abstract Long getLong(Object json, String field);
+
+ /**
+ * @return Date value of specified field from specified JSON object,
+ * or <code>null</code> if field does not exist
+ */
+ public abstract Date getDateTime(Object json, String field);
+
+ /**
+ * @return size of specified JSON array
+ */
+ public abstract int getArraySize(Object array);
+
+ /**
+ * @return element from specified JSON array
+ */
+ public abstract Object getArrayElement(Object array, int i);
+
+ /**
+ * @return JSON object
+ */
+ public abstract Object parse(String jsonStr);
+
+ /**
+ * @return field names in specified JSON object
+ */
+ public abstract Set<String> getFields(Object json);
+
}
*/
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;
public abstract class AbstractUnmarshaller<MODEL extends Model> implements Unmarshaller<MODEL> {
public final MODEL toModel(String json) {
- MODEL result = null;
- JSONArray array = (JSONArray) JSONValue.parse(json);
- if (array.size() >= 1) {
- JSONObject elt = (JSONObject) array.get(0);
+ WSUtils utils = WSUtils.getINSTANCE();
+ MODEL result = null;
+ Object array = utils.parse(json);
+ if (utils.getArraySize(array) >= 1) {
+ Object elt = utils.getArrayElement(array, 0);
if (elt != null) {
result = parse(elt);
}
}
public final List<MODEL> toModels(String json) {
+ WSUtils utils = WSUtils.getINSTANCE();
List<MODEL> result = new ArrayList<MODEL>();
- JSONArray array = (JSONArray) JSONValue.parse(json);
- for (Object anArray : array) {
- JSONObject elt = (JSONObject) anArray;
+ Object array = utils.parse(json);
+ for (int i = 0; i < utils.getArraySize(array); i++) {
+ Object elt = utils.getArrayElement(array, i);
if (elt != null) {
result.add(parse(elt));
}
return result;
}
- protected abstract MODEL parse(JSONObject elt);
+ protected abstract MODEL parse(Object elt);
}
*/
package org.sonar.wsclient.unmarshallers;
-import org.json.simple.JSONArray;
-import org.json.simple.JSONObject;
import org.sonar.wsclient.services.DependencyTree;
+import org.sonar.wsclient.services.WSUtils;
import java.util.ArrayList;
import java.util.List;
public class DependencyTreeUnmarshaller extends AbstractUnmarshaller<DependencyTree> {
@Override
- protected DependencyTree parse(JSONObject json) {
+ protected DependencyTree parse(Object json) {
+ WSUtils utils = WSUtils.getINSTANCE();
DependencyTree tree = new DependencyTree()
- .setDepId(JsonUtils.getString(json, "did"))
- .setResourceId(JsonUtils.getString(json, "rid"))
- .setResourceKey(JsonUtils.getString(json, "k"))
- .setResourceName(JsonUtils.getString(json, "n"))
- .setResourceScope(JsonUtils.getString(json, "s"))
- .setResourceQualifier(JsonUtils.getString(json, "q"))
- .setResourceVersion(JsonUtils.getString(json, "v"))
- .setUsage(JsonUtils.getString(json, "u"))
- .setWeight(JsonUtils.getInteger(json, "w"));
+ .setDepId(utils.getString(json, "did"))
+ .setResourceId(utils.getString(json, "rid"))
+ .setResourceKey(utils.getString(json, "k"))
+ .setResourceName(utils.getString(json, "n"))
+ .setResourceScope(utils.getString(json, "s"))
+ .setResourceQualifier(utils.getString(json, "q"))
+ .setResourceVersion(utils.getString(json, "v"))
+ .setUsage(utils.getString(json, "u"))
+ .setWeight(utils.getInteger(json, "w"));
List<DependencyTree> to = new ArrayList<DependencyTree>();
tree.setTo(to);
- JSONArray toJson = (JSONArray) json.get("to");
+ Object toJson = utils.getField(json, "to");
if (toJson != null) {
- for (Object aToJson : toJson) {
- to.add(parse((JSONObject) aToJson));
+ for (int i = 0; i < utils.getArraySize(toJson); i++) {
+ to.add(parse(utils.getArrayElement(toJson, i)));
}
}
return tree;
*/
package org.sonar.wsclient.unmarshallers;
-import org.json.simple.JSONObject;
import org.sonar.wsclient.services.Dependency;
+import org.sonar.wsclient.services.WSUtils;
public class DependencyUnmarshaller extends AbstractUnmarshaller<Dependency> {
@Override
- protected Dependency parse(JSONObject json) {
+ protected Dependency parse(Object json) {
+ WSUtils utils = WSUtils.getINSTANCE();
return new Dependency()
- .setId(JsonUtils.getString(json, "id"))
- .setFromId(JsonUtils.getLong(json, "fi"))
- .setToId(JsonUtils.getLong(json, "ti"))
- .setFromKey(JsonUtils.getString(json, "fk"))
- .setToKey(JsonUtils.getString(json, "tk"))
- .setUsage(JsonUtils.getString(json, "u"))
- .setWeight(JsonUtils.getInteger(json, "w"))
- .setFromName(JsonUtils.getString(json, "fn"))
- .setFromQualifier(JsonUtils.getString(json, "fq"))
- .setToName(JsonUtils.getString(json, "tn"))
- .setToQualifier(JsonUtils.getString(json, "tq"));
+ .setId(utils.getString(json, "id"))
+ .setFromId(utils.getLong(json, "fi"))
+ .setToId(utils.getLong(json, "ti"))
+ .setFromKey(utils.getString(json, "fk"))
+ .setToKey(utils.getString(json, "tk"))
+ .setUsage(utils.getString(json, "u"))
+ .setWeight(utils.getInteger(json, "w"))
+ .setFromName(utils.getString(json, "fn"))
+ .setFromQualifier(utils.getString(json, "fq"))
+ .setToName(utils.getString(json, "tn"))
+ .setToQualifier(utils.getString(json, "tq"));
}
-}
\ No newline at end of file
+}
*/
package org.sonar.wsclient.unmarshallers;
-import org.json.simple.JSONObject;
import org.sonar.wsclient.services.Event;
+import org.sonar.wsclient.services.WSUtils;
public class EventUnmarshaller extends AbstractUnmarshaller<Event> {
@Override
- protected Event parse(JSONObject json) {
+ protected Event parse(Object json) {
+ WSUtils utils = WSUtils.getINSTANCE();
return new Event()
- .setId(JsonUtils.getString(json, "id"))
- .setResourceKey(JsonUtils.getString(json, "rk"))
- .setName(JsonUtils.getString(json, "n"))
- .setCategory(JsonUtils.getString(json, "c"))
- .setDate(JsonUtils.getDateTime(json, "dt"))
- .setDescription(JsonUtils.getString(json, "ds"))
- .setData(JsonUtils.getString(json, "data"));
+ .setId(utils.getString(json, "id"))
+ .setResourceKey(utils.getString(json, "rk"))
+ .setName(utils.getString(json, "n"))
+ .setCategory(utils.getString(json, "c"))
+ .setDate(utils.getDateTime(json, "dt"))
+ .setDescription(utils.getString(json, "ds"))
+ .setData(utils.getString(json, "data"));
}
}
*/
package org.sonar.wsclient.unmarshallers;
-import org.json.simple.JSONObject;
import org.sonar.wsclient.services.Favourite;
+import org.sonar.wsclient.services.WSUtils;
public class FavouriteUnmarshaller extends AbstractUnmarshaller<Favourite> {
@Override
- protected Favourite parse(JSONObject json) {
+ protected Favourite parse(Object json) {
+ WSUtils utils = WSUtils.getINSTANCE();
return new Favourite()
- .setId(JsonUtils.getInteger(json, "id"))
- .setKey(JsonUtils.getString(json, "key"))
- .setName(JsonUtils.getString(json, "name"))
- .setScope(JsonUtils.getString(json, "scope"))
- .setQualifier(JsonUtils.getString(json, "qualifier"))
- .setLanguage(JsonUtils.getString(json, "lang"));
+ .setId(utils.getInteger(json, "id"))
+ .setKey(utils.getString(json, "key"))
+ .setName(utils.getString(json, "name"))
+ .setScope(utils.getString(json, "scope"))
+ .setQualifier(utils.getString(json, "qualifier"))
+ .setLanguage(utils.getString(json, "lang"));
}
}
*/
package org.sonar.wsclient.unmarshallers;
-import org.json.simple.JSONObject;
import org.sonar.wsclient.services.Metric;
+import org.sonar.wsclient.services.WSUtils;
public class MetricUnmarshaller extends AbstractUnmarshaller<Metric> {
@Override
- protected Metric parse(JSONObject json) {
+ protected Metric parse(Object json) {
+ WSUtils utils = WSUtils.getINSTANCE();
return new Metric()
- .setKey(JsonUtils.getString(json, "key"))
- .setName(JsonUtils.getString(json, "name"))
- .setDomain(JsonUtils.getString(json, "domain"))
- .setDescription(JsonUtils.getString(json, "description"))
- .setDirection(JsonUtils.getInteger(json, "direction"))
- .setType(JsonUtils.getString(json, "val_type"))
- .setUserManaged(JsonUtils.getBoolean(json, "user_managed"))
- .setHidden(JsonUtils.getBoolean(json, "hidden"));
+ .setKey(utils.getString(json, "key"))
+ .setName(utils.getString(json, "name"))
+ .setDomain(utils.getString(json, "domain"))
+ .setDescription(utils.getString(json, "description"))
+ .setDirection(utils.getInteger(json, "direction"))
+ .setType(utils.getString(json, "val_type"))
+ .setUserManaged(utils.getBoolean(json, "user_managed"))
+ .setHidden(utils.getBoolean(json, "hidden"));
}
}
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
public class PluginUnmarshaller extends AbstractUnmarshaller<Plugin> {
@Override
- protected Plugin parse(JSONObject json) {
+ protected Plugin parse(Object json) {
+ WSUtils utils = WSUtils.getINSTANCE();
return new Plugin()
- .setKey(JsonUtils.getString(json, "key"))
- .setName(JsonUtils.getString(json, "name"))
- .setVersion(JsonUtils.getString(json, "version"));
+ .setKey(utils.getString(json, "key"))
+ .setName(utils.getString(json, "name"))
+ .setVersion(utils.getString(json, "version"));
}
}
*/
package org.sonar.wsclient.unmarshallers;
-import org.json.simple.JSONObject;
import org.sonar.wsclient.services.Property;
+import org.sonar.wsclient.services.WSUtils;
public class PropertyUnmarshaller extends AbstractUnmarshaller<Property> {
@Override
- protected Property parse(JSONObject json) {
+ protected Property parse(Object json) {
+ WSUtils utils = WSUtils.getINSTANCE();
return new Property()
- .setKey(JsonUtils.getString(json, "key"))
- .setValue(JsonUtils.getString(json, "value"));
+ .setKey(utils.getString(json, "key"))
+ .setValue(utils.getString(json, "value"));
}
-}
\ No newline at end of file
+}
*/
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;
public class ResourceUnmarshaller extends AbstractUnmarshaller<Resource> {
@Override
- protected Resource parse(JSONObject json) {
+ protected Resource parse(Object json) {
Resource resource = new Resource();
parseResourceFields(json, resource);
parseMeasures(json, resource);
return resource;
}
- private void parseResourceFields(JSONObject json, Resource resource) {
- resource.setId(JsonUtils.getInteger(json, "id"))
- .setKey(JsonUtils.getString(json, "key"))
- .setName(JsonUtils.getString(json, "name"))
- .setLongName(JsonUtils.getString(json, "lname"))
- .setScope(JsonUtils.getString(json, "scope"))
- .setQualifier(JsonUtils.getString(json, "qualifier"))
- .setLanguage(JsonUtils.getString(json, "lang"))
- .setDescription(JsonUtils.getString(json, "description"))
- .setDate(JsonUtils.getDateTime(json, "date"))
- .setVersion(JsonUtils.getString(json, "version"))
- .setPeriod1Mode(JsonUtils.getString(json, "p1"))
- .setPeriod1Param(JsonUtils.getString(json, "p1p"))
- .setPeriod1Date(JsonUtils.getDateTime(json, "p1d"))
- .setPeriod2Mode(JsonUtils.getString(json, "p2"))
- .setPeriod2Param(JsonUtils.getString(json, "p2p"))
- .setPeriod2Date(JsonUtils.getDateTime(json, "p2d"))
- .setPeriod3Mode(JsonUtils.getString(json, "p3"))
- .setPeriod3Param(JsonUtils.getString(json, "p3p"))
- .setPeriod3Date(JsonUtils.getDateTime(json, "p3d"))
- .setPeriod4Mode(JsonUtils.getString(json, "p4"))
- .setPeriod4Param(JsonUtils.getString(json, "p4p"))
- .setPeriod4Date(JsonUtils.getDateTime(json, "p4d"))
- .setPeriod5Mode(JsonUtils.getString(json, "p5"))
- .setPeriod5Param(JsonUtils.getString(json, "p5p"))
- .setPeriod5Date(JsonUtils.getDateTime(json, "p5d"));
+ private void parseResourceFields(Object json, Resource resource) {
+ WSUtils utils = WSUtils.getINSTANCE();
+ resource.setId(utils.getInteger(json, "id"))
+ .setKey(utils.getString(json, "key"))
+ .setName(utils.getString(json, "name"))
+ .setLongName(utils.getString(json, "lname"))
+ .setScope(utils.getString(json, "scope"))
+ .setQualifier(utils.getString(json, "qualifier"))
+ .setLanguage(utils.getString(json, "lang"))
+ .setDescription(utils.getString(json, "description"))
+ .setDate(utils.getDateTime(json, "date"))
+ .setVersion(utils.getString(json, "version"))
+ .setPeriod1Mode(utils.getString(json, "p1"))
+ .setPeriod1Param(utils.getString(json, "p1p"))
+ .setPeriod1Date(utils.getDateTime(json, "p1d"))
+ .setPeriod2Mode(utils.getString(json, "p2"))
+ .setPeriod2Param(utils.getString(json, "p2p"))
+ .setPeriod2Date(utils.getDateTime(json, "p2d"))
+ .setPeriod3Mode(utils.getString(json, "p3"))
+ .setPeriod3Param(utils.getString(json, "p3p"))
+ .setPeriod3Date(utils.getDateTime(json, "p3d"))
+ .setPeriod4Mode(utils.getString(json, "p4"))
+ .setPeriod4Param(utils.getString(json, "p4p"))
+ .setPeriod4Date(utils.getDateTime(json, "p4d"))
+ .setPeriod5Mode(utils.getString(json, "p5"))
+ .setPeriod5Param(utils.getString(json, "p5p"))
+ .setPeriod5Date(utils.getDateTime(json, "p5d"));
}
- private void parseMeasures(JSONObject json, Resource resource) {
- JSONArray measuresJson = JsonUtils.getArray(json, "msr");
+ private void parseMeasures(Object json, Resource resource) {
+ WSUtils utils = WSUtils.getINSTANCE();
+ Object measuresJson = utils.getField(json, "msr");
if (measuresJson != null) {
resource.setMeasures(parseMeasures(measuresJson));
}
}
- private List<Measure> parseMeasures(JSONArray measuresJson) {
+ private List<Measure> parseMeasures(Object measuresJson) {
+ WSUtils utils = WSUtils.getINSTANCE();
List<Measure> projectMeasures = new ArrayList<Measure>();
- int len = measuresJson.size();
+ int len = utils.getArraySize(measuresJson);
for (int i = 0; i < len; i++) {
- JSONObject measureJson = (JSONObject) measuresJson.get(i);
+ Object measureJson = utils.getArrayElement(measuresJson, i);
if (measureJson != null) {
Measure measure = parseMeasure(measureJson);
projectMeasures.add(measure);
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;
}
}
*/
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;
public class RuleUnmarshaller extends AbstractUnmarshaller<Rule> {
@Override
- protected Rule parse(JSONObject json) {
+ protected Rule parse(Object json) {
Rule rule = new Rule();
parseRuleFields(json, rule);
parseParams(json, rule);
return rule;
}
- private void parseRuleFields(JSONObject json, Rule rule) {
- rule.setTitle(JsonUtils.getString(json, "title"))
- .setKey(JsonUtils.getString(json, "key"))
- .setRepository(JsonUtils.getString(json, "plugin"))
- .setDescription(JsonUtils.getString(json, "description"))
- .setSeverity(JsonUtils.getString(json, "priority"))
- .setActive("ACTIVE".equals(JsonUtils.getString(json, "status")));
+ private void parseRuleFields(Object json, Rule rule) {
+ WSUtils utils = WSUtils.getINSTANCE();
+
+ rule.setTitle(utils.getString(json, "title"))
+ .setKey(utils.getString(json, "key"))
+ .setRepository(utils.getString(json, "plugin"))
+ .setDescription(utils.getString(json, "description"))
+ .setSeverity(utils.getString(json, "priority"))
+ .setActive("ACTIVE".equals(utils.getString(json, "status")));
}
- private void parseParams(JSONObject json, Rule rule) {
- JSONArray paramsJson = (JSONArray) json.get("params");
+ private void parseParams(Object json, Rule rule) {
+ WSUtils utils = WSUtils.getINSTANCE();
+
+ Object paramsJson = utils.getField(json, "params");
if (paramsJson != null) {
rule.setParams(parseParams(paramsJson));
}
}
- private List<RuleParam> parseParams(JSONArray paramsJson) {
+ private List<RuleParam> parseParams(Object paramsJson) {
+ WSUtils utils = WSUtils.getINSTANCE();
+
List<RuleParam> ruleParams = new ArrayList<RuleParam>();
- int len = paramsJson.size();
+ int len = utils.getArraySize(paramsJson);
for (int i = 0; i < len; i++) {
- JSONObject paramJson = (JSONObject) paramsJson.get(i);
+ Object paramJson = utils.getArrayElement(paramsJson, i);
if (paramJson != null) {
RuleParam param = parseParam(paramJson);
ruleParams.add(param);
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;
}
*/
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;
*/
public class ServerUnmarshaller implements Unmarshaller<Server> {
public Server toModel(String json) {
- JSONObject map = (JSONObject) JSONValue.parse(json);
+ WSUtils utils = WSUtils.getINSTANCE();
+ Object map = utils.parse(json);
Server server = new Server()
- .setId(JsonUtils.getString(map, "id"))
- .setVersion(JsonUtils.getString(map, "version"))
- .setStatusMessage(JsonUtils.getString(map, "status_msg"));
- String status = JsonUtils.getString(map, "status");
+ .setId(utils.getString(map, "id"))
+ .setVersion(utils.getString(map, "version"))
+ .setStatusMessage(utils.getString(map, "status_msg"));
+ String status = utils.getString(map, "status");
if (status != null) {
server.setStatus(Server.Status.valueOf(status));
}
*/
package org.sonar.wsclient.unmarshallers;
-import org.json.simple.JSONObject;
import org.sonar.wsclient.services.Source;
-
-import java.util.Map;
+import org.sonar.wsclient.services.WSUtils;
public class SourceUnmarshaller extends AbstractUnmarshaller<Source> {
@Override
- protected Source parse(JSONObject json) {
+ protected Source parse(Object json) {
+ WSUtils utils = WSUtils.getINSTANCE();
Source source = new Source();
- for (Object o : json.entrySet()) {
- Map.Entry<String, String> entry = (Map.Entry<String, String>) o;
- source.addLine(Integer.parseInt(entry.getKey()), entry.getValue());
+ for (String field : utils.getFields(json)) {
+ source.addLine(Integer.parseInt(field), utils.getString(json, field));
}
return source;
*/
package org.sonar.wsclient.unmarshallers;
-import org.json.simple.JSONArray;
-import org.json.simple.JSONObject;
import org.sonar.wsclient.services.TimeMachine;
import org.sonar.wsclient.services.TimeMachineCell;
import org.sonar.wsclient.services.TimeMachineColumn;
+import org.sonar.wsclient.services.WSUtils;
public class TimeMachineUnmarshaller extends AbstractUnmarshaller<TimeMachine> {
- protected TimeMachine parse(JSONObject json) {
- JSONArray cols = JsonUtils.getArray(json, "cols");
- JSONArray cells = JsonUtils.getArray(json, "cells");
+ protected TimeMachine parse(Object json) {
+ WSUtils utils = WSUtils.getINSTANCE();
+ Object cols = utils.getField(json, "cols");
+ Object cells = utils.getField(json, "cells");
return new TimeMachine(toColumns(cols), toCells(cells));
}
- private TimeMachineColumn[] toColumns(JSONArray cols) {
- int size = cols.size();
+ private TimeMachineColumn[] toColumns(Object cols) {
+ WSUtils utils = WSUtils.getINSTANCE();
+ int size = utils.getArraySize(cols);
TimeMachineColumn[] result = new TimeMachineColumn[size];
for (int index = 0; index < size; index++) {
- JSONObject colJson = (JSONObject) cols.get(index);
- result[index] = new TimeMachineColumn(index, JsonUtils.getString(colJson, "metric"), null, null);
+ Object colJson = utils.getArrayElement(cols, index);
+ result[index] = new TimeMachineColumn(index, utils.getString(colJson, "metric"), null, null);
}
return result;
}
- private TimeMachineCell[] toCells(JSONArray cells) {
- int size = cells.size();
+ private TimeMachineCell[] toCells(Object cells) {
+ WSUtils utils = WSUtils.getINSTANCE();
+ int size = utils.getArraySize(cells);
TimeMachineCell[] result = new TimeMachineCell[size];
for (int i = 0; i < size; i++) {
- JSONObject cellJson = (JSONObject) cells.get(i);
- JSONArray valuesJson = JsonUtils.getArray(cellJson, "v");
+ Object cellJson = utils.getArrayElement(cells, i);
+ Object valuesJson = utils.getField(cellJson, "v");
- Object[] resultValues = new Object[valuesJson.size()];
- for (int indexValue = 0; indexValue < valuesJson.size(); indexValue++) {
- Object value = valuesJson.get(indexValue);
+ Object[] resultValues = new Object[utils.getArraySize(valuesJson)];
+ for (int indexValue = 0; indexValue < utils.getArraySize(valuesJson); indexValue++) {
+ Object value = utils.getArrayElement(valuesJson, indexValue);
resultValues[indexValue] = value;
}
- result[i] = new TimeMachineCell(JsonUtils.getDateTime(cellJson, "d"), resultValues);
+ result[i] = new TimeMachineCell(utils.getDateTime(cellJson, "d"), resultValues);
}
return result;
}
*/
package org.sonar.wsclient.unmarshallers;
-import org.json.simple.JSONObject;
import org.sonar.wsclient.services.Violation;
+import org.sonar.wsclient.services.WSUtils;
public class ViolationUnmarshaller extends AbstractUnmarshaller<Violation> {
@Override
- protected Violation parse(JSONObject json) {
+ protected Violation parse(Object json) {
+ WSUtils utils = WSUtils.getINSTANCE();
+
Violation violation = new Violation();
- violation.setMessage(JsonUtils.getString(json, "message"));
- violation.setLine(JsonUtils.getInteger(json, "line"));
- violation.setSeverity(JsonUtils.getString(json, "priority"));
- violation.setCreatedAt(JsonUtils.getDateTime(json, "createdAt"));
+ violation.setMessage(utils.getString(json, "message"));
+ violation.setLine(utils.getInteger(json, "line"));
+ violation.setSeverity(utils.getString(json, "priority"));
+ violation.setCreatedAt(utils.getDateTime(json, "createdAt"));
- JSONObject rule = (JSONObject) json.get("rule");
+ Object rule = utils.getField(json, "rule");
if (rule != null) {
- violation.setRuleKey(JsonUtils.getString(rule, "key"));
- violation.setRuleName(JsonUtils.getString(rule, "name"));
+ violation.setRuleKey(utils.getString(rule, "key"));
+ violation.setRuleName(utils.getString(rule, "name"));
}
- JSONObject resource = (JSONObject) json.get("resource");
+ Object resource = utils.getField(json, "resource");
if (resource != null) {
- violation.setResourceKey(JsonUtils.getString(resource, "key"));
- violation.setResourceName(JsonUtils.getString(resource, "name"));
- violation.setResourceQualifier(JsonUtils.getString(resource, "qualifier"));
- violation.setResourceScope(JsonUtils.getString(resource, "scope"));
+ violation.setResourceKey(utils.getString(resource, "key"));
+ violation.setResourceName(utils.getString(resource, "name"));
+ violation.setResourceQualifier(utils.getString(resource, "qualifier"));
+ violation.setResourceScope(utils.getString(resource, "scope"));
}
return violation;
}
import static org.hamcrest.core.Is.is;
import static org.junit.Assert.assertThat;
-public class DependencyTreeUnmarshallerTest {
+public class DependencyTreeUnmarshallerTest extends UnmarshallerTestCase {
@Test
public void singleDepthOfDependencies() {
List<DependencyTree> trees = new DependencyTreeUnmarshaller().toModels("[]");
assertThat(trees.size(), is(0));
- trees = new DependencyTreeUnmarshaller().toModels(WSTestUtils.loadFile("/dependency_tree/single_depth.json"));
+ trees = new DependencyTreeUnmarshaller().toModels(loadFile("/dependency_tree/single_depth.json"));
assertThat(trees.size(), is(2));
assertThat(trees.get(0).getDepId(), is("12345"));
assertThat(trees.get(0).getResourceId(), is("2000"));
@Test
public void manyDepthsOfDependencies() {
- List<DependencyTree> trees = new DependencyTreeUnmarshaller().toModels(WSTestUtils.loadFile("/dependency_tree/many_depths.json"));
+ List<DependencyTree> trees = new DependencyTreeUnmarshaller().toModels(loadFile("/dependency_tree/many_depths.json"));
assertThat(trees.size(), is(1));
List<DependencyTree> secondLevelTrees = trees.get(0).getTo();
assertThat(secondLevelTrees.size(), is(2));
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"));
Collection<Dependency> dependencies = new DependencyUnmarshaller().toModels("[]");
assertThat(dependencies.size(), is(0));
- dependencies = new DependencyUnmarshaller().toModels(WSTestUtils.loadFile("/dependencies/many.json"));
+ dependencies = new DependencyUnmarshaller().toModels(loadFile("/dependencies/many.json"));
assertThat(dependencies.size(), is(15));
}
import static org.hamcrest.core.Is.is;
import static org.junit.Assert.assertThat;
-public class EventUnmarshallerTest {
+public class EventUnmarshallerTest extends UnmarshallerTestCase {
@Test
public void toModel() throws Exception {
- List<Event> events = new EventUnmarshaller().toModels(WSTestUtils.loadFile("/events/events.json"));
+ List<Event> events = new EventUnmarshaller().toModels(loadFile("/events/events.json"));
Event event = events.get(0);
assertThat(event.getId(), is("10"));
assertThat(event.getName(), is("foo"));
import static org.hamcrest.CoreMatchers.nullValue;
import static org.junit.Assert.assertThat;
-public class JsonUtilsTest {
+public class JsonUtilsTest extends UnmarshallerTestCase {
@Test
public void getIntFields() {
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"));
Collection<Metric> metrics = new MetricUnmarshaller().toModels("[]");
assertThat(metrics.size(), is(0));
- metrics = new MetricUnmarshaller().toModels(WSTestUtils.loadFile("/metrics/one_metric.json"));
+ metrics = new MetricUnmarshaller().toModels(loadFile("/metrics/one_metric.json"));
assertThat(metrics.size(), is(1));
- metrics = new MetricUnmarshaller().toModels(WSTestUtils.loadFile("/metrics/many_metrics.json"));
+ metrics = new MetricUnmarshaller().toModels(loadFile("/metrics/many_metrics.json"));
assertThat(metrics.size(), is(10));
}
package org.sonar.wsclient.unmarshallers;
import org.junit.Test;
+import org.sonar.wsclient.JdkUtils;
import org.sonar.wsclient.services.Plugin;
+import org.sonar.wsclient.services.WSUtils;
import java.util.List;
import static org.hamcrest.core.Is.is;
import static org.junit.Assert.assertThat;
-public class PluginUnmarshallerTest {
+public class PluginUnmarshallerTest extends UnmarshallerTestCase {
@Test
public void toModel() throws Exception {
- List<Plugin> plugins = new PluginUnmarshaller().toModels(WSTestUtils.loadFile("/plugins/plugins.json"));
+ WSUtils.setInstance(new JdkUtils());
+
+ List<Plugin> plugins = new PluginUnmarshaller().toModels(loadFile("/plugins/plugins.json"));
Plugin plugin = plugins.get(0);
assertThat(plugin.getKey(), is("foo"));
assertThat(plugin.getName(), is("Foo"));
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"));
}
Collection<Property> properties = new PropertyUnmarshaller().toModels("[]");
assertThat(properties.size(), is(0));
- properties = new PropertyUnmarshaller().toModels(WSTestUtils.loadFile("/properties/single.json"));
+ properties = new PropertyUnmarshaller().toModels(loadFile("/properties/single.json"));
assertThat(properties.size(), is(1));
- properties = new PropertyUnmarshaller().toModels(WSTestUtils.loadFile("/properties/many.json"));
+ properties = new PropertyUnmarshaller().toModels(loadFile("/properties/many.json"));
assertThat(properties.size(), is(3));
}
}
import static org.hamcrest.core.IsNot.not;
import static org.junit.Assert.assertThat;
-public class ResourceUnmarshallerTest {
+public class ResourceUnmarshallerTest extends UnmarshallerTestCase {
@Test
public void singleResource() {
- String json = WSTestUtils.loadFile("/resources/single-resource.json");
+ String json = loadFile("/resources/single-resource.json");
assertSonar(new ResourceUnmarshaller().toModel(json));
List<Resource> resources = new ResourceUnmarshaller().toModels(json);
@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));
@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));
@Test
public void manyResources() {
- List<Resource> resources = new ResourceUnmarshaller().toModels(WSTestUtils.loadFile("/resources/many-resources.json"));
+ List<Resource> resources = new ResourceUnmarshaller().toModels(loadFile("/resources/many-resources.json"));
assertThat(resources.size(), is(19));
for (Resource resource : resources) {
@Test
public void manyResourcesWithMeasures() {
- List<Resource> resources = new ResourceUnmarshaller().toModels(WSTestUtils.loadFile("/resources/many-resources-with-measures.json"));
+ List<Resource> resources = new ResourceUnmarshaller().toModels(loadFile("/resources/many-resources-with-measures.json"));
assertThat(resources.size(), is(17));
for (Resource resource : resources) {
import static org.hamcrest.Matchers.nullValue;
import static org.junit.Assert.assertThat;
-public class RuleUnmarshallerTest {
+public class RuleUnmarshallerTest extends UnmarshallerTestCase {
@Test
public void toModels() {
List<Rule> rules = new RuleUnmarshaller().toModels("[]");
assertThat(rules.size(), is(0));
- rules = new RuleUnmarshaller().toModels(WSTestUtils.loadFile("/rules/rules.json"));
+ rules = new RuleUnmarshaller().toModels(loadFile("/rules/rules.json"));
assertThat(rules.size(), is(6));
rule = rules.get(0);
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));
@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());
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"));
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));
@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());
@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"));
--- /dev/null
+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);
+ }
+ }
+
+}
import static org.hamcrest.core.IsNot.not;
import static org.junit.Assert.assertThat;
-public class ViolationUnmarshallerTest {
+public class ViolationUnmarshallerTest extends UnmarshallerTestCase {
@Test
public void toModels() {
Violation violation = new ViolationUnmarshaller().toModel("[]");
assertThat(violation, nullValue());
- List<Violation> violations = new ViolationUnmarshaller().toModels(WSTestUtils.loadFile("/violations/violations.json"));
+ List<Violation> violations = new ViolationUnmarshaller().toModels(loadFile("/violations/violations.json"));
assertThat(violations.size(), is(2));
violation = violations.get(0);
@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());
+++ /dev/null
-/*
- * 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() {
- }
-
-}