]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-2371 UnitTestDecorator should not save anything if tests measure already exists
authorEvgeny Mandrikov <mandrikov@gmail.com>
Sun, 24 Apr 2011 03:07:02 +0000 (07:07 +0400)
committerEvgeny Mandrikov <mandrikov@gmail.com>
Mon, 25 Apr 2011 09:42:16 +0000 (13:42 +0400)
plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/sensors/UnitTestDecorator.java
plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/sensors/UnitTestDecoratorTest.java

index 0f4ce4b9b6960932b0621f1d50e1a0f03cc329d3..4229904a9245f5f940a23c17935db43241d49bdd 100644 (file)
@@ -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);
index 6caa66a8c69a4c173bbacc4f2cb8b85fa9784361..ad2ca4152a1a54f93eb3c7a358d10d485ce36b1d 100644 (file)
 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);
   }