diff options
author | Simon Brandhof <simon.brandhof@gmail.com> | 2013-01-25 18:00:45 +0100 |
---|---|---|
committer | Simon Brandhof <simon.brandhof@gmail.com> | 2013-01-25 18:18:45 +0100 |
commit | f6bcd1b80a758e9dd5e52af1ec6dd24c5ae45a31 (patch) | |
tree | a0daefb653b498b0f0631013899c7902c332896d /sonar-core | |
parent | e2390331215b0882c758ba218e50df764a00a6a6 (diff) | |
download | sonarqube-f6bcd1b80a758e9dd5e52af1ec6dd24c5ae45a31.tar.gz sonarqube-f6bcd1b80a758e9dd5e52af1ec6dd24c5ae45a31.zip |
SONAR-4087 fix compatibility of Tinkerpop Blueprints with Java 5
Diffstat (limited to 'sonar-core')
15 files changed, 606 insertions, 471 deletions
diff --git a/sonar-core/src/main/java/org/sonar/core/component/GraphReader.java b/sonar-core/src/main/java/org/sonar/core/component/GraphReader.java index 5a7722e9db0..965d3316b0f 100644 --- a/sonar-core/src/main/java/org/sonar/core/component/GraphReader.java +++ b/sonar-core/src/main/java/org/sonar/core/component/GraphReader.java @@ -21,11 +21,9 @@ package org.sonar.core.component; import com.tinkerpop.blueprints.Vertex; import com.tinkerpop.blueprints.impls.tg.TinkerGraph; -import org.json.simple.parser.ParseException; -import org.sonar.core.graph.GraphSONReader; +import com.tinkerpop.blueprints.util.io.graphson.GraphSONReader; import java.io.ByteArrayInputStream; -import java.io.IOException; public class GraphReader { @@ -36,9 +34,7 @@ public class GraphReader { GraphSONReader.inputGraph(graph, input); Vertex root = graph.getVertex(rootVertexId); return new ComponentGraph(graph, root); - } catch (IOException e) { - throw new IllegalStateException(e); - } catch (ParseException e) { + } catch (Exception e) { throw new IllegalStateException(e); } } diff --git a/sonar-core/src/main/java/org/sonar/core/graph/GraphSONReader.java b/sonar-core/src/main/java/org/sonar/core/graph/GraphSONReader.java deleted file mode 100644 index 6216f00aa5f..00000000000 --- a/sonar-core/src/main/java/org/sonar/core/graph/GraphSONReader.java +++ /dev/null @@ -1,135 +0,0 @@ -/* - * Sonar, open source software quality management tool. - * Copyright (C) 2008-2012 SonarSource - * 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.core.graph; - -import com.tinkerpop.blueprints.Graph; -import com.tinkerpop.blueprints.Vertex; -import com.tinkerpop.blueprints.util.io.graphson.ElementFactory; -import com.tinkerpop.blueprints.util.io.graphson.GraphElementFactory; -import com.tinkerpop.blueprints.util.io.graphson.GraphSONMode; -import com.tinkerpop.blueprints.util.io.graphson.GraphSONTokens; -import com.tinkerpop.blueprints.util.wrappers.batch.BatchGraph; -import org.json.simple.JSONArray; -import org.json.simple.JSONObject; -import org.json.simple.parser.JSONParser; -import org.json.simple.parser.ParseException; - -import java.io.IOException; -import java.io.InputStream; -import java.io.InputStreamReader; -import java.util.Set; - -/** - * GraphSONReader reads the data from a TinkerPop JSON stream to a graph. - * - * @author Stephen Mallette (http://stephen.genoprime.com) - */ -public class GraphSONReader { - - private final Graph graph; - - /** - * @param graph the graph to populate with the JSON data - */ - public GraphSONReader(final Graph graph) { - this.graph = graph; - } - - /** - * Input the JSON stream data into the graph. - * In practice, usually the provided graph is empty. - * - * @param graph the graph to populate with the JSON data - * @param jsonInputStream an InputStream of JSON data - * @throws java.io.IOException thrown when the JSON data is not correctly formatted - */ - public static void inputGraph(final Graph graph, final InputStream jsonInputStream) throws IOException, ParseException { - inputGraph(graph, jsonInputStream, 1000); - } - - public static void inputGraph(final Graph inputGraph, final InputStream jsonInputStream, int bufferSize) throws IOException, ParseException { - inputGraph(inputGraph, jsonInputStream, bufferSize, null, null); - } - - /** - * Input the JSON stream data into the graph. - * More control over how data is streamed is provided by this method. - * - * @param inputGraph the graph to populate with the JSON data - * @param jsonInputStream an InputStream of JSON data - * @param bufferSize the amount of elements to hold in memory before committing a transactions (only valid for TransactionalGraphs) - * @throws java.io.IOException thrown when the JSON data is not correctly formatted - */ - public static void inputGraph(final Graph inputGraph, final InputStream jsonInputStream, int bufferSize, - final Set<String> edgePropertyKeys, final Set<String> vertexPropertyKeys) throws ParseException, IOException { - - JSONParser parser = new JSONParser(); - JSONObject json = (JSONObject) parser.parse(new InputStreamReader(jsonInputStream)); - - // if this is a transactional graph then we're buffering - final BatchGraph graph = BatchGraph.wrap(inputGraph, bufferSize); - - final ElementFactory elementFactory = new GraphElementFactory(graph); - - final GraphSONMode mode = GraphSONMode.valueOf(json.get(GraphSONTokens.MODE).toString()); - GraphSONUtility graphson = new GraphSONUtility(mode, elementFactory, vertexPropertyKeys, edgePropertyKeys); - - JSONArray vertices = (JSONArray) json.get(GraphSONTokens.VERTICES); - for (Object vertice : vertices) { - graphson.vertexFromJson((JSONObject) vertice); - } - - JSONArray edges = (JSONArray) json.get(GraphSONTokens.EDGES); - for (Object edgeObject : edges) { - JSONObject edge = (JSONObject) edgeObject; - - final Vertex inV = graph.getVertex(edge.get(GraphSONTokens._IN_V)); - final Vertex outV = graph.getVertex(edge.get(GraphSONTokens._OUT_V)); - graphson.edgeFromJson(edge, outV, inV); - } - graph.shutdown(); - } - - /** - * Input the JSON stream data into the graph. - * In practice, usually the provided graph is empty. - * - * @param jsonInputStream an InputStream of JSON data - * @throws java.io.IOException thrown when the JSON data is not correctly formatted - */ - public void inputGraph(final InputStream jsonInputStream) throws IOException, ParseException { - GraphSONReader.inputGraph(this.graph, jsonInputStream, 1000); - } - - /** - * Input the JSON stream data into the graph. - * In practice, usually the provided graph is empty. - * - * @param jsonInputStream an InputStream of JSON data - * @param bufferSize the amount of elements to hold in memory before committing a transactions (only valid for TransactionalGraphs) - * @throws java.io.IOException thrown when the JSON data is not correctly formatted - */ - public void inputGraph(final InputStream jsonInputStream, int bufferSize) throws IOException, ParseException { - GraphSONReader.inputGraph(this.graph, jsonInputStream, bufferSize); - } - - -} diff --git a/sonar-core/src/main/java/org/sonar/core/graph/GraphSONWriter.java b/sonar-core/src/main/java/org/sonar/core/graph/GraphSONWriter.java deleted file mode 100644 index 6455a34e065..00000000000 --- a/sonar-core/src/main/java/org/sonar/core/graph/GraphSONWriter.java +++ /dev/null @@ -1,129 +0,0 @@ -/* - * Sonar, open source software quality management tool. - * Copyright (C) 2008-2012 SonarSource - * 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.core.graph; - - -import com.tinkerpop.blueprints.Edge; -import com.tinkerpop.blueprints.Graph; -import com.tinkerpop.blueprints.Vertex; -import com.tinkerpop.blueprints.util.io.graphson.GraphSONMode; -import com.tinkerpop.blueprints.util.io.graphson.GraphSONTokens; -import org.json.simple.JSONArray; -import org.json.simple.JSONObject; - -import java.io.IOException; -import java.io.OutputStream; -import java.util.Set; - -/** - * GraphSONWriter writes a Graph to a TinkerPop JSON OutputStream. - * - * @author Stephen Mallette - */ -public class GraphSONWriter { - - private final Graph graph; - - /** - * @param graph the Graph to pull the data from - */ - public GraphSONWriter(final Graph graph) { - this.graph = graph; - } - - /** - * Write the data in a Graph to a JSON OutputStream. All keys are written to JSON. Utilizing - * GraphSONMode.NORMAL. - * - * @param graph the graph to serialize to JSON - * @param jsonOutputStream the JSON OutputStream to write the Graph data to - * @throws java.io.IOException thrown if there is an error generating the JSON data - */ - public static void outputGraph(final Graph graph, final OutputStream jsonOutputStream) throws IOException { - final GraphSONWriter writer = new GraphSONWriter(graph); - writer.outputGraph(jsonOutputStream, null, null, GraphSONMode.NORMAL); - } - - /** - * Write the data in a Graph to a JSON OutputStream. All keys are written to JSON. - * - * @param graph the graph to serialize to JSON - * @param jsonOutputStream the JSON OutputStream to write the Graph data to - * @param mode determines the format of the GraphSON - * @throws java.io.IOException thrown if there is an error generating the JSON data - */ - public static void outputGraph(final Graph graph, final OutputStream jsonOutputStream, - final GraphSONMode mode) throws IOException { - final GraphSONWriter writer = new GraphSONWriter(graph); - writer.outputGraph(jsonOutputStream, null, null, mode); - } - - /** - * Write the data in a Graph to a JSON OutputStream. - * - * @param graph the graph to serialize to JSON - * @param jsonOutputStream the JSON OutputStream to write the Graph data to - * @param vertexPropertyKeys the keys of the vertex elements to write to JSON - * @param edgePropertyKeys the keys of the edge elements to write to JSON - * @param mode determines the format of the GraphSON - * @throws java.io.IOException thrown if there is an error generating the JSON data - */ - public static void outputGraph(final Graph graph, final OutputStream jsonOutputStream, - final Set<String> vertexPropertyKeys, final Set<String> edgePropertyKeys, - final GraphSONMode mode) throws IOException { - final GraphSONWriter writer = new GraphSONWriter(graph); - writer.outputGraph(jsonOutputStream, vertexPropertyKeys, edgePropertyKeys, mode); - } - - /** - * Write the data in a Graph to a JSON OutputStream. - * - * @param jsonOutputStream the JSON OutputStream to write the Graph data to - * @param vertexPropertyKeys the keys of the vertex elements to write to JSON - * @param edgePropertyKeys the keys of the edge elements to write to JSON - * @param mode determines the format of the GraphSON - * @throws java.io.IOException thrown if there is an error generating the JSON data - */ - public void outputGraph(final OutputStream jsonOutputStream, final Set<String> vertexPropertyKeys, - final Set<String> edgePropertyKeys, final GraphSONMode mode) throws IOException { - - JSONObject root = new JSONObject(); - - final GraphSONUtility graphson = new GraphSONUtility(mode, null, vertexPropertyKeys, edgePropertyKeys); - - root.put(GraphSONTokens.MODE, mode.toString()); - - JSONArray verticesArray = new JSONArray(); - for (Vertex v : this.graph.getVertices()) { - verticesArray.add(graphson.objectNodeFromElement(v)); - } - root.put(GraphSONTokens.VERTICES, verticesArray); - - JSONArray edgesArray = new JSONArray(); - for (Edge e : this.graph.getEdges()) { - edgesArray.add(graphson.objectNodeFromElement(e)); - } - root.put(GraphSONTokens.EDGES, edgesArray); - - jsonOutputStream.write(root.toString().getBytes()); - } - -} diff --git a/sonar-core/src/main/java/org/sonar/core/graph/GraphWriter.java b/sonar-core/src/main/java/org/sonar/core/graph/GraphWriter.java index ac08fc7d516..ac8b01a8efc 100644 --- a/sonar-core/src/main/java/org/sonar/core/graph/GraphWriter.java +++ b/sonar-core/src/main/java/org/sonar/core/graph/GraphWriter.java @@ -20,8 +20,9 @@ package org.sonar.core.graph; import com.tinkerpop.blueprints.Graph; -import com.tinkerpop.blueprints.util.io.graphson.GraphSONMode; import org.apache.commons.io.IOUtils; +import org.sonar.core.graph.graphson.GraphSONMode; +import org.sonar.core.graph.graphson.GraphSONWriter; import java.io.ByteArrayOutputStream; import java.io.IOException; @@ -31,7 +32,7 @@ public class GraphWriter { public String write(Graph graph) { ByteArrayOutputStream output = new ByteArrayOutputStream(); try { - GraphSONWriter.outputGraph(graph, output, GraphSONMode.COMPACT); + new GraphSONWriter().write(graph, output, GraphSONMode.COMPACT); output.flush(); output.close(); return new String(output.toByteArray()); diff --git a/sonar-core/src/main/java/org/sonar/core/graph/graphson/ElementFactory.java b/sonar-core/src/main/java/org/sonar/core/graph/graphson/ElementFactory.java new file mode 100644 index 00000000000..e7458c2ef66 --- /dev/null +++ b/sonar-core/src/main/java/org/sonar/core/graph/graphson/ElementFactory.java @@ -0,0 +1,47 @@ +/* + * Sonar, open source software quality management tool. + * Copyright (C) 2008-2012 SonarSource + * 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.core.graph.graphson; + +import com.tinkerpop.blueprints.Edge; +import com.tinkerpop.blueprints.Graph; +import com.tinkerpop.blueprints.Vertex; + +/** + * The standard factory used for most graph element creation. It uses an actual + * Graph implementation to construct vertices and edges + * + * @author Stephen Mallette (http://stephen.genoprime.com) + */ +class ElementFactory { + + private final Graph graph; + + ElementFactory(Graph g) { + this.graph = g; + } + + Edge createEdge(Object id, Vertex out, Vertex in, String label) { + return this.graph.addEdge(id, out, in, label); + } + + Vertex createVertex(Object id) { + return this.graph.addVertex(id); + } +} diff --git a/sonar-core/src/main/java/org/sonar/core/graph/graphson/ElementPropertyConfig.java b/sonar-core/src/main/java/org/sonar/core/graph/graphson/ElementPropertyConfig.java new file mode 100644 index 00000000000..a82265a9362 --- /dev/null +++ b/sonar-core/src/main/java/org/sonar/core/graph/graphson/ElementPropertyConfig.java @@ -0,0 +1,87 @@ +/* + * Sonar, open source software quality management tool. + * Copyright (C) 2008-2012 SonarSource + * 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.core.graph.graphson; + +import java.util.Set; + +/** + * Configure how the GraphSON utility treats edge and vertex properties. + * + * @author Stephen Mallette (http://stephen.genoprime.com) + */ +class ElementPropertyConfig { + + static enum ElementPropertiesRule { + INCLUDE, EXCLUDE + } + + private final Set<String> vertexPropertyKeys; + private final Set<String> edgePropertyKeys; + private final ElementPropertiesRule vertexPropertiesRule; + private final ElementPropertiesRule edgePropertiesRule; + + /** + * A configuration that includes all properties of vertices and edges. + */ + static ElementPropertyConfig AllProperties = new ElementPropertyConfig(null, null, + ElementPropertiesRule.INCLUDE, ElementPropertiesRule.INCLUDE); + + ElementPropertyConfig(Set<String> vertexPropertyKeys, Set<String> edgePropertyKeys, + ElementPropertiesRule vertexPropertiesRule, ElementPropertiesRule edgePropertiesRule) { + this.vertexPropertiesRule = vertexPropertiesRule; + this.vertexPropertyKeys = vertexPropertyKeys; + this.edgePropertiesRule = edgePropertiesRule; + this.edgePropertyKeys = edgePropertyKeys; + } + + /** + * Construct a configuration that includes the specified properties from both vertices and edges. + */ + static ElementPropertyConfig includeProperties(Set<String> vertexPropertyKeys, + Set<String> edgePropertyKeys) { + return new ElementPropertyConfig(vertexPropertyKeys, edgePropertyKeys, ElementPropertiesRule.INCLUDE, + ElementPropertiesRule.INCLUDE); + } + + /** + * Construct a configuration that excludes the specified properties from both vertices and edges. + */ + static ElementPropertyConfig excludeProperties(Set<String> vertexPropertyKeys, + Set<String> edgePropertyKeys) { + return new ElementPropertyConfig(vertexPropertyKeys, edgePropertyKeys, ElementPropertiesRule.EXCLUDE, + ElementPropertiesRule.EXCLUDE); + } + + Set<String> getVertexPropertyKeys() { + return vertexPropertyKeys; + } + + Set<String> getEdgePropertyKeys() { + return edgePropertyKeys; + } + + ElementPropertiesRule getVertexPropertiesRule() { + return vertexPropertiesRule; + } + + ElementPropertiesRule getEdgePropertiesRule() { + return edgePropertiesRule; + } +} diff --git a/sonar-core/src/main/java/org/sonar/core/graph/graphson/GraphSONMode.java b/sonar-core/src/main/java/org/sonar/core/graph/graphson/GraphSONMode.java new file mode 100644 index 00000000000..3fde6287c08 --- /dev/null +++ b/sonar-core/src/main/java/org/sonar/core/graph/graphson/GraphSONMode.java @@ -0,0 +1,45 @@ +/* + * Sonar, open source software quality management tool. + * Copyright (C) 2008-2012 SonarSource + * 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.core.graph.graphson; + +/** + * Modes of operation of the GraphSONUtility. + * + * @author Stephen Mallette + */ +public enum GraphSONMode { + /** + * COMPACT constructs GraphSON on the assumption that all property keys + * are fair game for exclusion including _type, _inV, _outV, _label and _id. + * It is possible to write GraphSON that cannot be read back into Graph, + * if some or all of these keys are excluded. + */ + COMPACT, + + /** + * NORMAL includes the _type field and JSON data typing. + */ + NORMAL, + + /** + * EXTENDED includes the _type field and explicit data typing. + */ + EXTENDED +} diff --git a/sonar-core/src/main/java/org/sonar/core/graph/graphson/GraphSONReader.java b/sonar-core/src/main/java/org/sonar/core/graph/graphson/GraphSONReader.java new file mode 100644 index 00000000000..1b3aee4d009 --- /dev/null +++ b/sonar-core/src/main/java/org/sonar/core/graph/graphson/GraphSONReader.java @@ -0,0 +1,81 @@ +/* + * Sonar, open source software quality management tool. + * Copyright (C) 2008-2012 SonarSource + * 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.core.graph.graphson; + +import com.tinkerpop.blueprints.Graph; +import com.tinkerpop.blueprints.Vertex; +import com.tinkerpop.blueprints.util.wrappers.batch.BatchGraph; +import org.json.simple.JSONArray; +import org.json.simple.JSONObject; +import org.json.simple.parser.JSONParser; + +import java.io.InputStream; +import java.io.InputStreamReader; +import java.util.Set; + +/** + * Greatly inspired by the Blueprints implementation based on Jettison/Jackson + */ +public class GraphSONReader { + + public Graph read(InputStream jsonInput, Graph toGraph) { + return read(jsonInput, toGraph, 1000, null, null); + } + + /** + * Input the JSON stream data into the graph. + * More control over how data is streamed is provided by this method. + * + * @param toGraph the graph to populate with the JSON data + * @param jsonInput an InputStream of JSON data + * @param bufferSize the amount of elements to hold in memory before committing a transactions (only valid for TransactionalGraphs) + */ + public Graph read(InputStream jsonInput, Graph toGraph, int bufferSize, Set<String> edgePropertyKeys, Set<String> vertexPropertyKeys) { + try { + JSONParser parser = new JSONParser(); + JSONObject json = (JSONObject) parser.parse(new InputStreamReader(jsonInput)); + + // if this is a transactional graph then we're buffering + final BatchGraph batchGraph = BatchGraph.wrap(toGraph, bufferSize); + + ElementFactory elementFactory = new ElementFactory(batchGraph); + + final GraphSONMode mode = GraphSONMode.valueOf(json.get(GraphSONTokens.MODE).toString()); + GraphsonUtil graphson = new GraphsonUtil(mode, elementFactory, vertexPropertyKeys, edgePropertyKeys); + + JSONArray vertices = (JSONArray) json.get(GraphSONTokens.VERTICES); + for (Object vertice : vertices) { + graphson.vertexFromJson((JSONObject) vertice); + } + + JSONArray edges = (JSONArray) json.get(GraphSONTokens.EDGES); + for (Object edgeObject : edges) { + JSONObject edge = (JSONObject) edgeObject; + final Vertex inV = batchGraph.getVertex(edge.get(GraphSONTokens._IN_V)); + final Vertex outV = batchGraph.getVertex(edge.get(GraphSONTokens._OUT_V)); + graphson.edgeFromJson(edge, outV, inV); + } + batchGraph.shutdown(); + return toGraph; + } catch (Exception e) { + throw new GraphSonException("Unable to parse GraphSON", e); + } + } +} diff --git a/sonar-core/src/main/java/org/sonar/core/graph/graphson/GraphSONTokens.java b/sonar-core/src/main/java/org/sonar/core/graph/graphson/GraphSONTokens.java new file mode 100644 index 00000000000..b67e713ca64 --- /dev/null +++ b/sonar-core/src/main/java/org/sonar/core/graph/graphson/GraphSONTokens.java @@ -0,0 +1,49 @@ +/* + * Sonar, open source software quality management tool. + * Copyright (C) 2008-2012 SonarSource + * 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.core.graph.graphson; + +/** + * @author Marko A. Rodriguez (http://markorodriguez.com) + * @author Stephen Mallette + */ +class GraphSONTokens { + public static final String VERTEX = "vertex"; + public static final String EDGE = "edge"; + public static final String _ID = "_id"; + public static final String _LABEL = "_label"; + public static final String _TYPE = "_type"; + public static final String _OUT_V = "_outV"; + public static final String _IN_V = "_inV"; + public static final String VALUE = "value"; + public static final String TYPE = "type"; + public static final String TYPE_LIST = "list"; + public static final String TYPE_STRING = "string"; + public static final String TYPE_DOUBLE = "double"; + public static final String TYPE_INTEGER = "integer"; + public static final String TYPE_FLOAT = "float"; + public static final String TYPE_MAP = "map"; + public static final String TYPE_BOOLEAN = "boolean"; + public static final String TYPE_LONG = "long"; + public static final String TYPE_UNKNOWN = "unknown"; + + public static final String VERTICES = "vertices"; + public static final String EDGES = "edges"; + public static final String MODE = "mode"; +} diff --git a/sonar-core/src/main/java/org/sonar/core/graph/graphson/GraphSONWriter.java b/sonar-core/src/main/java/org/sonar/core/graph/graphson/GraphSONWriter.java new file mode 100644 index 00000000000..a48a24db336 --- /dev/null +++ b/sonar-core/src/main/java/org/sonar/core/graph/graphson/GraphSONWriter.java @@ -0,0 +1,79 @@ +/* + * Sonar, open source software quality management tool. + * Copyright (C) 2008-2012 SonarSource + * 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.core.graph.graphson; + +import com.google.common.base.Charsets; +import com.tinkerpop.blueprints.Edge; +import com.tinkerpop.blueprints.Graph; +import com.tinkerpop.blueprints.Vertex; +import org.json.simple.JSONArray; +import org.json.simple.JSONObject; + +import javax.annotation.Nullable; + +import java.io.OutputStream; +import java.util.Set; + +/** + * GraphSONWriter writes a Graph to a TinkerPop JSON OutputStream. + * + * @author Stephen Mallette + */ +public class GraphSONWriter { + + public void write(Graph graph, OutputStream jsonOutputStream, GraphSONMode mode) { + write(graph, jsonOutputStream, mode, null, null); + } + + /** + * Write the data in a Graph to a JSON OutputStream. + * + * @param jsonOutputStream the JSON OutputStream to write the Graph data to + * @param vertexPropertyKeys the keys of the vertex elements to write to JSON + * @param edgePropertyKeys the keys of the edge elements to write to JSON + * @param mode determines the format of the GraphSON + * @throws java.io.IOException thrown if there is an error generating the JSON data + */ + public void write(Graph graph, OutputStream jsonOutputStream, GraphSONMode mode, @Nullable Set<String> vertexPropertyKeys, @Nullable Set<String> edgePropertyKeys) { + try { + JSONObject root = new JSONObject(); + GraphsonUtil graphson = new GraphsonUtil(mode, null, vertexPropertyKeys, edgePropertyKeys); + + root.put(GraphSONTokens.MODE, mode.toString()); + + JSONArray verticesArray = new JSONArray(); + for (Vertex v : graph.getVertices()) { + verticesArray.add(graphson.objectNodeFromElement(v)); + } + root.put(GraphSONTokens.VERTICES, verticesArray); + + JSONArray edgesArray = new JSONArray(); + for (Edge e : graph.getEdges()) { + edgesArray.add(graphson.objectNodeFromElement(e)); + } + root.put(GraphSONTokens.EDGES, edgesArray); + + jsonOutputStream.write(root.toString().getBytes("UTF-8")); + } catch (Exception e) { + throw new GraphSonException("Fail to generate GraphSON", e); + } + } + +} diff --git a/sonar-core/src/main/java/org/sonar/core/graph/graphson/GraphSonException.java b/sonar-core/src/main/java/org/sonar/core/graph/graphson/GraphSonException.java new file mode 100644 index 00000000000..f037e76249f --- /dev/null +++ b/sonar-core/src/main/java/org/sonar/core/graph/graphson/GraphSonException.java @@ -0,0 +1,30 @@ +/* + * Sonar, open source software quality management tool. + * Copyright (C) 2008-2012 SonarSource + * 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.core.graph.graphson; + +public class GraphSonException extends RuntimeException { + public GraphSonException(String message) { + super(message); + } + + public GraphSonException(String message, Throwable cause) { + super(message, cause); + } +} diff --git a/sonar-core/src/main/java/org/sonar/core/graph/GraphSONUtility.java b/sonar-core/src/main/java/org/sonar/core/graph/graphson/GraphsonUtil.java index 5e521aefa8f..60c213c33fb 100644 --- a/sonar-core/src/main/java/org/sonar/core/graph/GraphSONUtility.java +++ b/sonar-core/src/main/java/org/sonar/core/graph/graphson/GraphsonUtil.java @@ -11,28 +11,25 @@ * 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. + * Lesser General 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.core.graph; +package org.sonar.core.graph.graphson; import com.tinkerpop.blueprints.Direction; import com.tinkerpop.blueprints.Edge; import com.tinkerpop.blueprints.Element; import com.tinkerpop.blueprints.Vertex; -import com.tinkerpop.blueprints.util.io.graphson.ElementFactory; -import com.tinkerpop.blueprints.util.io.graphson.ElementPropertyConfig; -import com.tinkerpop.blueprints.util.io.graphson.GraphSONMode; -import com.tinkerpop.blueprints.util.io.graphson.GraphSONTokens; import org.json.simple.JSONArray; import org.json.simple.JSONObject; import org.json.simple.parser.JSONParser; import org.json.simple.parser.ParseException; +import javax.annotation.Nullable; + import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; @@ -44,14 +41,14 @@ import java.util.List; import java.util.Map; import java.util.Set; -import static com.tinkerpop.blueprints.util.io.graphson.ElementPropertyConfig.ElementPropertiesRule; +import static org.sonar.core.graph.graphson.ElementPropertyConfig.ElementPropertiesRule; /** * Helps write individual graph elements to TinkerPop JSON format known as GraphSON. * * @author Stephen Mallette (http://stephen.genoprime.com) */ -public final class GraphSONUtility { +class GraphsonUtil { private final GraphSONMode mode; private final Set<String> vertexPropertyKeys; @@ -72,20 +69,20 @@ public final class GraphSONUtility { /** * A GraphSONUtiltiy that includes all properties of vertices and edges. */ - public GraphSONUtility(final GraphSONMode mode, final ElementFactory factory) { + GraphsonUtil(GraphSONMode mode, ElementFactory factory) { this(mode, factory, ElementPropertyConfig.AllProperties); } /** * A GraphSONUtility that includes the specified properties. */ - public GraphSONUtility(final GraphSONMode mode, final ElementFactory factory, - final Set<String> vertexPropertyKeys, final Set<String> edgePropertyKeys) { - this(mode, factory, ElementPropertyConfig.IncludeProperties(vertexPropertyKeys, edgePropertyKeys)); + GraphsonUtil(GraphSONMode mode, ElementFactory factory, + Set<String> vertexPropertyKeys, Set<String> edgePropertyKeys) { + this(mode, factory, ElementPropertyConfig.includeProperties(vertexPropertyKeys, edgePropertyKeys)); } - public GraphSONUtility(final GraphSONMode mode, final ElementFactory factory, - final ElementPropertyConfig config) { + GraphsonUtil(GraphSONMode mode, ElementFactory factory, + ElementPropertyConfig config) { this.vertexPropertyKeys = config.getVertexPropertyKeys(); this.edgePropertyKeys = config.getEdgePropertyKeys(); this.vertexPropertiesRule = config.getVertexPropertiesRule(); @@ -111,10 +108,9 @@ public final class GraphSONUtility { * @param propertyKeys The property keys at the root of the element to serialize. If null, then all keys are serialized. * @param mode the type of GraphSON to be generated. */ - public static JSONObject jsonFromElement(final Element element, final Set<String> propertyKeys, - final GraphSONMode mode) { - final GraphSONUtility graphson = element instanceof Edge ? new GraphSONUtility(mode, null, null, propertyKeys) - : new GraphSONUtility(mode, null, propertyKeys, null); + static JSONObject jsonFromElement(Element element, @Nullable Set<String> propertyKeys, GraphSONMode mode) { + GraphsonUtil graphson = element instanceof Edge ? new GraphsonUtil(mode, null, null, propertyKeys) + : new GraphsonUtil(mode, null, propertyKeys, null); return graphson.jsonFromElement(element); } @@ -125,9 +121,9 @@ public final class GraphSONUtility { * @param propertyKeys The property keys at the root of the element to serialize. If null, then all keys are serialized. * @param mode The type of GraphSON to generate. */ - public static JSONObject objectNodeFromElement(final Element element, final Set<String> propertyKeys, final GraphSONMode mode) { - final GraphSONUtility graphson = element instanceof Edge ? new GraphSONUtility(mode, null, null, propertyKeys) - : new GraphSONUtility(mode, null, propertyKeys, null); + static JSONObject objectNodeFromElement(Element element, Set<String> propertyKeys, GraphSONMode mode) { + GraphsonUtil graphson = element instanceof Edge ? new GraphsonUtil(mode, null, null, propertyKeys) + : new GraphsonUtil(mode, null, propertyKeys, null); return graphson.objectNodeFromElement(element); } @@ -139,9 +135,9 @@ public final class GraphSONUtility { * @param mode the mode of the GraphSON * @param propertyKeys a list of keys to include on reading of element properties */ - public static Vertex vertexFromJson(final JSONObject json, final ElementFactory factory, final GraphSONMode mode, - final Set<String> propertyKeys) throws IOException { - final GraphSONUtility graphson = new GraphSONUtility(mode, factory, propertyKeys, null); + static Vertex vertexFromJson(JSONObject json, ElementFactory factory, GraphSONMode mode, + Set<String> propertyKeys) throws IOException { + GraphsonUtil graphson = new GraphsonUtil(mode, factory, propertyKeys, null); return graphson.vertexFromJson(json); } @@ -153,9 +149,9 @@ public final class GraphSONUtility { * @param mode the mode of the GraphSON * @param propertyKeys a list of keys to include on reading of element properties */ - public static Vertex vertexFromJson(final String json, final ElementFactory factory, final GraphSONMode mode, - final Set<String> propertyKeys) throws ParseException { - final GraphSONUtility graphson = new GraphSONUtility(mode, factory, propertyKeys, null); + static Vertex vertexFromJson(String json, ElementFactory factory, GraphSONMode mode, + Set<String> propertyKeys) throws ParseException { + GraphsonUtil graphson = new GraphsonUtil(mode, factory, propertyKeys, null); return graphson.vertexFromJson(json); } @@ -167,22 +163,22 @@ public final class GraphSONUtility { * @param mode the mode of the GraphSON * @param propertyKeys a list of keys to include on reading of element properties */ - public static Vertex vertexFromJson(final InputStream json, final ElementFactory factory, final GraphSONMode mode, - final Set<String> propertyKeys) throws IOException, ParseException { - final GraphSONUtility graphson = new GraphSONUtility(mode, factory, propertyKeys, null); + static Vertex vertexFromJson(InputStream json, ElementFactory factory, GraphSONMode mode, + Set<String> propertyKeys) throws IOException, ParseException { + GraphsonUtil graphson = new GraphsonUtil(mode, factory, propertyKeys, null); return graphson.vertexFromJson(json); } - private static boolean includeReservedKey(final GraphSONMode mode, final String key, - final Set<String> propertyKeys, - final ElementPropertiesRule rule) { + private static boolean includeReservedKey(GraphSONMode mode, String key, + Set<String> propertyKeys, + ElementPropertiesRule rule) { // the key is always included in modes other than compact. if it is compact, then validate that the // key is in the property key list return mode != GraphSONMode.COMPACT || includeKey(key, propertyKeys, rule); } - private static boolean includeKey(final String key, final Set<String> propertyKeys, - final ElementPropertiesRule rule) { + private static boolean includeKey(String key, Set<String> propertyKeys, + ElementPropertiesRule rule) { if (propertyKeys == null) { // when null always include the key and shortcut this piece return true; @@ -212,10 +208,10 @@ public final class GraphSONUtility { * @param mode the mode of the GraphSON * @param propertyKeys a list of keys to include when reading of element properties */ - public static Edge edgeFromJson(final String json, final Vertex out, final Vertex in, - final ElementFactory factory, final GraphSONMode mode, - final Set<String> propertyKeys) throws IOException, ParseException { - final GraphSONUtility graphson = new GraphSONUtility(mode, factory, null, propertyKeys); + static Edge edgeFromJson(String json, Vertex out, Vertex in, + ElementFactory factory, GraphSONMode mode, + Set<String> propertyKeys) throws IOException, ParseException { + GraphsonUtil graphson = new GraphsonUtil(mode, factory, null, propertyKeys); return graphson.edgeFromJson(json, out, in); } @@ -227,10 +223,10 @@ public final class GraphSONUtility { * @param mode the mode of the GraphSON * @param propertyKeys a list of keys to include when reading of element properties */ - public static Edge edgeFromJson(final InputStream json, final Vertex out, final Vertex in, - final ElementFactory factory, final GraphSONMode mode, - final Set<String> propertyKeys) throws IOException, ParseException { - final GraphSONUtility graphson = new GraphSONUtility(mode, factory, null, propertyKeys); + static Edge edgeFromJson(InputStream json, Vertex out, Vertex in, + ElementFactory factory, GraphSONMode mode, + Set<String> propertyKeys) throws IOException, ParseException { + GraphsonUtil graphson = new GraphsonUtil(mode, factory, null, propertyKeys); return graphson.edgeFromJson(json, out, in); } @@ -242,15 +238,15 @@ public final class GraphSONUtility { * @param mode the mode of the GraphSON * @param propertyKeys a list of keys to include when reading of element properties */ - public static Edge edgeFromJson(final JSONObject json, final Vertex out, final Vertex in, - final ElementFactory factory, final GraphSONMode mode, - final Set<String> propertyKeys) throws IOException { - final GraphSONUtility graphson = new GraphSONUtility(mode, factory, null, propertyKeys); + static Edge edgeFromJson(JSONObject json, Vertex out, Vertex in, + ElementFactory factory, GraphSONMode mode, + Set<String> propertyKeys) throws IOException { + GraphsonUtil graphson = new GraphsonUtil(mode, factory, null, propertyKeys); return graphson.edgeFromJson(json, out, in); } - static Map<String, Object> readProperties(final JSONObject node, final boolean ignoreReservedKeys, final boolean hasEmbeddedTypes) { - final Map<String, Object> map = new HashMap<String, Object>(); + static Map<String, Object> readProperties(JSONObject node, boolean ignoreReservedKeys, boolean hasEmbeddedTypes) { + Map<String, Object> map = new HashMap<String, Object>(); for (Object objKey : node.keySet()) { String key = (String) objKey; @@ -264,17 +260,17 @@ public final class GraphSONUtility { return map; } - private static boolean isReservedKey(final String key) { + private static boolean isReservedKey(String key) { return key.equals(GraphSONTokens._ID) || key.equals(GraphSONTokens._TYPE) || key.equals(GraphSONTokens._LABEL) - || key.equals(GraphSONTokens._OUT_V) || key.equals(GraphSONTokens._IN_V); + || key.equals(GraphSONTokens._OUT_V) || key.equals(GraphSONTokens._IN_V); } - private static JSONArray createJSONList(final List list, final Set<String> propertyKeys, final boolean showTypes) { + private static JSONArray createJSONList(List list, Set<String> propertyKeys, boolean showTypes) { JSONArray jsonList = new JSONArray(); for (Object item : list) { if (item instanceof Element) { jsonList.add(objectNodeFromElement((Element) item, propertyKeys, - showTypes ? GraphSONMode.EXTENDED : GraphSONMode.NORMAL)); + showTypes ? GraphSONMode.EXTENDED : GraphSONMode.NORMAL)); } else if (item instanceof List) { jsonList.add(createJSONList((List) item, propertyKeys, showTypes)); } else if (item instanceof Map) { @@ -289,8 +285,8 @@ public final class GraphSONUtility { } // - private static JSONObject createJSONMap(final Map map, final Set<String> propertyKeys, final boolean showTypes) { - final JSONObject jsonMap = new JSONObject(); + private static JSONObject createJSONMap(Map map, Set<String> propertyKeys, boolean showTypes) { + JSONObject jsonMap = new JSONObject(); for (Object key : map.keySet()) { Object value = map.get(key); if (value != null) { @@ -300,7 +296,7 @@ public final class GraphSONUtility { value = createJSONMap((Map) value, propertyKeys, showTypes); } else if (value instanceof Element) { value = objectNodeFromElement((Element) value, propertyKeys, - showTypes ? GraphSONMode.EXTENDED : GraphSONMode.NORMAL); + showTypes ? GraphSONMode.EXTENDED : GraphSONMode.NORMAL); } else if (value.getClass().isArray()) { value = createJSONList(convertArrayToList(value), propertyKeys, showTypes); } @@ -312,7 +308,7 @@ public final class GraphSONUtility { } - private static Object readProperty(final Object node, final boolean hasEmbeddedTypes) { + private static Object readProperty(Object node, boolean hasEmbeddedTypes) { Object propertyValue; if (hasEmbeddedTypes) { @@ -363,7 +359,7 @@ public final class GraphSONUtility { return propertyValue; } - private static void putObject(final JSONObject jsonMap, final String key, final Object value) { + private static void putObject(JSONObject jsonMap, String key, Object value) { if (value == null) { jsonMap.put(key, null); } else if (value instanceof Boolean) { @@ -387,8 +383,8 @@ public final class GraphSONUtility { } } - private static List readProperties(final Iterator<JSONObject> listOfNodes, final boolean hasEmbeddedTypes) { - final List array = new ArrayList(); + private static List readProperties(Iterator<JSONObject> listOfNodes, boolean hasEmbeddedTypes) { + List array = new ArrayList(); while (listOfNodes.hasNext()) { array.add(readProperty(listOfNodes.next(), hasEmbeddedTypes)); @@ -397,7 +393,7 @@ public final class GraphSONUtility { return array; } - private static void addObject(final JSONArray jsonList, final Object value) { + private static void addObject(JSONArray jsonList, Object value) { if (value == null) { jsonList.add(null); } else if (value instanceof Boolean) { @@ -421,8 +417,8 @@ public final class GraphSONUtility { } } - private static Map createPropertyMap(final Element element, final Set<String> propertyKeys, final ElementPropertiesRule rule) { - final Map map = new HashMap<String, Object>(); + private static Map createPropertyMap(Element element, Set<String> propertyKeys, ElementPropertiesRule rule) { + Map map = new HashMap<String, Object>(); if (propertyKeys == null) { for (String key : element.getPropertyKeys()) { @@ -448,7 +444,7 @@ public final class GraphSONUtility { return map; } - private static Object getValue(Object value, final boolean includeType) { + private static Object getValue(Object value, boolean includeType) { Object returnValue = value; @@ -509,7 +505,7 @@ public final class GraphSONUtility { return returnValue; } - private static List convertArrayToList(final Object value) { + private static List convertArrayToList(Object value) { // is there seriously no better way to do this...bah! List list = new ArrayList(); @@ -545,7 +541,7 @@ public final class GraphSONUtility { return list; } - private static String determineType(final Object value) { + private static String determineType(Object value) { String type = GraphSONTokens.TYPE_STRING; if (value == null) { type = "unknown"; @@ -571,37 +567,37 @@ public final class GraphSONUtility { /** * Creates a vertex from GraphSON using settings supplied in the constructor. */ - public Vertex vertexFromJson(final InputStream json) throws ParseException, IOException { + Vertex vertexFromJson(InputStream json) throws ParseException, IOException { return this.vertexFromJson((JSONObject) parser.parse(new InputStreamReader(json))); } /** * Creates an edge from GraphSON using settings supplied in the constructor. */ - public Edge edgeFromJson(final String json, final Vertex out, final Vertex in) throws IOException, ParseException { + Edge edgeFromJson(String json, Vertex out, Vertex in) throws IOException, ParseException { return this.edgeFromJson((JSONObject) parser.parse(json), out, in); } /** * Creates an edge from GraphSON using settings supplied in the constructor. */ - public Edge edgeFromJson(final InputStream json, final Vertex out, final Vertex in) throws IOException, ParseException { + Edge edgeFromJson(InputStream json, Vertex out, Vertex in) throws IOException, ParseException { return this.edgeFromJson((JSONObject) parser.parse(new InputStreamReader(json)), out, in); } /** * Creates an edge from GraphSON using settings supplied in the constructor. */ - public Edge edgeFromJson(final JSONObject json, final Vertex out, final Vertex in) throws IOException { - final Map<String, Object> props = GraphSONUtility.readProperties(json, true, this.hasEmbeddedTypes); + Edge edgeFromJson(JSONObject json, Vertex out, Vertex in) throws IOException { + Map<String, Object> props = GraphsonUtil.readProperties(json, true, this.hasEmbeddedTypes); -// final Object edgeId = getTypedValueFromJsonNode(json.get(GraphSONTokens._ID)); - final Object edgeId = json.get(GraphSONTokens._ID); +// Object edgeId = getTypedValueFromJsonNode(json.get(GraphSONTokens._ID)); + Object edgeId = json.get(GraphSONTokens._ID); - final Object nodeLabel = json.get(GraphSONTokens._LABEL); - final String label = nodeLabel == null ? null : nodeLabel.toString(); + Object nodeLabel = json.get(GraphSONTokens._LABEL); + String label = nodeLabel == null ? null : nodeLabel.toString(); - final Edge e = factory.createEdge(edgeId, out, in, label); + Edge e = factory.createEdge(edgeId, out, in, label); for (Map.Entry<String, Object> entry : props.entrySet()) { // if (this.edgePropertyKeys == null || this.edgePropertyKeys.contains(entry.getKey())) { @@ -616,19 +612,19 @@ public final class GraphSONUtility { /** * Creates a vertex from GraphSON using settings supplied in the constructor. */ - public Vertex vertexFromJson(final String json) throws ParseException { + Vertex vertexFromJson(String json) throws ParseException { return this.vertexFromJson((JSONObject) parser.parse(json)); } /** * Creates a vertex from GraphSON using settings supplied in the constructor. */ - public Vertex vertexFromJson(final JSONObject json) { - final Map<String, Object> props = readProperties(json, true, this.hasEmbeddedTypes); + Vertex vertexFromJson(JSONObject json) { + Map<String, Object> props = readProperties(json, true, this.hasEmbeddedTypes); - //final Object vertexId = getTypedValueFromJsonNode((JSONObject)json.get(GraphSONTokens._ID)); - final Object vertexId = json.get(GraphSONTokens._ID); - final Vertex v = factory.createVertex(vertexId); + //Object vertexId = getTypedValueFromJsonNode((JSONObject)json.get(GraphSONTokens._ID)); + Object vertexId = json.get(GraphSONTokens._ID); + Vertex v = factory.createVertex(vertexId); for (Map.Entry<String, Object> entry : props.entrySet()) { //if (this.vertexPropertyKeys == null || vertexPropertyKeys.contains(entry.getKey())) { @@ -643,21 +639,21 @@ public final class GraphSONUtility { /** * Creates GraphSON for a single graph element. */ - public JSONObject jsonFromElement(final Element element) { - final JSONObject objectNode = this.objectNodeFromElement(element); + JSONObject jsonFromElement(Element element) { + JSONObject objectNode = this.objectNodeFromElement(element); return objectNode; } /** * Creates GraphSON for a single graph element. */ - public org.json.simple.JSONObject objectNodeFromElement(final Element element) { - final boolean isEdge = element instanceof Edge; - final boolean showTypes = mode == GraphSONMode.EXTENDED; - final Set<String> propertyKeys = isEdge ? this.edgePropertyKeys : this.vertexPropertyKeys; - final ElementPropertiesRule elementPropertyConfig = isEdge ? this.edgePropertiesRule : this.vertexPropertiesRule; + org.json.simple.JSONObject objectNodeFromElement(Element element) { + boolean isEdge = element instanceof Edge; + boolean showTypes = mode == GraphSONMode.EXTENDED; + Set<String> propertyKeys = isEdge ? this.edgePropertyKeys : this.vertexPropertyKeys; + ElementPropertiesRule elementPropertyConfig = isEdge ? this.edgePropertiesRule : this.vertexPropertiesRule; - final org.json.simple.JSONObject jsonElement = createJSONMap(createPropertyMap(element, propertyKeys, elementPropertyConfig), propertyKeys, showTypes); + org.json.simple.JSONObject jsonElement = createJSONMap(createPropertyMap(element, propertyKeys, elementPropertyConfig), propertyKeys, showTypes); if ((isEdge && this.includeReservedEdgeId) || (!isEdge && this.includeReservedVertexId)) { putObject(jsonElement, GraphSONTokens._ID, element.getId()); @@ -666,7 +662,7 @@ public final class GraphSONUtility { // it's important to keep the order of these straight. check Edge first and then Vertex because there // are graph implementations that have Edge extend from Vertex if (element instanceof Edge) { - final Edge edge = (Edge) element; + Edge edge = (Edge) element; if (this.includeReservedEdgeId) { putObject(jsonElement, GraphSONTokens._ID, element.getId()); diff --git a/sonar-core/src/test/java/org/sonar/core/graph/GraphSONReaderTest.java b/sonar-core/src/test/java/org/sonar/core/graph/graphson/GraphsonReaderTest.java index 69ba8119e4a..9c0cb908c8a 100644 --- a/sonar-core/src/test/java/org/sonar/core/graph/GraphSONReaderTest.java +++ b/sonar-core/src/test/java/org/sonar/core/graph/graphson/GraphsonReaderTest.java @@ -17,17 +17,13 @@ * License along with Sonar; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 */ - -package org.sonar.core.graph; - +package org.sonar.core.graph.graphson; import com.tinkerpop.blueprints.Direction; import com.tinkerpop.blueprints.Edge; import com.tinkerpop.blueprints.Vertex; import com.tinkerpop.blueprints.impls.tg.TinkerGraph; import com.tinkerpop.blueprints.impls.tg.TinkerGraphFactory; -import com.tinkerpop.blueprints.util.io.graphson.GraphSONMode; -import com.tinkerpop.blueprints.util.io.graphson.GraphSONTokens; import org.junit.Assert; import org.junit.Test; @@ -40,7 +36,7 @@ import java.util.List; import java.util.Map; import java.util.Set; -public class GraphSONReaderTest { +public class GraphsonReaderTest { @Test public void inputGraphModeExtended() throws Exception { @@ -51,7 +47,7 @@ public class GraphSONReaderTest { byte[] bytes = json.getBytes(); InputStream inputStream = new ByteArrayInputStream(bytes); - GraphSONReader.inputGraph(graph, inputStream); + new GraphSONReader().read(inputStream, graph); Assert.assertEquals(2, getIterableCount(graph.getVertices())); Assert.assertEquals(1, getIterableCount(graph.getEdges())); @@ -102,7 +98,7 @@ public class GraphSONReaderTest { byte[] bytes = json.getBytes(); InputStream inputStream = new ByteArrayInputStream(bytes); - GraphSONReader.inputGraph(graph, inputStream); + new GraphSONReader().read(inputStream, graph); Assert.assertEquals(2, getIterableCount(graph.getVertices())); Assert.assertEquals(1, getIterableCount(graph.getEdges())); @@ -153,7 +149,7 @@ public class GraphSONReaderTest { byte[] bytes = json.getBytes(); InputStream inputStream = new ByteArrayInputStream(bytes); - GraphSONReader.inputGraph(graph, inputStream); + new GraphSONReader().read(inputStream, graph); Assert.assertEquals(2, getIterableCount(graph.getVertices())); Assert.assertEquals(1, getIterableCount(graph.getEdges())); @@ -172,8 +168,8 @@ public class GraphSONReaderTest { Assert.assertEquals(4, list.size()); boolean foundNull = false; - for (int ix = 0; ix < list.size(); ix++) { - if (list.get(ix) == null) { + for (Object aList : list) { + if (aList == null) { foundNull = true; break; } @@ -201,8 +197,8 @@ public class GraphSONReaderTest { ByteArrayOutputStream stream = new ByteArrayOutputStream(); - GraphSONWriter writer = new GraphSONWriter(graph); - writer.outputGraph(stream, null, null, GraphSONMode.EXTENDED); + GraphSONWriter writer = new GraphSONWriter(); + writer.write(graph, stream, GraphSONMode.EXTENDED); stream.flush(); stream.close(); @@ -213,7 +209,7 @@ public class GraphSONReaderTest { InputStream inputStream = new ByteArrayInputStream(bytes); TinkerGraph emptyGraph = new TinkerGraph(); - GraphSONReader.inputGraph(emptyGraph, inputStream); + new GraphSONReader().read(inputStream, emptyGraph); Assert.assertEquals(6, getIterableCount(emptyGraph.getVertices())); Assert.assertEquals(6, getIterableCount(emptyGraph.getEdges())); @@ -255,8 +251,8 @@ public class GraphSONReaderTest { Set<String> vertexKeys = new HashSet<String>(); vertexKeys.add(GraphSONTokens._ID); - GraphSONWriter writer = new GraphSONWriter(graph); - writer.outputGraph(stream, vertexKeys, edgeKeys, GraphSONMode.COMPACT); + GraphSONWriter writer = new GraphSONWriter(); + writer.write(graph, stream, GraphSONMode.EXTENDED, vertexKeys, edgeKeys); stream.flush(); stream.close(); @@ -267,7 +263,7 @@ public class GraphSONReaderTest { InputStream inputStream = new ByteArrayInputStream(bytes); TinkerGraph emptyGraph = new TinkerGraph(); - GraphSONReader.inputGraph(emptyGraph, inputStream); + new GraphSONReader().read(inputStream, emptyGraph); Assert.assertEquals(6, getIterableCount(emptyGraph.getVertices())); Assert.assertEquals(6, getIterableCount(emptyGraph.getEdges())); @@ -300,7 +296,7 @@ public class GraphSONReaderTest { } - @Test(expected = IllegalArgumentException.class) + @Test(expected = GraphSonException.class) public void inputGraphCompactFullCycleBroken() throws Exception { TinkerGraph graph = TinkerGraphFactory.createTinkerGraph(); @@ -313,8 +309,8 @@ public class GraphSONReaderTest { Set<String> vertexKeys = new HashSet<String>(); - GraphSONWriter writer = new GraphSONWriter(graph); - writer.outputGraph(stream, vertexKeys, edgeKeys, GraphSONMode.COMPACT); + GraphSONWriter writer = new GraphSONWriter(); + writer.write(graph, stream, GraphSONMode.COMPACT, vertexKeys, edgeKeys); stream.flush(); stream.close(); @@ -325,7 +321,7 @@ public class GraphSONReaderTest { InputStream inputStream = new ByteArrayInputStream(bytes); TinkerGraph emptyGraph = new TinkerGraph(); - GraphSONReader.inputGraph(emptyGraph, inputStream); + new GraphSONReader().read(inputStream, emptyGraph); } diff --git a/sonar-core/src/test/java/org/sonar/core/graph/GraphSONUtilityTest.java b/sonar-core/src/test/java/org/sonar/core/graph/graphson/GraphsonUtilTest.java index 23b2653d07e..ad24c277774 100644 --- a/sonar-core/src/test/java/org/sonar/core/graph/GraphSONUtilityTest.java +++ b/sonar-core/src/test/java/org/sonar/core/graph/graphson/GraphsonUtilTest.java @@ -17,18 +17,13 @@ * License along with Sonar; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 */ - -package org.sonar.core.graph; +package org.sonar.core.graph.graphson; import com.tinkerpop.blueprints.Direction; import com.tinkerpop.blueprints.Edge; import com.tinkerpop.blueprints.Graph; import com.tinkerpop.blueprints.Vertex; import com.tinkerpop.blueprints.impls.tg.TinkerGraph; -import com.tinkerpop.blueprints.util.io.graphson.ElementFactory; -import com.tinkerpop.blueprints.util.io.graphson.ElementPropertyConfig; -import com.tinkerpop.blueprints.util.io.graphson.GraphElementFactory; -import com.tinkerpop.blueprints.util.io.graphson.GraphSONMode; import com.tinkerpop.blueprints.util.io.graphson.GraphSONTokens; import org.json.simple.JSONArray; import org.json.simple.JSONObject; @@ -48,7 +43,7 @@ import java.util.Set; import static org.fest.assertions.Assertions.assertThat; -public class GraphSONUtilityTest { +public class GraphsonUtilTest { private final String vertexJson1 = "{\"name\":\"marko\",\"age\":29,\"_id\":1,\"_type\":\"vertex\"}"; private final String vertexJson2 = "{\"name\":\"vadas\",\"age\":27,\"_id\":2,\"_type\":\"vertex\"}"; private final String edgeJsonLight = "{\"weight\":0.5,\"_outV\":1,\"_inV\":2}"; @@ -73,7 +68,7 @@ public class GraphSONUtilityTest { Edge e = this.graph.addEdge(3, v1, v2, "test"); e.setProperty("weight", 0.5f); - JSONObject json = GraphSONUtility.jsonFromElement(e, null, GraphSONMode.NORMAL); + JSONObject json = GraphsonUtil.jsonFromElement(e, null, GraphSONMode.NORMAL); assertThat(json).isNotNull(); assertThat(json.containsKey(GraphSONTokens._ID)).isTrue(); @@ -102,7 +97,7 @@ public class GraphSONUtilityTest { add(GraphSONTokens._ID); }}; - JSONObject json = GraphSONUtility.jsonFromElement(e, propertiesToInclude, GraphSONMode.COMPACT); + JSONObject json = GraphsonUtil.jsonFromElement(e, propertiesToInclude, GraphSONMode.COMPACT); assertThat(json).isNotNull(); assertThat(json.containsKey(GraphSONTokens._TYPE)).isFalse(); @@ -115,7 +110,7 @@ public class GraphSONUtilityTest { @Test public void jsonFromElementEdgeCompactIdOnlyAsExclude() { - ElementFactory factory = new GraphElementFactory(this.graph); + ElementFactory factory = new ElementFactory(this.graph); Vertex v1 = this.graph.addVertex(1); Vertex v2 = this.graph.addVertex(2); @@ -134,7 +129,7 @@ public class GraphSONUtilityTest { ElementPropertyConfig config = new ElementPropertyConfig(null, propertiesToExclude, ElementPropertyConfig.ElementPropertiesRule.INCLUDE, ElementPropertyConfig.ElementPropertiesRule.EXCLUDE); - GraphSONUtility utility = new GraphSONUtility(GraphSONMode.COMPACT, factory, config); + GraphsonUtil utility = new GraphsonUtil(GraphSONMode.COMPACT, factory, config); JSONObject json = utility.jsonFromElement(e); assertThat(json).isNotNull(); @@ -156,7 +151,7 @@ public class GraphSONUtilityTest { Edge e = this.graph.addEdge(3, v1, v2, "test"); e.setProperty("weight", 0.5f); - JSONObject json = GraphSONUtility.jsonFromElement(e, null, GraphSONMode.COMPACT); + JSONObject json = GraphsonUtil.jsonFromElement(e, null, GraphSONMode.COMPACT); assertThat(json).isNotNull(); assertThat(json.containsKey(GraphSONTokens._ID)).isTrue(); @@ -172,7 +167,7 @@ public class GraphSONUtilityTest { Vertex v = this.graph.addVertex(1); v.setProperty("name", "marko"); - JSONObject json = GraphSONUtility.jsonFromElement(v, null, GraphSONMode.NORMAL); + JSONObject json = GraphsonUtil.jsonFromElement(v, null, GraphSONMode.NORMAL); assertThat(json).isNotNull(); assertThat(json.containsKey(GraphSONTokens._ID)).isTrue(); @@ -191,7 +186,7 @@ public class GraphSONUtilityTest { add(GraphSONTokens._ID); }}; - JSONObject json = GraphSONUtility.jsonFromElement(v, propertiesToInclude, GraphSONMode.COMPACT); + JSONObject json = GraphsonUtil.jsonFromElement(v, propertiesToInclude, GraphSONMode.COMPACT); assertThat(json).isNotNull(); assertThat(json.containsKey(GraphSONTokens._TYPE)).isFalse(); @@ -201,7 +196,7 @@ public class GraphSONUtilityTest { @Test public void jsonFromElementVertexCompactIdNameOnlyAsExclude() { - GraphElementFactory factory = new GraphElementFactory(this.graph); + ElementFactory factory = new ElementFactory(this.graph); Vertex v = this.graph.addVertex(1); v.setProperty("name", "marko"); @@ -213,7 +208,7 @@ public class GraphSONUtilityTest { ElementPropertyConfig.ElementPropertiesRule.EXCLUDE, ElementPropertyConfig.ElementPropertiesRule.EXCLUDE); - GraphSONUtility utility = new GraphSONUtility(GraphSONMode.COMPACT, factory, config); + GraphsonUtil utility = new GraphsonUtil(GraphSONMode.COMPACT, factory, config); JSONObject json = utility.jsonFromElement(v); assertThat(json).isNotNull(); @@ -227,7 +222,7 @@ public class GraphSONUtilityTest { Vertex v = this.graph.addVertex(1); v.setProperty("name", "marko"); - JSONObject json = GraphSONUtility.jsonFromElement(v, null, GraphSONMode.COMPACT); + JSONObject json = GraphsonUtil.jsonFromElement(v, null, GraphSONMode.COMPACT); assertThat(json).isNotNull(); assertThat(json.containsKey(GraphSONTokens._TYPE)).isTrue(); @@ -246,7 +241,7 @@ public class GraphSONUtilityTest { v.setProperty("keyDouble", 4.4); v.setProperty("keyBoolean", true); - JSONObject json = GraphSONUtility.jsonFromElement(v, null, GraphSONMode.NORMAL); + JSONObject json = GraphsonUtil.jsonFromElement(v, null, GraphSONMode.NORMAL); assertThat(json).isNotNull(); assertThat(json.containsKey(GraphSONTokens._ID)).isTrue(); @@ -276,7 +271,7 @@ public class GraphSONUtilityTest { v.setProperty("keyMap", map); - JSONObject json = GraphSONUtility.jsonFromElement(v, null, GraphSONMode.NORMAL); + JSONObject json = GraphsonUtil.jsonFromElement(v, null, GraphSONMode.NORMAL); assertThat(json).isNotNull(); assertThat(json.get(GraphSONTokens._ID)).isEqualTo("1"); @@ -301,7 +296,7 @@ public class GraphSONUtilityTest { v.setProperty("keyList", list); - JSONObject json = GraphSONUtility.jsonFromElement(v, null, GraphSONMode.NORMAL); + JSONObject json = GraphsonUtil.jsonFromElement(v, null, GraphSONMode.NORMAL); assertThat(json).isNotNull(); assertThat(json.get(GraphSONTokens._ID)).isEqualTo("1"); @@ -319,7 +314,7 @@ public class GraphSONUtilityTest { v.setProperty("keyStringArray", stringArray); - JSONObject json = GraphSONUtility.jsonFromElement(v, null, GraphSONMode.NORMAL); + JSONObject json = GraphsonUtil.jsonFromElement(v, null, GraphSONMode.NORMAL); assertThat(json).isNotNull(); assertThat(json.get(GraphSONTokens._ID)).isEqualTo("1"); @@ -337,7 +332,7 @@ public class GraphSONUtilityTest { v.setProperty("keyDoubleArray", doubleArray); - JSONObject json = GraphSONUtility.jsonFromElement(v, null, GraphSONMode.NORMAL); + JSONObject json = GraphsonUtil.jsonFromElement(v, null, GraphSONMode.NORMAL); assertThat(json).isNotNull(); assertThat(json.get(GraphSONTokens._ID)).isEqualTo("1"); @@ -355,7 +350,7 @@ public class GraphSONUtilityTest { v.setProperty("keyIntArray", intArray); - JSONObject json = GraphSONUtility.jsonFromElement(v, null, GraphSONMode.NORMAL); + JSONObject json = GraphsonUtil.jsonFromElement(v, null, GraphSONMode.NORMAL); assertThat(json).isNotNull(); assertThat(json.get(GraphSONTokens._ID)).isEqualTo("1"); @@ -373,7 +368,7 @@ public class GraphSONUtilityTest { v.setProperty("keyLongArray", longArray); - JSONObject json = GraphSONUtility.jsonFromElement(v, null, GraphSONMode.NORMAL); + JSONObject json = GraphsonUtil.jsonFromElement(v, null, GraphSONMode.NORMAL); assertThat(json).isNotNull(); assertThat(json.get(GraphSONTokens._ID)).isEqualTo("1"); @@ -391,7 +386,7 @@ public class GraphSONUtilityTest { v.setProperty("keyFloatArray", floatArray); - JSONObject json = GraphSONUtility.jsonFromElement(v, null, GraphSONMode.NORMAL); + JSONObject json = GraphsonUtil.jsonFromElement(v, null, GraphSONMode.NORMAL); assertThat(json).isNotNull(); assertThat(json.get(GraphSONTokens._ID)).isEqualTo("1"); @@ -409,7 +404,7 @@ public class GraphSONUtilityTest { v.setProperty("keyBooleanArray", booleanArray); - JSONObject json = GraphSONUtility.jsonFromElement(v, null, GraphSONMode.NORMAL); + JSONObject json = GraphsonUtil.jsonFromElement(v, null, GraphSONMode.NORMAL); assertThat(json).isNotNull(); assertThat(json.get(GraphSONTokens._ID)).isEqualTo("1"); @@ -425,7 +420,7 @@ public class GraphSONUtilityTest { Vertex v = this.graph.addVertex(1); v.setProperty("mycat", new Cat("smithers")); - JSONObject json = GraphSONUtility.jsonFromElement(v, null, GraphSONMode.NORMAL); + JSONObject json = GraphsonUtil.jsonFromElement(v, null, GraphSONMode.NORMAL); assertThat(json).isNotNull(); assertThat(json.get(GraphSONTokens._ID)).isEqualTo("1"); @@ -438,7 +433,7 @@ public class GraphSONUtilityTest { Vertex v = this.graph.addVertex(1); v.setProperty("mycat", new Cat("smithers")); - JSONObject json = GraphSONUtility.jsonFromElement(v, null, GraphSONMode.EXTENDED); + JSONObject json = GraphsonUtil.jsonFromElement(v, null, GraphSONMode.EXTENDED); assertThat(json).isNotNull(); assertThat(json.get(GraphSONTokens._ID)).isEqualTo("1"); @@ -458,7 +453,7 @@ public class GraphSONUtilityTest { v.setProperty("cats", cats); - JSONObject json = GraphSONUtility.jsonFromElement(v, null, GraphSONMode.NORMAL); + JSONObject json = GraphsonUtil.jsonFromElement(v, null, GraphSONMode.NORMAL); assertThat(json).isNotNull(); assertThat(json.get(GraphSONTokens._ID)).isEqualTo("1"); @@ -494,7 +489,7 @@ public class GraphSONUtilityTest { v.setProperty("crazy-map", map); - JSONObject json = GraphSONUtility.jsonFromElement(v, null, GraphSONMode.NORMAL); + JSONObject json = GraphsonUtil.jsonFromElement(v, null, GraphSONMode.NORMAL); assertThat(json).isNotNull(); assertThat(json.get(GraphSONTokens._ID)).isEqualTo("1"); @@ -528,7 +523,7 @@ public class GraphSONUtilityTest { Set<String> propertiesToInclude = new HashSet<String>(); propertiesToInclude.add("y"); - JSONObject json = GraphSONUtility.jsonFromElement(v, propertiesToInclude, GraphSONMode.NORMAL); + JSONObject json = GraphsonUtil.jsonFromElement(v, propertiesToInclude, GraphSONMode.NORMAL); assertThat(json).isNotNull(); assertThat(json.get(GraphSONTokens._ID)).isEqualTo("1"); @@ -556,7 +551,7 @@ public class GraphSONUtilityTest { propertiesToInclude.add("y"); propertiesToInclude.add("v"); - JSONObject json = GraphSONUtility.jsonFromElement(v, propertiesToInclude, GraphSONMode.NORMAL); + JSONObject json = GraphsonUtil.jsonFromElement(v, propertiesToInclude, GraphSONMode.NORMAL); assertThat(json).isNotNull(); assertThat(json.get(GraphSONTokens._ID)).isEqualTo("1"); @@ -583,7 +578,7 @@ public class GraphSONUtilityTest { v.setProperty("keyDouble", 4.4); v.setProperty("keyBoolean", true); - JSONObject json = GraphSONUtility.jsonFromElement(v, null, GraphSONMode.EXTENDED); + JSONObject json = GraphsonUtil.jsonFromElement(v, null, GraphSONMode.EXTENDED); assertThat(json).isNotNull(); assertThat(json.get(GraphSONTokens._ID)).isEqualTo("1"); @@ -636,7 +631,7 @@ public class GraphSONUtilityTest { v.setProperty("keyList", list); - JSONObject json = GraphSONUtility.jsonFromElement(v, null, GraphSONMode.EXTENDED); + JSONObject json = GraphsonUtil.jsonFromElement(v, null, GraphSONMode.EXTENDED); assertThat(json).isNotNull(); assertThat(json.get(GraphSONTokens._ID)).isEqualTo("1"); @@ -671,7 +666,7 @@ public class GraphSONUtilityTest { v.setProperty("keyList", list); - JSONObject json = GraphSONUtility.jsonFromElement(v, null, GraphSONMode.EXTENDED); + JSONObject json = GraphsonUtil.jsonFromElement(v, null, GraphSONMode.EXTENDED); assertThat(json).isNotNull(); assertThat(json.get(GraphSONTokens._ID)).isEqualTo("1"); @@ -706,7 +701,7 @@ public class GraphSONUtilityTest { v.setProperty("keyList", list); - JSONObject json = GraphSONUtility.jsonFromElement(v, null, GraphSONMode.EXTENDED); + JSONObject json = GraphsonUtil.jsonFromElement(v, null, GraphSONMode.EXTENDED); assertThat(json).isNotNull(); assertThat(json.get(GraphSONTokens._ID)).isEqualTo("1"); @@ -741,7 +736,7 @@ public class GraphSONUtilityTest { v.setProperty("keyList", list); - JSONObject json = GraphSONUtility.jsonFromElement(v, null, GraphSONMode.EXTENDED); + JSONObject json = GraphsonUtil.jsonFromElement(v, null, GraphSONMode.EXTENDED); assertThat(json).isNotNull(); assertThat(json.get(GraphSONTokens._ID)).isEqualTo("1"); @@ -779,7 +774,7 @@ public class GraphSONUtilityTest { v.setProperty("keyList", listList); - JSONObject json = GraphSONUtility.jsonFromElement(v, null, GraphSONMode.EXTENDED); + JSONObject json = GraphsonUtil.jsonFromElement(v, null, GraphSONMode.EXTENDED); assertThat(json).isNotNull(); assertThat(json.get(GraphSONTokens._ID)).isEqualTo("1"); @@ -816,7 +811,7 @@ public class GraphSONUtilityTest { v.setProperty("keyMap", map); - JSONObject json = GraphSONUtility.jsonFromElement(v, null, GraphSONMode.EXTENDED); + JSONObject json = GraphsonUtil.jsonFromElement(v, null, GraphSONMode.EXTENDED); assertThat(json).isNotNull(); assertThat(json.get(GraphSONTokens._ID)).isEqualTo("1"); @@ -866,7 +861,7 @@ public class GraphSONUtilityTest { list.add("string"); v.setProperty("keyList", list); - JSONObject json = GraphSONUtility.jsonFromElement(v, null, GraphSONMode.NORMAL); + JSONObject json = GraphsonUtil.jsonFromElement(v, null, GraphSONMode.NORMAL); assertThat(json).isNotNull(); assertThat(json.get("key")).isNull(); @@ -908,7 +903,7 @@ public class GraphSONUtilityTest { list.add("string"); v.setProperty("keyList", list); - JSONObject json = GraphSONUtility.jsonFromElement(v, null, GraphSONMode.EXTENDED); + JSONObject json = GraphsonUtil.jsonFromElement(v, null, GraphSONMode.EXTENDED); assertThat(json).isNotNull(); @@ -941,9 +936,9 @@ public class GraphSONUtilityTest { @Test public void vertexFromJsonValid() throws Exception { Graph g = new TinkerGraph(); - ElementFactory factory = new GraphElementFactory(g); + ElementFactory factory = new ElementFactory(g); - Vertex v = GraphSONUtility.vertexFromJson((JSONObject) JSONValue.parse(vertexJson1), factory, GraphSONMode.NORMAL, null); + Vertex v = GraphsonUtil.vertexFromJson((JSONObject) JSONValue.parse(vertexJson1), factory, GraphSONMode.NORMAL, null); assertThat(v).isSameAs(g.getVertex(1)); @@ -956,9 +951,9 @@ public class GraphSONUtilityTest { @Test public void vertexFromJsonStringValid() throws Exception { Graph g = new TinkerGraph(); - ElementFactory factory = new GraphElementFactory(g); + ElementFactory factory = new ElementFactory(g); - Vertex v = GraphSONUtility.vertexFromJson(vertexJson1, factory, GraphSONMode.NORMAL, null); + Vertex v = GraphsonUtil.vertexFromJson(vertexJson1, factory, GraphSONMode.NORMAL, null); assertThat(v).isSameAs(g.getVertex(1)); @@ -971,10 +966,10 @@ public class GraphSONUtilityTest { @Test public void vertexFromJsonStringValidExtended() throws Exception { Graph g = new TinkerGraph(); - ElementFactory factory = new GraphElementFactory(g); + ElementFactory factory = new ElementFactory(g); String vertexJson = "{\"person\":{\"value\":\"marko\",\"type\":\"string\"},\"_id\":1,\"_type\":\"vertex\"}"; - Vertex v = GraphSONUtility.vertexFromJson(vertexJson, factory, GraphSONMode.EXTENDED, null); + Vertex v = GraphsonUtil.vertexFromJson(vertexJson, factory, GraphSONMode.EXTENDED, null); Assert.assertSame(v, g.getVertex(1)); @@ -986,9 +981,9 @@ public class GraphSONUtilityTest { @Test public void vertexFromJsonInputStreamValid() throws Exception { Graph g = new TinkerGraph(); - ElementFactory factory = new GraphElementFactory(g); + ElementFactory factory = new ElementFactory(g); - Vertex v = GraphSONUtility.vertexFromJson(inputStreamVertexJson1, factory, GraphSONMode.NORMAL, null); + Vertex v = GraphsonUtil.vertexFromJson(inputStreamVertexJson1, factory, GraphSONMode.NORMAL, null); Assert.assertSame(v, g.getVertex(1)); @@ -1001,12 +996,12 @@ public class GraphSONUtilityTest { @Test public void vertexFromJsonIgnoreKeyValid() throws Exception { Graph g = new TinkerGraph(); - ElementFactory factory = new GraphElementFactory(g); + ElementFactory factory = new ElementFactory(g); Set<String> ignoreAge = new HashSet<String>(); ignoreAge.add("age"); - ElementPropertyConfig config = ElementPropertyConfig.ExcludeProperties(ignoreAge, null); - GraphSONUtility utility = new GraphSONUtility(GraphSONMode.NORMAL, factory, config); + ElementPropertyConfig config = ElementPropertyConfig.excludeProperties(ignoreAge, null); + GraphsonUtil utility = new GraphsonUtil(GraphSONMode.NORMAL, factory, config); Vertex v = utility.vertexFromJson((JSONObject) JSONValue.parse(vertexJson1)); Assert.assertSame(v, g.getVertex(1)); @@ -1020,11 +1015,11 @@ public class GraphSONUtilityTest { @Test public void edgeFromJsonValid() throws Exception { Graph g = new TinkerGraph(); - ElementFactory factory = new GraphElementFactory(g); + ElementFactory factory = new ElementFactory(g); - Vertex v1 = GraphSONUtility.vertexFromJson((JSONObject) JSONValue.parse(vertexJson1), factory, GraphSONMode.NORMAL, null); - Vertex v2 = GraphSONUtility.vertexFromJson((JSONObject) JSONValue.parse(vertexJson2), factory, GraphSONMode.NORMAL, null); - Edge e = GraphSONUtility.edgeFromJson((JSONObject) JSONValue.parse(edgeJson), v1, v2, factory, GraphSONMode.NORMAL, null); + Vertex v1 = GraphsonUtil.vertexFromJson((JSONObject) JSONValue.parse(vertexJson1), factory, GraphSONMode.NORMAL, null); + Vertex v2 = GraphsonUtil.vertexFromJson((JSONObject) JSONValue.parse(vertexJson2), factory, GraphSONMode.NORMAL, null); + Edge e = GraphsonUtil.edgeFromJson((JSONObject) JSONValue.parse(edgeJson), v1, v2, factory, GraphSONMode.NORMAL, null); Assert.assertSame(v1, g.getVertex(1)); Assert.assertSame(v2, g.getVertex(2)); @@ -1041,11 +1036,11 @@ public class GraphSONUtilityTest { @Test public void edgeFromJsonStringValid() throws Exception { Graph g = new TinkerGraph(); - ElementFactory factory = new GraphElementFactory(g); + ElementFactory factory = new ElementFactory(g); - Vertex v1 = GraphSONUtility.vertexFromJson(vertexJson1, factory, GraphSONMode.NORMAL, null); - Vertex v2 = GraphSONUtility.vertexFromJson(vertexJson2, factory, GraphSONMode.NORMAL, null); - Edge e = GraphSONUtility.edgeFromJson(edgeJson, v1, v2, factory, GraphSONMode.NORMAL, null); + Vertex v1 = GraphsonUtil.vertexFromJson(vertexJson1, factory, GraphSONMode.NORMAL, null); + Vertex v2 = GraphsonUtil.vertexFromJson(vertexJson2, factory, GraphSONMode.NORMAL, null); + Edge e = GraphsonUtil.edgeFromJson(edgeJson, v1, v2, factory, GraphSONMode.NORMAL, null); Assert.assertSame(v1, g.getVertex(1)); Assert.assertSame(v2, g.getVertex(2)); @@ -1062,15 +1057,15 @@ public class GraphSONUtilityTest { @Test public void edgeFromJsonIgnoreWeightValid() throws Exception { Graph g = new TinkerGraph(); - ElementFactory factory = new GraphElementFactory(g); + ElementFactory factory = new ElementFactory(g); - Vertex v1 = GraphSONUtility.vertexFromJson((JSONObject) JSONValue.parse(vertexJson1), factory, GraphSONMode.NORMAL, null); - Vertex v2 = GraphSONUtility.vertexFromJson((JSONObject) JSONValue.parse(vertexJson2), factory, GraphSONMode.NORMAL, null); + Vertex v1 = GraphsonUtil.vertexFromJson((JSONObject) JSONValue.parse(vertexJson1), factory, GraphSONMode.NORMAL, null); + Vertex v2 = GraphsonUtil.vertexFromJson((JSONObject) JSONValue.parse(vertexJson2), factory, GraphSONMode.NORMAL, null); Set<String> ignoreWeight = new HashSet<String>(); ignoreWeight.add("weight"); - ElementPropertyConfig config = ElementPropertyConfig.ExcludeProperties(null, ignoreWeight); - GraphSONUtility utility = new GraphSONUtility(GraphSONMode.NORMAL, factory, config); + ElementPropertyConfig config = ElementPropertyConfig.excludeProperties(null, ignoreWeight); + GraphsonUtil utility = new GraphsonUtil(GraphSONMode.NORMAL, factory, config); Edge e = utility.edgeFromJson((JSONObject) JSONValue.parse(edgeJson), v1, v2); Assert.assertSame(v1, g.getVertex(1)); @@ -1088,11 +1083,11 @@ public class GraphSONUtilityTest { @Test public void edgeFromJsonNormalLabelOrIdOnEdge() throws Exception { Graph g = new TinkerGraph(); - ElementFactory factory = new GraphElementFactory(g); + ElementFactory factory = new ElementFactory(g); - Vertex v1 = GraphSONUtility.vertexFromJson((JSONObject) JSONValue.parse(vertexJson1), factory, GraphSONMode.NORMAL, null); - Vertex v2 = GraphSONUtility.vertexFromJson((JSONObject) JSONValue.parse(vertexJson2), factory, GraphSONMode.NORMAL, null); - Edge e = GraphSONUtility.edgeFromJson((JSONObject) JSONValue.parse(edgeJsonLight), v1, v2, factory, GraphSONMode.NORMAL, null); + Vertex v1 = GraphsonUtil.vertexFromJson((JSONObject) JSONValue.parse(vertexJson1), factory, GraphSONMode.NORMAL, null); + Vertex v2 = GraphsonUtil.vertexFromJson((JSONObject) JSONValue.parse(vertexJson2), factory, GraphSONMode.NORMAL, null); + Edge e = GraphsonUtil.edgeFromJson((JSONObject) JSONValue.parse(edgeJsonLight), v1, v2, factory, GraphSONMode.NORMAL, null); Assert.assertSame(v1, g.getVertex(1)); Assert.assertSame(v2, g.getVertex(2)); @@ -1102,11 +1097,11 @@ public class GraphSONUtilityTest { @Test public void edgeFromJsonInputStreamCompactLabelOrIdOnEdge() throws Exception { Graph g = new TinkerGraph(); - ElementFactory factory = new GraphElementFactory(g); + ElementFactory factory = new ElementFactory(g); - Vertex v1 = GraphSONUtility.vertexFromJson((JSONObject) JSONValue.parse(vertexJson1), factory, GraphSONMode.COMPACT, null); - Vertex v2 = GraphSONUtility.vertexFromJson((JSONObject) JSONValue.parse(vertexJson2), factory, GraphSONMode.COMPACT, null); - Edge e = GraphSONUtility.edgeFromJson(inputStreamEdgeJsonLight, v1, v2, factory, GraphSONMode.COMPACT, null); + Vertex v1 = GraphsonUtil.vertexFromJson((JSONObject) JSONValue.parse(vertexJson1), factory, GraphSONMode.COMPACT, null); + Vertex v2 = GraphsonUtil.vertexFromJson((JSONObject) JSONValue.parse(vertexJson2), factory, GraphSONMode.COMPACT, null); + Edge e = GraphsonUtil.edgeFromJson(inputStreamEdgeJsonLight, v1, v2, factory, GraphSONMode.COMPACT, null); Assert.assertSame(v1, g.getVertex(1)); Assert.assertSame(v2, g.getVertex(2)); @@ -1116,7 +1111,7 @@ public class GraphSONUtilityTest { @Test public void edgeFromJsonInputStreamCompactNoIdOnEdge() throws Exception { Graph g = new TinkerGraph(); - ElementFactory factory = new GraphElementFactory(g); + ElementFactory factory = new ElementFactory(g); Set<String> vertexKeys = new HashSet<String>() {{ add(GraphSONTokens._ID); @@ -1126,7 +1121,7 @@ public class GraphSONUtilityTest { add(GraphSONTokens._IN_V); }}; - GraphSONUtility graphson = new GraphSONUtility(GraphSONMode.COMPACT, factory, vertexKeys, edgeKeys); + GraphsonUtil graphson = new GraphsonUtil(GraphSONMode.COMPACT, factory, vertexKeys, edgeKeys); Vertex v1 = graphson.vertexFromJson((JSONObject) JSONValue.parse(vertexJson1)); Vertex v2 = graphson.vertexFromJson((JSONObject) JSONValue.parse(vertexJson2)); diff --git a/sonar-core/src/test/java/org/sonar/core/graph/GraphSONWriterTest.java b/sonar-core/src/test/java/org/sonar/core/graph/graphson/GraphsonWriterTest.java index f62122a843d..54551df7f1d 100644 --- a/sonar-core/src/test/java/org/sonar/core/graph/GraphSONWriterTest.java +++ b/sonar-core/src/test/java/org/sonar/core/graph/graphson/GraphsonWriterTest.java @@ -17,13 +17,10 @@ * License along with Sonar; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 */ - -package org.sonar.core.graph; +package org.sonar.core.graph.graphson; import com.tinkerpop.blueprints.Graph; import com.tinkerpop.blueprints.impls.tg.TinkerGraphFactory; -import com.tinkerpop.blueprints.util.io.graphson.GraphSONMode; -import com.tinkerpop.blueprints.util.io.graphson.GraphSONTokens; import org.json.simple.JSONArray; import org.json.simple.JSONObject; import org.json.simple.JSONValue; @@ -33,7 +30,7 @@ import java.io.ByteArrayOutputStream; import static org.fest.assertions.Assertions.assertThat; -public class GraphSONWriterTest { +public class GraphsonWriterTest { @Test public void outputGraphNoEmbeddedTypes() throws Exception { @@ -41,8 +38,8 @@ public class GraphSONWriterTest { ByteArrayOutputStream stream = new ByteArrayOutputStream(); - GraphSONWriter writer = new GraphSONWriter(g); - writer.outputGraph(stream, null, null, GraphSONMode.NORMAL); + GraphSONWriter writer = new GraphSONWriter(); + writer.write(g, stream, GraphSONMode.NORMAL); stream.flush(); stream.close(); @@ -74,8 +71,8 @@ public class GraphSONWriterTest { ByteArrayOutputStream stream = new ByteArrayOutputStream(); - GraphSONWriter writer = new GraphSONWriter(g); - writer.outputGraph(stream, null, null, GraphSONMode.EXTENDED); + GraphSONWriter writer = new GraphSONWriter(); + writer.write(g, stream, GraphSONMode.EXTENDED); stream.flush(); stream.close(); @@ -107,8 +104,8 @@ public class GraphSONWriterTest { ByteArrayOutputStream stream = new ByteArrayOutputStream(); - GraphSONWriter writer = new GraphSONWriter(g); - writer.outputGraph(stream, null, null, GraphSONMode.COMPACT); + GraphSONWriter writer = new GraphSONWriter(); + writer.write(g, stream, GraphSONMode.COMPACT); stream.flush(); stream.close(); |