aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorFabrice Bellingard <fabrice.bellingard@sonarsource.com>2012-09-24 10:27:30 +0200
committerFabrice Bellingard <fabrice.bellingard@sonarsource.com>2012-09-24 10:27:30 +0200
commit7be34e5e7f9d555da901fb70d36b6effa58f1933 (patch)
treebae9d7ad7173be307e85724a4fd296ac52e2ce00
parent3c3a09ef51296a106efcb71f7f97214be8aa4140 (diff)
downloadsonarqube-7be34e5e7f9d555da901fb70d36b6effa58f1933.tar.gz
sonarqube-7be34e5e7f9d555da901fb70d36b6effa58f1933.zip
SONAR-3823 Don't activate Findbugs extensions on non-Java projects
-rw-r--r--plugins/sonar-findbugs-plugin/src/main/java/org/sonar/plugins/findbugs/FindbugsMavenInitializer.java7
-rw-r--r--plugins/sonar-findbugs-plugin/src/main/java/org/sonar/plugins/findbugs/FindbugsSensor.java6
-rw-r--r--plugins/sonar-findbugs-plugin/src/test/java/org/sonar/plugins/findbugs/FindbugsMavenInitializerTest.java34
-rw-r--r--plugins/sonar-findbugs-plugin/src/test/java/org/sonar/plugins/findbugs/FindbugsSensorTest.java54
4 files changed, 89 insertions, 12 deletions
diff --git a/plugins/sonar-findbugs-plugin/src/main/java/org/sonar/plugins/findbugs/FindbugsMavenInitializer.java b/plugins/sonar-findbugs-plugin/src/main/java/org/sonar/plugins/findbugs/FindbugsMavenInitializer.java
index 86bbd8b5eaa..76116116e6c 100644
--- a/plugins/sonar-findbugs-plugin/src/main/java/org/sonar/plugins/findbugs/FindbugsMavenInitializer.java
+++ b/plugins/sonar-findbugs-plugin/src/main/java/org/sonar/plugins/findbugs/FindbugsMavenInitializer.java
@@ -24,6 +24,7 @@ import org.sonar.api.batch.Initializer;
import org.sonar.api.batch.SupportedEnvironment;
import org.sonar.api.batch.maven.MavenPlugin;
import org.sonar.api.batch.maven.MavenUtils;
+import org.sonar.api.resources.Java;
import org.sonar.api.resources.Project;
/**
@@ -39,6 +40,12 @@ public class FindbugsMavenInitializer extends Initializer {
private static final String FINDBUGS_ARTIFACT_ID = "findbugs-maven-plugin";
@Override
+ public boolean shouldExecuteOnProject(Project project) {
+ return Java.KEY.equals(project.getLanguageKey())
+ && !project.getFileSystem().mainFiles(Java.KEY).isEmpty();
+ }
+
+ @Override
public void execute(Project project) {
Configuration conf = project.getConfiguration();
if (!conf.containsKey(FindbugsConstants.EXCLUDES_FILTERS_PROPERTY)) {
diff --git a/plugins/sonar-findbugs-plugin/src/main/java/org/sonar/plugins/findbugs/FindbugsSensor.java b/plugins/sonar-findbugs-plugin/src/main/java/org/sonar/plugins/findbugs/FindbugsSensor.java
index ca3d55b4990..d9ca6cecfe3 100644
--- a/plugins/sonar-findbugs-plugin/src/main/java/org/sonar/plugins/findbugs/FindbugsSensor.java
+++ b/plugins/sonar-findbugs-plugin/src/main/java/org/sonar/plugins/findbugs/FindbugsSensor.java
@@ -25,6 +25,7 @@ import org.sonar.api.CoreProperties;
import org.sonar.api.batch.Sensor;
import org.sonar.api.batch.SensorContext;
import org.sonar.api.profiles.RulesProfile;
+import org.sonar.api.resources.Java;
import org.sonar.api.resources.JavaFile;
import org.sonar.api.resources.Project;
import org.sonar.api.rules.Rule;
@@ -49,8 +50,9 @@ public class FindbugsSensor implements Sensor {
}
public boolean shouldExecuteOnProject(Project project) {
- return project.getFileSystem().hasJavaSourceFiles()
- && !profile.getActiveRulesByRepository(FindbugsConstants.REPOSITORY_KEY).isEmpty();
+ return Java.KEY.equals(project.getLanguageKey())
+ && !project.getFileSystem().mainFiles(Java.KEY).isEmpty()
+ && !profile.getActiveRulesByRepository(FindbugsConstants.REPOSITORY_KEY).isEmpty();
}
public void analyse(Project project, SensorContext context) {
diff --git a/plugins/sonar-findbugs-plugin/src/test/java/org/sonar/plugins/findbugs/FindbugsMavenInitializerTest.java b/plugins/sonar-findbugs-plugin/src/test/java/org/sonar/plugins/findbugs/FindbugsMavenInitializerTest.java
index a5faba2481d..c7219e182b7 100644
--- a/plugins/sonar-findbugs-plugin/src/test/java/org/sonar/plugins/findbugs/FindbugsMavenInitializerTest.java
+++ b/plugins/sonar-findbugs-plugin/src/test/java/org/sonar/plugins/findbugs/FindbugsMavenInitializerTest.java
@@ -19,12 +19,18 @@
*/
package org.sonar.plugins.findbugs;
+import com.google.common.collect.Lists;
import org.apache.commons.configuration.Configuration;
import org.junit.Before;
import org.junit.Test;
+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 java.util.ArrayList;
+
import static org.fest.assertions.Assertions.assertThat;
import static org.mockito.Matchers.anyString;
import static org.mockito.Matchers.eq;
@@ -45,6 +51,34 @@ public class FindbugsMavenInitializerTest {
}
@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 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 doNotSetExcludesFiltersIfAlreadyConfigured() {
Configuration configuration = mock(Configuration.class);
when(configuration.containsKey(FindbugsConstants.EXCLUDES_FILTERS_PROPERTY)).thenReturn(true);
diff --git a/plugins/sonar-findbugs-plugin/src/test/java/org/sonar/plugins/findbugs/FindbugsSensorTest.java b/plugins/sonar-findbugs-plugin/src/test/java/org/sonar/plugins/findbugs/FindbugsSensorTest.java
index f1932c410d3..317b72e46d6 100644
--- a/plugins/sonar-findbugs-plugin/src/test/java/org/sonar/plugins/findbugs/FindbugsSensorTest.java
+++ b/plugins/sonar-findbugs-plugin/src/test/java/org/sonar/plugins/findbugs/FindbugsSensorTest.java
@@ -19,11 +19,14 @@
*/
package org.sonar.plugins.findbugs;
+import com.google.common.collect.Lists;
import org.apache.commons.configuration.Configuration;
import org.junit.Test;
import org.sonar.api.CoreProperties;
import org.sonar.api.batch.SensorContext;
import org.sonar.api.profiles.RulesProfile;
+import org.sonar.api.resources.InputFile;
+import org.sonar.api.resources.InputFileUtils;
import org.sonar.api.resources.JavaFile;
import org.sonar.api.resources.Project;
import org.sonar.api.resources.ProjectFileSystem;
@@ -33,9 +36,9 @@ import org.sonar.api.rules.Violation;
import org.sonar.api.test.IsViolation;
import java.io.File;
+import java.util.ArrayList;
-import static org.junit.Assert.assertFalse;
-import static org.junit.Assert.assertTrue;
+import static org.fest.assertions.Assertions.assertThat;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.argThat;
import static org.mockito.Mockito.mock;
@@ -47,17 +50,48 @@ import static org.mockito.Mockito.when;
public class FindbugsSensorTest extends FindbugsTests {
@Test
- public void shouldExecuteWhenSomeRulesAreActive() throws Exception {
- FindbugsSensor sensor = new FindbugsSensor(createRulesProfileWithActiveRules(), new FakeRuleFinder(), null);
- Project project = createProject();
- assertTrue(sensor.shouldExecuteOnProject(project));
+ public void shouldNotAnalyseIfNoJavaProject() {
+ Project project = mock(Project.class);
+ when(project.getLanguageKey()).thenReturn("php");
+
+ FindbugsSensor sensor = new FindbugsSensor(null, null, null);
+ assertThat(sensor.shouldExecuteOnProject(project)).isFalse();
}
@Test
- public void shouldNotExecuteWhenNoRulesAreActive() throws Exception {
- FindbugsSensor analyser = new FindbugsSensor(RulesProfile.create(), new FakeRuleFinder(), null);
- Project project = createProject();
- assertFalse(analyser.shouldExecuteOnProject(project));
+ 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");
+
+ FindbugsSensor sensor = new FindbugsSensor(null, null, null);
+ assertThat(sensor.shouldExecuteOnProject(project)).isFalse();
+ }
+
+ @Test
+ public void shouldNotAnalyseIfJavaProjectButNoRules() {
+ 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");
+
+ FindbugsSensor sensor = new FindbugsSensor(RulesProfile.create(), null, null);
+ 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");
+
+ FindbugsSensor sensor = new FindbugsSensor(createRulesProfileWithActiveRules(), null, null);
+ assertThat(sensor.shouldExecuteOnProject(project)).isTrue();
}
@Test