]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-2371 SurefireSensor should not insert unit test measures when there is no reports
authorEvgeny Mandrikov <mandrikov@gmail.com>
Tue, 19 Apr 2011 15:33:47 +0000 (19:33 +0400)
committerEvgeny Mandrikov <mandrikov@gmail.com>
Tue, 19 Apr 2011 21:41:21 +0000 (01:41 +0400)
- UnitTestDecorator should save zero for unit test measure for project, when there is
  no values for summarizing and type of analysis is reuse reports or dynamic

- Don't use pom.getPackaging() in AbstractSurefireParser

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
plugins/sonar-surefire-plugin/src/main/java/org/sonar/plugins/surefire/api/AbstractSurefireParser.java
plugins/sonar-surefire-plugin/src/test/java/org/sonar/plugins/surefire/api/AbstractSurefireParserTest.java

index c61a46bfec24b970db3fe1f2d47ff30d4cb614fd..0f4ce4b9b6960932b0621f1d50e1a0f03cc329d3 100644 (file)
@@ -60,6 +60,9 @@ public class UnitTestDecorator implements Decorator {
       if (isPositive(tests, true) && isPositive(errors, false) && isPositive(failures, false)) {
         Double errorsAndFailuresRatio = (errors + failures) * 100.0 / tests;
         context.saveMeasure(CoreMetrics.TEST_SUCCESS_DENSITY, 100.0 - errorsAndFailuresRatio);
+      } else if (tests == null && ResourceUtils.isProject(resource)) {
+        // See http://jira.codehaus.org/browse/SONAR-2371
+        context.saveMeasure(CoreMetrics.TESTS, 0.0);
       }
     }
   }
index 3b46ae33a0f8629e01d891b30f6e6196117cff92..da3823d9a6de86441912f29207f27a96f00f4756 100644 (file)
@@ -21,25 +21,52 @@ package org.sonar.plugins.core.sensors;
 
 import static org.hamcrest.CoreMatchers.is;
 import static org.junit.Assert.assertThat;
-import org.junit.Test;
 import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.verify;
 import static org.mockito.Mockito.when;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.sonar.api.batch.DecoratorContext;
+import org.sonar.api.measures.CoreMetrics;
 import org.sonar.api.resources.Project;
 
 public class UnitTestDecoratorTest {
 
+  private UnitTestDecorator decorator;
+
+  @Before
+  public void setUp() {
+    decorator = new UnitTestDecorator();
+  }
+
   @Test
   public void generatesMetrics() {
-    assertThat(new UnitTestDecorator().generatesMetrics().size(), is(5));
+    assertThat(decorator.generatesMetrics().size(), is(5));
   }
 
   @Test
   public void doNotDecorateStaticAnalysis() {
     Project project = mock(Project.class);
     when(project.getAnalysisType()).thenReturn(Project.AnalysisType.STATIC);
-    assertThat(new UnitTestDecorator().shouldExecuteOnProject(project), is(false));
+    assertThat(decorator.shouldExecuteOnProject(project), is(false));
 
     when(project.getAnalysisType()).thenReturn(Project.AnalysisType.DYNAMIC);
-    assertThat(new UnitTestDecorator().shouldExecuteOnProject(project), is(true));
+    assertThat(decorator.shouldExecuteOnProject(project), is(true));
   }
+
+  /**
+   * See http://jira.codehaus.org/browse/SONAR-2371
+   */
+  @Test
+  public void shouldSaveZeroOnProject() {
+    DecoratorContext context = mock(DecoratorContext.class);
+    Project project = new Project("");
+    project.setAnalysisType(Project.AnalysisType.DYNAMIC);
+
+    decorator.decorate(project, context);
+
+    verify(context).saveMeasure(CoreMetrics.TESTS, 0.0);
+  }
+
 }
index c8d6a5fd37267b7fbfadca96c7c061a186646013..d0cc62620932ca7a0e7bcddf29e0c35d53e9dea2 100644 (file)
@@ -33,11 +33,12 @@ import org.sonar.plugins.surefire.data.SurefireStaxHandler;
 import org.sonar.plugins.surefire.data.UnitTestClassReport;
 import org.sonar.plugins.surefire.data.UnitTestIndex;
 
-import javax.xml.stream.XMLStreamException;
 import java.io.File;
 import java.io.FilenameFilter;
 import java.util.Map;
 
+import javax.xml.stream.XMLStreamException;
+
 /**
  * @since 2.4
  */
@@ -46,9 +47,7 @@ public abstract class AbstractSurefireParser {
   public void collect(Project project, SensorContext context, File reportsDir) {
     File[] xmlFiles = getReports(reportsDir);
 
-    if (xmlFiles.length == 0) {
-      insertZeroWhenNoReports(project, context);
-    } else {
+    if (xmlFiles.length != 0) {
       parseFiles(context, xmlFiles);
     }
   }
@@ -64,18 +63,11 @@ public abstract class AbstractSurefireParser {
     });
   }
 
-  private void insertZeroWhenNoReports(Project project, SensorContext context) {
-    if (!StringUtils.equalsIgnoreCase("pom", project.getPackaging())) {
-      context.saveMeasure(CoreMetrics.TESTS, 0.0);
-    }
-  }
-
   private void parseFiles(SensorContext context, File[] reports) {
     UnitTestIndex index = new UnitTestIndex();
     parseFiles(reports, index);
     sanitize(index);
     save(index, context);
-
   }
 
   private void parseFiles(File[] reports, UnitTestIndex index) {
@@ -121,7 +113,6 @@ public abstract class AbstractSurefireParser {
     }
   }
 
-
   private void saveMeasure(SensorContext context, Resource resource, Metric metric, double value) {
     if (!Double.isNaN(value)) {
       context.saveMeasure(resource, metric, value);
index 697c94a5750f2de893e160fd323655554955a9e8..15085fee0c4d9cf6d8545c2f825ea4d767c330b8 100644 (file)
@@ -22,7 +22,6 @@ package org.sonar.plugins.surefire.api;
 import org.hamcrest.BaseMatcher;
 import org.hamcrest.Description;
 import org.junit.Test;
-import org.mockito.internal.matchers.StartsWith;
 import org.sonar.api.batch.SensorContext;
 import org.sonar.api.measures.CoreMetrics;
 import org.sonar.api.measures.Metric;
@@ -60,7 +59,7 @@ public class AbstractSurefireParserTest {
 
     parser.collect(new Project("foo"), context, getDir("noReports"));
 
-    verify(context).saveMeasure(CoreMetrics.TESTS, 0.0);
+    verifyZeroInteractions(context);
   }
 
   @Test