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();
}
}
- return writeXmlReport(project, report);
+ writeXmlReport(project, report);
+
+ return report;
} finally {
profiler.stop();
*/
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 {
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
}
}
+ private void analyseReport(Report report, Project project, SensorContext context) {
+ Iterator<IRuleViolation> 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();
import org.sonar.api.utils.StaxParser;
import javax.xml.stream.XMLStreamException;
+
import java.io.File;
+@Deprecated
class PmdViolationsXmlParser {
private Project project;
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;
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);
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);
@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));
}