Merge branch 'analysis-listener' into text-utils-simple

This commit is contained in:
Clément Fournier
2021-12-22 19:23:44 +01:00
3 changed files with 6 additions and 19 deletions

View File

@ -5,31 +5,18 @@
package net.sourceforge.pmd;
import java.util.function.BiConsumer;
import java.util.function.Consumer;
import org.junit.Assert;
import org.junit.Test;
import net.sourceforge.pmd.Report.ReportBuilderListener;
import net.sourceforge.pmd.lang.ast.Node;
import net.sourceforge.pmd.lang.ast.RootNode;
import net.sourceforge.pmd.lang.ast.impl.DummyTreeUtil;
import net.sourceforge.pmd.reporting.FileAnalysisListener;
public class RuleContextTest {
public static Report getReport(Consumer<FileAnalysisListener> sideEffects) {
ReportBuilderListener listener = new ReportBuilderListener();
try {
sideEffects.accept(listener);
} finally {
listener.close();
}
return listener.getResult();
}
public static Report getReport(Rule rule, BiConsumer<Rule, RuleContext> sideEffects) throws Exception {
return getReport(listener -> sideEffects.accept(rule, RuleContext.create(listener, rule)));
return Report.buildReport(listener -> sideEffects.accept(rule, RuleContext.create(listener, rule)));
}
public static Report getReportForRuleApply(Rule rule, Node node) throws Exception {
@ -37,7 +24,7 @@ public class RuleContextTest {
}
public static Report getReportForRuleSetApply(RuleSet ruleset, RootNode node) throws Exception {
return getReport(listener -> new RuleSets(ruleset).apply(node, listener));
return Report.buildReport(listener -> new RuleSets(ruleset).apply(node, listener));
}
@Test

View File

@ -423,7 +423,7 @@ public class RuleSetTest {
RuleSets ruleSets = new RuleSets(listOf(ruleSet1, ruleSet2));
// Two violations
Report report = RuleContextTest.getReport(ctx -> ruleSets.apply(makeCompilationUnits(), ctx));
Report report = Report.buildReport(ctx1 -> ruleSets.apply(makeCompilationUnits(), ctx1));
assertEquals("Violations", 2, report.getViolations().size());
// One violation
@ -434,7 +434,7 @@ public class RuleSetTest {
RuleSets ruleSets2 = new RuleSets(listOf(ruleSet1, ruleSet2));
report = RuleContextTest.getReport(ctx -> ruleSets2.apply(makeCompilationUnits("C:\\package\\RandomClass.java"), ctx));
report = Report.buildReport(ctx -> ruleSets2.apply(makeCompilationUnits("C:\\package\\RandomClass.java"), ctx));
assertEquals("Violations", 1, report.getViolations().size());
}

View File

@ -7,10 +7,10 @@ package net.sourceforge.pmd.cpd;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;
import org.apache.commons.io.IOUtils;
import org.junit.Test;
@ -55,7 +55,7 @@ public class FileReporterTest {
}
private String readFile(File file) throws IOException {
try (BufferedReader reader = new BufferedReader(new FileReader(file))) {
try (Reader reader = new FileReader(file)) {
String text = IOUtils.toString(reader);
return text.replaceAll("\\R", "\n");
}