diff options
author | Godin <mandrikov@gmail.com> | 2010-11-15 23:22:48 +0000 |
---|---|---|
committer | Godin <mandrikov@gmail.com> | 2010-11-15 23:22:48 +0000 |
commit | 07bf6c1878f04691234d189f75dbbf41b2564ede (patch) | |
tree | 59b3f2e43cc8129a8b777127e54d8e2c2a2f95b6 /plugins/sonar-findbugs-plugin | |
parent | f295fab3f8650f33dd132fa6060d69f3c0306671 (diff) | |
download | sonarqube-07bf6c1878f04691234d189f75dbbf41b2564ede.tar.gz sonarqube-07bf6c1878f04691234d189f75dbbf41b2564ede.zip |
SONAR-1977: Ignore violations from findbugs report, if rule not activated in Sonar
Diffstat (limited to 'plugins/sonar-findbugs-plugin')
-rw-r--r-- | plugins/sonar-findbugs-plugin/src/main/java/org/sonar/plugins/findbugs/FindbugsSensor.java | 18 | ||||
-rw-r--r-- | plugins/sonar-findbugs-plugin/src/test/java/org/sonar/plugins/findbugs/FindbugsSensorTest.java | 26 | ||||
-rw-r--r-- | plugins/sonar-findbugs-plugin/src/test/java/org/sonar/plugins/findbugs/FindbugsXmlReportParserTest.java | 2 | ||||
-rw-r--r-- | plugins/sonar-findbugs-plugin/src/test/resources/org/sonar/plugins/findbugs/findbugsReport.xml (renamed from plugins/sonar-findbugs-plugin/src/test/resources/org/sonar/plugins/findbugs/findbugsXml.xml) | 6 | ||||
-rw-r--r-- | plugins/sonar-findbugs-plugin/src/test/resources/org/sonar/plugins/findbugs/findbugsReportWithUnknownRule.xml | 39 |
5 files changed, 76 insertions, 15 deletions
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 24350cb61ee..01b649544e5 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 @@ -19,6 +19,9 @@ */ package org.sonar.plugins.findbugs; +import java.io.File; +import java.util.List; + import org.apache.commons.lang.StringUtils; import org.sonar.api.CoreProperties; import org.sonar.api.batch.Sensor; @@ -31,9 +34,6 @@ import org.sonar.api.rules.RuleFinder; import org.sonar.api.rules.Violation; import org.sonar.api.utils.Logs; -import java.io.File; -import java.util.List; - public class FindbugsSensor implements Sensor { private RulesProfile profile; private RuleFinder ruleFinder; @@ -63,10 +63,14 @@ public class FindbugsSensor implements Sensor { List<FindbugsXmlReportParser.Violation> fbViolations = reportParser.getViolations(); for (FindbugsXmlReportParser.Violation fbViolation : fbViolations) { Rule rule = ruleFinder.findByKey(FindbugsConstants.REPOSITORY_KEY, fbViolation.getType()); - JavaFile resource = new JavaFile(fbViolation.getSonarJavaFileKey()); - if (context.getResource(resource) != null) { - Violation violation = Violation.create(rule, resource).setLineId(fbViolation.getStart()).setMessage(fbViolation.getLongMessage()); - context.saveViolation(violation); + if (rule != null) { // ignore violations from report, if rule not activated in Sonar + JavaFile resource = new JavaFile(fbViolation.getSonarJavaFileKey()); + if (context.getResource(resource) != null) { + Violation violation = Violation.create(rule, resource).setLineId(fbViolation.getStart()).setMessage(fbViolation.getLongMessage()); + context.saveViolation(violation); + } + } else { + Logs.INFO.debug("Findbugs rule '{}' not active in Sonar", fbViolation.getType()); } } } 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 fb4bac2ebf7..d5124bf0f1a 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 @@ -29,6 +29,8 @@ import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; +import java.io.File; + import org.apache.commons.configuration.Configuration; import org.apache.maven.project.MavenProject; import org.junit.Test; @@ -42,8 +44,6 @@ import org.sonar.api.resources.Resource; import org.sonar.api.rules.Violation; import org.sonar.api.test.IsViolation; -import java.io.File; - public class FindbugsSensorTest extends FindbugsTests { @Test @@ -82,7 +82,8 @@ public class FindbugsSensorTest extends FindbugsTests { 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()); + // We assume that this report was generated during findbugs execution + File xmlFile = new File(getClass().getResource("/org/sonar/plugins/findbugs/findbugsReport.xml").toURI()); when(project.getConfiguration()).thenReturn(conf); when(executor.execute()).thenReturn(xmlFile); when(context.getResource(any(Resource.class))).thenReturn(new JavaFile("org.sonar.MyClass")); @@ -110,7 +111,7 @@ public class FindbugsSensorTest extends FindbugsTests { 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()); + File xmlFile = new File(getClass().getResource("/org/sonar/plugins/findbugs/findbugsReport.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")); @@ -132,6 +133,23 @@ public class FindbugsSensorTest extends FindbugsTests { verify(context).saveViolation(argThat(new IsViolation(wanted))); } + @Test + public void shouldIgnoreNotActiveViolations() 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/findbugsReportWithUnknownRule.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")); + + FindbugsSensor analyser = new FindbugsSensor(createRulesProfileWithActiveRules(), new FindbugsRuleFinder(), executor); + analyser.analyse(project, context); + + verify(context, never()).saveViolation(any(Violation.class)); + } + private Project createProject() { DefaultProjectFileSystem fileSystem = mock(DefaultProjectFileSystem.class); when(fileSystem.hasJavaSourceFiles()).thenReturn(Boolean.TRUE); diff --git a/plugins/sonar-findbugs-plugin/src/test/java/org/sonar/plugins/findbugs/FindbugsXmlReportParserTest.java b/plugins/sonar-findbugs-plugin/src/test/java/org/sonar/plugins/findbugs/FindbugsXmlReportParserTest.java index ca6073ffdae..35511b8cd3f 100644 --- a/plugins/sonar-findbugs-plugin/src/test/java/org/sonar/plugins/findbugs/FindbugsXmlReportParserTest.java +++ b/plugins/sonar-findbugs-plugin/src/test/java/org/sonar/plugins/findbugs/FindbugsXmlReportParserTest.java @@ -36,7 +36,7 @@ public class FindbugsXmlReportParserTest { @Before public void init() { - File findbugsXmlReport = getFile("/org/sonar/plugins/findbugs/findbugsXml.xml"); + File findbugsXmlReport = getFile("/org/sonar/plugins/findbugs/findbugsReport.xml"); FindbugsXmlReportParser xmlParser = new FindbugsXmlReportParser(findbugsXmlReport); violations = xmlParser.getViolations(); } diff --git a/plugins/sonar-findbugs-plugin/src/test/resources/org/sonar/plugins/findbugs/findbugsXml.xml b/plugins/sonar-findbugs-plugin/src/test/resources/org/sonar/plugins/findbugs/findbugsReport.xml index d5d47cf39b7..c4485350c6b 100644 --- a/plugins/sonar-findbugs-plugin/src/test/resources/org/sonar/plugins/findbugs/findbugsXml.xml +++ b/plugins/sonar-findbugs-plugin/src/test/resources/org/sonar/plugins/findbugs/findbugsReport.xml @@ -23,7 +23,7 @@ </SourceLine> <SourceLine endBytecode='65' startBytecode='65' start='112' classname='org.sonar.commons.ZipUtils' primary='true' sourcepath='org/sonar/commons/ZipUtils.java' sourcefile='ZipUtils.java' end='124'> <Message>At ZipUtils.java:[line 107]</Message> - </SourceLine> + </SourceLine> </BugInstance> <BugInstance category='PERFORMANCE' instanceHash='6ba81067bf4e178b360e52be449b1b60' instanceOccurrenceNum='0' priority='3' abbrev='SIC' type='SIC_INNER_SHOULD_BE_STATIC_ANON' instanceOccurrenceMax='0'> <ShortMessage>Could be refactored into a named static inner class</ShortMessage> @@ -37,7 +37,7 @@ <SourceLine start='56' classname='org.sonar.commons.resources.MeasuresDao$1' sourcepath='org/sonar/commons/resources/MeasuresDao.java' synthetic='true' sourcefile='MeasuresDao.java' end='57'> <Message>At MeasuresDao.java:[lines 56-57]</Message> </SourceLine> - </BugInstance> + </BugInstance> <FindBugsSummary> <FindBugsProfile> <ClassProfile avgMicrosecondsPerInvocation='398' maxMicrosecondsPerInvocation='107179' name='edu.umd.cs.findbugs.OpcodeStack$JumpInfoFactory' invocations='4722' totalMilliseconds='1881' standardDeviationMircosecondsPerInvocation='3050'></ClassProfile> @@ -45,4 +45,4 @@ </FindBugsSummary> <ClassFeatures></ClassFeatures> <History></History> -</BugCollection>
\ No newline at end of file +</BugCollection> diff --git a/plugins/sonar-findbugs-plugin/src/test/resources/org/sonar/plugins/findbugs/findbugsReportWithUnknownRule.xml b/plugins/sonar-findbugs-plugin/src/test/resources/org/sonar/plugins/findbugs/findbugsReportWithUnknownRule.xml new file mode 100644 index 00000000000..0b19ae4d818 --- /dev/null +++ b/plugins/sonar-findbugs-plugin/src/test/resources/org/sonar/plugins/findbugs/findbugsReportWithUnknownRule.xml @@ -0,0 +1,39 @@ +<BugCollection version="1.3.9" sequence="0" timestamp="1289571354000" analysisTimestamp="1289571687788" release=""> + <Project projectName=""> + </Project> + <BugInstance type="OBL_UNSATISFIED_OBLIGATION" priority="2" abbrev="OBL" category="EXPERIMENTAL" instanceHash="4d5bb62bd620a4713ef04e9450237c02" instanceOccurrenceNum="0" instanceOccurrenceMax="0"> + <ShortMessage>Method may fail to clean up stream or resource</ShortMessage> + <LongMessage>Method com.exedio.csvtools.DBTool.executeUpdate(String) may fail to clean up java.sql.Statement</LongMessage> + <Class classname="com.exedio.csvtools.DBTool" primary="true"> + <SourceLine classname="com.exedio.csvtools.DBTool" start="55" end="338" sourcefile="DBTool.java" sourcepath="com/exedio/csvtools/DBTool.java"> + <Message>At DBTool.java:[lines 55-338]</Message> + </SourceLine> + <Message>In class com.exedio.csvtools.DBTool</Message> + </Class> + <Method classname="com.exedio.csvtools.DBTool" name="executeUpdate" signature="(Ljava/lang/String;)V" isStatic="false" primary="true"> + <SourceLine classname="com.exedio.csvtools.DBTool" start="301" end="313" startBytecode="0" endBytecode="234" sourcefile="DBTool.java" sourcepath="com/exedio/csvtools/DBTool.java"/> + <Message>In method com.exedio.csvtools.DBTool.executeUpdate(String)</Message> + </Method> + <Class classname="java.sql.Statement" role="CLASS_REFTYPE"> + <SourceLine classname="java.sql.Statement" sourcefile="Statement.java" sourcepath="java/sql/Statement.java"> + <Message>In Statement.java</Message> + </SourceLine> + <Message>Reference type java.sql.Statement</Message> + </Class> + <Int value="1" role="INT_OBLIGATIONS_REMAINING"> + <Message>1 instances of obligation remaining</Message> + </Int> + <SourceLine classname="com.exedio.csvtools.DBTool" primary="true" start="302" end="302" startBytecode="8" endBytecode="8" sourcefile="DBTool.java" sourcepath="com/exedio/csvtools/DBTool.java" role="SOURCE_LINE_OBLIGATION_CREATED"> + <Message>Obligation to clean up resource created at DBTool.java:[line 302] is not discharged</Message> + </SourceLine> + <SourceLine classname="com.exedio.csvtools.DBTool" start="303" end="303" startBytecode="14" endBytecode="14" sourcefile="DBTool.java" sourcepath="com/exedio/csvtools/DBTool.java" role="SOURCE_LINE_PATH_CONTINUES"> + <Message>Path continues at DBTool.java:[line 303]</Message> + </SourceLine> + <SourceLine classname="com.exedio.csvtools.DBTool" start="313" end="313" startBytecode="91" endBytecode="91" sourcefile="DBTool.java" sourcepath="com/exedio/csvtools/DBTool.java" role="SOURCE_LINE_PATH_CONTINUES"> + <Message>Path continues at DBTool.java:[line 313]</Message> + </SourceLine> + <String value="{Statement x 1}" role="STRING_REMAINING_OBLIGATIONS"> + <Message>Remaining obligations: {Statement x 1}</Message> + </String> + </BugInstance> +</BugCollection> |