aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-core
diff options
context:
space:
mode:
authorSimon Brandhof <simon.brandhof@gmail.com>2013-01-30 18:31:16 +0100
committerSimon Brandhof <simon.brandhof@gmail.com>2013-01-30 18:36:39 +0100
commit04e8a2dc3dbd73170008e6eb9bd02285c3eac9ff (patch)
treeafa6946c148131733bb74ed28103b54958b6a9e2 /sonar-core
parent5c9ee1b4ed291f2cd2465769b9a4d57dec25976c (diff)
downloadsonarqube-04e8a2dc3dbd73170008e6eb9bd02285c3eac9ff.tar.gz
sonarqube-04e8a2dc3dbd73170008e6eb9bd02285c3eac9ff.zip
SONAR-2501 add preconditions and refactor some method names
Diffstat (limited to 'sonar-core')
-rw-r--r--sonar-core/src/main/java/org/sonar/core/component/ComponentVertex.java5
-rw-r--r--sonar-core/src/main/java/org/sonar/core/graph/BeanGraph.java5
-rw-r--r--sonar-core/src/main/java/org/sonar/core/graph/GraphWriter.java5
-rw-r--r--sonar-core/src/main/java/org/sonar/core/graph/graphson/GraphsonReader.java23
-rw-r--r--sonar-core/src/main/java/org/sonar/core/graph/graphson/GraphsonUtil.java2
-rw-r--r--sonar-core/src/main/java/org/sonar/core/test/DefaultCover.java43
-rw-r--r--sonar-core/src/main/java/org/sonar/core/test/DefaultTestCase.java57
-rw-r--r--sonar-core/src/main/java/org/sonar/core/test/DefaultTestPlan.java35
-rw-r--r--sonar-core/src/main/java/org/sonar/core/test/DefaultTestable.java32
-rw-r--r--sonar-core/src/test/java/org/sonar/core/test/DefaultTestCaseTest.java93
-rw-r--r--sonar-core/src/test/java/org/sonar/core/test/DefaultTestPlanTest.java93
-rw-r--r--sonar-core/src/test/java/org/sonar/core/test/DefaultTestableTest.java29
12 files changed, 330 insertions, 92 deletions
diff --git a/sonar-core/src/main/java/org/sonar/core/component/ComponentVertex.java b/sonar-core/src/main/java/org/sonar/core/component/ComponentVertex.java
index 8d139007b36..0228b569e40 100644
--- a/sonar-core/src/main/java/org/sonar/core/component/ComponentVertex.java
+++ b/sonar-core/src/main/java/org/sonar/core/component/ComponentVertex.java
@@ -45,4 +45,9 @@ public class ComponentVertex extends BeanVertex implements Component {
setProperty("rid", ((ResourceComponent) component).resourceId());
}
}
+
+ @Override
+ public String toString() {
+ return key();
+ }
}
diff --git a/sonar-core/src/main/java/org/sonar/core/graph/BeanGraph.java b/sonar-core/src/main/java/org/sonar/core/graph/BeanGraph.java
index 2cbbc1a3c2b..8a5d5996963 100644
--- a/sonar-core/src/main/java/org/sonar/core/graph/BeanGraph.java
+++ b/sonar-core/src/main/java/org/sonar/core/graph/BeanGraph.java
@@ -23,6 +23,7 @@ import com.tinkerpop.blueprints.Edge;
import com.tinkerpop.blueprints.Element;
import com.tinkerpop.blueprints.Graph;
import com.tinkerpop.blueprints.Vertex;
+import com.tinkerpop.blueprints.impls.tg.TinkerGraph;
import com.tinkerpop.blueprints.util.ElementHelper;
public class BeanGraph {
@@ -34,6 +35,10 @@ public class BeanGraph {
this.beans = new BeanElements();
}
+ public static BeanGraph createInMemory() {
+ return new BeanGraph(new TinkerGraph());
+ }
+
public final <T extends BeanElement> T wrap(Element element, Class<T> beanClass) {
return beans.wrap(element, beanClass, this);
}
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 b1e5a52e5ab..abc4ddef444 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
@@ -32,7 +32,10 @@ public class GraphWriter {
public String write(Graph graph) {
StringWriter output = new StringWriter();
try {
- new GraphsonWriter().write(graph, output, GraphsonMode.COMPACT);
+ 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);
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 d1540295285..e09cb2c47a0 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
@@ -21,13 +21,10 @@ package org.sonar.core.graph.graphson;
import com.tinkerpop.blueprints.Graph;
import com.tinkerpop.blueprints.Vertex;
-import com.tinkerpop.blueprints.util.wrappers.batch.BatchGraph;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
-import java.io.InputStream;
-import java.io.InputStreamReader;
import java.io.Reader;
import java.util.Set;
@@ -37,7 +34,7 @@ import java.util.Set;
public class GraphsonReader {
public Graph read(Reader jsonInput, Graph toGraph) {
- return read(jsonInput, toGraph, 1000, null, null);
+ return read(jsonInput, toGraph, null, null);
}
/**
@@ -45,20 +42,16 @@ 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 input 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(Reader input, Graph toGraph, int bufferSize, Set<String> edgePropertyKeys, Set<String> vertexPropertyKeys) {
+ public Graph read(Reader input, Graph toGraph, Set<String> edgePropertyKeys, Set<String> vertexPropertyKeys) {
try {
JSONParser parser = new JSONParser();
JSONObject json = (JSONObject) parser.parse(input);
- // if this is a transactional graph then we're buffering
- final BatchGraph batchGraph = BatchGraph.wrap(toGraph, bufferSize);
-
- ElementFactory elementFactory = new ElementFactory(batchGraph);
-
- final GraphsonMode mode = GraphsonMode.valueOf(json.get(GraphsonTokens.MODE).toString());
+ ElementFactory elementFactory = new ElementFactory(toGraph);
+ GraphsonMode mode = GraphsonMode.valueOf(json.get(GraphsonTokens.MODE).toString());
GraphsonUtil graphson = new GraphsonUtil(mode, elementFactory, vertexPropertyKeys, edgePropertyKeys);
JSONArray vertices = (JSONArray) json.get(GraphsonTokens.VERTICES);
@@ -69,11 +62,11 @@ public class GraphsonReader {
JSONArray edges = (JSONArray) json.get(GraphsonTokens.EDGES);
for (Object edgeObject : edges) {
JSONObject edge = (JSONObject) edgeObject;
- final Vertex inV = batchGraph.getVertex(edge.get(GraphsonTokens._IN_V));
- final Vertex outV = batchGraph.getVertex(edge.get(GraphsonTokens._OUT_V));
+ Vertex inV = toGraph.getVertex(edge.get(GraphsonTokens._IN_V));
+ Vertex outV = toGraph.getVertex(edge.get(GraphsonTokens._OUT_V));
graphson.edgeFromJson(edge, outV, inV);
}
- batchGraph.shutdown();
+ toGraph.shutdown();
return toGraph;
} catch (Exception e) {
throw new GraphsonException("Unable to parse GraphSON", e);
diff --git a/sonar-core/src/main/java/org/sonar/core/graph/graphson/GraphsonUtil.java b/sonar-core/src/main/java/org/sonar/core/graph/graphson/GraphsonUtil.java
index 73d195f8af3..b06f953287a 100644
--- a/sonar-core/src/main/java/org/sonar/core/graph/graphson/GraphsonUtil.java
+++ b/sonar-core/src/main/java/org/sonar/core/graph/graphson/GraphsonUtil.java
@@ -630,7 +630,7 @@ class GraphsonUtil {
Set<String> propertyKeys = isEdge ? this.edgePropertyKeys : this.vertexPropertyKeys;
ElementPropertiesRule elementPropertyConfig = isEdge ? this.edgePropertiesRule : this.vertexPropertiesRule;
- org.json.simple.JSONObject jsonElement = createJSONMap(createPropertyMap(element, propertyKeys, elementPropertyConfig), propertyKeys, showTypes);
+ JSONObject jsonElement = createJSONMap(createPropertyMap(element, propertyKeys, elementPropertyConfig), propertyKeys, showTypes);
if ((isEdge && this.includeReservedEdgeId) || (!isEdge && this.includeReservedVertexId)) {
putObject(jsonElement, GraphsonTokens._ID, element.getId());
diff --git a/sonar-core/src/main/java/org/sonar/core/test/DefaultCover.java b/sonar-core/src/main/java/org/sonar/core/test/DefaultCover.java
new file mode 100644
index 00000000000..8e0f9928f2f
--- /dev/null
+++ b/sonar-core/src/main/java/org/sonar/core/test/DefaultCover.java
@@ -0,0 +1,43 @@
+/*
+ * 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 com.tinkerpop.blueprints.Direction;
+import org.sonar.api.test.Cover;
+import org.sonar.api.test.TestCase;
+import org.sonar.api.test.Testable;
+import org.sonar.core.graph.BeanEdge;
+
+import java.util.List;
+
+public class DefaultCover extends BeanEdge implements Cover {
+
+ public TestCase testCase() {
+ return getVertex(DefaultTestCase.class, Direction.OUT);
+ }
+
+ public Testable testable() {
+ return getVertex(DefaultTestable.class, Direction.IN);
+ }
+
+ public List<Integer> lines() {
+ return (List<Integer>) getProperty("lines");
+ }
+}
diff --git a/sonar-core/src/main/java/org/sonar/core/test/DefaultTestCase.java b/sonar-core/src/main/java/org/sonar/core/test/DefaultTestCase.java
index d14fddd3fe0..e87695d5c7a 100644
--- a/sonar-core/src/main/java/org/sonar/core/test/DefaultTestCase.java
+++ b/sonar-core/src/main/java/org/sonar/core/test/DefaultTestCase.java
@@ -19,12 +19,10 @@
*/
package org.sonar.core.test;
-import com.google.common.base.Preconditions;
-import com.google.common.collect.Iterables;
import com.tinkerpop.blueprints.Direction;
import com.tinkerpop.blueprints.Edge;
import com.tinkerpop.blueprints.Vertex;
-import org.sonar.api.test.CoveredTestable;
+import org.sonar.api.test.exception.IllegalDurationException;
import org.sonar.api.test.MutableTestCase;
import org.sonar.api.test.TestPlan;
import org.sonar.api.test.Testable;
@@ -33,21 +31,18 @@ import org.sonar.core.graph.GraphUtil;
import javax.annotation.Nullable;
-import java.util.Collection;
import java.util.List;
public class DefaultTestCase extends BeanVertex implements MutableTestCase {
- public String type() {
- return (String) getProperty("type");
- }
-
public Long durationInMs() {
return (Long) getProperty("duration");
}
public MutableTestCase setDurationInMs(@Nullable Long l) {
- Preconditions.checkArgument(l == null || l >= 0, String.format("Duration must be positive (got %d)", l));
+ if (l != null && l < 0) {
+ throw new IllegalDurationException("Test duration must be positive (got: " + l + ")");
+ }
setProperty("duration", l);
return this;
}
@@ -100,9 +95,8 @@ public class DefaultTestCase extends BeanVertex implements MutableTestCase {
return this;
}
- public void covers(Testable testable, List<Integer> lines) {
- BeanVertex testableVertex = ((BeanVertex) testable);
- beanGraph().getUnderlyingGraph().addEdge(null, element(), testableVertex.element(), "covers").setProperty("lines", lines);
+ public void setCover(Testable testable, List<Integer> lines) {
+ beanGraph().getUnderlyingGraph().addEdge(null, element(), ((BeanVertex) testable).element(), "covers").setProperty("lines", lines);
}
public TestPlan testPlan() {
@@ -110,43 +104,20 @@ public class DefaultTestCase extends BeanVertex implements MutableTestCase {
return beanGraph().wrap(plan, DefaultTestPlan.class);
}
- public boolean hasCoveredLines(){
- return Iterables.size(element().getEdges(Direction.OUT, "covers")) > 0;
+ public boolean doesCover() {
+ return element().getEdges(Direction.OUT, "covers").iterator().hasNext();
}
public int countCoveredLines() {
- int coveredBlocks = 0;
- for (Edge edge : element().getEdges(Direction.OUT, "covers")){
+ int result = 0;
+ for (Edge edge : element().getEdges(Direction.OUT, "covers")) {
List<Integer> lines = (List<Integer>) edge.getProperty("lines");
- coveredBlocks = coveredBlocks + lines.size();
+ result = result + lines.size();
}
- return coveredBlocks;
- }
-
- public Collection<CoveredTestable> coveredTestable() {
- return null;
- }
-
- @Override
- public boolean equals(Object o) {
- if (this == o) {
- return true;
- }
- if (o == null || getClass() != o.getClass()) {
- return false;
- }
-
- DefaultTestCase that = (DefaultTestCase) o;
-
- if (key() != null ? !key().equals(that.key()) : that.key() != null) {
- return false;
- }
-
- return true;
+ return result;
}
- @Override
- public int hashCode() {
- return key() != null ? key().hashCode() : 0;
+ public Iterable covers() {
+ return getEdges(DefaultCover.class, Direction.OUT, "covers");
}
}
diff --git a/sonar-core/src/main/java/org/sonar/core/test/DefaultTestPlan.java b/sonar-core/src/main/java/org/sonar/core/test/DefaultTestPlan.java
index b3975902df1..c1947f9b168 100644
--- a/sonar-core/src/main/java/org/sonar/core/test/DefaultTestPlan.java
+++ b/sonar-core/src/main/java/org/sonar/core/test/DefaultTestPlan.java
@@ -19,17 +19,17 @@
*/
package org.sonar.core.test;
-import com.google.common.collect.Lists;
import com.tinkerpop.blueprints.Direction;
import com.tinkerpop.blueprints.Vertex;
import org.sonar.api.component.Component;
+import org.sonar.api.test.exception.TestCaseAlreadyExistsException;
import org.sonar.api.test.MutableTestCase;
import org.sonar.api.test.MutableTestPlan;
import org.sonar.core.component.ComponentVertex;
import org.sonar.core.graph.BeanVertex;
import org.sonar.core.graph.GraphUtil;
-import java.util.List;
+import javax.annotation.CheckForNull;
public class DefaultTestPlan extends BeanVertex implements MutableTestPlan {
public Component component() {
@@ -37,17 +37,36 @@ public class DefaultTestPlan extends BeanVertex implements MutableTestPlan {
return beanGraph().wrap(component, ComponentVertex.class);
}
+ public String type() {
+ return (String) getProperty("type");
+ }
+
+ public MutableTestPlan setType(String s) {
+ setProperty("type", s);
+ return this;
+ }
+
+ @CheckForNull
+ public MutableTestCase testCaseByKey(String key) {
+ for (MutableTestCase testCase : testCases()) {
+ if (key.equals(testCase.key())) {
+ return testCase;
+ }
+ }
+ return null;
+ }
+
public MutableTestCase addTestCase(String key) {
+ if (testCaseByKey(key)!=null) {
+ throw new TestCaseAlreadyExistsException(component().key(), key);
+ }
DefaultTestCase testCase = beanGraph().createAdjacentVertex(this, DefaultTestCase.class, "testcase");
testCase.setKey(key);
return testCase;
}
- public List<MutableTestCase> testCases() {
- List<MutableTestCase> testCases = Lists.newArrayList();
- for (Vertex testCaseVertex : element().getVertices(Direction.OUT, "testcase")) {
- testCases.add(beanGraph().wrap(testCaseVertex, DefaultTestCase.class));
- }
- return testCases;
+ public Iterable<MutableTestCase> testCases() {
+ return (Iterable) getVertices(DefaultTestCase.class, Direction.OUT, "testcase");
}
+
}
diff --git a/sonar-core/src/main/java/org/sonar/core/test/DefaultTestable.java b/sonar-core/src/main/java/org/sonar/core/test/DefaultTestable.java
index 74a232fb4ad..d927f9db7f5 100644
--- a/sonar-core/src/main/java/org/sonar/core/test/DefaultTestable.java
+++ b/sonar-core/src/main/java/org/sonar/core/test/DefaultTestable.java
@@ -32,12 +32,9 @@ import org.sonar.core.component.ComponentVertex;
import org.sonar.core.graph.BeanVertex;
import org.sonar.core.graph.GraphUtil;
-import java.util.Collection;
import java.util.List;
import java.util.SortedSet;
-import static com.google.common.collect.Lists.newArrayList;
-
public class DefaultTestable extends BeanVertex implements MutableTestable {
public Component component() {
@@ -45,29 +42,30 @@ public class DefaultTestable extends BeanVertex implements MutableTestable {
return beanGraph().wrap(component, ComponentVertex.class);
}
- public Collection<TestCase> testCases() {
- List<TestCase> testCases = newArrayList();
- for (Edge edge : getCovers()) {
- Vertex testable = edge.getVertex(Direction.OUT);
- testCases.add(beanGraph().wrap(testable, DefaultTestCase.class));
+ public List<TestCase> testCases() {
+ ImmutableList.Builder<TestCase> cases = ImmutableList.builder();
+ for (Edge coversEdge : getCovers()) {
+ Vertex testable = coversEdge.getVertex(Direction.OUT);
+ cases.add(beanGraph().wrap(testable, DefaultTestCase.class));
}
- return testCases;
+ return cases.build();
}
public int countTestCasesOfLine(int line) {
int number = 0;
+ // TODO filter on edge
for (Edge edge : getCovers()) {
- if (Iterables.contains(testedLines(edge), Long.valueOf(line))) {
+ if (Iterables.contains(lines(edge), line)) {
number++;
}
}
return number;
}
- public Collection<TestCase> testCasesOfLine(int line) {
+ public List<TestCase> testCasesOfLine(int line) {
ImmutableList.Builder<TestCase> cases = ImmutableList.builder();
for (Edge edge : getCovers()) {
- if (Iterables.contains(testedLines(edge), Long.valueOf(line))) {
+ if (lines(edge).contains(line)) {
Vertex vertexTestable = edge.getVertex(Direction.OUT);
DefaultTestCase testCase = beanGraph().wrap(vertexTestable, DefaultTestCase.class);
cases.add(testCase);
@@ -76,10 +74,10 @@ public class DefaultTestable extends BeanVertex implements MutableTestable {
return cases.build();
}
- public SortedSet<Long> testedLines() {
- ImmutableSortedSet.Builder<Long> coveredLines = ImmutableSortedSet.naturalOrder();
+ public SortedSet<Integer> testedLines() {
+ ImmutableSortedSet.Builder<Integer> coveredLines = ImmutableSortedSet.naturalOrder();
for (Edge edge : getCovers()) {
- coveredLines.addAll(testedLines(edge));
+ coveredLines.addAll(lines(edge));
}
return coveredLines.build();
}
@@ -88,8 +86,8 @@ public class DefaultTestable extends BeanVertex implements MutableTestable {
return element().getEdges(Direction.IN, "covers");
}
- private List<Long> testedLines(Edge edge) {
- return (List<Long>) edge.getProperty("lines");
+ private List<Integer> lines(Edge edge) {
+ return (List<Integer>) edge.getProperty("lines");
}
} \ No newline at end of file
diff --git a/sonar-core/src/test/java/org/sonar/core/test/DefaultTestCaseTest.java b/sonar-core/src/test/java/org/sonar/core/test/DefaultTestCaseTest.java
new file mode 100644
index 00000000000..cceb46414cd
--- /dev/null
+++ b/sonar-core/src/test/java/org/sonar/core/test/DefaultTestCaseTest.java
@@ -0,0 +1,93 @@
+/*
+ * 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.Rule;
+import org.junit.Test;
+import org.junit.rules.ExpectedException;
+import org.sonar.api.test.exception.IllegalDurationException;
+import org.sonar.api.test.TestCase;
+import org.sonar.core.graph.BeanGraph;
+
+import java.util.Arrays;
+
+import static org.fest.assertions.Assertions.assertThat;
+
+public class DefaultTestCaseTest {
+
+ @Rule
+ public ExpectedException thrown = ExpectedException.none();
+
+ @Test
+ public void no_coverage_data() {
+ BeanGraph beanGraph = BeanGraph.createInMemory();
+ DefaultTestCase testCase = beanGraph.createVertex(DefaultTestCase.class);
+
+ assertThat(testCase.doesCover()).isFalse();
+ assertThat(testCase.countCoveredLines()).isEqualTo(0);
+ assertThat(testCase.covers()).isEmpty();
+ }
+
+ @Test
+ public void should_cover_testables() {
+ BeanGraph beanGraph = BeanGraph.createInMemory();
+ DefaultTestable testable1 = beanGraph.createVertex(DefaultTestable.class);
+ DefaultTestable testable2 = beanGraph.createVertex(DefaultTestable.class);
+ DefaultTestCase testCase = beanGraph.createVertex(DefaultTestCase.class);
+
+ testCase.setCover(testable1, Arrays.asList(10, 11, 12));
+ testCase.setCover(testable2, Arrays.asList(12, 13, 14));
+
+ assertThat(testCase.doesCover()).isTrue();
+ assertThat(testCase.countCoveredLines()).isEqualTo(6);
+ assertThat(testCase.covers()).hasSize(2);
+ }
+
+ @Test
+ public void should_set_metadata() {
+ BeanGraph beanGraph = BeanGraph.createInMemory();
+ DefaultTestCase testCase = beanGraph.createVertex(DefaultTestCase.class);
+
+ testCase.setKey("T1")
+ .setName("Test one")
+ .setDurationInMs(1234L)
+ .setMessage("Error msg")
+ .setStackTrace("xxx")
+ .setStatus(TestCase.STATUS_FAIL);
+
+ assertThat(testCase.key()).isEqualTo("T1");
+ assertThat(testCase.name()).isEqualTo("Test one");
+ assertThat(testCase.message()).isEqualTo("Error msg");
+ assertThat(testCase.stackTrace()).isEqualTo("xxx");
+ assertThat(testCase.durationInMs()).isEqualTo(1234L);
+ assertThat(testCase.status()).isEqualTo(TestCase.STATUS_FAIL);
+ }
+
+ @Test
+ public void duration_should_be_positive() {
+ thrown.expect(IllegalDurationException.class);
+ thrown.expectMessage("Test duration must be positive (got: -1234)");
+
+ BeanGraph beanGraph = BeanGraph.createInMemory();
+ DefaultTestCase testCase = beanGraph.createVertex(DefaultTestCase.class);
+
+ testCase.setDurationInMs(-1234L);
+ }
+}
diff --git a/sonar-core/src/test/java/org/sonar/core/test/DefaultTestPlanTest.java b/sonar-core/src/test/java/org/sonar/core/test/DefaultTestPlanTest.java
new file mode 100644
index 00000000000..63e623aa751
--- /dev/null
+++ b/sonar-core/src/test/java/org/sonar/core/test/DefaultTestPlanTest.java
@@ -0,0 +1,93 @@
+/*
+ * 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 com.google.common.collect.Iterables;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.ExpectedException;
+import org.sonar.api.test.exception.TestCaseAlreadyExistsException;
+import org.sonar.api.test.MutableTestCase;
+import org.sonar.api.test.TestPlan;
+import org.sonar.core.component.ComponentVertex;
+import org.sonar.core.graph.BeanGraph;
+
+import static org.fest.assertions.Assertions.assertThat;
+
+public class DefaultTestPlanTest {
+ @Rule
+ public ExpectedException thrown = ExpectedException.none();
+
+ @Test
+ public void should_not_have_test_cases() {
+ BeanGraph beanGraph = BeanGraph.createInMemory();
+
+ DefaultTestPlan plan = beanGraph.createVertex(DefaultTestPlan.class);
+ assertThat(plan.testCases()).isEmpty();
+ }
+
+ @Test
+ public void should_add_test_cases() {
+ BeanGraph beanGraph = BeanGraph.createInMemory();
+
+ DefaultTestPlan plan = beanGraph.createVertex(DefaultTestPlan.class);
+ plan.addTestCase("T1");
+ 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");
+ }
+
+ @Test
+ public void should_find_test_case_by_key() {
+ BeanGraph beanGraph = BeanGraph.createInMemory();
+
+ DefaultTestPlan plan = beanGraph.createVertex(DefaultTestPlan.class);
+ plan.addTestCase("T1");
+ plan.addTestCase("T2");
+
+ assertThat(plan.testCaseByKey("T1").key()).isEqualTo("T1");
+ assertThat(plan.testCaseByKey("T3")).isNull();
+ }
+
+ @Test
+ public void should_set_type() {
+ BeanGraph beanGraph = BeanGraph.createInMemory();
+
+ DefaultTestPlan plan = beanGraph.createVertex(DefaultTestPlan.class);
+ assertThat(plan.type()).isNull();
+
+ plan.setType(TestPlan.TYPE_UNIT);
+ assertThat(plan.type()).isEqualTo(TestPlan.TYPE_UNIT);
+ }
+
+ @Test
+ public void keys_of_test_cases_should_be_unique() {
+ thrown.expect(TestCaseAlreadyExistsException.class);
+
+ BeanGraph beanGraph = BeanGraph.createInMemory();
+ ComponentVertex component = beanGraph.createVertex(ComponentVertex.class);
+
+ DefaultTestPlan plan = beanGraph.createAdjacentVertex(component, DefaultTestPlan.class, "testplan");
+ plan.addTestCase("T1");
+ plan.addTestCase("T1");
+ }
+}
diff --git a/sonar-core/src/test/java/org/sonar/core/test/DefaultTestableTest.java b/sonar-core/src/test/java/org/sonar/core/test/DefaultTestableTest.java
index a149c0b7714..65a240e973a 100644
--- a/sonar-core/src/test/java/org/sonar/core/test/DefaultTestableTest.java
+++ b/sonar-core/src/test/java/org/sonar/core/test/DefaultTestableTest.java
@@ -19,7 +19,6 @@
*/
package org.sonar.core.test;
-import com.tinkerpop.blueprints.impls.tg.TinkerGraph;
import org.junit.Test;
import org.sonar.core.graph.BeanGraph;
@@ -29,23 +28,39 @@ import static org.fest.assertions.Assertions.assertThat;
public class DefaultTestableTest {
@Test
- public void no_covered_lines() {
- BeanGraph beanGraph = new BeanGraph(new TinkerGraph());
+ public void should_not_have_tested_lines() {
+ BeanGraph beanGraph = BeanGraph.createInMemory();
DefaultTestable testable = beanGraph.createVertex(DefaultTestable.class);
assertThat(testable.testedLines()).isEmpty();
}
@Test
- public void covered_lines() {
- BeanGraph beanGraph = new BeanGraph(new TinkerGraph());
+ public void should_have_tested_lines() {
+ BeanGraph beanGraph = BeanGraph.createInMemory();
DefaultTestable testable = beanGraph.createVertex(DefaultTestable.class);
DefaultTestCase testCase1 = beanGraph.createVertex(DefaultTestCase.class);
- testCase1.covers(testable, Arrays.asList(10, 11, 12));
+ testCase1.setCover(testable, Arrays.asList(10, 11, 12));
DefaultTestCase testCase2 = beanGraph.createVertex(DefaultTestCase.class);
- testCase2.covers(testable, Arrays.asList(12, 48, 49));
+ testCase2.setCover(testable, Arrays.asList(12, 48, 49));
assertThat(testable.testedLines()).containsOnly(10, 11, 12, 48, 49);
}
+
+ @Test
+ public void should_get_test_cases() {
+ BeanGraph beanGraph = BeanGraph.createInMemory();
+
+ DefaultTestable testable = beanGraph.createVertex(DefaultTestable.class);
+ DefaultTestCase testCase1 = beanGraph.createVertex(DefaultTestCase.class);
+ testCase1.setCover(testable, Arrays.asList(10, 11, 12));
+ DefaultTestCase testCase2 = beanGraph.createVertex(DefaultTestCase.class);
+ testCase2.setCover(testable, Arrays.asList(12, 48, 49));
+
+ assertThat(testable.testCases()).containsOnly(testCase1, testCase2);
+ assertThat(testable.testCasesOfLine(5)).isEmpty();
+ assertThat(testable.testCasesOfLine(10)).containsExactly(testCase1);
+ assertThat(testable.testCasesOfLine(12)).contains(testCase1, testCase2);
+ }
}