]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-2501 fix direction of copied edges in sub-graph
authorSimon Brandhof <simon.brandhof@gmail.com>
Tue, 29 Jan 2013 16:23:14 +0000 (17:23 +0100)
committerSimon Brandhof <simon.brandhof@gmail.com>
Tue, 29 Jan 2013 16:23:14 +0000 (17:23 +0100)
sonar-core/src/main/java/org/sonar/core/graph/SubGraph.java
sonar-core/src/test/java/org/sonar/core/graph/SubGraphTest.java

index 530b13155ed62d62420ef3cbfdd0656deaf3c8a0..39e4bc86a40e0bb0e3a848aee9d399104d6e1104 100644 (file)
@@ -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);
         }
       }
     }
index 7c46193fbdc919feb78cdafb3332c769511a5fe4..0f3d2f92e83c0167183355adacbb6c6d96a74422 100644 (file)
@@ -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();
   }
 }