]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-2501 code refactoring
authorSimon Brandhof <simon.brandhof@gmail.com>
Thu, 31 Jan 2013 11:56:15 +0000 (12:56 +0100)
committerSimon Brandhof <simon.brandhof@gmail.com>
Thu, 31 Jan 2013 11:56:15 +0000 (12:56 +0100)
14 files changed:
sonar-core/src/main/java/org/sonar/core/component/PerspectiveBuilder.java
sonar-core/src/main/java/org/sonar/core/component/ScanGraphStore.java
sonar-core/src/main/java/org/sonar/core/graph/EdgePath.java [new file with mode: 0644]
sonar-core/src/main/java/org/sonar/core/graph/GraphWriter.java [deleted file]
sonar-core/src/main/java/org/sonar/core/graph/SubGraph.java
sonar-core/src/main/java/org/sonar/core/test/TestPlanBuilder.java
sonar-core/src/main/java/org/sonar/core/test/TestableBuilder.java
sonar-core/src/test/java/org/sonar/core/graph/BeanGraphTest.java
sonar-core/src/test/java/org/sonar/core/graph/EdgePathTest.java [new file with mode: 0644]
sonar-core/src/test/java/org/sonar/core/graph/SubGraphTest.java
sonar-core/src/test/java/org/sonar/core/test/DefaultTestCaseTest.java
sonar-core/src/test/java/org/sonar/core/test/DefaultTestPlanTest.java
sonar-core/src/test/java/org/sonar/core/test/DefaultTestableTest.java
sonar-core/src/test/java/org/sonar/core/test/TestableBuilderTest.java [new file with mode: 0644]

index d74ef70f99815a7b84311f54810e00f9e131a4ed..0a66905e0775a91055c815176d8eb5c3e8786a9f 100644 (file)
@@ -22,6 +22,7 @@ package org.sonar.core.component;
 import org.sonar.api.BatchComponent;
 import org.sonar.api.ServerComponent;
 import org.sonar.api.component.Perspective;
+import org.sonar.core.graph.EdgePath;
 
 import javax.annotation.CheckForNull;
 
@@ -48,6 +49,6 @@ public abstract class PerspectiveBuilder<T extends Perspective> implements Batch
 
   public abstract T create(ComponentVertex component);
 
-  public abstract Object[] storagePath();
+  public abstract EdgePath path();
 
 }
index eb91efe58342c451162bc1b3767755690564a727..3d88849a6bdcddd0065f03d88d2887993a04c3b1 100644 (file)
 package org.sonar.core.component;
 
 import com.tinkerpop.blueprints.Graph;
+import org.apache.commons.io.IOUtils;
 import org.slf4j.LoggerFactory;
 import org.sonar.api.component.Perspective;
-import org.sonar.core.graph.GraphWriter;
 import org.sonar.core.graph.SubGraph;
+import org.sonar.core.graph.graphson.GraphsonMode;
+import org.sonar.core.graph.graphson.GraphsonWriter;
 import org.sonar.core.graph.jdbc.GraphDto;
 import org.sonar.core.graph.jdbc.GraphDtoMapper;
 import org.sonar.core.persistence.BatchSession;
 import org.sonar.core.persistence.MyBatis;
 
+import java.io.StringWriter;
+
 public class ScanGraphStore {
   private final MyBatis myBatis;
   private final ScanGraph projectGraph;
@@ -45,15 +49,14 @@ public class ScanGraphStore {
     BatchSession session = myBatis.openBatchSession();
     GraphDtoMapper mapper = session.getMapper(GraphDtoMapper.class);
     try {
-      GraphWriter writer = new GraphWriter();
       for (ComponentVertex component : projectGraph.getComponents()) {
         Long snapshotId = (Long) component.element().getProperty("sid");
         if (snapshotId != null) {
           for (PerspectiveBuilder builder : builders) {
             Perspective perspective = builder.load(component);
             if (perspective != null) {
-              Graph subGraph = SubGraph.extract(component.element(), builder.storagePath());
-              String data = writer.write(subGraph);
+              Graph subGraph = SubGraph.extract(component.element(), builder.path());
+              String data = write(subGraph);
               mapper.insert(new GraphDto()
                 .setData(data)
                 .setFormat("graphson")
@@ -72,4 +75,14 @@ public class ScanGraphStore {
       session.close();
     }
   }
+
+  private String write(Graph graph) {
+    StringWriter output = new StringWriter();
+    try {
+      new GraphsonWriter().write(graph, output, GraphsonMode.EXTENDED);
+      return output.toString();
+    } finally {
+      IOUtils.closeQuietly(output);
+    }
+  }
 }
diff --git a/sonar-core/src/main/java/org/sonar/core/graph/EdgePath.java b/sonar-core/src/main/java/org/sonar/core/graph/EdgePath.java
new file mode 100644 (file)
index 0000000..00c856b
--- /dev/null
@@ -0,0 +1,55 @@
+/*
+ * 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.google.common.base.Preconditions;
+import com.google.common.collect.ImmutableList;
+import com.tinkerpop.blueprints.Direction;
+
+import java.util.List;
+
+public class EdgePath {
+  private List<Object> elements;
+
+  private EdgePath(Object[] elements) {
+    Preconditions.checkArgument(elements != null && elements.length > 0, "Elements can't be null or empty");
+    Preconditions.checkArgument(elements.length % 2 == 0, "Odd number of elements (" + elements.length + ")");
+
+    for (int i = 0; i < elements.length; i++) {
+      if (i % 2 == 0) {
+        Preconditions.checkArgument(elements[i] instanceof Direction,
+          "Element " + i + " must be a " + Direction.class.getName() + " (got " + elements[i].getClass().getName() + ")");
+      } else {
+        Preconditions.checkArgument(elements[i] instanceof String,
+          "Element " + i + " must be a String" + " (got " + elements[i].getClass().getName() + ")");
+      }
+    }
+
+    this.elements = ImmutableList.copyOf(elements);
+  }
+
+  public List<Object> getElements() {
+    return elements;
+  }
+
+  public static EdgePath create(Object... elements) {
+    return new EdgePath(elements);
+  }
+}
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
deleted file mode 100644 (file)
index abc4dde..0000000
+++ /dev/null
@@ -1,44 +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 org.apache.commons.io.IOUtils;
-import org.sonar.core.graph.graphson.GraphsonMode;
-import org.sonar.core.graph.graphson.GraphsonWriter;
-
-import java.io.IOException;
-import java.io.StringWriter;
-
-public class GraphWriter {
-
-  public String write(Graph graph) {
-    StringWriter output = new StringWriter();
-    try {
-      new GraphsonWriter().write(graph, output, GraphsonMode.EXTENDED);
-      System.out.println("------------------------------------------------");
-      System.out.println(output.toString());
-      System.out.println("------------------------------------------------");
-      return output.toString();
-    } finally {
-      IOUtils.closeQuietly(output);
-    }
-  }
-}
index 22d3d27c4c9e4c61da64b6d5999ece0c28d315bb..048a59f65c8ab4f8c7a7aa6c5fe122356288b169 100644 (file)
@@ -27,6 +27,7 @@ import com.tinkerpop.blueprints.Vertex;
 import com.tinkerpop.blueprints.impls.tg.TinkerGraph;
 import com.tinkerpop.blueprints.util.ElementHelper;
 
+import java.util.List;
 import java.util.Set;
 
 /**
@@ -40,13 +41,13 @@ public class SubGraph {
   private SubGraph() {
   }
 
-  public static Graph extract(Vertex start, Object... edgePath) {
+  public static Graph extract(Vertex start, EdgePath edgePath) {
     return new SubGraph().process(start, edgePath);
   }
 
-  private Graph process(Vertex start, Object... edgePath) {
+  private Graph process(Vertex start, EdgePath edgePath) {
     copy(start);
-    browse(start, 0, edgePath);
+    browse(start, 0, edgePath.getElements());
     for (Edge edge : edgesToCopy) {
       Vertex from = edge.getVertex(Direction.OUT);
       Vertex to = edge.getVertex(Direction.IN);
@@ -56,11 +57,11 @@ public class SubGraph {
     return sub;
   }
 
-  private void browse(Vertex from, int cursor, Object... edgePath) {
+  private void browse(Vertex from, int cursor, List<Object> edgePath) {
     if (from != null) {
-      if (cursor < edgePath.length) {
-        String edgeLabel = (String) edgePath[cursor];
-        Direction edgeDirection = (Direction) edgePath[cursor + 1];
+      if (cursor < edgePath.size()) {
+        Direction edgeDirection = (Direction) edgePath.get(cursor);
+        String edgeLabel = (String) edgePath.get(cursor + 1);
         Iterable<Edge> edges = from.getEdges(edgeDirection, edgeLabel);
         for (Edge edge : edges) {
           edgesToCopy.add(edge);
index 98e7f44f0bcbe651aaead141e3a51b0948f7690e..3084d6e3891d5ae9017ac5a274f1ac3d7f26c778 100644 (file)
@@ -24,18 +24,19 @@ import com.tinkerpop.blueprints.Vertex;
 import org.sonar.api.test.MutableTestPlan;
 import org.sonar.core.component.ComponentVertex;
 import org.sonar.core.component.PerspectiveBuilder;
+import org.sonar.core.graph.EdgePath;
 import org.sonar.core.graph.GraphUtil;
 
 public class TestPlanBuilder extends PerspectiveBuilder<MutableTestPlan> {
 
   static final String PERSPECTIVE_KEY = "testplan";
 
-  private static final Object[] PATH = new Object[]{
-    "testplan", Direction.OUT,
-    "testcase", Direction.OUT,
-    "covers", Direction.OUT,
-    "testable", Direction.IN
-  };
+  private static final EdgePath PATH = EdgePath.create(
+    Direction.OUT, "testplan",
+    Direction.OUT, "testcase",
+    Direction.OUT, "covers",
+    Direction.IN, "testable"
+  );
 
   public TestPlanBuilder() {
     super(PERSPECTIVE_KEY, MutableTestPlan.class);
@@ -56,7 +57,7 @@ public class TestPlanBuilder extends PerspectiveBuilder<MutableTestPlan> {
   }
 
   @Override
-  public Object[] storagePath() {
+  public EdgePath path() {
     return PATH;
   }
 }
index e19380784f8494bfe061a1d914c9c0e7ca105441..a7a868132135e3fa4ccd7853c7f1d75aef51cc43 100644 (file)
@@ -23,6 +23,7 @@ import com.tinkerpop.blueprints.Direction;
 import com.tinkerpop.blueprints.Vertex;
 import org.sonar.api.test.MutableTestable;
 import org.sonar.core.component.ComponentVertex;
+import org.sonar.core.graph.EdgePath;
 import org.sonar.core.component.PerspectiveBuilder;
 import org.sonar.core.graph.GraphUtil;
 
@@ -30,12 +31,12 @@ public class TestableBuilder extends PerspectiveBuilder<MutableTestable> {
 
   static final String PERSPECTIVE_KEY = "testable";
 
-  private static final Object[] PATH = new Object[]{
-    "testable", Direction.OUT,
-    "covers", Direction.IN,
-    "testcase", Direction.IN,
-    "testplan", Direction.IN
-  };
+  private static final EdgePath PATH = EdgePath.create(
+    Direction.OUT, "testable",
+    Direction.IN,"covers",
+    Direction.IN,"testcase",
+    Direction.IN,"testplan"
+  );
 
   public TestableBuilder() {
     super(PERSPECTIVE_KEY, MutableTestable.class);
@@ -56,7 +57,7 @@ public class TestableBuilder extends PerspectiveBuilder<MutableTestable> {
   }
 
   @Override
-  public Object[] storagePath() {
+  public EdgePath path() {
     return PATH;
   }
 }
index 23bf7d657a7ea9e5d8ee1abd62bd4c3d741af8f0..0181d81e111536afc347f63728334ba80813a246 100644 (file)
@@ -38,8 +38,7 @@ public class BeanGraphTest {
 
   @Test
   public void should_create_bean_vertex() {
-    TinkerGraph graph = new TinkerGraph();
-    BeanGraph beanGraph = new BeanGraph(graph);
+    BeanGraph beanGraph = BeanGraph.createInMemory();
     Person person = beanGraph.createVertex(Person.class);
 
     assertThat(person).isNotNull();
@@ -64,8 +63,7 @@ public class BeanGraphTest {
 
   @Test
   public void should_create_adjacent_bean_vertex() {
-    TinkerGraph graph = new TinkerGraph();
-    BeanGraph beanGraph = new BeanGraph(graph);
+    BeanGraph beanGraph = BeanGraph.createInMemory();
     Person person = beanGraph.createVertex(Person.class);
 
     Person adjacent = beanGraph.createAdjacentVertex(person, Person.class, "knows", "type", "family");
diff --git a/sonar-core/src/test/java/org/sonar/core/graph/EdgePathTest.java b/sonar-core/src/test/java/org/sonar/core/graph/EdgePathTest.java
new file mode 100644 (file)
index 0000000..f4251e1
--- /dev/null
@@ -0,0 +1,68 @@
+/*
+ * 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.Direction;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.ExpectedException;
+
+import static org.fest.assertions.Assertions.assertThat;
+
+public class EdgePathTest {
+
+  @Rule
+  public ExpectedException thrown = ExpectedException.none();
+
+  @Test
+  public void create_valid_edge_path() {
+    EdgePath path = EdgePath.create(Direction.OUT, "knows", Direction.OUT, "has");
+
+    assertThat(path).isNotNull();
+    assertThat(path.getElements()).hasSize(4);
+    assertThat(path.getElements().get(0)).isEqualTo(Direction.OUT);
+    assertThat(path.getElements().get(1)).isEqualTo("knows");
+    assertThat(path.getElements().get(2)).isEqualTo(Direction.OUT);
+    assertThat(path.getElements().get(3)).isEqualTo("has");
+  }
+
+  @Test
+  public void should_have_even_number_of_elements() {
+    thrown.expect(IllegalArgumentException.class);
+    thrown.expectMessage("Odd number of elements (3)");
+    EdgePath.create(Direction.OUT, "knows", Direction.OUT);
+  }
+
+  @Test
+  public void should_have_sequence_of_directions_and_strings_1() {
+    thrown.expect(IllegalArgumentException.class);
+    thrown.expectMessage("Element 0 must be a com.tinkerpop.blueprints.Direction (got java.lang.String)");
+
+    EdgePath.create("knows", Direction.OUT);
+  }
+
+  @Test
+  public void should_have_sequence_of_directions_and_strings_2() {
+    thrown.expect(IllegalArgumentException.class);
+    thrown.expectMessage("Element 1 must be a String (got com.tinkerpop.blueprints.Direction)");
+
+    EdgePath.create(Direction.OUT, Direction.OUT);
+  }
+}
index d34d9ff087e91790819c8e57eb25426c986779e4..e65e4d1aa5114c77242f32c296b36f636fa50d6d 100644 (file)
@@ -48,7 +48,7 @@ public class SubGraphTest {
     // a -uses-> b -inherits -> c
     // a -uses-> d -implements-> e
 
-    Graph sub = SubGraph.extract(a, "uses", Direction.OUT, "implements", Direction.OUT);
+    Graph sub = SubGraph.extract(a, EdgePath.create(Direction.OUT, "uses", Direction.OUT, "implements"));
 
     // a -uses-> b
     // a -uses-> d -implements-> e
@@ -85,7 +85,7 @@ public class SubGraphTest {
     // a -uses-> b -implements-> c -package-> e
     // a -uses-> d -implements-> c -package-> e
 
-    Graph sub = SubGraph.extract(a, "uses", Direction.OUT, "implements", Direction.OUT, "package", Direction.OUT);
+    Graph sub = SubGraph.extract(a, EdgePath.create(Direction.OUT, "uses", Direction.OUT, "implements", Direction.OUT, "package"));
 
     // same graph
     assertThat(sub.getVertices()).hasSize(5);
@@ -109,7 +109,7 @@ public class SubGraphTest {
     // a -uses-> b -inherits -> c
     // a -uses-> d -implements-> e
 
-    Graph sub = SubGraph.extract(a, "uses", Direction.IN /* instead of out */, "implements", Direction.OUT);
+    Graph sub = SubGraph.extract(a, EdgePath.create(Direction.IN /* instead of out */, "uses", Direction.OUT, "implements"));
 
     assertThat(sub.getVertices()).hasSize(1);
     assertThat(sub.getVertex(a.getId())).isNotNull();
index cceb46414cdcb88d6c4b63d05a7ab2b97b20ad7b..d6bf3168e10d343734521f410b67c52a76ee0630 100644 (file)
  */
 package org.sonar.core.test;
 
+import com.google.common.collect.Iterables;
 import org.junit.Rule;
 import org.junit.Test;
 import org.junit.rules.ExpectedException;
-import org.sonar.api.test.exception.IllegalDurationException;
+import org.sonar.api.test.Cover;
 import org.sonar.api.test.TestCase;
+import org.sonar.api.test.exception.IllegalDurationException;
 import org.sonar.core.graph.BeanGraph;
 
 import java.util.Arrays;
@@ -46,7 +48,24 @@ public class DefaultTestCaseTest {
   }
 
   @Test
-  public void should_cover_testables() {
+  public void should_cover_testable() {
+    BeanGraph beanGraph = BeanGraph.createInMemory();
+    DefaultTestable testable = beanGraph.createVertex(DefaultTestable.class);
+    DefaultTestCase testCase = beanGraph.createVertex(DefaultTestCase.class);
+    testCase.setCover(testable, Arrays.asList(10, 11, 12));
+
+    assertThat(testCase.doesCover()).isTrue();
+    assertThat(testCase.countCoveredLines()).isEqualTo(3);
+    assertThat(testCase.covers()).hasSize(1);
+
+    Cover cover = Iterables.<Cover>getFirst(testCase.covers(), null);
+    assertThat(cover.testCase()).isEqualTo(testCase);
+    assertThat(cover.testable()).isSameAs(testable);
+    assertThat(cover.lines()).containsExactly(10, 11, 12);
+  }
+
+  @Test
+  public void should_cover_multiple_testables() {
     BeanGraph beanGraph = BeanGraph.createInMemory();
     DefaultTestable testable1 = beanGraph.createVertex(DefaultTestable.class);
     DefaultTestable testable2 = beanGraph.createVertex(DefaultTestable.class);
index 63e623aa7517d96611da2c62d8bbc4ee5912b91a..32a3c32157e482f1f198d63f7151cb197d0e5a82 100644 (file)
@@ -52,8 +52,13 @@ public class DefaultTestPlanTest {
     plan.addTestCase("T2");
 
     assertThat(plan.testCases()).hasSize(2);
-    assertThat(Iterables.<MutableTestCase>get(plan.testCases(), 0).key()).isEqualTo("T1");
-    assertThat(Iterables.<MutableTestCase>get(plan.testCases(), 1).key()).isEqualTo("T2");
+    MutableTestCase firstTestCase = Iterables.get(plan.testCases(), 0);
+    assertThat(firstTestCase.key()).isEqualTo("T1");
+    assertThat(firstTestCase.testPlan()).isSameAs(plan);
+
+    MutableTestCase secondTestCase = Iterables.get(plan.testCases(), 1);
+    assertThat(secondTestCase.key()).isEqualTo("T2");
+    assertThat(secondTestCase.testPlan()).isSameAs(plan);
   }
 
   @Test
index 65a240e973a258cdf4d0ceba8e1c2e98f71094f4..962902be5b7c80d103d402141fb3ff02160ecff3 100644 (file)
@@ -46,6 +46,9 @@ public class DefaultTestableTest {
     testCase2.setCover(testable, Arrays.asList(12, 48, 49));
 
     assertThat(testable.testedLines()).containsOnly(10, 11, 12, 48, 49);
+    assertThat(testable.countTestCasesOfLine(2)).isEqualTo(0);
+    assertThat(testable.countTestCasesOfLine(10)).isEqualTo(1);
+    assertThat(testable.countTestCasesOfLine(12)).isEqualTo(2);
   }
 
   @Test
diff --git a/sonar-core/src/test/java/org/sonar/core/test/TestableBuilderTest.java b/sonar-core/src/test/java/org/sonar/core/test/TestableBuilderTest.java
new file mode 100644 (file)
index 0000000..a78553e
--- /dev/null
@@ -0,0 +1,33 @@
+/*
+ * 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.test;
+
+import org.junit.Test;
+
+import static org.fest.assertions.Assertions.assertThat;
+
+public class TestableBuilderTest {
+  @Test
+  public void storagePath() {
+    TestableBuilder builder = new TestableBuilder();
+
+    assertThat(builder.path().getElements()).isNotEmpty();
+  }
+}