From: Evgeny Mandrikov Date: Mon, 26 Mar 2012 05:48:13 +0000 (+0600) Subject: SONAR-3346 Consume violations directly from PMD - no need to parse XML X-Git-Tag: 3.0~130 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=c831c3b8e7038c79edb817a440f3a550d80b7a8d;p=sonarqube.git SONAR-3346 Consume violations directly from PMD - no need to parse XML --- diff --git a/plugins/sonar-pmd-plugin/src/main/java/org/sonar/plugins/pmd/PmdExecutor.java b/plugins/sonar-pmd-plugin/src/main/java/org/sonar/plugins/pmd/PmdExecutor.java index 7c7e5e38bdb..9b5eb9862bd 100644 --- a/plugins/sonar-pmd-plugin/src/main/java/org/sonar/plugins/pmd/PmdExecutor.java +++ b/plugins/sonar-pmd-plugin/src/main/java/org/sonar/plugins/pmd/PmdExecutor.java @@ -49,7 +49,7 @@ public class PmdExecutor implements BatchExtension { this.configuration = configuration; } - public File execute() throws IOException, PMDException { + public Report execute() throws IOException, PMDException { TimeProfiler profiler = new TimeProfiler().start("Execute PMD " + PmdVersion.getVersion()); ClassLoader initialClassLoader = Thread.currentThread().getContextClassLoader(); @@ -80,7 +80,9 @@ public class PmdExecutor implements BatchExtension { } } - return writeXmlReport(project, report); + writeXmlReport(project, report); + + return report; } finally { profiler.stop(); diff --git a/plugins/sonar-pmd-plugin/src/main/java/org/sonar/plugins/pmd/PmdSensor.java b/plugins/sonar-pmd-plugin/src/main/java/org/sonar/plugins/pmd/PmdSensor.java index d71283507bd..c129ec1dfda 100644 --- a/plugins/sonar-pmd-plugin/src/main/java/org/sonar/plugins/pmd/PmdSensor.java +++ b/plugins/sonar-pmd-plugin/src/main/java/org/sonar/plugins/pmd/PmdSensor.java @@ -19,14 +19,21 @@ */ package org.sonar.plugins.pmd; +import net.sourceforge.pmd.IRuleViolation; +import net.sourceforge.pmd.Report; +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.JavaFile; import org.sonar.api.resources.Project; +import org.sonar.api.resources.Resource; +import org.sonar.api.rules.Rule; import org.sonar.api.rules.RuleFinder; +import org.sonar.api.rules.Violation; import org.sonar.api.utils.XmlParserException; -import java.io.File; +import java.util.Iterator; public class PmdSensor implements Sensor { @@ -42,8 +49,8 @@ public class PmdSensor implements Sensor { public void analyse(Project project, SensorContext context) { try { - File xmlReport = executor.execute(); - getStaxParser(project, context).parse(xmlReport); + Report report = executor.execute(); + analyseReport(report, project, context); } catch (Exception e) { // TOFIX @@ -51,15 +58,32 @@ public class PmdSensor implements Sensor { } } + private void analyseReport(Report report, Project project, SensorContext context) { + Iterator pmdViolationIter = report.iterator(); + while (pmdViolationIter.hasNext()) { + IRuleViolation pmdViolation = pmdViolationIter.next(); + int lineId = pmdViolation.getBeginLine(); + String ruleKey = pmdViolation.getRule().getName(); + String message = pmdViolation.getDescription(); + String filename = pmdViolation.getFilename(); + Resource resource = JavaFile.fromAbsolutePath(filename, project.getFileSystem().getSourceDirs(), false); + // Save violations only for existing resources + if (context.getResource(resource) != null) { + Rule rule = rulesFinder.findByKey(CoreProperties.PMD_PLUGIN, ruleKey); + // Save violations only for enabled rules + if (rule != null) { + Violation violation = Violation.create(rule, resource).setLineId(lineId).setMessage(message); + context.saveViolation(violation); + } + } + } + } + public boolean shouldExecuteOnProject(Project project) { return project.getFileSystem().hasJavaSourceFiles() && !profile.getActiveRulesByRepository(PmdConstants.REPOSITORY_KEY).isEmpty(); } - private PmdViolationsXmlParser getStaxParser(Project project, SensorContext context) { - return new PmdViolationsXmlParser(project, rulesFinder, context); - } - @Override public String toString() { return getClass().getSimpleName(); diff --git a/plugins/sonar-pmd-plugin/src/main/java/org/sonar/plugins/pmd/PmdViolationsXmlParser.java b/plugins/sonar-pmd-plugin/src/main/java/org/sonar/plugins/pmd/PmdViolationsXmlParser.java index 794bbf0c306..f38b8cb8ba3 100644 --- a/plugins/sonar-pmd-plugin/src/main/java/org/sonar/plugins/pmd/PmdViolationsXmlParser.java +++ b/plugins/sonar-pmd-plugin/src/main/java/org/sonar/plugins/pmd/PmdViolationsXmlParser.java @@ -33,8 +33,10 @@ import org.sonar.api.rules.Violation; import org.sonar.api.utils.StaxParser; import javax.xml.stream.XMLStreamException; + import java.io.File; +@Deprecated class PmdViolationsXmlParser { private Project project; diff --git a/plugins/sonar-pmd-plugin/src/test/java/org/sonar/plugins/pmd/PmdExecutorTest.java b/plugins/sonar-pmd-plugin/src/test/java/org/sonar/plugins/pmd/PmdExecutorTest.java index a5597bb6872..4ed43df3951 100644 --- a/plugins/sonar-pmd-plugin/src/test/java/org/sonar/plugins/pmd/PmdExecutorTest.java +++ b/plugins/sonar-pmd-plugin/src/test/java/org/sonar/plugins/pmd/PmdExecutorTest.java @@ -22,7 +22,9 @@ package org.sonar.plugins.pmd; import net.sourceforge.pmd.PMDException; import org.apache.commons.io.FileUtils; import org.apache.commons.lang.StringUtils; +import org.junit.Rule; import org.junit.Test; +import org.junit.rules.TemporaryFolder; import org.sonar.api.resources.Java; import org.sonar.api.resources.Project; import org.sonar.api.resources.ProjectFileSystem; @@ -42,15 +44,19 @@ import static org.mockito.Mockito.when; public class PmdExecutorTest { + @Rule + public TemporaryFolder temp = new TemporaryFolder(); + @Test public void executeOnManySourceDirs() throws URISyntaxException, IOException, PMDException { + final File workDir = temp.getRoot(); Project project = new Project("two-source-dirs"); ProjectFileSystem fs = mock(ProjectFileSystem.class); File root = new File(getClass().getResource("/org/sonar/plugins/pmd/PmdExecutorTest/executeOnManySourceDirs/").toURI()); when(fs.getSourceFiles(Java.INSTANCE)).thenReturn(Arrays.asList(new File(root, "src1/FirstClass.java"), new File(root, "src2/SecondClass.java"))); when(fs.getSourceCharset()).thenReturn(Charset.forName("UTF-8")); - when(fs.getSonarWorkingDirectory()).thenReturn(new File("target")); + when(fs.getSonarWorkingDirectory()).thenReturn(workDir); project.setFileSystem(fs); PmdConfiguration conf = mock(PmdConfiguration.class); @@ -58,7 +64,8 @@ public class PmdExecutorTest { when(conf.getRulesets()).thenReturn(Arrays.asList(file.getAbsolutePath())); PmdExecutor executor = new PmdExecutor(project, conf); - File xmlReport = executor.execute(); + executor.execute(); + File xmlReport = new File(workDir, "pmd-result.xml"); assertThat(xmlReport.exists(), is(true)); String xml = FileUtils.readFileToString(xmlReport); @@ -70,19 +77,21 @@ public class PmdExecutorTest { @Test public void ignorePmdFailures() throws URISyntaxException, IOException, PMDException { + final File workDir = temp.getRoot(); Project project = new Project("ignorePmdFailures"); ProjectFileSystem fs = mock(ProjectFileSystem.class); when(fs.getSourceFiles(Java.INSTANCE)).thenReturn(Arrays.asList(new File("test-resources/ignorePmdFailures/DoesNotCompile.java"))); when(fs.getSourceCharset()).thenReturn(Charset.forName("UTF-8")); - when(fs.getSonarWorkingDirectory()).thenReturn(new File("target")); + when(fs.getSonarWorkingDirectory()).thenReturn(workDir); project.setFileSystem(fs); PmdConfiguration conf = mock(PmdConfiguration.class); when(conf.getRulesets()).thenReturn(Arrays.asList(new File("test-resources/ignorePmdFailures/pmd.xml").getAbsolutePath())); PmdExecutor executor = new PmdExecutor(project, conf); - File xmlReport = executor.execute(); + executor.execute(); + File xmlReport = new File(workDir, "pmd-result.xml"); assertThat(xmlReport.exists(), is(true)); }