diff options
author | Simon Brandhof <simon.brandhof@gmail.com> | 2013-01-29 17:23:14 +0100 |
---|---|---|
committer | Simon Brandhof <simon.brandhof@gmail.com> | 2013-01-29 17:23:14 +0100 |
commit | c964f2ea5fc57d72d19d7f2bfbedf9fe62c7c33c (patch) | |
tree | fcbcb9961c47105a910ac265d09cf9306ec81a54 | |
parent | 0494af6265e90301ac3087eb5fddd244fa202757 (diff) | |
download | sonarqube-c964f2ea5fc57d72d19d7f2bfbedf9fe62c7c33c.tar.gz sonarqube-c964f2ea5fc57d72d19d7f2bfbedf9fe62c7c33c.zip |
SONAR-2501 fix direction of copied edges in sub-graph
-rw-r--r-- | sonar-core/src/main/java/org/sonar/core/graph/SubGraph.java | 24 | ||||
-rw-r--r-- | sonar-core/src/test/java/org/sonar/core/graph/SubGraphTest.java | 48 |
2 files changed, 46 insertions, 26 deletions
diff --git a/sonar-core/src/main/java/org/sonar/core/graph/SubGraph.java b/sonar-core/src/main/java/org/sonar/core/graph/SubGraph.java index 530b13155ed..39e4bc86a40 100644 --- a/sonar-core/src/main/java/org/sonar/core/graph/SubGraph.java +++ b/sonar-core/src/main/java/org/sonar/core/graph/SubGraph.java @@ -40,31 +40,33 @@ public class SubGraph { private SubGraph() { } + public static Graph extract(Vertex start, Object... edgePath) { + return new SubGraph().process(start, edgePath); + } + private Graph process(Vertex start, Object... edgePath) { + copy(start); browse(start, 0, edgePath); for (Edge edge : edgesToCopy) { - Vertex from = edge.getVertex(Direction.IN); - Vertex to = edge.getVertex(Direction.OUT); + Vertex from = edge.getVertex(Direction.OUT); + Vertex to = edge.getVertex(Direction.IN); Edge copyEdge = sub.addEdge(edge.getId(), sub.getVertex(from.getId()), sub.getVertex(to.getId()), edge.getLabel()); ElementHelper.copyProperties(edge, copyEdge); } return sub; } - public static Graph extract(Vertex start, Object... edgePath) { - return new SubGraph().process(start, edgePath); - } - - private void browse(Vertex vertex, int cursor, Object... edgePath) { - if (vertex != null) { - copy(vertex); + private void browse(Vertex from, int cursor, Object... edgePath) { + if (from != null) { if (cursor < edgePath.length) { String edgeLabel = (String) edgePath[cursor]; Direction edgeDirection = (Direction) edgePath[cursor + 1]; - Iterable<Edge> edges = vertex.getEdges(edgeDirection, edgeLabel); + Iterable<Edge> edges = from.getEdges(edgeDirection, edgeLabel); for (Edge edge : edges) { edgesToCopy.add(edge); - browse(edge.getVertex(edgeDirection.opposite()), cursor + 2, edgePath); + Vertex tail = edge.getVertex(edgeDirection.opposite()); + copy(tail); + browse(tail, cursor + 2, edgePath); } } } diff --git a/sonar-core/src/test/java/org/sonar/core/graph/SubGraphTest.java b/sonar-core/src/test/java/org/sonar/core/graph/SubGraphTest.java index 7c46193fbdc..0f3d2f92e83 100644 --- a/sonar-core/src/test/java/org/sonar/core/graph/SubGraphTest.java +++ b/sonar-core/src/test/java/org/sonar/core/graph/SubGraphTest.java @@ -25,28 +25,37 @@ import com.tinkerpop.blueprints.Graph; import com.tinkerpop.blueprints.Vertex; import com.tinkerpop.blueprints.impls.tg.TinkerGraph; import com.tinkerpop.blueprints.util.GraphHelper; +import org.junit.Before; import org.junit.Test; import static org.fest.assertions.Assertions.assertThat; public class SubGraphTest { - @Test - public void extract_acyclic_graph() { - TinkerGraph graph = new TinkerGraph(); - Vertex a = GraphHelper.addVertex(graph, null, "key", "a"); - Vertex b = GraphHelper.addVertex(graph, null, "key", "b"); - Vertex c = GraphHelper.addVertex(graph, null, "key", "c"); - Vertex d = GraphHelper.addVertex(graph, null, "key", "d"); - Vertex e = GraphHelper.addVertex(graph, null, "key", "e"); - Edge ab = GraphHelper.addEdge(graph, null, a, b, "uses"); - Edge bc = GraphHelper.addEdge(graph, null, b, c, "inherits"); - Edge ad = GraphHelper.addEdge(graph, null, a, d, "uses"); - Edge de = GraphHelper.addEdge(graph, null, d, e, "implements"); + TinkerGraph graph; + Vertex a, b, c, d, e; + Edge ab, bc, ad, de; + + @Before + public void before() { + graph = new TinkerGraph(); + a = GraphHelper.addVertex(graph, null, "key", "a"); + b = GraphHelper.addVertex(graph, null, "key", "b"); + c = GraphHelper.addVertex(graph, null, "key", "c"); + d = GraphHelper.addVertex(graph, null, "key", "d"); + e = GraphHelper.addVertex(graph, null, "key", "e"); + + ab = GraphHelper.addEdge(graph, null, a, b, "uses"); + bc = GraphHelper.addEdge(graph, null, b, c, "inherits"); + ad = GraphHelper.addEdge(graph, null, a, d, "uses"); + de = GraphHelper.addEdge(graph, null, d, e, "implements"); // a -uses-> b -inherits -> c // a -uses-> d -implements-> e + } + @Test + public void should_extract_graph() { Graph sub = SubGraph.extract(a, "uses", Direction.OUT, "implements", Direction.OUT); // a -uses-> b @@ -60,9 +69,18 @@ public class SubGraphTest { assertThat(sub.getEdges()).hasSize(3); assertThat(sub.getEdge(ab.getId()).getLabel()).isEqualTo("uses"); - assertThat(sub.getEdge(ab.getId()).getId()).isEqualTo(ab.getId()); + assertThat(sub.getEdge(ab.getId()).toString()).isEqualTo(ab.toString()); assertThat(sub.getEdge(bc.getId())).isNull(); - assertThat(sub.getEdge(ad.getId()).getId()).isEqualTo(ad.getId()); - assertThat(sub.getEdge(de.getId()).getId()).isEqualTo(de.getId()); + assertThat(sub.getEdge(ad.getId()).toString()).isEqualTo(ad.toString()); + assertThat(sub.getEdge(de.getId()).toString()).isEqualTo(de.toString()); + } + + @Test + public void should_check_edge_direction() { + Graph sub = SubGraph.extract(a, "uses", Direction.IN /* instead of out */, "implements", Direction.OUT); + + assertThat(sub.getVertices()).hasSize(1); + assertThat(sub.getVertex(a.getId())).isNotNull(); + assertThat(sub.getEdges()).isEmpty(); } } |