diff options
Diffstat (limited to 'sonar-core')
3 files changed, 27 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)); + } } |