diff options
author | Evgeny Mandrikov <mandrikov@gmail.com> | 2011-04-24 07:07:02 +0400 |
---|---|---|
committer | Evgeny Mandrikov <mandrikov@gmail.com> | 2011-04-25 13:42:16 +0400 |
commit | 4eb630d0ff939898efdd9ef29330736ca4e50ebc (patch) | |
tree | c12275afbe97d8ef06b14f1185335daf7d1e5628 | |
parent | 44561d49e67ec6b2d5e110925a1591b594937bb7 (diff) | |
download | sonarqube-4eb630d0ff939898efdd9ef29330736ca4e50ebc.tar.gz sonarqube-4eb630d0ff939898efdd9ef29330736ca4e50ebc.zip |
SONAR-2371 UnitTestDecorator should not save anything if tests measure already exists
2 files changed, 50 insertions, 8 deletions
diff --git a/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/sensors/UnitTestDecorator.java b/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/sensors/UnitTestDecorator.java index 0f4ce4b9b69..4229904a924 100644 --- a/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/sensors/UnitTestDecorator.java +++ b/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/sensors/UnitTestDecorator.java @@ -45,12 +45,12 @@ public class UnitTestDecorator implements Decorator { return !Project.AnalysisType.STATIC.equals(project.getAnalysisType()); } - public boolean shouldDecorateResource(Resource resource) { - return ResourceUtils.isUnitTestClass(resource) || !ResourceUtils.isEntity(resource); + public boolean shouldDecorateResource(Resource resource, DecoratorContext context) { + return context.getMeasure(CoreMetrics.TESTS) == null && (ResourceUtils.isUnitTestClass(resource) || !ResourceUtils.isEntity(resource)); } public void decorate(Resource resource, DecoratorContext context) { - if (shouldDecorateResource(resource)) { + if (shouldDecorateResource(resource, context)) { sumChildren(context, CoreMetrics.TEST_EXECUTION_TIME); sumChildren(context, CoreMetrics.SKIPPED_TESTS); Double tests = sumChildren(context, CoreMetrics.TESTS); diff --git a/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/sensors/UnitTestDecoratorTest.java b/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/sensors/UnitTestDecoratorTest.java index 6caa66a8c69..ad2ca4152a1 100644 --- a/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/sensors/UnitTestDecoratorTest.java +++ b/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/sensors/UnitTestDecoratorTest.java @@ -20,19 +20,23 @@ package org.sonar.plugins.core.sensors; import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.Matchers.closeTo; import static org.junit.Assert.assertThat; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.never; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; +import static org.mockito.Matchers.doubleThat; +import static org.mockito.Matchers.eq; +import static org.mockito.Mockito.*; import org.junit.Before; import org.junit.Test; import org.sonar.api.batch.DecoratorContext; import org.sonar.api.measures.CoreMetrics; +import org.sonar.api.measures.Measure; +import org.sonar.api.measures.Metric; import org.sonar.api.resources.JavaPackage; import org.sonar.api.resources.Project; +import java.util.Arrays; + public class UnitTestDecoratorTest { private UnitTestDecorator decorator; @@ -59,6 +63,44 @@ public class UnitTestDecoratorTest { assertThat(decorator.shouldExecuteOnProject(project), is(true)); } + @Test + public void shouldSumChildren() { + Project project = mock(Project.class); + mockChildrenMeasures(CoreMetrics.TESTS, 3.0); + mockChildrenMeasures(CoreMetrics.TEST_ERRORS, 1.0); + mockChildrenMeasures(CoreMetrics.TEST_FAILURES, 1.0); + mockChildrenMeasures(CoreMetrics.SKIPPED_TESTS, 1.0); + mockChildrenMeasures(CoreMetrics.TEST_EXECUTION_TIME, 1.0); + + decorator.decorate(project, context); + + verify(context).saveMeasure(eq(CoreMetrics.TESTS), eq(6.0)); + verify(context).saveMeasure(eq(CoreMetrics.TEST_ERRORS), eq(2.0)); + verify(context).saveMeasure(eq(CoreMetrics.TEST_FAILURES), eq(2.0)); + verify(context).saveMeasure(eq(CoreMetrics.SKIPPED_TESTS), eq(2.0)); + verify(context).saveMeasure(eq(CoreMetrics.TEST_EXECUTION_TIME), eq(2.0)); + verify(context).saveMeasure(eq(CoreMetrics.TEST_SUCCESS_DENSITY), doubleThat(closeTo(33.3, 0.1))); + } + + private void mockChildrenMeasures(Metric metric, double value) { + when(context.getChildrenMeasures(metric)).thenReturn(Arrays.asList(new Measure(metric, value), new Measure(metric, value))); + } + + /** + * See http://jira.codehaus.org/browse/SONAR-2371 + */ + @Test + public void doNotDecorateIfTestsMeasureAlreadyExists() { + Project project = mock(Project.class); + when(context.getMeasure(CoreMetrics.TESTS)).thenReturn(new Measure()); + + decorator.decorate(project, context); + + assertThat(decorator.shouldDecorateResource(project, context), is(false)); + verify(context, atLeastOnce()).getMeasure(CoreMetrics.TESTS); + verifyNoMoreInteractions(context); + } + /** * See http://jira.codehaus.org/browse/SONAR-2371 */ @@ -80,7 +122,7 @@ public class UnitTestDecoratorTest { decorator.decorate(pkg, context); - assertThat(decorator.shouldDecorateResource(pkg), is(true)); + assertThat(decorator.shouldDecorateResource(pkg, context), is(true)); verify(context, never()).saveMeasure(CoreMetrics.TESTS, 0.0); } |