aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--sonar-core/src/main/java/org/sonar/core/test/DefaultTestCase.java4
-rw-r--r--sonar-core/src/main/java/org/sonar/core/test/DefaultTestable.java1
-rw-r--r--sonar-core/src/test/java/org/sonar/core/test/DefaultTestCaseTest.java27
-rw-r--r--sonar-plugin-api/src/main/java/org/sonar/api/test/exception/CoverageAlreadyExistsException.java26
4 files changed, 53 insertions, 5 deletions
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 bb0fd524fde..ef4be64798e 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
@@ -29,6 +29,7 @@ import org.sonar.api.test.MutableTestCase;
import org.sonar.api.test.MutableTestPlan;
import org.sonar.api.test.TestPlan;
import org.sonar.api.test.Testable;
+import org.sonar.api.test.exception.CoverageAlreadyExistsException;
import org.sonar.api.test.exception.IllegalDurationException;
import org.sonar.core.graph.BeanVertex;
import org.sonar.core.graph.GraphUtil;
@@ -97,6 +98,9 @@ public class DefaultTestCase extends BeanVertex implements MutableTestCase {
}
public MutableTestCase setCoverageBlock(Testable testable, List<Integer> lines) {
+ if (coverageBlock(testable)!=null) {
+ throw new CoverageAlreadyExistsException("The link between " + name() + " and " + testable.component().key() + " already exists");
+ }
beanGraph().getUnderlyingGraph().addEdge(null, element(), ((BeanVertex) testable).element(), "covers").setProperty("lines", lines);
return this;
}
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 f44589e1421..255e352aeec 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
@@ -110,5 +110,4 @@ public class DefaultTestable extends BeanVertex implements MutableTestable {
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
index f6026702ed1..fd815e62c42 100644
--- a/sonar-core/src/test/java/org/sonar/core/test/DefaultTestCaseTest.java
+++ b/sonar-core/src/test/java/org/sonar/core/test/DefaultTestCaseTest.java
@@ -26,6 +26,7 @@ import org.junit.rules.ExpectedException;
import org.sonar.api.component.mock.MockSourceFile;
import org.sonar.api.test.CoverageBlock;
import org.sonar.api.test.TestCase;
+import org.sonar.api.test.exception.CoverageAlreadyExistsException;
import org.sonar.api.test.exception.IllegalDurationException;
import org.sonar.core.component.ComponentVertex;
import org.sonar.core.component.ScanGraph;
@@ -69,10 +70,12 @@ public class DefaultTestCaseTest {
@Test
public void should_cover_multiple_testables() {
- BeanGraph beanGraph = BeanGraph.createInMemory();
- DefaultTestable testable1 = beanGraph.createVertex(DefaultTestable.class);
- DefaultTestable testable2 = beanGraph.createVertex(DefaultTestable.class);
- DefaultTestCase testCase = beanGraph.createVertex(DefaultTestCase.class);
+ ScanGraph graph = ScanGraph.create();
+ ComponentVertex file1 = graph.addComponent(MockSourceFile.createMain("org.foo.Bar"));
+ DefaultTestable testable1 = graph.createAdjacentVertex(file1, DefaultTestable.class, "testable");
+ ComponentVertex file2 = graph.addComponent(MockSourceFile.createMain("org.foo.File"));
+ DefaultTestable testable2 = graph.createAdjacentVertex(file2, DefaultTestable.class, "testable");
+ DefaultTestCase testCase = graph.createVertex(DefaultTestCase.class);
testCase.setCoverageBlock(testable1, Arrays.asList(10, 11, 12));
testCase.setCoverageBlock(testable2, Arrays.asList(12, 13, 14));
@@ -131,4 +134,20 @@ public class DefaultTestCaseTest {
testCase.setDurationInMs(-1234L);
}
+
+ @Test
+ public void should_fail_if_coverage_block_already_exits() {
+ thrown.expect(CoverageAlreadyExistsException.class);
+
+ ScanGraph graph = ScanGraph.create();
+
+ ComponentVertex file = graph.addComponent(MockSourceFile.createMain("org.foo.Bar"));
+ DefaultTestable testable = graph.createAdjacentVertex(file, DefaultTestable.class, "testable");
+
+ DefaultTestCase testCase = graph.createVertex(DefaultTestCase.class);
+ testCase.setCoverageBlock(testable, Arrays.asList(10, 11, 12));
+
+ // error
+ testCase.setCoverageBlock(testable, Arrays.asList(20));
+ }
}
diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/test/exception/CoverageAlreadyExistsException.java b/sonar-plugin-api/src/main/java/org/sonar/api/test/exception/CoverageAlreadyExistsException.java
new file mode 100644
index 00000000000..925d14ffa0c
--- /dev/null
+++ b/sonar-plugin-api/src/main/java/org/sonar/api/test/exception/CoverageAlreadyExistsException.java
@@ -0,0 +1,26 @@
+/*
+ * 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.api.test.exception;
+
+public class CoverageAlreadyExistsException extends TestException {
+ public CoverageAlreadyExistsException(String message) {
+ super(message);
+ }
+}