Skip to content

Commit

Permalink
[core] Refactoring renderer tests
Browse files Browse the repository at this point in the history
  • Loading branch information
adangel committed Feb 17, 2022
1 parent 6b2dde8 commit 3eed116
Show file tree
Hide file tree
Showing 14 changed files with 74 additions and 115 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import net.sourceforge.pmd.Report.ConfigurationError;
import net.sourceforge.pmd.Report.ProcessingError;
import net.sourceforge.pmd.ReportTest;
import net.sourceforge.pmd.Rule;
import net.sourceforge.pmd.RuleContext;
import net.sourceforge.pmd.RulePriority;
import net.sourceforge.pmd.RuleViolation;
Expand Down Expand Up @@ -64,26 +65,53 @@ public void testNullPassedIn() throws Exception {

protected Report reportOneViolation() {
Report report = new Report();
report.addRuleViolation(newRuleViolation(1));
report.addRuleViolation(newRuleViolation(1, 1, 1, 1, createFooRule()));
return report;
}

private Report reportTwoViolations() {
protected Report reportTwoViolations() {
Report report = new Report();
RuleViolation informationalRuleViolation = newRuleViolation(1);
informationalRuleViolation.getRule().setPriority(RulePriority.LOW);
RuleViolation informationalRuleViolation = newRuleViolation(1, 1, 1, 1, createFooRule());
report.addRuleViolation(informationalRuleViolation);
RuleViolation severeRuleViolation = newRuleViolation(2);
severeRuleViolation.getRule().setPriority(RulePriority.HIGH);
RuleViolation severeRuleViolation = newRuleViolation(1, 1, 1, 2, createBooRule());
report.addRuleViolation(severeRuleViolation);
return report;
}

protected RuleViolation newRuleViolation(int endColumn) {
DummyNode node = createNode(endColumn);
protected DummyNode createNode(int beginLine, int beginColumn, int endLine, int endColumn) {
DummyNode node = new DummyNode(1);
node.testingOnlySetBeginLine(beginLine);
node.testingOnlySetBeginColumn(beginColumn);
node.testingOnlySetEndLine(endLine);
node.testingOnlySetEndColumn(endColumn);
return node;
}

protected RuleViolation newRuleViolation(int beginLine, int beginColumn, int endLine, int endColumn, Rule rule) {
DummyNode node = createNode(beginLine, beginColumn, endLine, endColumn);
RuleContext ctx = new RuleContext();
ctx.setSourceCodeFile(new File(getSourceCodeFilename()));
return new ParametricRuleViolation<Node>(new FooRule(), ctx, node, "blah");
return new ParametricRuleViolation<Node>(rule, ctx, node, "blah");
}

/**
* Creates a new rule instance with name "Boo" and priority {@link RulePriority#HIGH}.
*/
protected Rule createBooRule() {
Rule booRule = new FooRule();
booRule.setName("Boo");
booRule.setPriority(RulePriority.HIGH);
return booRule;
}

/**
* Creates a new rule instance with name "Foo" and priority {@link RulePriority#LOW}.
*/
protected Rule createFooRule() {
Rule fooRule = new FooRule();
fooRule.setName("Foo");
fooRule.setPriority(RulePriority.LOW);
return fooRule;
}

protected static DummyNode createNode(int endColumn) {
Expand All @@ -97,14 +125,11 @@ protected static DummyNode createNode(int endColumn) {

@Test
public void testRuleWithProperties() throws Exception {
DummyNode node = createNode(1);
RuleContext ctx = new RuleContext();
ctx.setSourceCodeFile(new File(getSourceCodeFilename()));
Report report = new Report();
RuleWithProperties theRule = new RuleWithProperties();
theRule.setProperty(RuleWithProperties.STRING_PROPERTY_DESCRIPTOR,
"the string value\nsecond line with \"quotes\"");
report.addRuleViolation(new ParametricRuleViolation<Node>(theRule, ctx, node, "blah"));
report.addRuleViolation(newRuleViolation(1, 1, 1, 1, theRule));
String rendered = ReportTest.render(getRenderer(), report);
assertEquals(filter(getExpectedWithProperties()), filter(rendered));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public String getExpectedEmpty() {
public String getExpectedMultiple() {
return getHeader()
+ "\"1\",\"\",\"" + getSourceCodeFilename() + "\",\"5\",\"1\",\"blah\",\"RuleSet\",\"Foo\"" + PMD.EOL
+ "\"2\",\"\",\"" + getSourceCodeFilename() + "\",\"1\",\"1\",\"blah\",\"RuleSet\",\"Foo\"" + PMD.EOL;
+ "\"2\",\"\",\"" + getSourceCodeFilename() + "\",\"1\",\"1\",\"blah\",\"RuleSet\",\"Boo\"" + PMD.EOL;
}

@Override
Expand All @@ -46,8 +46,4 @@ public String getExpectedError(ConfigurationError error) {
private String getHeader() {
return "\"Problem\",\"Package\",\"File\",\"Priority\",\"Line\",\"Description\",\"Rule set\",\"Rule\"" + PMD.EOL;
}

public static junit.framework.Test suite() {
return new junit.framework.JUnit4TestAdapter(CSVRendererTest.class);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,11 @@

import static org.junit.Assert.assertEquals;

import java.io.File;

import org.junit.Test;

import net.sourceforge.pmd.PMD;
import net.sourceforge.pmd.Report;
import net.sourceforge.pmd.ReportTest;
import net.sourceforge.pmd.RuleContext;
import net.sourceforge.pmd.lang.ast.DummyNode;
import net.sourceforge.pmd.lang.ast.Node;
import net.sourceforge.pmd.lang.rule.ParametricRuleViolation;
import net.sourceforge.pmd.lang.rule.XPathRule;

public class CodeClimateRendererTest extends AbstractRendererTest {
Expand Down Expand Up @@ -75,8 +69,8 @@ public String getExpectedMultiple() {
+ "violationSuppressRegex | | Suppress violations with messages matching a regular expression\\n"
+ "violationSuppressXPath | | Suppress violations on nodes which match a given relative XPath expression.\\n"
+ "\"},\"categories\":[\"Style\"],\"location\":{\"path\":\"" + getSourceCodeFilename() + "\",\"lines\":{\"begin\":1,\"end\":1}},\"severity\":\"info\",\"remediation_points\":50000}"
+ "\u0000" + PMD.EOL + "{\"type\":\"issue\",\"check_name\":\"Foo\",\"description\":\"blah\","
+ "\"content\":{\"body\":\"## Foo\\n\\nSince: PMD null\\n\\nPriority: High\\n\\n"
+ "\u0000" + PMD.EOL + "{\"type\":\"issue\",\"check_name\":\"Boo\",\"description\":\"blah\","
+ "\"content\":{\"body\":\"## Boo\\n\\nSince: PMD null\\n\\nPriority: High\\n\\n"
+ "[Categories](https://github.com/codeclimate/platform/blob/master/spec/analyzers/SPEC.md#categories): Style\\n\\n"
+ "[Remediation Points](https://github.com/codeclimate/platform/blob/master/spec/analyzers/SPEC.md#remediation-points): 50000\\n\\n"
+ "desc\\n\\n"
Expand All @@ -90,18 +84,15 @@ public String getExpectedMultiple() {

@Test
public void testXPathRule() throws Exception {
DummyNode node = createNode(1);
RuleContext ctx = new RuleContext();
ctx.setSourceCodeFile(new File(getSourceCodeFilename()));
Report report = new Report();

XPathRule theRule = new XPathRule();
theRule.setProperty(XPathRule.XPATH_DESCRIPTOR, "//dummyNode");

// Setup as FooRule
theRule.setDescription("desc");
theRule.setName("Foo");

report.addRuleViolation(new ParametricRuleViolation<Node>(theRule, ctx, node, "blah"));
report.addRuleViolation(newRuleViolation(1, 1, 1, 2, theRule));
String rendered = ReportTest.render(getRenderer(), report);

// Output should be the exact same as for non xpath rules
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import org.junit.Assert;
import org.junit.Test;

import net.sourceforge.pmd.FooRule;
import net.sourceforge.pmd.Report;
import net.sourceforge.pmd.Report.ConfigurationError;
import net.sourceforge.pmd.Report.ProcessingError;
Expand Down Expand Up @@ -90,7 +91,7 @@ public void suppressedViolations() throws IOException {
Map<Integer, String> suppressedLines = new HashMap<>();
suppressedLines.put(1, "test");
rep.suppress(suppressedLines);
rep.addRuleViolation(newRuleViolation(1));
rep.addRuleViolation(newRuleViolation(1, 1, 1, 1, new FooRule()));
String actual = ReportTest.render(getRenderer(), rep);
String expected = readFile("expected-suppressed.json");
Assert.assertEquals(filter(expected), filter(actual));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public String getExpectedEmpty() {
public String getExpectedMultiple() {
return "* file: " + getSourceCodeFilename() + PMD.EOL + " src: " + getSourceCodeFilename() + ":1:1" + PMD.EOL + " rule: Foo" + PMD.EOL
+ " msg: blah" + PMD.EOL + " code: public class Foo {}" + PMD.EOL + PMD.EOL + " src: "
+ getSourceCodeFilename() + ":1:1" + PMD.EOL + " rule: Foo" + PMD.EOL + " msg: blah" + PMD.EOL
+ getSourceCodeFilename() + ":1:1" + PMD.EOL + " rule: Boo" + PMD.EOL + " msg: blah" + PMD.EOL
+ " code: public class Foo {}" + PMD.EOL + PMD.EOL + PMD.EOL + PMD.EOL + "Summary:" + PMD.EOL
+ PMD.EOL + " : 2" + PMD.EOL + "* warnings: 2" + PMD.EOL;
}
Expand All @@ -67,8 +67,4 @@ public String getExpectedError(ConfigurationError error) {
+ " err: a configuration error" + PMD.EOL + PMD.EOL
+ "* errors: 1" + PMD.EOL + "* warnings: 0" + PMD.EOL;
}

public static junit.framework.Test suite() {
return new junit.framework.JUnit4TestAdapter(PapariTextRendererTest.class);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,12 @@
EmacsRendererTest.class,
HTMLRendererTest.class,
IDEAJRendererTest.class,
JsonRendererTest.class,
PapariTextRendererTest.class,
SarifRendererTest.class,
SummaryHTMLRendererTest.class,
TextPadRendererTest.class,
TextRendererTest.class,
VBHTMLRendererTest.class,
XMLRendererTest.class,
XSLTRendererTest.class,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

import static org.junit.Assert.assertEquals;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
Expand All @@ -16,17 +15,9 @@
import org.json.JSONObject;
import org.junit.Test;

import net.sourceforge.pmd.FooRule;
import net.sourceforge.pmd.Report;
import net.sourceforge.pmd.ReportTest;
import net.sourceforge.pmd.Rule;
import net.sourceforge.pmd.RuleContext;
import net.sourceforge.pmd.RulePriority;
import net.sourceforge.pmd.RuleViolation;
import net.sourceforge.pmd.lang.ast.DummyNode;
import net.sourceforge.pmd.lang.ast.Node;
import net.sourceforge.pmd.lang.rule.AbstractRule;
import net.sourceforge.pmd.lang.rule.ParametricRuleViolation;

public class SarifRendererTest extends AbstractRendererTest {
@Override
Expand Down Expand Up @@ -76,28 +67,16 @@ public String getExpectedErrorWithoutMessage(Report.ProcessingError error) {

@Override
public String filter(String expected) {
return expected.replaceAll("\r\n", "\n"); // make the test run on Windows, too
}

@Override
@Test
public void testRendererMultiple() throws Exception {
Report rep = reportTwoViolations();
String actual = ReportTest.render(getRenderer(), rep);
assertEquals(filter(getExpectedMultiple()), filter(actual));
}

private Report reportTwoViolations() {
Report report = new Report();
RuleViolation informationalRuleViolation = newRuleViolation(1, "Foo");
informationalRuleViolation.getRule().setPriority(RulePriority.LOW);
report.addRuleViolation(informationalRuleViolation);
RuleViolation severeRuleViolation = newRuleViolation(2, "Boo");
severeRuleViolation.getRule().setPriority(RulePriority.HIGH);
report.addRuleViolation(severeRuleViolation);
return report;
return expected.replaceAll("\r\n", "\n") // make the test run on Windows, too
.replaceAll("\"version\": \".+\",", "\"version\": \"unknown\",");
}

/**
* Multiple occurrences of the same rule should be reported as individual results.
*
* @see <a href="https://github.com/pmd/pmd/issues/3768"> [core] SARIF formatter reports multiple locations
* when it should report multiple results #3768</a>
*/
@Test
public void testRendererMultipleLocations() throws Exception {
Report rep = reportThreeViolationsTwoRules();
Expand All @@ -110,12 +89,8 @@ public void testRendererMultipleLocations() throws Exception {
}

private Report reportThreeViolationsTwoRules() {
Rule fooRule = new FooRule();
fooRule.setName("Foo");
fooRule.setPriority(RulePriority.LOW);
Rule booRule = new FooRule();
booRule.setName("Boo");
booRule.setPriority(RulePriority.HIGH);
Rule fooRule = createFooRule();
Rule booRule = createBooRule();

Report report = new Report();
report.addRuleViolation(newRuleViolation(1, 1, 1, 10, fooRule));
Expand All @@ -124,27 +99,6 @@ private Report reportThreeViolationsTwoRules() {
return report;
}

private RuleViolation newRuleViolation(int beginLine, int beginColumn, int endLine, int endColumn, Rule rule) {
DummyNode node = new DummyNode(1);
node.testingOnlySetBeginLine(beginLine);
node.testingOnlySetBeginColumn(beginColumn);
node.testingOnlySetEndLine(endLine);
node.testingOnlySetEndColumn(endColumn);

RuleContext ctx = new RuleContext();
ctx.setSourceCodeFile(new File(getSourceCodeFilename()));
return new ParametricRuleViolation<Node>(rule, ctx, node, "blah");
}

private RuleViolation newRuleViolation(int endColumn, String ruleName) {
DummyNode node = createNode(endColumn);
RuleContext ctx = new RuleContext();
ctx.setSourceCodeFile(new File(getSourceCodeFilename()));
AbstractRule fooRule = new FooRule();
fooRule.setName(ruleName);
return new ParametricRuleViolation<Node>(fooRule, ctx, node, "blah");
}

private String readFile(String name) {
try (InputStream in = SarifRendererTest.class.getResourceAsStream("sarif/" + name)) {
String asd = IOUtils.toString(in, StandardCharsets.UTF_8);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@ public String getExpectedMultiple() {
return "<html><head><title>PMD</title></head><body>" + PMD.EOL + "<center><h2>Summary</h2></center>" + PMD.EOL
+ "<table align=\"center\" cellspacing=\"0\" cellpadding=\"3\">" + PMD.EOL
+ "<tr><th>Rule name</th><th>Number of violations</th></tr>" + PMD.EOL
+ "<tr><td>Foo</td><td align=center>2</td></tr>" + PMD.EOL + "</table>" + PMD.EOL
+ "<tr><td>Boo</td><td align=center>1</td></tr>" + PMD.EOL
+ "<tr><td>Foo</td><td align=center>1</td></tr>" + PMD.EOL + "</table>" + PMD.EOL
+ "<center><h2>Detail</h2></center><table align=\"center\" cellspacing=\"0\" cellpadding=\"3\"><tr>"
+ PMD.EOL
+ "<center><h3>PMD report</h3></center><center><h3>Problems found</h3></center><table align=\"center\" cellspacing=\"0\" cellpadding=\"3\"><tr>"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,6 @@ public String getExpectedEmpty() {

@Override
public String getExpectedMultiple() {
return getSourceCodeFilename() + "(1, Foo): blah" + PMD.EOL + getSourceCodeFilename() + "(1, Foo): blah" + PMD.EOL;
}

public static junit.framework.Test suite() {
return new junit.framework.JUnit4TestAdapter(TextPadRendererTest.class);
return getSourceCodeFilename() + "(1, Foo): blah" + PMD.EOL + getSourceCodeFilename() + "(1, Boo): blah" + PMD.EOL;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public String getExpectedEmpty() {
@Override
public String getExpectedMultiple() {
return getSourceCodeFilename() + ":1:\tFoo:\tblah" + PMD.EOL
+ getSourceCodeFilename() + ":1:\tFoo:\tblah" + PMD.EOL;
+ getSourceCodeFilename() + ":1:\tBoo:\tblah" + PMD.EOL;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,4 @@ public String getExpectedError(ConfigurationError error) {
+ "--></style><body><center><br><table border=\"0\" width=\"80%\"><tr id=TableHeader><td colspan=\"2\"><font class=title>&nbsp;Configuration problems found</font></td></tr><tr id=RowColor2><td><font class=body>"
+ error.rule().getName() + "</font></td><td><font class=body>" + error.issue() + "</font></td></tr></table></center></body></html>" + PMD.EOL;
}

public static junit.framework.Test suite() {
return new junit.framework.JUnit4TestAdapter(VBHTMLRendererTest.class);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public String getExpectedMultiple() {
return getHeader() + "<file name=\"" + getSourceCodeFilename() + "\">" + PMD.EOL
+ "<violation beginline=\"1\" endline=\"1\" begincolumn=\"1\" endcolumn=\"1\" rule=\"Foo\" ruleset=\"RuleSet\" priority=\"5\">"
+ PMD.EOL + "blah" + PMD.EOL + "</violation>" + PMD.EOL
+ "<violation beginline=\"1\" endline=\"1\" begincolumn=\"1\" endcolumn=\"2\" rule=\"Foo\" ruleset=\"RuleSet\" priority=\"1\">"
+ "<violation beginline=\"1\" endline=\"1\" begincolumn=\"1\" endcolumn=\"2\" rule=\"Boo\" ruleset=\"RuleSet\" priority=\"1\">"
+ PMD.EOL + "blah" + PMD.EOL + "</violation>" + PMD.EOL + "</file>" + PMD.EOL + "</pmd>" + PMD.EOL;
}

Expand Down

0 comments on commit 3eed116

Please sign in to comment.