]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-2371 Rework UnitTestDecorator and SurefireSensor
authorEvgeny Mandrikov <mandrikov@gmail.com>
Tue, 3 May 2011 21:47:38 +0000 (01:47 +0400)
committerEvgeny Mandrikov <mandrikov@gmail.com>
Wed, 4 May 2011 15:09:54 +0000 (19:09 +0400)
* UnitTestDecorator should not save zero value for tests metric

* SurefireSensor should save zero when no reports and project do not
  contain any modules

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

index 4229904a9245f5f940a23c17935db43241d49bdd..0658a9733abaa64a214551c5fff419c55e82f1b3 100644 (file)
@@ -60,9 +60,6 @@ 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 ad2ca4152a1a54f93eb3c7a358d10d485ce36b1d..0bcd1a5756feb6af1cfa26720fefec5bad8a407e 100644 (file)
@@ -32,7 +32,6 @@ 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;
@@ -86,9 +85,6 @@ public class UnitTestDecoratorTest {
     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);
@@ -101,29 +97,4 @@ public class UnitTestDecoratorTest {
     verifyNoMoreInteractions(context);
   }
 
-  /**
-   * See http://jira.codehaus.org/browse/SONAR-2371
-   */
-  @Test
-  public void shouldSaveZeroOnProject() {
-    Project project = new Project("").setAnalysisType(Project.AnalysisType.DYNAMIC);
-
-    decorator.decorate(project, context);
-
-    verify(context).saveMeasure(CoreMetrics.TESTS, 0.0);
-  }
-
-  /**
-   * See http://jira.codehaus.org/browse/SONAR-2371
-   */
-  @Test
-  public void shouldNotSaveZeroOnPackage() {
-    JavaPackage pkg = new JavaPackage();
-
-    decorator.decorate(pkg, context);
-
-    assertThat(decorator.shouldDecorateResource(pkg, context), is(true));
-    verify(context, never()).saveMeasure(CoreMetrics.TESTS, 0.0);
-  }
-
 }
index d0cc62620932ca7a0e7bcddf29e0c35d53e9dea2..85e7a2682f841fbbd35cb1d304737dcb8bef48e9 100644 (file)
@@ -47,7 +47,12 @@ public abstract class AbstractSurefireParser {
   public void collect(Project project, SensorContext context, File reportsDir) {
     File[] xmlFiles = getReports(reportsDir);
 
-    if (xmlFiles.length != 0) {
+    if (xmlFiles.length == 0) {
+      // See http://jira.codehaus.org/browse/SONAR-2371
+      if (project.getModules().isEmpty()) {
+        context.saveMeasure(CoreMetrics.TESTS, 0.0);
+      }
+    } else {
       parseFiles(context, xmlFiles);
     }
   }
index 690603fc09164b17cf80c0f0d40e6c0f20cdff75..d288fc12454411cf51dc807fc0514500c732e27f 100644 (file)
  */
 package org.sonar.plugins.surefire;
 
+import static org.hamcrest.CoreMatchers.is;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertThat;
+import static org.mockito.Matchers.*;
+import static org.mockito.Mockito.*;
+
 import org.apache.commons.lang.ObjectUtils;
 import org.custommonkey.xmlunit.DetailedDiff;
 import org.custommonkey.xmlunit.Diff;
@@ -40,15 +46,6 @@ import java.io.FileReader;
 import java.io.StringReader;
 import java.net.URISyntaxException;
 
-import static org.hamcrest.CoreMatchers.is;
-import static org.junit.Assert.assertFalse;
-import static org.junit.Assert.assertThat;
-import static org.mockito.Matchers.anyDouble;
-import static org.mockito.Matchers.anyObject;
-import static org.mockito.Matchers.argThat;
-import static org.mockito.Matchers.eq;
-import static org.mockito.Mockito.*;
-
 public class SurefireSensorTest {
 
   @Test
index 15085fee0c4d9cf6d8545c2f825ea4d767c330b8..e4d195ebe4243c6efc3a7d99134460fdc3ed9bbf 100644 (file)
@@ -26,11 +26,11 @@ import org.sonar.api.batch.SensorContext;
 import org.sonar.api.measures.CoreMetrics;
 import org.sonar.api.measures.Metric;
 import org.sonar.api.resources.*;
-import org.sonar.api.resources.File;
 import org.sonar.api.test.IsMeasure;
 import org.sonar.api.test.IsResource;
 
 import java.net.URISyntaxException;
+import java.util.Arrays;
 
 import static org.mockito.Matchers.any;
 import static org.mockito.Matchers.anyDouble;
@@ -52,34 +52,43 @@ public class AbstractSurefireParserTest {
     verify(context, times(6)).saveMeasure(argThat(new IsResource(Scopes.FILE, Qualifiers.FILE)), argThat(new IsMeasure(CoreMetrics.TEST_DATA)));
   }
 
+  /**
+   * See http://jira.codehaus.org/browse/SONAR-2371
+   */
   @Test
-  public void shouldNotFailIfNoReports() throws URISyntaxException {
+  public void shouldInsertZeroWhenNoReports() throws URISyntaxException {
     AbstractSurefireParser parser = newParser();
     SensorContext context = mockContext();
+    Project project = mock(Project.class);
 
-    parser.collect(new Project("foo"), context, getDir("noReports"));
+    parser.collect(project, context, getDir("noReports"));
 
-    verifyZeroInteractions(context);
+    verify(context).saveMeasure(CoreMetrics.TESTS, 0.0);
   }
 
+  /**
+   * See http://jira.codehaus.org/browse/SONAR-2371
+   */
   @Test
-  public void shouldNotInsertZeroOnFiles() throws URISyntaxException {
+  public void shouldNotInsertZeroWhenNoReports() throws URISyntaxException {
     AbstractSurefireParser parser = newParser();
     SensorContext context = mockContext();
+    Project project = mock(Project.class);
+    when(project.getModules()).thenReturn(Arrays.asList(new Project("foo")));
 
-    parser.collect(new Project("foo"), context, getDir("noTests"));
+    parser.collect(project, context, getDir("noReports"));
 
-    verify(context, never()).saveMeasure(any(Resource.class),(Metric)anyObject(), anyDouble());
+    verify(context, never()).saveMeasure(CoreMetrics.TESTS, 0.0);
   }
 
   @Test
-  public void shouldNotInsertMeasuresOnPomProjects() throws URISyntaxException {
+  public void shouldNotInsertZeroOnFiles() throws URISyntaxException {
     AbstractSurefireParser parser = newParser();
     SensorContext context = mockContext();
 
-    parser.collect(new Project("foo").setPackaging("pom"), context, getDir("noReports"));
+    parser.collect(new Project("foo"), context, getDir("noTests"));
 
-    verify(context, never()).saveMeasure(eq(CoreMetrics.TESTS), anyDouble());
+    verify(context, never()).saveMeasure(any(Resource.class),(Metric)anyObject(), anyDouble());
   }
 
   @Test