diff options
author | Godin <mandrikov@gmail.com> | 2010-10-19 20:47:17 +0000 |
---|---|---|
committer | Godin <mandrikov@gmail.com> | 2010-10-19 20:47:17 +0000 |
commit | 2a800be6f83fc3a1366c3a5e8d34c8725dbf24e8 (patch) | |
tree | 599ce7d313fb729ca8e7fc7781ae84b98ab9a6c7 /plugins/sonar-findbugs-plugin | |
parent | 9edb39c091f8bf970be6849746ac7d9128d44fad (diff) | |
download | sonarqube-2a800be6f83fc3a1366c3a5e8d34c8725dbf24e8.tar.gz sonarqube-2a800be6f83fc3a1366c3a5e8d34c8725dbf24e8.zip |
SONAR-1772: Reuse report
Diffstat (limited to 'plugins/sonar-findbugs-plugin')
2 files changed, 63 insertions, 8 deletions
diff --git a/plugins/sonar-findbugs-plugin/src/main/java/org/sonar/plugins/findbugs/FindbugsNativeSensor.java b/plugins/sonar-findbugs-plugin/src/main/java/org/sonar/plugins/findbugs/FindbugsNativeSensor.java index 9d0d823e234..08e9dcc865e 100644 --- a/plugins/sonar-findbugs-plugin/src/main/java/org/sonar/plugins/findbugs/FindbugsNativeSensor.java +++ b/plugins/sonar-findbugs-plugin/src/main/java/org/sonar/plugins/findbugs/FindbugsNativeSensor.java @@ -1,6 +1,7 @@ package org.sonar.plugins.findbugs; import org.apache.commons.lang.StringUtils; +import org.sonar.api.CoreProperties; import org.sonar.api.batch.Sensor; import org.sonar.api.batch.SensorContext; import org.sonar.api.profiles.RulesProfile; @@ -10,6 +11,7 @@ import org.sonar.api.rules.Rule; import org.sonar.api.rules.RuleFinder; import org.sonar.api.rules.Violation; +import java.io.File; import java.util.List; /** @@ -36,7 +38,11 @@ public class FindbugsNativeSensor implements Sensor { } public void analyse(Project project, SensorContext context) { - FindbugsXmlReportParser reportParser = new FindbugsXmlReportParser(executor.execute()); + File report = getFindbugsReportFile(project); + if (report == null) { + report = executor.execute(); + } + FindbugsXmlReportParser reportParser = new FindbugsXmlReportParser(report); List<FindbugsXmlReportParser.Violation> fbViolations = reportParser.getViolations(); for (FindbugsXmlReportParser.Violation fbViolation : fbViolations) { Rule rule = ruleFinder.findByKey(FindbugsConstants.REPOSITORY_KEY, fbViolation.getType()); @@ -48,6 +54,13 @@ public class FindbugsNativeSensor implements Sensor { } } + protected final File getFindbugsReportFile(Project project) { + if (project.getConfiguration().getString(CoreProperties.FINDBUGS_REPORT_PATH) != null) { + return new File(project.getConfiguration().getString(CoreProperties.FINDBUGS_REPORT_PATH)); + } + return null; + } + @Override public String toString() { return getClass().getSimpleName(); diff --git a/plugins/sonar-findbugs-plugin/src/test/java/org/sonar/plugins/findbugs/FindbugsNativeSensorTest.java b/plugins/sonar-findbugs-plugin/src/test/java/org/sonar/plugins/findbugs/FindbugsNativeSensorTest.java index 4530003378f..8c5c37a8c17 100644 --- a/plugins/sonar-findbugs-plugin/src/test/java/org/sonar/plugins/findbugs/FindbugsNativeSensorTest.java +++ b/plugins/sonar-findbugs-plugin/src/test/java/org/sonar/plugins/findbugs/FindbugsNativeSensorTest.java @@ -1,28 +1,42 @@ package org.sonar.plugins.findbugs; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; +import static org.mockito.Matchers.any; +import static org.mockito.Matchers.argThat; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.never; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import org.apache.commons.configuration.Configuration; import org.apache.maven.project.MavenProject; 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.DefaultProjectFileSystem; +import org.sonar.api.resources.JavaFile; import org.sonar.api.resources.Project; +import org.sonar.api.resources.Resource; +import org.sonar.api.rules.Violation; +import org.sonar.api.test.IsViolation; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; +import java.io.File; public class FindbugsNativeSensorTest extends FindbugsTests { @Test public void shouldExecuteWhenSomeRulesAreActive() throws Exception { - FindbugsSensor sensor = new FindbugsSensor(createRulesProfileWithActiveRules(), new FindbugsRuleFinder(), null); + FindbugsNativeSensor sensor = new FindbugsNativeSensor(createRulesProfileWithActiveRules(), new FindbugsRuleFinder(), null); Project project = createProject(); assertTrue(sensor.shouldExecuteOnProject(project)); } @Test public void shouldNotExecuteWhenNoRulesAreActive() throws Exception { - FindbugsSensor analyser = new FindbugsSensor(RulesProfile.create(), new FindbugsRuleFinder(), null); + FindbugsNativeSensor analyser = new FindbugsNativeSensor(RulesProfile.create(), new FindbugsRuleFinder(), null); Project pom = createProject(); assertFalse(analyser.shouldExecuteOnProject(pom)); } @@ -31,10 +45,38 @@ public class FindbugsNativeSensorTest extends FindbugsTests { public void shouldNotExecuteOnEar() { Project project = createProject(); when(project.getPom().getPackaging()).thenReturn("ear"); - FindbugsSensor analyser = new FindbugsSensor(createRulesProfileWithActiveRules(), new FindbugsRuleFinder(), null); + FindbugsNativeSensor analyser = new FindbugsNativeSensor(createRulesProfileWithActiveRules(), new FindbugsRuleFinder(), null); assertFalse(analyser.shouldExecuteOnProject(project)); } + @Test + public void shouldReuseReport() throws Exception { + Project project = createProject(); + FindbugsExecutor executor = mock(FindbugsExecutor.class); + SensorContext context = mock(SensorContext.class); + Configuration conf = mock(Configuration.class); + File xmlFile = new File(getClass().getResource("/org/sonar/plugins/findbugs/findbugsXml.xml").toURI()); + when(conf.getString(CoreProperties.FINDBUGS_REPORT_PATH)).thenReturn(xmlFile.getAbsolutePath()); + when(project.getConfiguration()).thenReturn(conf); + when(context.getResource(any(Resource.class))).thenReturn(new JavaFile("org.sonar.MyClass")); + + FindbugsNativeSensor analyser = new FindbugsNativeSensor(createRulesProfileWithActiveRules(), new FindbugsRuleFinder(), executor); + analyser.analyse(project, context); + + verify(executor, never()).execute(); + verify(context, times(3)).saveViolation(any(Violation.class)); + + Violation wanted = new Violation(null, new JavaFile("org.sonar.commons.ZipUtils")).setMessage( + "Empty zip file entry created in org.sonar.commons.ZipUtils._zip(String, File, ZipOutputStream)").setLineId(107); + + verify(context).saveViolation(argThat(new IsViolation(wanted))); + + wanted = new Violation(null, new JavaFile("org.sonar.commons.resources.MeasuresDao")).setMessage( + "The class org.sonar.commons.resources.MeasuresDao$1 could be refactored into a named _static_ inner class").setLineId(56); + + verify(context).saveViolation(argThat(new IsViolation(wanted))); + } + private Project createProject() { DefaultProjectFileSystem fileSystem = mock(DefaultProjectFileSystem.class); when(fileSystem.hasJavaSourceFiles()).thenReturn(Boolean.TRUE); |