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);
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;
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
*/
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);
}