net.sourceforge.pmd.Report Java Examples

The following examples show how to use net.sourceforge.pmd.Report. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: DiffTextRenderer.java    From diff-check with GNU Lesser General Public License v2.1 6 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public void end() throws IOException {
    Writer writer = getWriter();
    StringBuilder buf = new StringBuilder(500);
    if (!errors.isEmpty()) {

        for (Report.ProcessingError error : errors) {
        buf.setLength(0);
        buf.append(error.getFile());
        buf.append("\t-\t").append(error.getMsg()).append(PMD.EOL);
        writer.write(buf.toString());
        }
    }

    for (Report.SuppressedViolation excluded : suppressed) {
        buf.setLength(0);
        buf.append(excluded.getRuleViolation().getRule().getName());
        buf.append(" rule violation suppressed by ");
        buf.append(excluded.suppressedByNOPMD() ? "//NOPMD" : "Annotation");
        buf.append(" in ").append(excluded.getRuleViolation().getFilename()).append(PMD.EOL);
        writer.write(buf.toString());
    }
}
 
Example #2
Source File: PmdConfiguration.java    From sonar-p3c-pmd with MIT License 6 votes vote down vote up
public File dumpXmlReport(Report report) {
  if (!settings.getBoolean(PROPERTY_GENERATE_XML)) {
    return null;
  }

  try {
    String reportAsString = reportToString(report);

    File reportFile = writeToWorkingDirectory(reportAsString, PMD_RESULT_XML);

    LOG.info("PMD output report: " + reportFile.getAbsolutePath());

    return reportFile;
  } catch (IOException e) {
    throw new IllegalStateException("Fail to save the PMD report", e);
  }
}
 
Example #3
Source File: PmdExecutorTest.java    From sonar-p3c-pmd with MIT License 6 votes vote down vote up
@Test
public void should_execute_pmd_on_source_files_and_test_files() throws Exception {
  InputFile srcFile = file("src/Class.java", Type.MAIN);
  InputFile tstFile = file("test/ClassTest.java", Type.TEST);
  setupPmdRuleSet(PmdConstants.REPOSITORY_KEY, "simple.xml");
  setupPmdRuleSet(PmdConstants.TEST_REPOSITORY_KEY, "junit.xml");
  fileSystem.add(srcFile);
  fileSystem.add(tstFile);

  Report report = pmdExecutor.execute();

  assertThat(report).isNotNull();

  // setting java source version to the default value
  settings.removeProperty(PmdConstants.JAVA_SOURCE_VERSION);
  report = pmdExecutor.execute();

  assertThat(report).isNotNull();
}
 
Example #4
Source File: PmdConfigurationTest.java    From sonar-p3c-pmd with MIT License 5 votes vote down vote up
@Test
public void should_ignore_xml_report_when_property_is_not_set() {
  File reportFile = configuration.dumpXmlReport(new Report());

  assertThat(reportFile).isNull();
  verifyZeroInteractions(fs);
}
 
Example #5
Source File: CollectorRendererTest.java    From sputnik with Apache License 2.0 5 votes vote down vote up
@NotNull
private Report createReportWithVolation(@NotNull RuleViolation violation, @NotNull Configuration config) {
    renderer = new CollectorRenderer(config);
    Report report = mock(Report.class);
    List<RuleViolation> list = new ArrayList<RuleViolation>();
    list.add(violation);
    when(report.iterator()).thenReturn(list.iterator());

    return report;
}
 
Example #6
Source File: CollectorRendererTest.java    From sputnik with Apache License 2.0 5 votes vote down vote up
@Test
void shouldReportViolationWithoutDetails() throws IOException {
    Configuration config = new ConfigurationSetup().setUp(ImmutableMap.of(GeneralOption.PMD_SHOW_VIOLATION_DETAILS.getKey(), "false"));
    Rule rule = createRule(RULE_DESCRIPTION, EXTERNAL_INFO_URL, RulePriority.MEDIUM, config);
    Report report = createReportWithVolation(createRuleViolation(rule, VIOLATION_DESCRIPTION, config), config);

    renderer.renderFileReport(report);

    Violation violation = renderer.getReviewResult().getViolations().get(0);
    assertThat(violation.getMessage()).isEqualTo(VIOLATION_DESCRIPTION);
}
 
Example #7
Source File: CollectorRendererTest.java    From sputnik with Apache License 2.0 5 votes vote down vote up
@Test
void shouldReportViolationWithDetails() throws IOException {
    Configuration config = new ConfigurationSetup().setUp(ImmutableMap.of(GeneralOption.PMD_SHOW_VIOLATION_DETAILS.getKey(), "true"));
    Rule rule = createRule(RULE_DESCRIPTION, EXTERNAL_INFO_URL, RulePriority.HIGH, config);
    Report report = createReportWithVolation(createRuleViolation(rule, VIOLATION_DESCRIPTION, config), config);

    renderer.renderFileReport(report);

    Violation violation = renderer.getReviewResult().getViolations().get(0);
    assertThat(violation.getMessage()).isEqualTo(DESCRIPTION_WITH_DETAILS);
    assertThat(violation.getSeverity()).isEqualTo(Severity.ERROR);
}
 
Example #8
Source File: CollectorRenderer.java    From sputnik with Apache License 2.0 5 votes vote down vote up
@Override
public void renderFileReport(Report report) throws IOException {
    boolean showDetails = Boolean.valueOf(configuration.getProperty(GeneralOption.PMD_SHOW_VIOLATION_DETAILS));

    for (RuleViolation ruleViolation : report) {
        String violationDescription = showDetails ? renderViolationDetails(ruleViolation) :ruleViolation.getDescription();
        reviewResult.add(new Violation(ruleViolation.getFilename(), ruleViolation.getBeginLine(), violationDescription, convert(ruleViolation.getRule().getPriority())));
    }
}
 
Example #9
Source File: PmdExecutorTest.java    From sonar-p3c-pmd with MIT License 5 votes vote down vote up
@Test
public void should_dump_configuration_as_xml() {
  when(pmdProfileExporter.exportProfile(PmdConstants.REPOSITORY_KEY, rulesProfile)).thenReturn(PmdTestUtils.getResourceContent("/org/sonar/plugins/pmd/simple.xml"));
  when(pmdProfileExporter.exportProfile(PmdConstants.TEST_REPOSITORY_KEY, rulesProfile)).thenReturn(PmdTestUtils.getResourceContent("/org/sonar/plugins/pmd/junit.xml"));

  Report report = pmdExecutor.execute();

  verify(pmdConfiguration).dumpXmlReport(report);
}
 
Example #10
Source File: PmdSensorTest.java    From sonar-p3c-pmd with MIT License 5 votes vote down vote up
@Test
public void shouldnt_report_invalid_violation() {
  RuleViolation pmdViolation = violation();
  Report report = report(pmdViolation);
  when(executor.execute()).thenReturn(report);
  when(report.iterator()).thenReturn(Iterators.forArray(pmdViolation));

  pmdSensor.analyse(project, sensorContext);

  verifyZeroInteractions(sensorContext);
}
 
Example #11
Source File: PmdSensorTest.java    From sonar-p3c-pmd with MIT License 5 votes vote down vote up
@Test
public void shouldnt_report_zero_violation() {
  Report report = report();
  when(executor.execute()).thenReturn(report);

  pmdSensor.analyse(project, sensorContext);

  verifyZeroInteractions(sensorContext);
}
 
Example #12
Source File: PmdSensorTest.java    From sonar-p3c-pmd with MIT License 5 votes vote down vote up
@Test
public void should_report_violations() {
  RuleViolation pmdViolation = violation();
  Report report = report(pmdViolation);
  when(executor.execute()).thenReturn(report);

  pmdSensor.analyse(project, sensorContext);

  verify(pmdViolationRecorder).saveViolation(pmdViolation);
}
 
Example #13
Source File: Main.java    From diff-check with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * Create a report, filter out any defective rules, and keep a record of
 * them.
 *
 * @param rs the rules
 * @param ctx the rule context
 * @param fileName the filename of the source file, which should appear in the report
 * @return the Report
 */
public static Report setupReport(RuleSets rs, RuleContext ctx, String fileName) {

    Set<Rule> brokenRules = removeBrokenRules(rs);
    Report report = Report.createReport(ctx, fileName);

    for (Rule rule : brokenRules) {
        report.addConfigError(new Report.RuleConfigurationError(rule, rule.dysfunctionReason()));
    }

    return report;
}
 
Example #14
Source File: PmdConfigurationTest.java    From sonar-p3c-pmd with MIT License 5 votes vote down vote up
@Test
public void should_fail_to_dump_xml_report() throws Exception {
  when(fs.workDir()).thenReturn(new File("xxx"));

  settings.setProperty(PmdConfiguration.PROPERTY_GENERATE_XML, true);

  expectedException.expect(IllegalStateException.class);
  expectedException.expectMessage("Fail to save the PMD report");

  configuration.dumpXmlReport(new Report());
}
 
Example #15
Source File: PmdConfigurationTest.java    From sonar-p3c-pmd with MIT License 5 votes vote down vote up
@Test
public void should_dump_xml_report() throws IOException {
  when(fs.workDir()).thenReturn(WORK_DIR);

  settings.setProperty(PmdConfiguration.PROPERTY_GENERATE_XML, true);
  File reportFile = configuration.dumpXmlReport(new Report());

  assertThat(reportFile).isEqualTo(new File(WORK_DIR, "pmd-result.xml"));
  List<String> writtenLines = Files.readLines(reportFile, Charsets.UTF_8);
  assertThat(writtenLines).hasSize(3);
  assertThat(writtenLines.get(1)).contains("<pmd");
}
 
Example #16
Source File: PmdSensor.java    From sonar-p3c-pmd with MIT License 5 votes vote down vote up
@Override
public void analyse(Project project, SensorContext context) {
  try {
    Report report = executor.execute();
    for (RuleViolation violation : report) {
      pmdViolationRecorder.saveViolation(violation);
    }
  } catch (Exception e) {
    throw new XmlParserException(e);
  }
}
 
Example #17
Source File: PmdExecutor.java    From sonar-p3c-pmd with MIT License 5 votes vote down vote up
private Report executePmd(URLClassLoader classLoader) {
  Report report = new Report();

  RuleContext context = new RuleContext();
  context.setReport(report);

  PmdTemplate pmdFactory = createPmdTemplate(classLoader);
  executeRules(pmdFactory, context, javaFiles(Type.MAIN), PmdConstants.REPOSITORY_KEY);
  executeRules(pmdFactory, context, javaFiles(Type.TEST), PmdConstants.TEST_REPOSITORY_KEY);

  pmdConfiguration.dumpXmlReport(report);

  return report;
}
 
Example #18
Source File: PmdExecutor.java    From sonar-p3c-pmd with MIT License 5 votes vote down vote up
public Report execute() {
  TimeProfiler profiler = new TimeProfiler().start("Execute PMD " + PmdVersion.getVersion());

  ClassLoader initialClassLoader = Thread.currentThread().getContextClassLoader();
  URLClassLoader classLoader = createClassloader();
  try {
    Thread.currentThread().setContextClassLoader(getClass().getClassLoader());

    return executePmd(classLoader);
  } finally {
    Closeables.closeQuietly(classLoader);
    Thread.currentThread().setContextClassLoader(initialClassLoader);
    profiler.stop();
  }
}
 
Example #19
Source File: PmdConfiguration.java    From sonar-p3c-pmd with MIT License 5 votes vote down vote up
private static String reportToString(Report report) throws IOException {
  StringWriter output = new StringWriter();

  Renderer xmlRenderer = new XMLRenderer();
  xmlRenderer.setWriter(output);
  xmlRenderer.start();
  xmlRenderer.renderFileReport(report);
  xmlRenderer.end();

  return output.toString();
}
 
Example #20
Source File: Main.java    From diff-check with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * Creates a new rule context, initialized with a new, empty report.
 *
 * @param sourceCodeFilename the source code filename
 * @param sourceCodeFile the source code file
 * @return the rule context
 */
public static RuleContext newRuleContext(String sourceCodeFilename, File sourceCodeFile) {

    RuleContext context = new RuleContext();
    context.setSourceCodeFile(sourceCodeFile);
    context.setSourceCodeFilename(sourceCodeFilename);
    context.setReport(new Report());
    return context;
}