]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-1977: Ignore violations from findbugs report, if rule not activated in Sonar
authorGodin <mandrikov@gmail.com>
Mon, 15 Nov 2010 23:22:48 +0000 (23:22 +0000)
committerGodin <mandrikov@gmail.com>
Mon, 15 Nov 2010 23:22:48 +0000 (23:22 +0000)
plugins/sonar-findbugs-plugin/src/main/java/org/sonar/plugins/findbugs/FindbugsSensor.java
plugins/sonar-findbugs-plugin/src/test/java/org/sonar/plugins/findbugs/FindbugsSensorTest.java
plugins/sonar-findbugs-plugin/src/test/java/org/sonar/plugins/findbugs/FindbugsXmlReportParserTest.java
plugins/sonar-findbugs-plugin/src/test/resources/org/sonar/plugins/findbugs/findbugsReport.xml [new file with mode: 0644]
plugins/sonar-findbugs-plugin/src/test/resources/org/sonar/plugins/findbugs/findbugsReportWithUnknownRule.xml [new file with mode: 0644]
plugins/sonar-findbugs-plugin/src/test/resources/org/sonar/plugins/findbugs/findbugsXml.xml [deleted file]

index 24350cb61eef240b5acc27d8441fe4e0d627aa3c..01b649544e57019967377b4cf49c761fcb3ec4a0 100644 (file)
@@ -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());
       }
     }
   }
index fb4bac2ebf7dc89f9a9ff7685708c8424e1b561a..d5124bf0f1a131069d691d0f63db4a0c977524d2 100644 (file)
@@ -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);
index ca6073ffdaefbcdb5f59740e5b7596306beed1e9..35511b8cd3f9345d0ab0999dd166107263ebe793 100644 (file)
@@ -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/findbugsReport.xml b/plugins/sonar-findbugs-plugin/src/test/resources/org/sonar/plugins/findbugs/findbugsReport.xml
new file mode 100644 (file)
index 0000000..c448535
--- /dev/null
@@ -0,0 +1,48 @@
+<BugCollection timestamp='1282919233000' analysisTimestamp='1282919402891' sequence='0' release='' version='1.3.9'>
+  <Project projectName=''>
+    <Jar>/Users/freddy/Documents/sonar_projects/sonar/sonar-commons/target/classes</Jar>
+    <AuxClasspathEntry>/Users/freddy/.m2/repository/org/apache/maven/reporting/maven-reporting-impl/2.0/maven-reporting-impl-2.0.jar</AuxClasspathEntry>
+    <SrcDir>/Users/freddy/Documents/sonar_projects/sonar/sonar-commons/src/main/java</SrcDir>
+    <WrkDir>/Users/freddy/Documents/sonar_projects/sonar/sonar-commons/target</WrkDir>
+  </Project>
+  <BugInstance category='BAD_PRACTICE' instanceHash='65c6770c9055a84db4899b95826e4edd' instanceOccurrenceNum='0' priority='2' abbrev='AM' type='AM_CREATES_EMPTY_ZIP_FILE_ENTRY' instanceOccurrenceMax='0'>
+    <ShortMessage>Creates an empty zip file entry</ShortMessage>
+    <LongMessage>Empty zip file entry created in org.sonar.commons.ZipUtils._zip(String, File, ZipOutputStream)</LongMessage>
+    <Class classname='org.sonar.commons.ZipUtils' primary='true'>
+      <SourceLine start='33' classname='org.sonar.commons.ZipUtils' sourcepath='org/sonar/commons/ZipUtils.java' sourcefile='ZipUtils.java' end='139'>
+        <Message>At ZipUtils.java:[lines 33-139]</Message>
+      </SourceLine>
+      <Message>In class org.sonar.commons.ZipUtils</Message>
+    </Class>
+    <Method isStatic='true' classname='org.sonar.commons.ZipUtils' name='_zip' primary='true' signature='(Ljava/lang/String;Ljava/io/File;Ljava/util/zip/ZipOutputStream;)V'>
+      <SourceLine endBytecode='353' startBytecode='0' start='103' classname='org.sonar.commons.ZipUtils' sourcepath='org/sonar/commons/ZipUtils.java' sourcefile='ZipUtils.java' end='124'></SourceLine>
+      <Message>In method org.sonar.commons.ZipUtils._zip(String, File, ZipOutputStream)</Message>
+    </Method>
+    <SourceLine endBytecode='42' startBytecode='42' start='107' classname='org.sonar.commons.ZipUtils' primary='true' sourcepath='org/sonar/commons/ZipUtils.java' sourcefile='ZipUtils.java' end='107'>
+      <Message>At ZipUtils.java:[line 107]</Message>
+    </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>
+  </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>
+    <LongMessage>The class org.sonar.commons.resources.MeasuresDao$1 could be refactored into a named _static_ inner class</LongMessage>
+    <Class classname='org.sonar.commons.resources.MeasuresDao$1' primary='true'>
+      <SourceLine start='56' classname='org.sonar.commons.resources.MeasuresDao$1' sourcepath='org/sonar/commons/resources/MeasuresDao.java' sourcefile='MeasuresDao.java' end='57'>
+        <Message>At MeasuresDao.java:[lines 56-57]</Message>
+      </SourceLine>
+      <Message>In class org.sonar.commons.resources.MeasuresDao$1</Message>
+    </Class>
+    <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>
+  <FindBugsSummary>
+    <FindBugsProfile>
+      <ClassProfile avgMicrosecondsPerInvocation='398' maxMicrosecondsPerInvocation='107179' name='edu.umd.cs.findbugs.OpcodeStack$JumpInfoFactory' invocations='4722' totalMilliseconds='1881' standardDeviationMircosecondsPerInvocation='3050'></ClassProfile>
+    </FindBugsProfile>
+  </FindBugsSummary>
+  <ClassFeatures></ClassFeatures>
+  <History></History>
+</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 (file)
index 0000000..0b19ae4
--- /dev/null
@@ -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>
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/findbugsXml.xml
deleted file mode 100644 (file)
index d5d47cf..0000000
+++ /dev/null
@@ -1,48 +0,0 @@
-<BugCollection timestamp='1282919233000' analysisTimestamp='1282919402891' sequence='0' release='' version='1.3.9'>
-  <Project projectName=''>
-    <Jar>/Users/freddy/Documents/sonar_projects/sonar/sonar-commons/target/classes</Jar>
-    <AuxClasspathEntry>/Users/freddy/.m2/repository/org/apache/maven/reporting/maven-reporting-impl/2.0/maven-reporting-impl-2.0.jar</AuxClasspathEntry>
-    <SrcDir>/Users/freddy/Documents/sonar_projects/sonar/sonar-commons/src/main/java</SrcDir>
-    <WrkDir>/Users/freddy/Documents/sonar_projects/sonar/sonar-commons/target</WrkDir>
-  </Project>
-  <BugInstance category='BAD_PRACTICE' instanceHash='65c6770c9055a84db4899b95826e4edd' instanceOccurrenceNum='0' priority='2' abbrev='AM' type='AM_CREATES_EMPTY_ZIP_FILE_ENTRY' instanceOccurrenceMax='0'>
-    <ShortMessage>Creates an empty zip file entry</ShortMessage>
-    <LongMessage>Empty zip file entry created in org.sonar.commons.ZipUtils._zip(String, File, ZipOutputStream)</LongMessage>
-    <Class classname='org.sonar.commons.ZipUtils' primary='true'>
-      <SourceLine start='33' classname='org.sonar.commons.ZipUtils' sourcepath='org/sonar/commons/ZipUtils.java' sourcefile='ZipUtils.java' end='139'>
-        <Message>At ZipUtils.java:[lines 33-139]</Message>
-      </SourceLine>
-      <Message>In class org.sonar.commons.ZipUtils</Message>
-    </Class>
-    <Method isStatic='true' classname='org.sonar.commons.ZipUtils' name='_zip' primary='true' signature='(Ljava/lang/String;Ljava/io/File;Ljava/util/zip/ZipOutputStream;)V'>
-      <SourceLine endBytecode='353' startBytecode='0' start='103' classname='org.sonar.commons.ZipUtils' sourcepath='org/sonar/commons/ZipUtils.java' sourcefile='ZipUtils.java' end='124'></SourceLine>
-      <Message>In method org.sonar.commons.ZipUtils._zip(String, File, ZipOutputStream)</Message>
-    </Method>
-    <SourceLine endBytecode='42' startBytecode='42' start='107' classname='org.sonar.commons.ZipUtils' primary='true' sourcepath='org/sonar/commons/ZipUtils.java' sourcefile='ZipUtils.java' end='107'>
-      <Message>At ZipUtils.java:[line 107]</Message>
-    </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>    
-  </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>
-    <LongMessage>The class org.sonar.commons.resources.MeasuresDao$1 could be refactored into a named _static_ inner class</LongMessage>
-    <Class classname='org.sonar.commons.resources.MeasuresDao$1' primary='true'>
-      <SourceLine start='56' classname='org.sonar.commons.resources.MeasuresDao$1' sourcepath='org/sonar/commons/resources/MeasuresDao.java' sourcefile='MeasuresDao.java' end='57'>
-        <Message>At MeasuresDao.java:[lines 56-57]</Message>
-      </SourceLine>
-      <Message>In class org.sonar.commons.resources.MeasuresDao$1</Message>
-    </Class>
-    <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>  
-  <FindBugsSummary>
-    <FindBugsProfile>
-      <ClassProfile avgMicrosecondsPerInvocation='398' maxMicrosecondsPerInvocation='107179' name='edu.umd.cs.findbugs.OpcodeStack$JumpInfoFactory' invocations='4722' totalMilliseconds='1881' standardDeviationMircosecondsPerInvocation='3050'></ClassProfile>
-    </FindBugsProfile>
-  </FindBugsSummary>
-  <ClassFeatures></ClassFeatures>
-  <History></History>
-</BugCollection>  
\ No newline at end of file