]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-3911 refactor the plugins cobertura and jacoco
authorSimon Brandhof <simon.brandhof@gmail.com>
Mon, 29 Oct 2012 20:25:21 +0000 (21:25 +0100)
committerSimon Brandhof <simon.brandhof@gmail.com>
Mon, 29 Oct 2012 20:26:49 +0000 (21:26 +0100)
26 files changed:
plugins/sonar-cobertura-plugin/src/main/java/org/sonar/plugins/cobertura/CoberturaMavenInitializer.java
plugins/sonar-cobertura-plugin/src/main/java/org/sonar/plugins/cobertura/CoberturaMavenPluginHandler.java
plugins/sonar-cobertura-plugin/src/main/java/org/sonar/plugins/cobertura/CoberturaPlugin.java
plugins/sonar-cobertura-plugin/src/main/java/org/sonar/plugins/cobertura/CoberturaSensor.java
plugins/sonar-cobertura-plugin/src/main/java/org/sonar/plugins/cobertura/CoberturaSettings.java [new file with mode: 0644]
plugins/sonar-cobertura-plugin/src/test/java/org/sonar/plugins/cobertura/CoberturaMavenInitializerTest.java
plugins/sonar-cobertura-plugin/src/test/java/org/sonar/plugins/cobertura/CoberturaMavenPluginHandlerTest.java
plugins/sonar-cobertura-plugin/src/test/java/org/sonar/plugins/cobertura/CoberturaPluginTest.java
plugins/sonar-cobertura-plugin/src/test/java/org/sonar/plugins/cobertura/CoberturaSensorTest.java
plugins/sonar-cobertura-plugin/src/test/java/org/sonar/plugins/cobertura/CoberturaSettingsTest.java [new file with mode: 0644]
plugins/sonar-jacoco-plugin/src/main/java/org/sonar/plugins/jacoco/JaCoCoItSensor.java
plugins/sonar-jacoco-plugin/src/main/java/org/sonar/plugins/jacoco/JaCoCoOverallSensor.java
plugins/sonar-jacoco-plugin/src/main/java/org/sonar/plugins/jacoco/JaCoCoPlugin.java
plugins/sonar-jacoco-plugin/src/main/java/org/sonar/plugins/jacoco/JaCoCoSensor.java
plugins/sonar-jacoco-plugin/src/main/java/org/sonar/plugins/jacoco/JacocoAntInitializer.java
plugins/sonar-jacoco-plugin/src/main/java/org/sonar/plugins/jacoco/JacocoConfiguration.java
plugins/sonar-jacoco-plugin/src/main/java/org/sonar/plugins/jacoco/JacocoMavenInitializer.java
plugins/sonar-jacoco-plugin/src/test/java/org/sonar/plugins/jacoco/JaCoCoItSensorTest.java
plugins/sonar-jacoco-plugin/src/test/java/org/sonar/plugins/jacoco/JaCoCoOverallSensorTest.java
plugins/sonar-jacoco-plugin/src/test/java/org/sonar/plugins/jacoco/JaCoCoSensorTest.java
plugins/sonar-jacoco-plugin/src/test/java/org/sonar/plugins/jacoco/JacocoConfigurationTest.java
plugins/sonar-jacoco-plugin/src/test/java/org/sonar/plugins/jacoco/JacocoMavenInitializerTest.java
sonar-batch/src/main/java/org/sonar/batch/bootstrap/BatchPluginRepository.java
sonar-batch/src/main/java/org/sonar/batch/bootstrap/DatabaseBatchCompatibility.java
sonar-plugin-api/src/main/java/org/sonar/api/platform/PluginRepository.java
sonar-server/src/main/java/org/sonar/server/plugins/DefaultServerPluginRepository.java

index 473064784f3c101d0049f25e7914640098a42830..54135a9a7ae45cd0915e066ae90ddd9479d6cbbd 100644 (file)
@@ -26,10 +26,8 @@ import org.sonar.api.batch.Initializer;
 import org.sonar.api.batch.maven.DependsUponMavenPlugin;
 import org.sonar.api.batch.maven.MavenPlugin;
 import org.sonar.api.batch.maven.MavenPluginHandler;
-import org.sonar.api.resources.Java;
 import org.sonar.api.resources.Project;
 import org.sonar.plugins.cobertura.api.CoberturaUtils;
-import org.sonar.plugins.java.api.JavaSettings;
 
 /**
  * Provides {@link CoberturaMavenPluginHandler} and configures correct path to report.
@@ -38,26 +36,20 @@ import org.sonar.plugins.java.api.JavaSettings;
 public class CoberturaMavenInitializer extends Initializer implements CoverageExtension, DependsUponMavenPlugin {
 
   private CoberturaMavenPluginHandler handler;
-  private JavaSettings javaSettings;
+  private CoberturaSettings coberturaSettings;
 
-  public CoberturaMavenInitializer(CoberturaMavenPluginHandler handler, JavaSettings javaSettings) {
+  public CoberturaMavenInitializer(CoberturaMavenPluginHandler handler, CoberturaSettings coberturaSettings) {
     this.handler = handler;
-    this.javaSettings = javaSettings;
+    this.coberturaSettings = coberturaSettings;
   }
 
   public MavenPluginHandler getMavenPluginHandler(Project project) {
-    if (project.getAnalysisType().equals(Project.AnalysisType.DYNAMIC)) {
-      return handler;
-    }
-    return null;
+    return project.getAnalysisType().equals(Project.AnalysisType.DYNAMIC) ? handler : null;
   }
 
   @Override
   public boolean shouldExecuteOnProject(Project project) {
-    return Java.KEY.equals(project.getLanguageKey())
-      && CoberturaPlugin.PLUGIN_KEY.equals(javaSettings.getEnabledCoveragePlugin())
-      && !project.getFileSystem().mainFiles(Java.KEY).isEmpty()
-      && project.getAnalysisType().isDynamic(true);
+    return coberturaSettings.isEnabled(project);
   }
 
   @Override
@@ -78,9 +70,9 @@ public class CoberturaMavenInitializer extends Initializer implements CoverageEx
 
   private static String getReportPathFromPluginConfiguration(Project project) {
     MavenPlugin mavenPlugin = MavenPlugin.getPlugin(
-        project.getPom(),
-        CoberturaUtils.COBERTURA_GROUP_ID,
-        CoberturaUtils.COBERTURA_ARTIFACT_ID);
+      project.getPom(),
+      CoberturaUtils.COBERTURA_GROUP_ID,
+      CoberturaUtils.COBERTURA_ARTIFACT_ID);
     if (mavenPlugin != null) {
       String path = mavenPlugin.getParameter("outputDirectory");
       if (path != null) {
index 2be357df91e8b36144c183b090cae62e3220563c..90628197be7c75a77b302076000388eb77a927ee 100644 (file)
 package org.sonar.plugins.cobertura;
 
 import org.apache.commons.lang.StringUtils;
-import org.sonar.api.CoreProperties;
 import org.sonar.api.batch.maven.MavenPlugin;
 import org.sonar.api.batch.maven.MavenPluginHandler;
 import org.sonar.api.batch.maven.MavenSurefireUtils;
-import org.sonar.api.config.Settings;
 import org.sonar.api.resources.Project;
 import org.sonar.plugins.cobertura.api.CoberturaUtils;
 
 public class CoberturaMavenPluginHandler implements MavenPluginHandler {
 
-  private Settings settings;
+  private CoberturaSettings settings;
 
-  public CoberturaMavenPluginHandler(Settings settings) {
+  public CoberturaMavenPluginHandler(CoberturaSettings settings) {
     this.settings = settings;
   }
 
@@ -72,14 +70,6 @@ public class CoberturaMavenPluginHandler implements MavenPluginHandler {
       }
       coberturaPlugin.addParameter("instrumentation/excludes/exclude", pattern);
     }
-    String maxmem = "";
-    // http://jira.codehaus.org/browse/SONAR-2897: there used to be a typo in the parameter name (was "sonar.cobertura.maxmen")
-    if (settings.hasKey("sonar.cobertura.maxmen")) {
-      maxmem = settings.getString("sonar.cobertura.maxmen");
-    } else {
-      // use the "normal" key
-      maxmem = settings.getString(CoreProperties.COBERTURA_MAXMEM_PROPERTY);
-    }
-    coberturaPlugin.setParameter("maxmem", maxmem);
+    coberturaPlugin.setParameter("maxmem", settings.getMaxMemory());
   }
 }
index 6bdda1bf9da3ba566bd7c207fc01254010e498f4..8fa103a25b80668fbd513b3780bbf9849f5de72f 100644 (file)
@@ -47,6 +47,7 @@ public final class CoberturaPlugin extends SonarPlugin {
 
   public List<?> getExtensions() {
     return ImmutableList.of(
+        CoberturaSettings.class,
         CoberturaSensor.class,
         CoberturaMavenPluginHandler.class,
         CoberturaMavenInitializer.class);
index 91c0347b3a6973287568353513e60298fa6b9a97..2293ff8ca8931d01c8a1bac224219609cc44c20e 100644 (file)
@@ -24,28 +24,23 @@ 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.Java;
 import org.sonar.api.resources.JavaFile;
 import org.sonar.api.resources.Project;
 import org.sonar.api.resources.Resource;
 import org.sonar.plugins.cobertura.api.AbstractCoberturaParser;
-import org.sonar.plugins.java.api.JavaSettings;
 
 import java.io.File;
 
 public class CoberturaSensor implements Sensor, CoverageExtension {
 
-  private JavaSettings javaSettings;
+  private CoberturaSettings settings;
 
-  public CoberturaSensor(JavaSettings javaSettings) {
-    this.javaSettings = javaSettings;
+  public CoberturaSensor(CoberturaSettings settings) {
+    this.settings = settings;
   }
 
   public boolean shouldExecuteOnProject(Project project) {
-    return Java.KEY.equals(project.getLanguageKey())
-      && CoberturaPlugin.PLUGIN_KEY.equals(javaSettings.getEnabledCoveragePlugin())
-      && !project.getFileSystem().mainFiles(Java.KEY).isEmpty()
-      && project.getAnalysisType().isDynamic(true);
+    return settings.isEnabled(project);
   }
 
   public void analyse(Project project, SensorContext context) {
diff --git a/plugins/sonar-cobertura-plugin/src/main/java/org/sonar/plugins/cobertura/CoberturaSettings.java b/plugins/sonar-cobertura-plugin/src/main/java/org/sonar/plugins/cobertura/CoberturaSettings.java
new file mode 100644 (file)
index 0000000..441274d
--- /dev/null
@@ -0,0 +1,53 @@
+/*
+ * Sonar, open source software quality management tool.
+ * Copyright (C) 2008-2012 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * Sonar is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * Sonar is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with Sonar; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02
+ */
+package org.sonar.plugins.cobertura;
+
+import org.apache.commons.lang.StringUtils;
+import org.sonar.api.BatchExtension;
+import org.sonar.api.CoreProperties;
+import org.sonar.api.config.Settings;
+import org.sonar.api.resources.Java;
+import org.sonar.api.resources.Project;
+import org.sonar.plugins.java.api.JavaSettings;
+
+public class CoberturaSettings implements BatchExtension {
+  private Settings settings;
+  private JavaSettings javaSettings;
+
+  public CoberturaSettings(Settings settings, JavaSettings javaSettings) {
+    this.settings = settings;
+    this.javaSettings = javaSettings;
+  }
+
+  public boolean isEnabled(Project project) {
+    return Java.KEY.equals(project.getLanguageKey())
+      && CoberturaPlugin.PLUGIN_KEY.equals(javaSettings.getEnabledCoveragePlugin())
+      && !project.getFileSystem().mainFiles(Java.KEY).isEmpty()
+      && project.getAnalysisType().isDynamic(true);
+  }
+
+  public String getMaxMemory() {
+    // http://jira.codehaus.org/browse/SONAR-2897: there used to be a typo in the parameter name (was "sonar.cobertura.maxmen")
+    return StringUtils.defaultIfEmpty(
+      settings.getString("sonar.cobertura.maxmen"),
+      settings.getString(CoreProperties.COBERTURA_MAXMEM_PROPERTY)
+    );
+  }
+}
index 1e3b4790cabac9dd4270340aab6a3635edca47d2..b7788a0902f720073f2285095de195099dfa8f4b 100644 (file)
  */
 package org.sonar.plugins.cobertura;
 
-import com.google.common.collect.Lists;
 import org.apache.commons.configuration.Configuration;
 import org.apache.maven.project.MavenProject;
 import org.junit.Before;
 import org.junit.Test;
 import org.sonar.api.CoreProperties;
-import org.sonar.api.config.Settings;
-import org.sonar.api.resources.InputFile;
-import org.sonar.api.resources.InputFileUtils;
 import org.sonar.api.resources.Project;
 import org.sonar.api.resources.ProjectFileSystem;
 import org.sonar.api.test.MavenTestUtils;
-import org.sonar.plugins.java.api.JavaSettings;
 
 import java.io.File;
-import java.util.ArrayList;
 
 import static org.fest.assertions.Assertions.assertThat;
-import static org.hamcrest.CoreMatchers.is;
-import static org.hamcrest.Matchers.nullValue;
-import static org.hamcrest.core.IsNot.not;
-import static org.junit.Assert.assertThat;
 import static org.mockito.Matchers.anyString;
 import static org.mockito.Matchers.eq;
 import static org.mockito.Mockito.mock;
@@ -52,82 +42,34 @@ public class CoberturaMavenInitializerTest {
 
   private Project project;
   private CoberturaMavenInitializer initializer;
+  private CoberturaMavenPluginHandler handler;
+  private CoberturaSettings settings;
 
   @Before
   public void setUp() {
     project = mock(Project.class);
-    JavaSettings javaSettings = mock(JavaSettings.class);
-    when(javaSettings.getEnabledCoveragePlugin()).thenReturn("cobertura");
-    initializer = new CoberturaMavenInitializer(new CoberturaMavenPluginHandler(new Settings()), javaSettings);
+    handler = mock(CoberturaMavenPluginHandler.class);
+    settings = mock(CoberturaSettings.class);
+    initializer = new CoberturaMavenInitializer(handler, settings);
   }
 
-  @Test
-  public void shouldNotAnalyseIfNoJavaProject() {
-    Project project = mock(Project.class);
-    when(project.getLanguageKey()).thenReturn("php");
-    assertThat(initializer.shouldExecuteOnProject(project)).isFalse();
-  }
-
-  @Test
-  public void shouldNotAnalyseIfJavaProjectButNoSource() {
-    Project project = mock(Project.class);
-    ProjectFileSystem fs = mock(ProjectFileSystem.class);
-    when(fs.mainFiles("java")).thenReturn(new ArrayList<InputFile>());
-    when(project.getFileSystem()).thenReturn(fs);
-    when(project.getLanguageKey()).thenReturn("java");
-    assertThat(initializer.shouldExecuteOnProject(project)).isFalse();
-  }
-
-  @Test
-  public void shouldNotAnalyseIfJavaProjectWithSourceButStatic() {
-    Project project = mock(Project.class);
-    ProjectFileSystem fs = mock(ProjectFileSystem.class);
-    when(fs.mainFiles("java")).thenReturn(Lists.newArrayList(InputFileUtils.create(null, "")));
-    when(project.getFileSystem()).thenReturn(fs);
-    when(project.getLanguageKey()).thenReturn("java");
-    when(project.getAnalysisType()).thenReturn(Project.AnalysisType.STATIC);
-    assertThat(initializer.shouldExecuteOnProject(project)).isFalse();
-  }
-
-  @Test
-  public void shouldAnalyse() {
-    Project project = mock(Project.class);
-    ProjectFileSystem fs = mock(ProjectFileSystem.class);
-    when(fs.mainFiles("java")).thenReturn(Lists.newArrayList(InputFileUtils.create(null, "")));
-    when(project.getFileSystem()).thenReturn(fs);
-    when(project.getLanguageKey()).thenReturn("java");
-    when(project.getAnalysisType()).thenReturn(Project.AnalysisType.DYNAMIC);
-    assertThat(initializer.shouldExecuteOnProject(project)).isTrue();
-  }
-
-  @Test
-  public void shouldAnalyseIfReuseDynamicReports() {
-    Project project = mock(Project.class);
-    ProjectFileSystem fs = mock(ProjectFileSystem.class);
-    when(fs.mainFiles("java")).thenReturn(Lists.newArrayList(InputFileUtils.create(null, "")));
-    when(project.getFileSystem()).thenReturn(fs);
-    when(project.getLanguageKey()).thenReturn("java");
-    when(project.getAnalysisType()).thenReturn(Project.AnalysisType.REUSE_REPORTS);
-    assertThat(initializer.shouldExecuteOnProject(project)).isTrue();
-  }
 
   @Test
   public void doNotExecuteMavenPluginIfReuseReports() {
     when(project.getAnalysisType()).thenReturn(Project.AnalysisType.REUSE_REPORTS);
-    assertThat(initializer.getMavenPluginHandler(project), nullValue());
+    assertThat(initializer.getMavenPluginHandler(project)).isNull();
   }
 
   @Test
   public void doNotExecuteMavenPluginIfStaticAnalysis() {
     when(project.getAnalysisType()).thenReturn(Project.AnalysisType.STATIC);
-    assertThat(initializer.getMavenPluginHandler(project), nullValue());
+    assertThat(initializer.getMavenPluginHandler(project)).isNull();
   }
 
   @Test
   public void executeMavenPluginIfDynamicAnalysis() {
     when(project.getAnalysisType()).thenReturn(Project.AnalysisType.DYNAMIC);
-    assertThat(initializer.getMavenPluginHandler(project), not(nullValue()));
-    assertThat(initializer.getMavenPluginHandler(project).getArtifactId(), is("cobertura-maven-plugin"));
+    assertThat(initializer.getMavenPluginHandler(project)).isSameAs(handler);
   }
 
   @Test
index c140ce4be47a0f4dff920277b4770956ea96fa31..756fc211bdab6df130451915dfc150ebd4a21f79 100644 (file)
  */
 package org.sonar.plugins.cobertura;
 
-import static org.hamcrest.CoreMatchers.is;
-import static org.junit.Assert.assertThat;
-import static org.mockito.Mockito.mock;
-import static org.mockito.Mockito.when;
-
 import org.apache.maven.project.MavenProject;
 import org.junit.Before;
 import org.junit.Test;
+import org.mockito.Mockito;
 import org.sonar.api.batch.maven.MavenPlugin;
-import org.sonar.api.config.PropertyDefinitions;
-import org.sonar.api.config.Settings;
 import org.sonar.api.resources.Project;
 import org.sonar.plugins.cobertura.api.CoberturaUtils;
 
+import static org.fest.assertions.Assertions.assertThat;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
 public class CoberturaMavenPluginHandlerTest {
 
-  protected CoberturaMavenPluginHandler handler;
+  private CoberturaMavenPluginHandler handler;
+  private CoberturaSettings settings;
 
   @Before
   public void before() {
-    handler = new CoberturaMavenPluginHandler(new Settings(new PropertyDefinitions(CoberturaPlugin.class)));
+    settings = mock(CoberturaSettings.class);
+    handler = new CoberturaMavenPluginHandler(settings);
   }
 
   @Test
-  public void notFixedVersion() {
+  public void users_could_change_version() {
     // first of all, version was fixed : see http://jira.codehaus.org/browse/SONAR-1055
     // but it's more reasonable to let users change the version : see http://jira.codehaus.org/browse/SONAR-1310
-    assertThat(new CoberturaMavenPluginHandler(null).isFixedVersion(), is(false));
+    assertThat(handler.isFixedVersion()).isFalse();
   }
 
   @Test
-  public void activateXmlFormat() {
+  public void test_metadata() {
+    assertThat(handler.getGroupId()).isEqualTo("org.codehaus.mojo");
+    assertThat(handler.getArtifactId()).isEqualTo("cobertura-maven-plugin");
+    assertThat(handler.getVersion()).isEqualTo("2.5.1");
+    assertThat(handler.getGoals()).containsOnly("cobertura");
+  }
+
+  @Test
+  public void should_enable_xml_format() {
     Project project = mock(Project.class);
     when(project.getPom()).thenReturn(new MavenProject());
     when(project.getExclusionPatterns()).thenReturn(new String[0]);
@@ -58,38 +66,31 @@ public class CoberturaMavenPluginHandlerTest {
     MavenPlugin coberturaPlugin = new MavenPlugin(CoberturaUtils.COBERTURA_GROUP_ID, CoberturaUtils.COBERTURA_ARTIFACT_ID, null);
     handler.configure(project, coberturaPlugin);
 
-    assertThat(coberturaPlugin.getParameter("formats/format"), is("xml"));
+    assertThat(coberturaPlugin.getParameter("formats/format")).isEqualTo("xml");
   }
 
   @Test
-  public void setCoberturaExclusions() {
+  public void should_set_cobertura_exclusions() {
     Project project = mock(Project.class);
     when(project.getPom()).thenReturn(new MavenProject());
-    when(project.getExclusionPatterns()).thenReturn(new String[] { "**/Foo.java", "com/*Test.*", "com/*" });
+    when(project.getExclusionPatterns()).thenReturn(new String[]{"**/Foo.java", "com/*Test.*", "com/*"});
 
     MavenPlugin coberturaPlugin = new MavenPlugin(CoberturaUtils.COBERTURA_GROUP_ID, CoberturaUtils.COBERTURA_ARTIFACT_ID, null);
     handler.configure(project, coberturaPlugin);
 
-    assertThat(coberturaPlugin.getParameters("instrumentation/excludes/exclude")[0], is("**/Foo.class"));
-    assertThat(coberturaPlugin.getParameters("instrumentation/excludes/exclude")[1], is("com/*Test.*"));
-    assertThat(coberturaPlugin.getParameters("instrumentation/excludes/exclude")[2], is("com/*.class"));
+    assertThat(coberturaPlugin.getParameters("instrumentation/excludes/exclude")).isEqualTo(new String[]{
+      "**/Foo.class", "com/*Test.*", "com/*.class"
+    });
   }
 
   @Test
-  // http://jira.codehaus.org/browse/SONAR-2897: there used to be a typo in the parameter name (was "sonar.cobertura.maxmen")
-  public void checkOldParamNameCompatibility() {
-    Settings settings = new Settings(new PropertyDefinitions(CoberturaPlugin.class));
-    settings.setProperty("sonar.cobertura.maxmen", "FOO");
-    CoberturaMavenPluginHandler coberturaMavenPluginHandler = new CoberturaMavenPluginHandler(settings);
-
-    Project project = mock(Project.class);
-    when(project.getPom()).thenReturn(new MavenProject());
-    when(project.getExclusionPatterns()).thenReturn(new String[0]);
-
+  public void should_set_max_memory() {
+    when(settings.getMaxMemory()).thenReturn("128m");
     MavenPlugin coberturaPlugin = new MavenPlugin(CoberturaUtils.COBERTURA_GROUP_ID, CoberturaUtils.COBERTURA_ARTIFACT_ID, null);
 
-    coberturaMavenPluginHandler.configure(project, coberturaPlugin);
+    Project project = mock(Project.class, Mockito.RETURNS_MOCKS);
+    handler.configure(project, coberturaPlugin);
 
-    assertThat(coberturaPlugin.getParameter("maxmem"), is("FOO"));
+    assertThat(coberturaPlugin.getParameter("maxmem")).isEqualTo("128m");
   }
 }
index deb4fcf7fc228c69e34b51a219d6e5125eb2155f..6d26094addf280cf697fdc5fb934510c8f3e76a0 100644 (file)
  */
 package org.sonar.plugins.cobertura;
 
-import static org.hamcrest.number.OrderingComparisons.greaterThan;
-import static org.junit.Assert.assertThat;
 import org.junit.Test;
 
+import static org.fest.assertions.Assertions.assertThat;
+
 public class CoberturaPluginTest {
 
   @Test
-  public void coberturaExtensions() {
-    assertThat(new CoberturaPlugin().getExtensions().size(), greaterThan(1));
+  public void test_getExtensions() {
+    assertThat(new CoberturaPlugin().getExtensions().size()).isGreaterThan(1);
   }
 }
index 6129610d1834722280b62638483ba1ee63a420d1..3262d8037d105083ea954d3981f95a2f0a8f84f9 100644 (file)
  */
 package org.sonar.plugins.cobertura;
 
-import com.google.common.collect.Lists;
 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;
-import org.sonar.api.resources.InputFile;
-import org.sonar.api.resources.InputFileUtils;
 import org.sonar.api.resources.JavaFile;
 import org.sonar.api.resources.JavaPackage;
 import org.sonar.api.resources.Project;
@@ -37,13 +34,10 @@ import org.sonar.api.resources.Resource;
 import org.sonar.api.resources.Scopes;
 import org.sonar.api.test.IsMeasure;
 import org.sonar.api.test.IsResource;
-import org.sonar.plugins.java.api.JavaSettings;
 
 import java.io.File;
 import java.net.URISyntaxException;
-import java.util.ArrayList;
 
-import static org.fest.assertions.Assertions.assertThat;
 import static org.hamcrest.CoreMatchers.is;
 import static org.mockito.Matchers.any;
 import static org.mockito.Matchers.anyDouble;
@@ -63,72 +57,21 @@ public class CoberturaSensorTest {
   @Before
   public void setUp() {
     context = mock(SensorContext.class);
-    JavaSettings javaSettings = mock(JavaSettings.class);
-    when(javaSettings.getEnabledCoveragePlugin()).thenReturn("cobertura");
-    sensor = new CoberturaSensor(javaSettings);
-  }
-
-  @Test
-  public void shouldNotAnalyseIfNoJavaProject() {
-    Project project = mock(Project.class);
-    when(project.getLanguageKey()).thenReturn("php");
-    assertThat(sensor.shouldExecuteOnProject(project)).isFalse();
-  }
-
-  @Test
-  public void shouldNotAnalyseIfJavaProjectButNoSource() {
-    Project project = mock(Project.class);
-    ProjectFileSystem fs = mock(ProjectFileSystem.class);
-    when(fs.mainFiles("java")).thenReturn(new ArrayList<InputFile>());
-    when(project.getFileSystem()).thenReturn(fs);
-    when(project.getLanguageKey()).thenReturn("java");
-    assertThat(sensor.shouldExecuteOnProject(project)).isFalse();
-  }
-
-  @Test
-  public void shouldNotAnalyseIfJavaProjectWithSourceButStatic() {
-    Project project = mock(Project.class);
-    ProjectFileSystem fs = mock(ProjectFileSystem.class);
-    when(fs.mainFiles("java")).thenReturn(Lists.newArrayList(InputFileUtils.create(null, "")));
-    when(project.getFileSystem()).thenReturn(fs);
-    when(project.getLanguageKey()).thenReturn("java");
-    when(project.getAnalysisType()).thenReturn(Project.AnalysisType.STATIC);
-    assertThat(sensor.shouldExecuteOnProject(project)).isFalse();
-  }
-
-  @Test
-  public void shouldAnalyse() {
-    Project project = mock(Project.class);
-    ProjectFileSystem fs = mock(ProjectFileSystem.class);
-    when(fs.mainFiles("java")).thenReturn(Lists.newArrayList(InputFileUtils.create(null, "")));
-    when(project.getFileSystem()).thenReturn(fs);
-    when(project.getLanguageKey()).thenReturn("java");
-    when(project.getAnalysisType()).thenReturn(Project.AnalysisType.DYNAMIC);
-    assertThat(sensor.shouldExecuteOnProject(project)).isTrue();
-  }
-
-  @Test
-  public void shouldAnalyseIfReuseDynamicReports() {
-    Project project = mock(Project.class);
-    ProjectFileSystem fs = mock(ProjectFileSystem.class);
-    when(fs.mainFiles("java")).thenReturn(Lists.newArrayList(InputFileUtils.create(null, "")));
-    when(project.getFileSystem()).thenReturn(fs);
-    when(project.getLanguageKey()).thenReturn("java");
-    when(project.getAnalysisType()).thenReturn(Project.AnalysisType.REUSE_REPORTS);
-    assertThat(sensor.shouldExecuteOnProject(project)).isTrue();
+    CoberturaSettings settings = mock(CoberturaSettings.class);
+    sensor = new CoberturaSensor(settings);
   }
 
   @Test
   public void shouldNotFailIfReportNotSpecifiedOrNotFound() {
     ProjectFileSystem pfs = mock(ProjectFileSystem.class);
     when(pfs.resolvePath(anyString()))
-        .thenReturn(new File("notFound.xml"));
+      .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);
+      .thenReturn("notFound.xml")
+      .thenReturn(null);
 
     sensor.analyse(project, context);
     sensor.analyse(project, context);
@@ -225,52 +168,52 @@ public class CoberturaSensorTest {
   @Test
   public void shouldInsertCoverageAtFileLevel() throws URISyntaxException {
     File coverage = new File(getClass().getResource(
-        "/org/sonar/plugins/cobertura/CoberturaSensorTest/shouldInsertCoverageAtFileLevel/coverage.xml").toURI());
+      "/org/sonar/plugins/cobertura/CoberturaSensorTest/shouldInsertCoverageAtFileLevel/coverage.xml").toURI());
     when(context.getResource(any(Resource.class))).thenReturn(new JavaFile("org.sonar.MyClass"));
     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)));
+      argThat(new IsMeasure(CoreMetrics.LINES_TO_COVER, 35.0)));
     verify(context).saveMeasure(argThat(new IsResource(Scopes.FILE, Qualifiers.CLASS, "org.sonar.samples.InnerClass")),
-        argThat(new IsMeasure(CoreMetrics.UNCOVERED_LINES, 22.0)));
+      argThat(new IsMeasure(CoreMetrics.UNCOVERED_LINES, 22.0)));
 
     verify(context).saveMeasure(argThat(new IsResource(Scopes.FILE, Qualifiers.CLASS, "org.sonar.samples.InnerClass")),
-        argThat(new IsMeasure(CoreMetrics.CONDITIONS_TO_COVER, 4.0)));
+      argThat(new IsMeasure(CoreMetrics.CONDITIONS_TO_COVER, 4.0)));
     verify(context).saveMeasure(argThat(new IsResource(Scopes.FILE, Qualifiers.CLASS, "org.sonar.samples.InnerClass")),
-        argThat(new IsMeasure(CoreMetrics.UNCOVERED_CONDITIONS, 3.0)));
+      argThat(new IsMeasure(CoreMetrics.UNCOVERED_CONDITIONS, 3.0)));
 
     verify(context, never()).saveMeasure(
-        argThat(new IsResource(Scopes.FILE, Qualifiers.CLASS, "org.sonar.samples.InnerClass$InnerClassInside")),
-        argThat(new IsMeasure(CoreMetrics.LINES_TO_COVER)));
+      argThat(new IsResource(Scopes.FILE, Qualifiers.CLASS, "org.sonar.samples.InnerClass$InnerClassInside")),
+      argThat(new IsMeasure(CoreMetrics.LINES_TO_COVER)));
     verify(context, never()).saveMeasure(
-        argThat(new IsResource(Scopes.FILE, Qualifiers.CLASS, "org.sonar.samples.InnerClass$InnerClassInside")),
-        argThat(new IsMeasure(CoreMetrics.CONDITIONS_TO_COVER)));
+      argThat(new IsResource(Scopes.FILE, Qualifiers.CLASS, "org.sonar.samples.InnerClass$InnerClassInside")),
+      argThat(new IsMeasure(CoreMetrics.CONDITIONS_TO_COVER)));
     verify(context, never()).saveMeasure(
-        argThat(new IsResource(Scopes.FILE, Qualifiers.CLASS, "org.sonar.samples.InnerClass$InnerClassInside")),
-        argThat(new IsMeasure(CoreMetrics.UNCOVERED_CONDITIONS)));
+      argThat(new IsResource(Scopes.FILE, Qualifiers.CLASS, "org.sonar.samples.InnerClass$InnerClassInside")),
+      argThat(new IsMeasure(CoreMetrics.UNCOVERED_CONDITIONS)));
     verify(context, never()).saveMeasure(
-        argThat(new IsResource(Scopes.FILE, Qualifiers.CLASS, "org.sonar.samples.InnerClass$InnerClassInside")),
-        argThat(new IsMeasure(CoreMetrics.UNCOVERED_LINES)));
+      argThat(new IsResource(Scopes.FILE, Qualifiers.CLASS, "org.sonar.samples.InnerClass$InnerClassInside")),
+      argThat(new IsMeasure(CoreMetrics.UNCOVERED_LINES)));
 
     verify(context, never()).saveMeasure(
-        argThat(new IsResource(Scopes.FILE, Qualifiers.CLASS, "org.sonar.samples.PrivateClass")),
-        argThat(new IsMeasure(CoreMetrics.LINES_TO_COVER)));
+      argThat(new IsResource(Scopes.FILE, Qualifiers.CLASS, "org.sonar.samples.PrivateClass")),
+      argThat(new IsMeasure(CoreMetrics.LINES_TO_COVER)));
     verify(context, never()).saveMeasure(
-        argThat(new IsResource(Scopes.FILE, Qualifiers.CLASS, "org.sonar.samples.PrivateClass")),
-        argThat(new IsMeasure(CoreMetrics.CONDITIONS_TO_COVER)));
+      argThat(new IsResource(Scopes.FILE, Qualifiers.CLASS, "org.sonar.samples.PrivateClass")),
+      argThat(new IsMeasure(CoreMetrics.CONDITIONS_TO_COVER)));
     verify(context, never()).saveMeasure(
-        argThat(new IsResource(Scopes.FILE, Qualifiers.CLASS, "org.sonar.samples.PrivateClass")),
-        argThat(new IsMeasure(CoreMetrics.UNCOVERED_CONDITIONS)));
+      argThat(new IsResource(Scopes.FILE, Qualifiers.CLASS, "org.sonar.samples.PrivateClass")),
+      argThat(new IsMeasure(CoreMetrics.UNCOVERED_CONDITIONS)));
     verify(context, never()).saveMeasure(
-        argThat(new IsResource(Scopes.FILE, Qualifiers.CLASS, "org.sonar.samples.PrivateClass")),
-        argThat(new IsMeasure(CoreMetrics.UNCOVERED_LINES)));
+      argThat(new IsResource(Scopes.FILE, Qualifiers.CLASS, "org.sonar.samples.PrivateClass")),
+      argThat(new IsMeasure(CoreMetrics.UNCOVERED_LINES)));
 
     verify(context)
-        .saveMeasure(
-            eq(new JavaFile("org.sonar.samples.InnerClass")),
-            argThat(new IsMeasure(
-                CoreMetrics.COVERAGE_LINE_HITS_DATA,
-                "22=2;25=0;26=0;29=0;30=0;31=0;34=1;35=1;36=1;37=0;39=1;41=1;44=2;46=1;47=1;50=0;51=0;52=0;53=0;55=0;57=0;60=0;61=0;64=1;71=1;73=1;76=0;77=0;80=0;81=0;85=0;87=0;91=0;93=0;96=1")));
+      .saveMeasure(
+        eq(new JavaFile("org.sonar.samples.InnerClass")),
+        argThat(new IsMeasure(
+          CoreMetrics.COVERAGE_LINE_HITS_DATA,
+          "22=2;25=0;26=0;29=0;30=0;31=0;34=1;35=1;36=1;37=0;39=1;41=1;44=2;46=1;47=1;50=0;51=0;52=0;53=0;55=0;57=0;60=0;61=0;64=1;71=1;73=1;76=0;77=0;80=0;81=0;85=0;87=0;91=0;93=0;96=1")));
   }
 
   @Test
@@ -278,9 +221,9 @@ public class CoberturaSensorTest {
     when(context.getResource(any(Resource.class))).thenReturn(new JavaFile("org.sonar.MyClass"));
     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")));
+      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
@@ -290,7 +233,7 @@ public class CoberturaSensorTest {
     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
+      argThat(new IsMeasure(CoreMetrics.LINES_TO_COVER, 5.0))); // do not count line 26 twice
   }
 
   private File getCoverageReport() throws URISyntaxException {
diff --git a/plugins/sonar-cobertura-plugin/src/test/java/org/sonar/plugins/cobertura/CoberturaSettingsTest.java b/plugins/sonar-cobertura-plugin/src/test/java/org/sonar/plugins/cobertura/CoberturaSettingsTest.java
new file mode 100644 (file)
index 0000000..c36661b
--- /dev/null
@@ -0,0 +1,106 @@
+/*
+ * Sonar, open source software quality management tool.
+ * Copyright (C) 2008-2012 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * Sonar is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * Sonar is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with Sonar; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02
+ */
+package org.sonar.plugins.cobertura;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.sonar.api.config.Settings;
+import org.sonar.api.resources.InputFile;
+import org.sonar.api.resources.InputFileUtils;
+import org.sonar.api.resources.Java;
+import org.sonar.api.resources.Project;
+import org.sonar.api.resources.ProjectFileSystem;
+import org.sonar.plugins.java.api.JavaSettings;
+
+import java.util.Arrays;
+import java.util.Collections;
+
+import static org.fest.assertions.Assertions.assertThat;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+public class CoberturaSettingsTest {
+
+  private Settings settings;
+  private JavaSettings javaSettings;
+  private ProjectFileSystem fileSystem;
+  private Project javaProject;
+  private CoberturaSettings coberturaSettings;
+
+  @Before
+  public void before() {
+    settings = new Settings();
+    javaSettings = mock(JavaSettings.class);
+    when(javaSettings.getEnabledCoveragePlugin()).thenReturn("cobertura");
+    fileSystem = mock(ProjectFileSystem.class);
+    when(fileSystem.mainFiles(Java.KEY)).thenReturn(Arrays.asList(InputFileUtils.create(null, "")));
+    javaProject = mock(Project.class);
+    when(javaProject.getLanguageKey()).thenReturn(Java.KEY);
+    when(javaProject.getFileSystem()).thenReturn(fileSystem);
+    when(javaProject.getAnalysisType()).thenReturn(Project.AnalysisType.DYNAMIC);
+    coberturaSettings = new CoberturaSettings(settings, javaSettings);
+  }
+
+  @Test
+  public void should_be_enabled_if_project_with_java_sources() {
+    assertThat(coberturaSettings.isEnabled(javaProject)).isTrue();
+  }
+
+  @Test
+  public void should_be_disabled_if_not_java() {
+    Project phpProject = mock(Project.class);
+    when(phpProject.getLanguageKey()).thenReturn("php");
+
+    assertThat(coberturaSettings.isEnabled(phpProject)).isFalse();
+  }
+
+  @Test
+  public void should_be_disabled_if_java_project_without_sources() {
+    when(fileSystem.mainFiles(Java.KEY)).thenReturn(Collections.<InputFile>emptyList());
+    assertThat(coberturaSettings.isEnabled(javaProject)).isFalse();
+  }
+
+  @Test
+  public void should_be_disabled_if_static_analysis_only() {
+    when(javaProject.getAnalysisType()).thenReturn(Project.AnalysisType.STATIC);
+    assertThat(coberturaSettings.isEnabled(javaProject)).isFalse();
+  }
+
+  @Test
+  public void should_be_enabled_if_reuse_report_mode() {
+    when(javaProject.getAnalysisType()).thenReturn(Project.AnalysisType.REUSE_REPORTS);
+    assertThat(coberturaSettings.isEnabled(javaProject)).isTrue();
+  }
+
+  @Test
+  public void should_configure_max_memory() {
+    settings.setProperty("sonar.cobertura.maxmem", "128m");
+    assertThat(coberturaSettings.getMaxMemory()).isEqualTo("128m");
+  }
+
+  /**
+   * http://jira.codehaus.org/browse/SONAR-2897: there used to be a typo in the parameter name (was "sonar.cobertura.maxmen")
+   */
+  @Test
+  public void should_support_deprecated_max_memory() {
+    settings.setProperty("sonar.cobertura.maxmen", "128m");
+    assertThat(coberturaSettings.getMaxMemory()).isEqualTo("128m");
+  }
+}
index 87355615f7703660d1d04cd917ef253920fb0876..881c513819ea9db82a4edd42724824ba95da5fac 100644 (file)
@@ -40,7 +40,7 @@ public class JaCoCoItSensor implements Sensor {
   }
 
   public boolean shouldExecuteOnProject(Project project) {
-    return StringUtils.isNotBlank(configuration.getItReportPath()) && project.getAnalysisType().isDynamic(true);
+    return configuration.isEnabled(project) && StringUtils.isNotBlank(configuration.getItReportPath());
   }
 
   public void analyse(Project project, SensorContext context) {
index 4ead9748ed605d785b39e07968baaea9ede36648..84c0c5f0a39c40e1477474ace34ad29a87dc53c8 100644 (file)
@@ -53,8 +53,7 @@ public class JaCoCoOverallSensor implements Sensor {
   }
 
   public boolean shouldExecuteOnProject(Project project) {
-    return StringUtils.isNotBlank(configuration.getItReportPath())
-      && project.getAnalysisType().isDynamic(true);
+    return configuration.isEnabled(project) && StringUtils.isNotBlank(configuration.getItReportPath());
   }
 
   public void analyse(Project project, SensorContext context) {
index a875b48740538864107fd516ce2afa04846b426f..025d4dd17149c3c413da398f618b0d0e1c513cdf 100644 (file)
@@ -20,7 +20,6 @@
 package org.sonar.plugins.jacoco;
 
 import com.google.common.collect.ImmutableList;
-import org.sonar.api.BatchExtension;
 import org.sonar.api.SonarPlugin;
 
 import java.util.List;
index 8c7a485356304764f79388b406d0b788fbd7da50..d5d19c416afdd3bb62b8e29b2f50f4c4ac93fbdc 100644 (file)
@@ -45,9 +45,7 @@ public class JaCoCoSensor implements Sensor, CoverageExtension {
   }
 
   public boolean shouldExecuteOnProject(Project project) {
-    return Java.KEY.equals(project.getLanguageKey())
-      && configuration.isEnabled()
-      && project.getAnalysisType().isDynamic(true);
+    return configuration.isEnabled(project);
   }
 
   class UnitTestsAnalyzer extends AbstractAnalyzer {
index 590d6a23ca49e7155cfa3e49e1f8d0a23c16b7aa..85fefdebecb6ce6b66359c832c51509587dda622 100644 (file)
@@ -42,7 +42,7 @@ public class JacocoAntInitializer extends Initializer implements CoverageExtensi
 
   @Override
   public boolean shouldExecuteOnProject(org.sonar.api.resources.Project project) {
-    return project.getAnalysisType().equals(Project.AnalysisType.DYNAMIC);
+    return configuration.isEnabled(project) && project.getAnalysisType().equals(Project.AnalysisType.DYNAMIC);
   }
 
   @Override
index 65854800198f3563addcc605b33d6c5c9ced571a..7d13a75263007a40e47ba3229e0f4b166f9afc12 100644 (file)
@@ -25,6 +25,8 @@ import org.sonar.api.BatchExtension;
 import org.sonar.api.Properties;
 import org.sonar.api.Property;
 import org.sonar.api.config.Settings;
+import org.sonar.api.resources.Java;
+import org.sonar.api.resources.Project;
 import org.sonar.plugins.java.api.JavaSettings;
 
 @Properties({
@@ -118,8 +120,10 @@ public class JacocoConfiguration implements BatchExtension {
     this.javaSettings = javaSettings;
   }
 
-  public boolean isEnabled() {
-    return JaCoCoUtils.PLUGIN_KEY.equals(javaSettings.getEnabledCoveragePlugin());
+  public boolean isEnabled(Project project) {
+    return Java.KEY.equals(project.getLanguageKey()) &&
+      project.getAnalysisType().isDynamic(true) &&
+      JaCoCoUtils.PLUGIN_KEY.equals(javaSettings.getEnabledCoveragePlugin());
   }
 
   public String getReportPath() {
index e35dea775640596004dd8810d01658631e07c518..c7c320537a3f8f500ca91c542760a8a464673751 100644 (file)
@@ -40,7 +40,7 @@ public class JacocoMavenInitializer extends Initializer implements CoverageExten
 
   @Override
   public boolean shouldExecuteOnProject(Project project) {
-    return configuration.isEnabled()
+    return configuration.isEnabled(project)
       && project.getAnalysisType().equals(Project.AnalysisType.DYNAMIC)
       && !project.getFileSystem().testFiles(Java.KEY).isEmpty();
   }
index 18d3a641f2b54f5063db9c9f119ec62490eb838c..a36882c4f0ade7dc21668d56038e000212b2b3f5 100644 (file)
@@ -37,8 +37,7 @@ import org.sonar.test.TestUtils;
 import java.io.File;
 import java.io.IOException;
 
-import static org.hamcrest.Matchers.is;
-import static org.junit.Assert.assertThat;
+import static org.fest.assertions.Assertions.assertThat;
 import static org.mockito.Matchers.any;
 import static org.mockito.Matchers.anyString;
 import static org.mockito.Matchers.argThat;
@@ -72,7 +71,7 @@ public class JaCoCoItSensorTest {
 
   @Test
   public void testSensorDefinition() {
-    assertThat(sensor.toString(), is("JaCoCoItSensor"));
+    assertThat(sensor.toString()).isEqualTo("JaCoCoItSensor");
   }
 
   @Test
@@ -80,17 +79,27 @@ public class JaCoCoItSensorTest {
     Project project = mock(Project.class);
     when(configuration.getItReportPath()).thenReturn("");
 
-    assertThat(sensor.shouldExecuteOnProject(project), is(false));
+    assertThat(sensor.shouldExecuteOnProject(project)).isFalse();
   }
 
   @Test
-  public void shouldExecuteOnProject() {
+  public void shouldExecuteIfReportPathIsDefined() {
     Project project = mock(Project.class);
+    when(project.getAnalysisType()).thenReturn(AnalysisType.DYNAMIC).thenReturn(AnalysisType.REUSE_REPORTS);
     when(configuration.getItReportPath()).thenReturn("target/it-jacoco.exec");
+    when(configuration.isEnabled(project)).thenReturn(true);
+
+    assertThat(sensor.shouldExecuteOnProject(project)).isTrue();
+  }
+
+  @Test
+  public void shouldNotExecuteIfReportPathIsNotDefined() {
+    Project project = mock(Project.class);
     when(project.getAnalysisType()).thenReturn(AnalysisType.DYNAMIC).thenReturn(AnalysisType.REUSE_REPORTS);
+    when(configuration.getItReportPath()).thenReturn(null);
+    when(configuration.isEnabled(project)).thenReturn(true);
 
-    assertThat(sensor.shouldExecuteOnProject(project), is(true));
-    assertThat(sensor.shouldExecuteOnProject(project), is(true));
+    assertThat(sensor.shouldExecuteOnProject(project)).isFalse();
   }
 
   @Test
@@ -110,7 +119,7 @@ public class JaCoCoItSensorTest {
     verify(context).saveMeasure(eq(resource), argThat(new IsMeasure(CoreMetrics.IT_LINES_TO_COVER, 7.0)));
     verify(context).saveMeasure(eq(resource), argThat(new IsMeasure(CoreMetrics.IT_UNCOVERED_LINES, 3.0)));
     verify(context).saveMeasure(eq(resource),
-        argThat(new IsMeasure(CoreMetrics.IT_COVERAGE_LINE_HITS_DATA, "6=1;7=1;8=1;11=1;15=0;16=0;18=0")));
+      argThat(new IsMeasure(CoreMetrics.IT_COVERAGE_LINE_HITS_DATA, "6=1;7=1;8=1;11=1;15=0;16=0;18=0")));
     verify(context).saveMeasure(eq(resource), argThat(new IsMeasure(CoreMetrics.IT_CONDITIONS_TO_COVER, 2.0)));
     verify(context).saveMeasure(eq(resource), argThat(new IsMeasure(CoreMetrics.IT_UNCOVERED_CONDITIONS, 2.0)));
     verify(context).saveMeasure(eq(resource), argThat(new IsMeasure(CoreMetrics.IT_CONDITIONS_BY_LINE, "15=2")));
index b9cabe9efe861ca8d26d361251c1e64eb4fa939c..0525c62b5bc7b657bb09088c58f225dde3d693fb 100644 (file)
@@ -51,10 +51,10 @@ public class JaCoCoOverallSensorTest {
   private final JaCoCoOverallSensor sensor = new JaCoCoOverallSensor(configuration);
 
   @Test
-  public void should_execute_on_project() {
+  public void should_execute_if_report_path_is_set() {
     Project project = mock(Project.class);
     when(configuration.getItReportPath()).thenReturn("target/it-jacoco.exec");
-    when(project.getAnalysisType()).thenReturn(Project.AnalysisType.DYNAMIC).thenReturn(Project.AnalysisType.REUSE_REPORTS);
+    when(configuration.isEnabled(project)).thenReturn(true);
 
     assertThat(sensor.shouldExecuteOnProject(project)).isTrue();
   }
index 6501a60dbaf862ac319892f8f268fc04e88ad21d..fcc6bacabd712a88c55d79f24f64774b3731603a 100644 (file)
@@ -26,7 +26,6 @@ import org.junit.Test;
 import org.sonar.api.batch.SensorContext;
 import org.sonar.api.measures.CoreMetrics;
 import org.sonar.api.measures.Measure;
-import org.sonar.api.resources.Java;
 import org.sonar.api.resources.JavaFile;
 import org.sonar.api.resources.Project;
 import org.sonar.api.resources.ProjectFileSystem;
@@ -71,7 +70,6 @@ public class JaCoCoSensorTest {
   @Before
   public void setUp() {
     configuration = mock(JacocoConfiguration.class);
-    when(configuration.isEnabled()).thenReturn(true);
     sensor = new JaCoCoSensor(configuration);
   }
 
@@ -81,20 +79,14 @@ public class JaCoCoSensorTest {
   }
 
   @Test
-  public void shouldNotExecuteOnProject() {
+  public void shouldExecuteIfEnabled() {
     Project project = mock(Project.class);
-    when(project.getLanguageKey()).thenReturn("flex");
-
-    assertThat(sensor.shouldExecuteOnProject(project), is(false));
-  }
-
-  @Test
-  public void shouldExecuteOnProject() {
-    Project project = mock(Project.class);
-    when(project.getLanguageKey()).thenReturn(Java.KEY);
-    when(project.getAnalysisType()).thenReturn(Project.AnalysisType.DYNAMIC);
 
+    when(configuration.isEnabled(project)).thenReturn(true);
     assertThat(sensor.shouldExecuteOnProject(project), is(true));
+
+    when(configuration.isEnabled(project)).thenReturn(false);
+    assertThat(sensor.shouldExecuteOnProject(project), is(false));
   }
 
   @Test
@@ -114,7 +106,7 @@ public class JaCoCoSensorTest {
     verify(context).saveMeasure(eq(resource), argThat(new IsMeasure(CoreMetrics.LINES_TO_COVER, 7.0)));
     verify(context).saveMeasure(eq(resource), argThat(new IsMeasure(CoreMetrics.UNCOVERED_LINES, 3.0)));
     verify(context).saveMeasure(eq(resource),
-        argThat(new IsMeasure(CoreMetrics.COVERAGE_LINE_HITS_DATA, "6=1;7=1;8=1;11=1;15=0;16=0;18=0")));
+      argThat(new IsMeasure(CoreMetrics.COVERAGE_LINE_HITS_DATA, "6=1;7=1;8=1;11=1;15=0;16=0;18=0")));
     verify(context).saveMeasure(eq(resource), argThat(new IsMeasure(CoreMetrics.CONDITIONS_TO_COVER, 2.0)));
     verify(context).saveMeasure(eq(resource), argThat(new IsMeasure(CoreMetrics.UNCOVERED_CONDITIONS, 2.0)));
     verify(context).saveMeasure(eq(resource), argThat(new IsMeasure(CoreMetrics.CONDITIONS_BY_LINE, "15=2" +
index 4889553a06437ea7c3a84e796a22a71a9bc50b71..f548d7963b4b2003d8f02467a9b21ce37cd6afd9 100644 (file)
@@ -23,20 +23,20 @@ import org.junit.Before;
 import org.junit.Test;
 import org.sonar.api.config.PropertyDefinitions;
 import org.sonar.api.config.Settings;
+import org.sonar.api.resources.Java;
+import org.sonar.api.resources.Project;
 import org.sonar.plugins.java.api.JavaSettings;
 
 import java.io.File;
 
-import static org.hamcrest.Matchers.is;
-import static org.hamcrest.Matchers.nullValue;
-import static org.junit.Assert.assertThat;
+import static org.fest.assertions.Assertions.assertThat;
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.when;
 
 public class JacocoConfigurationTest {
 
   private Settings settings;
-  private JacocoConfiguration jacocoConfiguration;
+  private JacocoConfiguration jacocoSettings;
   private JavaSettings javaSettings;
 
   @Before
@@ -45,42 +45,91 @@ public class JacocoConfigurationTest {
     when(downloader.getAgentJarFile()).thenReturn(new File("jacocoagent.jar"));
     javaSettings = mock(JavaSettings.class);
     settings = new Settings(new PropertyDefinitions(JacocoConfiguration.class));
+    jacocoSettings = new JacocoConfiguration(settings, downloader, javaSettings);
+  }
+
+  @Test
+  public void should_be_enabled() {
+    Project project = mock(Project.class);
+    when(project.getLanguageKey()).thenReturn(Java.KEY);
+    when(project.getAnalysisType()).thenReturn(Project.AnalysisType.DYNAMIC);
+    when(javaSettings.getEnabledCoveragePlugin()).thenReturn(JaCoCoUtils.PLUGIN_KEY);
+
+    assertThat(jacocoSettings.isEnabled(project)).isTrue();
+  }
+
+  @Test
+  public void should_be_enabled_if_reuse_report() {
+    Project project = mock(Project.class);
+    when(project.getLanguageKey()).thenReturn(Java.KEY);
+    when(project.getAnalysisType()).thenReturn(Project.AnalysisType.REUSE_REPORTS);
+    when(javaSettings.getEnabledCoveragePlugin()).thenReturn(JaCoCoUtils.PLUGIN_KEY);
+
+    assertThat(jacocoSettings.isEnabled(project)).isTrue();
+  }
+
+  @Test
+  public void should_be_enabled_if_static_analysis_only() {
+    Project project = mock(Project.class);
+    when(project.getLanguageKey()).thenReturn(Java.KEY);
+    when(project.getAnalysisType()).thenReturn(Project.AnalysisType.STATIC);
+    when(javaSettings.getEnabledCoveragePlugin()).thenReturn(JaCoCoUtils.PLUGIN_KEY);
+
+    assertThat(jacocoSettings.isEnabled(project)).isFalse();
+  }
+
+  @Test
+  public void plugin_should_be_disabled() {
+    Project project = mock(Project.class);
+    when(project.getLanguageKey()).thenReturn(Java.KEY);
+    when(project.getAnalysisType()).thenReturn(Project.AnalysisType.DYNAMIC);
+    when(javaSettings.getEnabledCoveragePlugin()).thenReturn("cobertura");
+
+    assertThat(jacocoSettings.isEnabled(project)).isFalse();
+  }
+
+  @Test
+  public void should_be_disabled_if_not_java() {
+    Project project = mock(Project.class);
+    when(project.getLanguageKey()).thenReturn("flex");
+    when(project.getAnalysisType()).thenReturn(Project.AnalysisType.DYNAMIC);
+    when(javaSettings.getEnabledCoveragePlugin()).thenReturn(JaCoCoUtils.PLUGIN_KEY);
 
-    jacocoConfiguration = new JacocoConfiguration(settings, downloader, javaSettings);
+    assertThat(jacocoSettings.isEnabled(project)).isFalse();
   }
 
   @Test
   public void defaults() {
-    assertThat(jacocoConfiguration.getReportPath(), is("target/jacoco.exec"));
-    assertThat(jacocoConfiguration.getJvmArgument(), is("-javaagent:jacocoagent.jar=destfile=target/jacoco.exec,excludes=*_javassist_*"));
+    assertThat(jacocoSettings.getReportPath()).isEqualTo("target/jacoco.exec");
+    assertThat(jacocoSettings.getJvmArgument()).isEqualTo("-javaagent:jacocoagent.jar=destfile=target/jacoco.exec,excludes=*_javassist_*");
 
-    assertThat(jacocoConfiguration.getItReportPath(), nullValue());
+    assertThat(jacocoSettings.getItReportPath()).isNull();
 
-    assertThat(jacocoConfiguration.getAntTargets(), is(new String[]{}));
+    assertThat(jacocoSettings.getAntTargets()).isEqualTo(new String[]{});
   }
 
   @Test
   public void shouldReturnAntTargets() {
     settings.setProperty(JacocoConfiguration.ANT_TARGETS_PROPERTY, "test");
-    assertThat(jacocoConfiguration.getAntTargets(), is(new String[]{"test"}));
+    assertThat(jacocoSettings.getAntTargets()).isEqualTo(new String[]{"test"});
 
     settings.setProperty(JacocoConfiguration.ANT_TARGETS_PROPERTY, "test1,test2");
-    assertThat(jacocoConfiguration.getAntTargets(), is(new String[]{"test1", "test2"}));
+    assertThat(jacocoSettings.getAntTargets()).isEqualTo(new String[]{"test1", "test2"});
   }
 
   @Test
   public void shouldReturnItReportPath() {
     settings.setProperty(JacocoConfiguration.IT_REPORT_PATH_PROPERTY, "target/it-jacoco.exec");
 
-    assertThat(jacocoConfiguration.getItReportPath(), is("target/it-jacoco.exec"));
+    assertThat(jacocoSettings.getItReportPath()).isEqualTo("target/it-jacoco.exec");
   }
 
   @Test
   public void shouldSetDestfile() {
     settings.setProperty(JacocoConfiguration.REPORT_PATH_PROPERTY, "jacoco.exec");
 
-    assertThat(jacocoConfiguration.getReportPath(), is("jacoco.exec"));
-    assertThat(jacocoConfiguration.getJvmArgument(), is("-javaagent:jacocoagent.jar=destfile=jacoco.exec,excludes=*_javassist_*"));
+    assertThat(jacocoSettings.getReportPath()).isEqualTo("jacoco.exec");
+    assertThat(jacocoSettings.getJvmArgument()).isEqualTo("-javaagent:jacocoagent.jar=destfile=jacoco.exec,excludes=*_javassist_*");
   }
 
   @Test
@@ -89,8 +138,9 @@ public class JacocoConfigurationTest {
     settings.setProperty(JacocoConfiguration.EXCLUDES_PROPERTY, "org.sonar.api.*");
     settings.setProperty(JacocoConfiguration.EXCLCLASSLOADER_PROPERTY, "sun.reflect.DelegatingClassLoader");
 
-    assertThat(jacocoConfiguration.getJvmArgument(),
-        is("-javaagent:jacocoagent.jar=destfile=target/jacoco.exec,includes=org.sonar.*,excludes=org.sonar.api.*,exclclassloader=sun.reflect.DelegatingClassLoader"));
+    assertThat(jacocoSettings.getJvmArgument()).isEqualTo(
+      "-javaagent:jacocoagent.jar=destfile=target/jacoco.exec,includes=org.sonar.*,excludes=org.sonar.api.*,exclclassloader=sun.reflect.DelegatingClassLoader"
+    );
   }
 
 }
index d6acbbd61dfa2df8d3243c41fb780974fec68058..e82fc70ad18ea7a72894aa6896af3c4f851beffe 100644 (file)
@@ -42,7 +42,7 @@ public class JacocoMavenInitializerTest {
   public void setUp() {
     mavenPluginHandler = mock(JaCoCoMavenPluginHandler.class);
     jacocoSettings = mock(JacocoConfiguration.class);
-    when(jacocoSettings.isEnabled()).thenReturn(true);
+    when(jacocoSettings.isEnabled(any(Project.class))).thenReturn(true);
     initializer = new JacocoMavenInitializer(mavenPluginHandler, jacocoSettings);
   }
 
index 64fbb25f8e7d90bfe2ac5aef3fe3e38e3c5c91e1..2adca6754bb08d8c9f31ef6709a67b9008f7d27f 100644 (file)
@@ -27,8 +27,6 @@ import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.sonar.api.CoreProperties;
 import org.sonar.api.Plugin;
-import org.sonar.api.Properties;
-import org.sonar.api.Property;
 import org.sonar.api.config.Settings;
 import org.sonar.api.platform.PluginMetadata;
 import org.sonar.api.platform.PluginRepository;
@@ -39,7 +37,6 @@ import org.sonar.core.plugins.RemotePlugin;
 import java.io.File;
 import java.util.Arrays;
 import java.util.Collection;
-import java.util.Collections;
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
index ebe8342d2216e1d550c49bd66845f194cdf4e61a..9017d3225bfd1292a4ebf67d44baa6253277565a 100644 (file)
@@ -22,12 +22,9 @@ package org.sonar.batch.bootstrap;
 import org.sonar.api.BatchComponent;
 import org.sonar.api.config.Settings;
 import org.sonar.api.database.DatabaseProperties;
-import org.sonar.api.utils.SonarException;
 import org.sonar.core.persistence.BadDatabaseVersion;
 import org.sonar.core.persistence.DatabaseVersion;
 
-import java.io.IOException;
-
 /**
  * Detects if database is not up-to-date with the version required by the batch.
  */
index f76d40def19e44a4e21ef5a9f54ac6993a99af65..8ee876bb65ac0948365935a437872c1083b4fca8 100644 (file)
@@ -21,7 +21,6 @@ package org.sonar.api.platform;
 
 import org.sonar.api.BatchComponent;
 import org.sonar.api.Plugin;
-import org.sonar.api.Property;
 import org.sonar.api.ServerComponent;
 
 import java.util.Collection;
index 9b678016e9e983f9b624e123af7db5f007e55a21..ffd6a6f299e4790139c357f256620be91126f237 100644 (file)
@@ -22,8 +22,6 @@ package org.sonar.server.plugins;
 import com.google.common.collect.Sets;
 import org.slf4j.LoggerFactory;
 import org.sonar.api.Plugin;
-import org.sonar.api.Properties;
-import org.sonar.api.Property;
 import org.sonar.api.platform.PluginMetadata;
 import org.sonar.api.platform.ServerPluginRepository;
 import org.sonar.core.plugins.PluginClassloaders;
@@ -94,7 +92,7 @@ public class DefaultServerPluginRepository implements ServerPluginRepository {
     return clazz;
   }
 
-public Collection<PluginMetadata> getMetadata() {
+  public Collection<PluginMetadata> getMetadata() {
     return deployer.getMetadata();
   }