]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-2321 Cobertura plugin must not search for XML reports when Cobertura has not...
authorEvgeny Mandrikov <mandrikov@gmail.com>
Thu, 28 Apr 2011 15:01:03 +0000 (19:01 +0400)
committerEvgeny Mandrikov <mandrikov@gmail.com>
Thu, 28 Apr 2011 17:34:30 +0000 (21:34 +0400)
plugins/sonar-cobertura-plugin/src/main/java/org/sonar/plugins/cobertura/CoberturaSensor.java
plugins/sonar-cobertura-plugin/src/main/java/org/sonar/plugins/cobertura/api/CoberturaUtils.java
plugins/sonar-cobertura-plugin/src/test/java/org/sonar/plugins/cobertura/CoberturaMavenInitializerTest.java
plugins/sonar-cobertura-plugin/src/test/java/org/sonar/plugins/cobertura/CoberturaSensorTest.java

index 3464d5e3a2c22dfb5888c2ee7fbc752014aa380c..f4b6e30426ecd02decbc6c0025a56c9e2fe0075e 100644 (file)
  */
 package org.sonar.plugins.cobertura;
 
-import java.io.File;
-
 import org.slf4j.LoggerFactory;
+import org.sonar.api.CoreProperties;
 import org.sonar.api.batch.CoverageExtension;
 import org.sonar.api.batch.Sensor;
 import org.sonar.api.batch.SensorContext;
 import org.sonar.api.resources.JavaFile;
 import org.sonar.api.resources.Project;
 import org.sonar.api.resources.Resource;
+import org.sonar.api.utils.Logs;
 import org.sonar.plugins.cobertura.api.AbstractCoberturaParser;
-import org.sonar.plugins.cobertura.api.CoberturaUtils;
+
+import java.io.File;
 
 public class CoberturaSensor implements Sensor, CoverageExtension {
 
@@ -38,10 +39,17 @@ public class CoberturaSensor implements Sensor, CoverageExtension {
   }
 
   public void analyse(Project project, SensorContext context) {
-    File report = CoberturaUtils.getReport(project);
-    if (report != null) {
-      parseReport(report, context);
+    String path = (String) project.getProperty(CoreProperties.COBERTURA_REPORT_PATH_PROPERTY);
+    if (path == null) {
+      // wasn't configured - skip
+      return;
+    }
+    File report = project.getFileSystem().resolvePath(path);
+    if (!report.exists() || !report.isFile()) {
+      Logs.INFO.warn("Cobertura report not found at {}", report);
+      return;
     }
+    parseReport(report, context);
   }
 
   protected void parseReport(File xmlFile, final SensorContext context) {
index 98ba2bb0cf68925290f7c05a1dc524052f035568..c4e24332fe2a21b1b69c5e2861bf7ed4bff9af55 100644 (file)
@@ -35,6 +35,10 @@ public final class CoberturaUtils {
   public static final String COBERTURA_GROUP_ID = MavenUtils.GROUP_ID_CODEHAUS_MOJO;
   public static final String COBERTURA_ARTIFACT_ID = "cobertura-maven-plugin";
 
+  /**
+   * @deprecated in 2.8, because assumes that Sonar executed from Maven. Not used any more in sonar-cobertura-plugin.
+   *             See http://jira.codehaus.org/browse/SONAR-2321
+   */
   public static File getReport(Project project) {
     File report = getReportFromProperty(project);
     if (report == null) {
index 7ac644c530ca6cb48715340b2c30a75ba88c90ae..fa6ef266f75b580d07e61a5bc43c9b95a6f93996 100644 (file)
@@ -36,8 +36,11 @@ import org.junit.Before;
 import org.junit.Test;
 import org.sonar.api.CoreProperties;
 import org.sonar.api.resources.Project;
+import org.sonar.api.resources.ProjectFileSystem;
 import org.sonar.api.test.MavenTestUtils;
 
+import java.io.File;
+
 public class CoberturaMavenInitializerTest {
 
   private Project project;
@@ -86,4 +89,15 @@ public class CoberturaMavenInitializerTest {
     initializer.execute(project);
     verify(configuration).setProperty(eq(CoreProperties.COBERTURA_REPORT_PATH_PROPERTY), eq("overridden/dir/coverage.xml"));
   }
+
+  @Test
+  public void shouldSetDefaultReportPath() {
+    ProjectFileSystem pfs = mock(ProjectFileSystem.class);
+    when(pfs.getReportOutputDir()).thenReturn(new File("target/sites"));
+    Configuration configuration = mock(Configuration.class);
+    when(project.getConfiguration()).thenReturn(configuration);
+    when(project.getFileSystem()).thenReturn(pfs);
+    initializer.execute(project);
+    verify(configuration).setProperty(eq(CoreProperties.COBERTURA_REPORT_PATH_PROPERTY), eq("target/sites/cobertura/coverage.xml"));
+  }
 }
index 682f4e682af0ade34d9ca399b8e0f9255e6853bd..8e907b6111326a087a0dc9aad00dd39e757db3c1 100644 (file)
  */
 package org.sonar.plugins.cobertura;
 
+import static org.hamcrest.CoreMatchers.is;
+import static org.mockito.Matchers.*;
+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 org.junit.Before;
 import org.junit.Test;
+import org.sonar.api.CoreProperties;
 import org.sonar.api.batch.SensorContext;
 import org.sonar.api.measures.CoreMetrics;
 import org.sonar.api.measures.Measure;
@@ -30,27 +39,43 @@ import org.sonar.api.test.IsResource;
 import java.io.File;
 import java.net.URISyntaxException;
 
-import static org.hamcrest.CoreMatchers.is;
-import static org.mockito.Matchers.any;
-import static org.mockito.Matchers.anyDouble;
-import static org.mockito.Matchers.argThat;
-import static org.mockito.Matchers.eq;
-import static org.mockito.Mockito.*;
-
 public class CoberturaSensorTest {
 
+  private SensorContext context;
+  private CoberturaSensor sensor;
+
+  @Before
+  public void setUp() {
+    context = mock(SensorContext.class);
+    sensor = new CoberturaSensor();
+  }
+
+  @Test
+  public void shouldNotFailIfReportNotSpecifiedOrNotFound() throws URISyntaxException {
+    ProjectFileSystem pfs = mock(ProjectFileSystem.class);
+    when(pfs.resolvePath(anyString()))
+        .thenReturn(new File("notFound.xml"));
+
+    Project project = mock(Project.class);
+    when(project.getFileSystem()).thenReturn(pfs);
+    when(project.getProperty(eq(CoreProperties.COBERTURA_REPORT_PATH_PROPERTY)))
+        .thenReturn("notFound.xml")
+        .thenReturn(null);
+
+    sensor.analyse(project, context);
+    sensor.analyse(project, context);
+  }
+
   @Test
   public void doNotCollectProjectCoverage() throws URISyntaxException {
-    SensorContext context = mock(SensorContext.class);
-    new CoberturaSensor().parseReport(getCoverageReport(), context);
+    sensor.parseReport(getCoverageReport(), context);
 
     verify(context, never()).saveMeasure(eq(CoreMetrics.COVERAGE), anyDouble());
   }
 
   @Test
   public void doNotCollectProjectLineCoverage() throws URISyntaxException {
-    SensorContext context = mock(SensorContext.class);
-    new CoberturaSensor().parseReport(getCoverageReport(), context);
+    sensor.parseReport(getCoverageReport(), context);
 
     verify(context, never()).saveMeasure(eq(CoreMetrics.LINE_COVERAGE), anyDouble());
     verify(context, never()).saveMeasure(argThat(new IsMeasure(CoreMetrics.COVERAGE_LINE_HITS_DATA)));
@@ -58,16 +83,14 @@ public class CoberturaSensorTest {
 
   @Test
   public void doNotCollectProjectBranchCoverage() throws URISyntaxException {
-    SensorContext context = mock(SensorContext.class);
-    new CoberturaSensor().parseReport(getCoverageReport(), context);
+    sensor.parseReport(getCoverageReport(), context);
 
     verify(context, never()).saveMeasure(eq(CoreMetrics.BRANCH_COVERAGE), anyDouble());
   }
 
   @Test
   public void collectPackageLineCoverage() throws URISyntaxException {
-    SensorContext context = mock(SensorContext.class);
-    new CoberturaSensor().parseReport(getCoverageReport(), context);
+    sensor.parseReport(getCoverageReport(), context);
 
     verify(context, never()).saveMeasure((Resource) argThat(is(JavaPackage.class)), eq(CoreMetrics.LINE_COVERAGE), anyDouble());
     verify(context, never()).saveMeasure((Resource) argThat(is(JavaPackage.class)), eq(CoreMetrics.UNCOVERED_LINES), anyDouble());
@@ -75,8 +98,7 @@ public class CoberturaSensorTest {
 
   @Test
   public void collectPackageBranchCoverage() throws URISyntaxException {
-    SensorContext context = mock(SensorContext.class);
-    new CoberturaSensor().parseReport(getCoverageReport(), context);
+    sensor.parseReport(getCoverageReport(), context);
 
     verify(context, never()).saveMeasure((Resource) argThat(is(JavaPackage.class)), eq(CoreMetrics.BRANCH_COVERAGE), anyDouble());
     verify(context, never()).saveMeasure((Resource) argThat(is(JavaPackage.class)), eq(CoreMetrics.UNCOVERED_CONDITIONS), anyDouble());
@@ -84,17 +106,15 @@ public class CoberturaSensorTest {
 
   @Test
   public void packageCoverageIsCalculatedLaterByDecorator() throws URISyntaxException {
-    SensorContext context = mock(SensorContext.class);
-    new CoberturaSensor().parseReport(getCoverageReport(), context);
+    sensor.parseReport(getCoverageReport(), context);
 
     verify(context, never()).saveMeasure((Resource) argThat(is(JavaPackage.class)), eq(CoreMetrics.COVERAGE), anyDouble());
   }
 
   @Test
   public void collectFileLineCoverage() throws URISyntaxException {
-    SensorContext context = mock(SensorContext.class);
     when(context.getResource(any(Resource.class))).thenReturn(new JavaFile("org.sonar.MyClass"));
-    new CoberturaSensor().parseReport(getCoverageReport(), context);
+    sensor.parseReport(getCoverageReport(), context);
 
     final JavaFile file = new JavaFile("org.apache.commons.chain.config.ConfigParser");
     verify(context).saveMeasure(eq(file), argThat(new IsMeasure(CoreMetrics.LINES_TO_COVER, 30.0)));
@@ -103,9 +123,8 @@ public class CoberturaSensorTest {
 
   @Test
   public void collectFileBranchCoverage() throws URISyntaxException {
-    SensorContext context = mock(SensorContext.class);
     when(context.getResource(any(Resource.class))).thenReturn(new JavaFile("org.sonar.MyClass"));
-    new CoberturaSensor().parseReport(getCoverageReport(), context);
+    sensor.parseReport(getCoverageReport(), context);
 
     final JavaFile file = new JavaFile("org.apache.commons.chain.config.ConfigParser");
     verify(context).saveMeasure(eq(file), argThat(new IsMeasure(CoreMetrics.CONDITIONS_TO_COVER, 6.0)));
@@ -114,16 +133,14 @@ public class CoberturaSensorTest {
 
   @Test
   public void testDoNotSaveMeasureOnResourceWhichDoesntExistInTheContext() throws URISyntaxException {
-    SensorContext context = mock(SensorContext.class);
     when(context.getResource(any(Resource.class))).thenReturn(null);
-    new CoberturaSensor().parseReport(getCoverageReport(), context);
+    sensor.parseReport(getCoverageReport(), context);
     verify(context, never()).saveMeasure(any(Resource.class), any(Measure.class));
   }
 
   @Test
   public void javaInterfaceHasNoCoverage() throws URISyntaxException {
-    SensorContext context = mock(SensorContext.class);
-    new CoberturaSensor().parseReport(getCoverageReport(), context);
+    sensor.parseReport(getCoverageReport(), context);
 
     final JavaFile interfaze = new JavaFile("org.apache.commons.chain.Chain");
     verify(context, never()).saveMeasure(eq(interfaze), argThat(new IsMeasure(CoreMetrics.COVERAGE)));
@@ -141,9 +158,8 @@ public class CoberturaSensorTest {
   public void shouldInsertCoverageAtFileLevel() throws URISyntaxException {
     File coverage = new File(getClass().getResource(
         "/org/sonar/plugins/cobertura/CoberturaSensorTest/shouldInsertCoverageAtFileLevel/coverage.xml").toURI());
-    SensorContext context = mock(SensorContext.class);
     when(context.getResource(any(Resource.class))).thenReturn(new JavaFile("org.sonar.MyClass"));
-    new CoberturaSensor().parseReport(coverage, context);
+    sensor.parseReport(coverage, context);
 
     verify(context).saveMeasure(argThat(new IsResource(Scopes.FILE, Qualifiers.CLASS, "org.sonar.samples.InnerClass")),
         argThat(new IsMeasure(CoreMetrics.LINES_TO_COVER, 35.0)));
@@ -191,28 +207,24 @@ public class CoberturaSensorTest {
 
   @Test
   public void collectFileLineHitsData() throws URISyntaxException {
-    SensorContext context = mock(SensorContext.class);
     when(context.getResource(any(Resource.class))).thenReturn(new JavaFile("org.sonar.MyClass"));
-    new CoberturaSensor().parseReport(getCoverageReport(), context);
+    sensor.parseReport(getCoverageReport(), context);
     verify(context).saveMeasure(
         eq(new JavaFile("org.apache.commons.chain.impl.CatalogBase")),
         argThat(new IsMeasure(CoreMetrics.COVERAGE_LINE_HITS_DATA,
             "48=117;56=234;66=0;67=0;68=0;84=999;86=999;98=318;111=18;121=0;122=0;125=0;126=0;127=0;128=0;131=0;133=0")));
   }
 
-
   @Test
   public void shouldNotCountTwiceAnonymousClasses() throws URISyntaxException {
     File coverage = new File(getClass().getResource("/org/sonar/plugins/cobertura/CoberturaSensorTest/shouldNotCountTwiceAnonymousClasses.xml").toURI());
-    SensorContext context = mock(SensorContext.class);
     when(context.getResource(any(Resource.class))).thenReturn(new JavaFile("org.sonar.samples.MyClass"));
-    new CoberturaSensor().parseReport(coverage, context);
+    sensor.parseReport(coverage, context);
 
     verify(context).saveMeasure(argThat(new IsResource(Scopes.FILE, Qualifiers.CLASS, "org.sonar.samples.MyFile")),
         argThat(new IsMeasure(CoreMetrics.LINES_TO_COVER, 5.0))); // do not count line 26 twice
   }
 
-
   private File getCoverageReport() throws URISyntaxException {
     return new File(getClass().getResource("/org/sonar/plugins/cobertura/CoberturaSensorTest/commons-chain-coverage.xml").toURI());
   }