diff options
author | Simon Brandhof <simon.brandhof@gmail.com> | 2013-01-29 14:00:31 +0100 |
---|---|---|
committer | Simon Brandhof <simon.brandhof@gmail.com> | 2013-01-29 14:00:50 +0100 |
commit | 8d597284b1ccf0024bee5b0b86989d3df49910df (patch) | |
tree | 1bfe3353852053f674316d8767697fa73f7d3b8f | |
parent | 0851ce683c7a5b74cea83a2cee05e021eb8455a4 (diff) | |
download | sonarqube-8d597284b1ccf0024bee5b0b86989d3df49910df.tar.gz sonarqube-8d597284b1ccf0024bee5b0b86989d3df49910df.zip |
SONAR-2501 use Reader/Writer instead of InputStream/OutputStream in Graphson classes
6 files changed, 46 insertions, 64 deletions
diff --git a/sonar-core/src/main/java/org/sonar/core/component/SnapshotPerspectives.java b/sonar-core/src/main/java/org/sonar/core/component/SnapshotPerspectives.java index b226a429d92..fb615caaaf8 100644 --- a/sonar-core/src/main/java/org/sonar/core/component/SnapshotPerspectives.java +++ b/sonar-core/src/main/java/org/sonar/core/component/SnapshotPerspectives.java @@ -29,7 +29,7 @@ import org.sonar.core.graph.jdbc.GraphDto; import javax.annotation.CheckForNull; -import java.io.ByteArrayInputStream; +import java.io.StringReader; import java.util.Map; public class SnapshotPerspectives implements ServerComponent { @@ -75,7 +75,7 @@ public class SnapshotPerspectives implements ServerComponent { } private SnapshotGraph read(String data, String rootVertexId) { - ByteArrayInputStream input = new ByteArrayInputStream(data.getBytes()); + StringReader input = new StringReader(data); try { TinkerGraph graph = new TinkerGraph(); new GraphsonReader().read(input, graph); 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 4c1e178bed8..b1e5a52e5ab 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 @@ -24,20 +24,16 @@ 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; +import java.io.StringWriter; public class GraphWriter { public String write(Graph graph) { - ByteArrayOutputStream output = new ByteArrayOutputStream(); + StringWriter output = new StringWriter(); try { new GraphsonWriter().write(graph, output, GraphsonMode.COMPACT); - output.flush(); - output.close(); - return new String(output.toByteArray()); - } catch (IOException e) { - throw new IllegalStateException("Fail to export graph to JSON", e); + return output.toString(); } finally { IOUtils.closeQuietly(output); } 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 index 7d0912279a3..d1540295285 100644 --- 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 @@ -28,6 +28,7 @@ import org.json.simple.parser.JSONParser; import java.io.InputStream; import java.io.InputStreamReader; +import java.io.Reader; import java.util.Set; /** @@ -35,7 +36,7 @@ import java.util.Set; */ public class GraphsonReader { - public Graph read(InputStream jsonInput, Graph toGraph) { + public Graph read(Reader jsonInput, Graph toGraph) { return read(jsonInput, toGraph, 1000, null, null); } @@ -44,13 +45,13 @@ public class GraphsonReader { * 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 input 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) { + public Graph read(Reader input, Graph toGraph, int bufferSize, Set<String> edgePropertyKeys, Set<String> vertexPropertyKeys) { try { JSONParser parser = new JSONParser(); - JSONObject json = (JSONObject) parser.parse(new InputStreamReader(jsonInput)); + JSONObject json = (JSONObject) parser.parse(input); // if this is a transactional graph then we're buffering final BatchGraph batchGraph = BatchGraph.wrap(toGraph, bufferSize); 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 index becdf3443fe..db130def47f 100644 --- 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 @@ -27,30 +27,25 @@ import org.json.simple.JSONObject; import javax.annotation.Nullable; -import java.io.OutputStream; +import java.io.Writer; 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); + public void write(Graph graph, Writer output, GraphsonMode mode) { + write(graph, output, 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 output the JSON Writer 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) { + public void write(Graph graph, Writer output, GraphsonMode mode, @Nullable Set<String> vertexPropertyKeys, @Nullable Set<String> edgePropertyKeys) { try { JSONObject root = new JSONObject(); GraphsonUtil graphson = new GraphsonUtil(mode, null, vertexPropertyKeys, edgePropertyKeys); @@ -69,7 +64,7 @@ public class GraphsonWriter { } root.put(GraphsonTokens.EDGES, edgesArray); - jsonOutputStream.write(root.toString().getBytes("UTF-8")); + output.write(root.toString()); } catch (Exception e) { throw new GraphsonException("Fail to generate GraphSON", e); } diff --git a/sonar-core/src/test/java/org/sonar/core/graph/graphson/GraphsonReaderTest.java b/sonar-core/src/test/java/org/sonar/core/graph/graphson/GraphsonReaderTest.java index 74713e288e4..fbd0c437c9e 100644 --- a/sonar-core/src/test/java/org/sonar/core/graph/graphson/GraphsonReaderTest.java +++ b/sonar-core/src/test/java/org/sonar/core/graph/graphson/GraphsonReaderTest.java @@ -19,6 +19,7 @@ */ package org.sonar.core.graph.graphson; +import com.google.common.collect.Sets; import com.tinkerpop.blueprints.Direction; import com.tinkerpop.blueprints.Edge; import com.tinkerpop.blueprints.Vertex; @@ -27,9 +28,8 @@ import com.tinkerpop.blueprints.impls.tg.TinkerGraphFactory; import org.junit.Assert; import org.junit.Test; -import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; -import java.io.InputStream; +import java.io.StringReader; +import java.io.StringWriter; import java.util.HashSet; import java.util.Iterator; import java.util.List; @@ -44,10 +44,8 @@ public class GraphsonReaderTest { String json = "{ \"mode\":\"EXTENDED\", \"vertices\": [ {\"_id\":1, \"_type\":\"vertex\", \"test\": { \"type\":\"string\", \"value\":\"please work\"}, \"testlist\":{\"type\":\"list\", \"value\":[{\"type\":\"int\", \"value\":1}, {\"type\":\"int\",\"value\":2}, {\"type\":\"int\",\"value\":3}, {\"type\":\"unknown\",\"value\":null}]}, \"testmap\":{\"type\":\"map\", \"value\":{\"big\":{\"type\":\"long\", \"value\":10000000000}, \"small\":{\"type\":\"double\", \"value\":0.4954959595959}, \"nullKey\":{\"type\":\"unknown\", \"value\":null}}}}, {\"_id\":2, \"_type\":\"vertex\", \"testagain\":{\"type\":\"string\", \"value\":\"please work again\"}}], \"edges\":[{\"_id\":100, \"_type\":\"edge\", \"_outV\":1, \"_inV\":2, \"_label\":\"works\", \"teste\": {\"type\":\"string\", \"value\":\"please worke\"}, \"keyNull\":{\"type\":\"unknown\", \"value\":null}}]}"; - byte[] bytes = json.getBytes(); - InputStream inputStream = new ByteArrayInputStream(bytes); - - new GraphsonReader().read(inputStream, graph); + StringReader input = new StringReader(json); + new GraphsonReader().read(input, graph); Assert.assertEquals(2, getIterableCount(graph.getVertices())); Assert.assertEquals(1, getIterableCount(graph.getEdges())); @@ -95,10 +93,8 @@ public class GraphsonReaderTest { String json = "{ \"mode\":\"NORMAL\",\"vertices\": [ {\"_id\":1, \"_type\":\"vertex\", \"test\": \"please work\", \"testlist\":[1, 2, 3, null], \"testmap\":{\"big\":10000000000, \"small\":0.4954959595959, \"nullKey\":null}}, {\"_id\":2, \"_type\":\"vertex\", \"testagain\":\"please work again\"}], \"edges\":[{\"_id\":100, \"_type\":\"edge\", \"_outV\":1, \"_inV\":2, \"_label\":\"works\", \"teste\": \"please worke\", \"keyNull\":null}]}"; - byte[] bytes = json.getBytes(); - InputStream inputStream = new ByteArrayInputStream(bytes); - - new GraphsonReader().read(inputStream, graph); + StringReader input = new StringReader(json); + new GraphsonReader().read(input, graph); Assert.assertEquals(2, getIterableCount(graph.getVertices())); Assert.assertEquals(1, getIterableCount(graph.getEdges())); @@ -146,10 +142,8 @@ public class GraphsonReaderTest { String json = "{ \"mode\":\"COMPACT\",\"vertices\": [ {\"_id\":1, \"test\": \"please work\", \"testlist\":[1, 2, 3, null], \"testmap\":{\"big\":10000000000, \"small\":0.4954959595959, \"nullKey\":null}}, {\"_id\":2, \"testagain\":\"please work again\"}], \"edges\":[{\"_id\":100, \"_outV\":1, \"_inV\":2, \"_label\":\"works\", \"teste\": \"please worke\", \"keyNull\":null}]}"; - byte[] bytes = json.getBytes(); - InputStream inputStream = new ByteArrayInputStream(bytes); - - new GraphsonReader().read(inputStream, graph); + StringReader input = new StringReader(json); + new GraphsonReader().read(input, graph); Assert.assertEquals(2, getIterableCount(graph.getVertices())); Assert.assertEquals(1, getIterableCount(graph.getEdges())); @@ -195,7 +189,7 @@ public class GraphsonReaderTest { public void inputGraphExtendedFullCycle() throws Exception { TinkerGraph graph = TinkerGraphFactory.createTinkerGraph(); - ByteArrayOutputStream stream = new ByteArrayOutputStream(); + StringWriter stream = new StringWriter(); GraphsonWriter writer = new GraphsonWriter(); writer.write(graph, stream, GraphsonMode.EXTENDED); @@ -203,13 +197,12 @@ public class GraphsonReaderTest { stream.flush(); stream.close(); - String jsonString = new String(stream.toByteArray()); + String jsonString = stream.toString(); - byte[] bytes = jsonString.getBytes(); - InputStream inputStream = new ByteArrayInputStream(bytes); + StringReader input = new StringReader(jsonString); TinkerGraph emptyGraph = new TinkerGraph(); - new GraphsonReader().read(inputStream, emptyGraph); + new GraphsonReader().read(input, emptyGraph); Assert.assertEquals(6, getIterableCount(emptyGraph.getVertices())); Assert.assertEquals(6, getIterableCount(emptyGraph.getEdges())); @@ -240,7 +233,7 @@ public class GraphsonReaderTest { public void inputGraphCompactFullCycle() throws Exception { TinkerGraph graph = TinkerGraphFactory.createTinkerGraph(); - ByteArrayOutputStream stream = new ByteArrayOutputStream(); + StringWriter stream = new StringWriter(); Set<String> edgeKeys = new HashSet<String>(); edgeKeys.add(GraphsonTokens._ID); @@ -248,7 +241,7 @@ public class GraphsonReaderTest { edgeKeys.add(GraphsonTokens._OUT_V); edgeKeys.add(GraphsonTokens._LABEL); - Set<String> vertexKeys = new HashSet<String>(); + Set<String> vertexKeys = Sets.newHashSet(); vertexKeys.add(GraphsonTokens._ID); GraphsonWriter writer = new GraphsonWriter(); @@ -257,13 +250,11 @@ public class GraphsonReaderTest { stream.flush(); stream.close(); - String jsonString = new String(stream.toByteArray()); - - byte[] bytes = jsonString.getBytes(); - InputStream inputStream = new ByteArrayInputStream(bytes); + String jsonString = stream.toString(); + StringReader input = new StringReader(jsonString); TinkerGraph emptyGraph = new TinkerGraph(); - new GraphsonReader().read(inputStream, emptyGraph); + new GraphsonReader().read(input, emptyGraph); Assert.assertEquals(6, getIterableCount(emptyGraph.getVertices())); Assert.assertEquals(6, getIterableCount(emptyGraph.getEdges())); @@ -300,14 +291,14 @@ public class GraphsonReaderTest { public void inputGraphCompactFullCycleBroken() throws Exception { TinkerGraph graph = TinkerGraphFactory.createTinkerGraph(); - ByteArrayOutputStream stream = new ByteArrayOutputStream(); + StringWriter stream = new StringWriter(); Set<String> edgeKeys = new HashSet<String>(); edgeKeys.add(GraphsonTokens._IN_V); edgeKeys.add(GraphsonTokens._OUT_V); edgeKeys.add(GraphsonTokens._LABEL); - Set<String> vertexKeys = new HashSet<String>(); + Set<String> vertexKeys = Sets.newHashSet(); GraphsonWriter writer = new GraphsonWriter(); writer.write(graph, stream, GraphsonMode.COMPACT, vertexKeys, edgeKeys); @@ -315,13 +306,11 @@ public class GraphsonReaderTest { stream.flush(); stream.close(); - String jsonString = new String(stream.toByteArray()); - - byte[] bytes = jsonString.getBytes(); - InputStream inputStream = new ByteArrayInputStream(bytes); + String jsonString = writer.toString(); + StringReader input = new StringReader(jsonString); TinkerGraph emptyGraph = new TinkerGraph(); - new GraphsonReader().read(inputStream, emptyGraph); + new GraphsonReader().read(input, emptyGraph); } diff --git a/sonar-core/src/test/java/org/sonar/core/graph/graphson/GraphsonWriterTest.java b/sonar-core/src/test/java/org/sonar/core/graph/graphson/GraphsonWriterTest.java index edc716badea..ff6cf8a4b83 100644 --- a/sonar-core/src/test/java/org/sonar/core/graph/graphson/GraphsonWriterTest.java +++ b/sonar-core/src/test/java/org/sonar/core/graph/graphson/GraphsonWriterTest.java @@ -27,6 +27,7 @@ import org.json.simple.JSONValue; import org.junit.Test; import java.io.ByteArrayOutputStream; +import java.io.StringWriter; import static org.fest.assertions.Assertions.assertThat; @@ -36,7 +37,7 @@ public class GraphsonWriterTest { public void outputGraphNoEmbeddedTypes() throws Exception { Graph g = TinkerGraphFactory.createTinkerGraph(); - ByteArrayOutputStream stream = new ByteArrayOutputStream(); + StringWriter stream = new StringWriter(); GraphsonWriter writer = new GraphsonWriter(); writer.write(g, stream, GraphsonMode.NORMAL); @@ -44,7 +45,7 @@ public class GraphsonWriterTest { stream.flush(); stream.close(); - String jsonString = new String(stream.toByteArray()); + String jsonString = stream.toString(); JSONObject rootNode = (JSONObject) JSONValue.parse(jsonString); @@ -69,7 +70,7 @@ public class GraphsonWriterTest { public void outputGraphWithEmbeddedTypes() throws Exception { Graph g = TinkerGraphFactory.createTinkerGraph(); - ByteArrayOutputStream stream = new ByteArrayOutputStream(); + StringWriter stream = new StringWriter(); GraphsonWriter writer = new GraphsonWriter(); writer.write(g, stream, GraphsonMode.EXTENDED); @@ -77,7 +78,7 @@ public class GraphsonWriterTest { stream.flush(); stream.close(); - String jsonString = new String(stream.toByteArray()); + String jsonString = stream.toString(); JSONObject rootNode = (JSONObject) JSONValue.parse(jsonString); @@ -102,7 +103,7 @@ public class GraphsonWriterTest { public void outputGraphWithCompact() throws Exception { Graph g = TinkerGraphFactory.createTinkerGraph(); - ByteArrayOutputStream stream = new ByteArrayOutputStream(); + StringWriter stream = new StringWriter(); GraphsonWriter writer = new GraphsonWriter(); writer.write(g, stream, GraphsonMode.COMPACT); @@ -110,7 +111,7 @@ public class GraphsonWriterTest { stream.flush(); stream.close(); - String jsonString = new String(stream.toByteArray()); + String jsonString = stream.toString(); JSONObject rootNode = (JSONObject) JSONValue.parse(jsonString); |