]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-4087 add unit test for MyBatis mapper
authorSimon Brandhof <simon.brandhof@gmail.com>
Fri, 25 Jan 2013 13:40:52 +0000 (14:40 +0100)
committerSimon Brandhof <simon.brandhof@gmail.com>
Fri, 25 Jan 2013 13:40:52 +0000 (14:40 +0100)
sonar-core/src/main/java/org/sonar/core/graph/GraphDto.java
sonar-core/src/main/resources/org/sonar/core/graph/GraphDtoMapper.xml
sonar-core/src/test/java/org/sonar/core/component/GraphUtilTest.java [deleted file]
sonar-core/src/test/java/org/sonar/core/graph/GraphDbTest.java [new file with mode: 0644]
sonar-core/src/test/java/org/sonar/core/graph/GraphUtilTest.java [new file with mode: 0644]
sonar-core/src/test/resources/org/sonar/core/graph/GraphDbTest/shared.xml [new file with mode: 0644]

index a8e1633327617a848be50d10267539cc9663af1c..7f06ece92a814a4811dbbdb580fde464599c2539 100644 (file)
@@ -20,6 +20,7 @@
 package org.sonar.core.graph;
 
 public class GraphDto {
+  private long id;
   private long snapshotId;
   private String format;
   private String perspective;
@@ -27,6 +28,15 @@ public class GraphDto {
   private String rootVertexId;
   private String data;
 
+  public long getId() {
+    return id;
+  }
+
+  public GraphDto setId(long id) {
+    this.id = id;
+    return this;
+  }
+
   public long getSnapshotId() {
     return snapshotId;
   }
index 3da0473498caafe31947c3bdf170d11b1d0e3454..d897ec76b067fad067a8d47f30ce1c3b44ae7e1e 100644 (file)
@@ -4,13 +4,13 @@
 <mapper namespace="org.sonar.core.graph.GraphDtoMapper">
 
   <select id="selectBySnapshot" parameterType="map" resultType="Graph">
-    SELECT snapshot_id as snapshotId, format, version, perspective, data
+    SELECT id, snapshot_id as snapshotId, format, version, perspective, root_vertex_id as rootVertexId, data
     FROM graphs
     WHERE snapshot_id = #{sid} AND perspective = #{perspective}
   </select>
 
   <select id="selectByComponent" parameterType="map" resultType="Graph">
-    SELECT g.snapshot_id as snapshotId, g.format, g.version, g.perspective, g.data
+    SELECT g.id, g.snapshot_id as snapshotId, g.format, g.version, g.perspective, g.root_vertex_id as rootVertexId, g.data
     FROM graphs g, snapshots s
     WHERE g.perspective = #{perspective} AND g.snapshot_id=s.id AND s.islast=${_true} and s.project_id=(
     select id from projects where enabled=${_true} and kee=#{key} and person_id is null and copy_resource_id is null
diff --git a/sonar-core/src/test/java/org/sonar/core/component/GraphUtilTest.java b/sonar-core/src/test/java/org/sonar/core/component/GraphUtilTest.java
deleted file mode 100644 (file)
index 82a2fbd..0000000
+++ /dev/null
@@ -1,47 +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.component;
-
-import com.tinkerpop.blueprints.Vertex;
-import com.tinkerpop.blueprints.impls.tg.TinkerGraph;
-import com.tinkerpop.gremlin.java.GremlinPipeline;
-import org.junit.Test;
-import org.sonar.core.graph.GraphUtil;
-
-import static org.fest.assertions.Assertions.assertThat;
-
-public class GraphUtilTest {
-  @Test
-  public void subGraph() {
-    TinkerGraph graph = new TinkerGraph();
-    Vertex a = graph.addVertex("1");
-    Vertex b = graph.addVertex("2");
-    Vertex c = graph.addVertex("3");
-    graph.addEdge("4", a, b, "likes");
-    graph.addEdge("5", b, c, "has");
-
-    TinkerGraph subGraph = new TinkerGraph();
-    GremlinPipeline pipeline = new GremlinPipeline(a).outE("likes").inV().path();
-    GraphUtil.subGraph(pipeline, subGraph);
-
-    assertThat(subGraph.getVertices()).hasSize(2);
-    assertThat(subGraph.getEdges()).hasSize(1);
-  }
-}
diff --git a/sonar-core/src/test/java/org/sonar/core/graph/GraphDbTest.java b/sonar-core/src/test/java/org/sonar/core/graph/GraphDbTest.java
new file mode 100644 (file)
index 0000000..922931a
--- /dev/null
@@ -0,0 +1,77 @@
+/*
+ * 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 org.junit.Before;
+import org.junit.Test;
+import org.sonar.core.persistence.AbstractDaoTestCase;
+
+import static org.fest.assertions.Assertions.assertThat;
+
+public class GraphDbTest extends AbstractDaoTestCase {
+  private GraphDao dao;
+
+  @Before
+  public void createDao() {
+    dao = new GraphDao(getMyBatis());
+    setupData("shared");
+  }
+
+  @Test
+  public void select_graph_by_snapshot() {
+    GraphDto testPlan = dao.selectBySnapshot("testplan", 11L);
+
+    assertThat(testPlan.getId()).isEqualTo(101L);
+    assertThat(testPlan.getSnapshotId()).isEqualTo(11L);
+    assertThat(testPlan.getFormat()).isEqualTo("graphson");
+    assertThat(testPlan.getVersion()).isEqualTo(1);
+    assertThat(testPlan.getPerspective()).isEqualTo("testplan");
+    assertThat(testPlan.getRootVertexId()).isEqualTo("3456");
+    assertThat(testPlan.getData()).isEqualTo("{testplan of snapshot 123}");
+  }
+
+  @Test
+  public void select_by_snapshot_and_missing_perspective() {
+    assertThat(dao.selectBySnapshot("duplicable", 123L)).isNull();
+  }
+
+  @Test
+  public void select_by_missing_snapshot() {
+    assertThat(dao.selectBySnapshot("duplicable", 7777L)).isNull();
+  }
+
+  @Test
+  public void select_by_component() {
+    GraphDto testPlan = dao.selectByComponent("testplan", "org.apache.struts:struts");
+
+    assertThat(testPlan.getId()).isEqualTo(101L);
+    assertThat(testPlan.getSnapshotId()).isEqualTo(11L);
+    assertThat(testPlan.getFormat()).isEqualTo("graphson");
+    assertThat(testPlan.getVersion()).isEqualTo(1);
+    assertThat(testPlan.getPerspective()).isEqualTo("testplan");
+    assertThat(testPlan.getRootVertexId()).isEqualTo("3456");
+    assertThat(testPlan.getData()).isEqualTo("{testplan of snapshot 123}");
+  }
+
+  @Test
+  public void select_by_missing_component() {
+    assertThat(dao.selectByComponent("testplan", "org.other:unknown")).isNull();
+  }
+}
diff --git a/sonar-core/src/test/java/org/sonar/core/graph/GraphUtilTest.java b/sonar-core/src/test/java/org/sonar/core/graph/GraphUtilTest.java
new file mode 100644 (file)
index 0000000..b39ecf4
--- /dev/null
@@ -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;
+
+import com.tinkerpop.blueprints.Vertex;
+import com.tinkerpop.blueprints.impls.tg.TinkerGraph;
+import com.tinkerpop.gremlin.java.GremlinPipeline;
+import org.junit.Test;
+import org.sonar.core.graph.GraphUtil;
+
+import static org.fest.assertions.Assertions.assertThat;
+
+public class GraphUtilTest {
+  @Test
+  public void subGraph() {
+    TinkerGraph graph = new TinkerGraph();
+    Vertex a = graph.addVertex("1");
+    Vertex b = graph.addVertex("2");
+    Vertex c = graph.addVertex("3");
+    graph.addEdge("4", a, b, "likes");
+    graph.addEdge("5", b, c, "has");
+
+    TinkerGraph subGraph = new TinkerGraph();
+    GremlinPipeline pipeline = new GremlinPipeline(a).outE("likes").inV().path();
+    GraphUtil.subGraph(pipeline, subGraph);
+
+    assertThat(subGraph.getVertices()).hasSize(2);
+    assertThat(subGraph.getEdges()).hasSize(1);
+  }
+}
diff --git a/sonar-core/src/test/resources/org/sonar/core/graph/GraphDbTest/shared.xml b/sonar-core/src/test/resources/org/sonar/core/graph/GraphDbTest/shared.xml
new file mode 100644 (file)
index 0000000..dfd0305
--- /dev/null
@@ -0,0 +1,9 @@
+<dataset>
+  <projects id="1" kee="org.apache.struts:struts" enabled="true"/>
+
+  <snapshots id="10" project_id="1" islast="false" />
+  <snapshots id="11" project_id="1" islast="true" />
+
+  <graphs id="100" snapshot_id="11" format="graphson" version="1" perspective="testable" root_vertex_id="7890" data="{testable of snapshot 123}"/>
+  <graphs id="101" snapshot_id="11" format="graphson" version="1" perspective="testplan" root_vertex_id="3456" data="{testplan of snapshot 123}"/>
+</dataset>
\ No newline at end of file