Fix tests
This commit is contained in:
@ -8,7 +8,6 @@ import static org.junit.Assert.assertEquals;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@ -17,11 +16,8 @@ import org.junit.runner.RunWith;
|
||||
import org.junit.runners.Parameterized;
|
||||
|
||||
import net.sourceforge.pmd.Report;
|
||||
import net.sourceforge.pmd.Rule;
|
||||
import net.sourceforge.pmd.RuleViolation;
|
||||
import net.sourceforge.pmd.lang.LanguageRegistry;
|
||||
import net.sourceforge.pmd.lang.apex.ApexLanguageModule;
|
||||
import net.sourceforge.pmd.testframework.RuleTst;
|
||||
import net.sourceforge.pmd.lang.apex.ast.ApexParserTestBase;
|
||||
|
||||
/**
|
||||
* <p>Sharing settings are not inherited by inner classes. Sharing settings need to be declared on the class that
|
||||
@ -32,7 +28,7 @@ import net.sourceforge.pmd.testframework.RuleTst;
|
||||
* should trigger a violation.</p>
|
||||
*/
|
||||
@RunWith(Parameterized.class)
|
||||
public class ApexSharingViolationsNestedClassTest extends RuleTst {
|
||||
public class ApexSharingViolationsNestedClassTest extends ApexParserTestBase {
|
||||
/**
|
||||
* Type of operation that may require a sharing declaration.
|
||||
*/
|
||||
@ -85,20 +81,15 @@ public class ApexSharingViolationsNestedClassTest extends RuleTst {
|
||||
@Test
|
||||
public void testSharingPermutation() {
|
||||
String apexClass = generateClass(outerSharingDeclared, outerOperation, innerSharingDeclared, innerOperation);
|
||||
Report rpt = executeRule(apexClass, new ApexSharingViolationsRule(),
|
||||
LanguageRegistry.getLanguage(ApexLanguageModule.NAME).getDefaultVersion());
|
||||
ApexSharingViolationsRule rule = new ApexSharingViolationsRule();
|
||||
rule.setMessage("gotcha!");
|
||||
Report rpt = apex.executeRule(rule, apexClass);
|
||||
List<RuleViolation> violations = rpt.getViolations();
|
||||
assertEquals("Unexpected Violation Size\n" + apexClass, expectedViolations, violations.size());
|
||||
List<Integer> lineNumbers = violations.stream().map(v -> v.getBeginLine()).collect(Collectors.toList());
|
||||
assertEquals("Unexpected Line Numbers\n" + apexClass, expectedLineNumbers, lineNumbers);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected List<Rule> getRules() {
|
||||
Rule rule = findRule("category/apex/security.xml", "ApexSharingViolations");
|
||||
return Collections.singletonList(rule);
|
||||
}
|
||||
|
||||
/**
|
||||
* Parameter provider that covers are all permutations
|
||||
*/
|
||||
|
@ -60,7 +60,9 @@ public interface RuleViolationFactory {
|
||||
}
|
||||
|
||||
|
||||
RuleViolation createViolation(Rule rule, @NonNull Node location, @NonNull String filename, @NonNull String formattedMessage);
|
||||
default RuleViolation createViolation(Rule rule, @NonNull Node location, @NonNull String filename, @NonNull String formattedMessage) {
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
SuppressedViolation suppressOrNull(Node location, RuleViolation violation);
|
||||
|
@ -13,7 +13,6 @@ import org.checkerframework.checker.nullness.qual.NonNull;
|
||||
|
||||
import net.sourceforge.pmd.Report.SuppressedViolation;
|
||||
import net.sourceforge.pmd.Rule;
|
||||
import net.sourceforge.pmd.RuleContext;
|
||||
import net.sourceforge.pmd.RuleViolation;
|
||||
import net.sourceforge.pmd.ViolationSuppressor;
|
||||
import net.sourceforge.pmd.lang.ast.Node;
|
||||
@ -56,17 +55,6 @@ public class DefaultRuleViolationFactory implements RuleViolationFactory {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
|
||||
protected RuleViolation createRuleViolation(Rule rule, RuleContext ruleContext, Node node, String message) {
|
||||
return new ParametricRuleViolation<>(rule, ruleContext, node, message);
|
||||
}
|
||||
|
||||
protected RuleViolation createRuleViolation(Rule rule, RuleContext ruleContext, Node node, String message,
|
||||
int beginLine, int endLine) {
|
||||
ParametricRuleViolation<Node> rv = new ParametricRuleViolation<>(rule, ruleContext, node, message);
|
||||
rv.setLines(beginLine, endLine);
|
||||
return rv;
|
||||
}
|
||||
|
||||
private Set<ViolationSuppressor> getAllSuppressors() {
|
||||
if (allSuppressors == null) {
|
||||
// lazy loaded because calling getSuppressors in constructor
|
||||
|
@ -66,6 +66,7 @@ public class ReportTest {
|
||||
hasViolation = true;
|
||||
}
|
||||
}
|
||||
|
||||
Report rpt = new Report();
|
||||
MyListener listener = new MyListener();
|
||||
rpt.addListener(listener);
|
||||
|
@ -14,6 +14,8 @@ import java.util.function.Consumer;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import net.sourceforge.pmd.lang.DummyLanguageModule;
|
||||
import net.sourceforge.pmd.lang.LanguageRegistry;
|
||||
import net.sourceforge.pmd.lang.ast.Node;
|
||||
import net.sourceforge.pmd.lang.ast.RootNode;
|
||||
|
||||
@ -23,10 +25,13 @@ public class RuleContextTest {
|
||||
public static Report getReport(Consumer<RuleContext> sideEffects) throws Exception {
|
||||
Report report = new Report();
|
||||
RuleContext ctx = new RuleContext();
|
||||
ctx.setSourceCodeFile(new File("test.dummy"));
|
||||
ctx.setReport(report);
|
||||
ctx.setLanguageVersion(LanguageRegistry.getLanguage(DummyLanguageModule.NAME).getDefaultVersion());
|
||||
sideEffects.accept(ctx);
|
||||
return report;
|
||||
}
|
||||
|
||||
public static Report getReport(Rule rule, BiConsumer<Rule, RuleContext> sideEffects) throws Exception {
|
||||
return getReport(ctx -> sideEffects.accept(rule, ctx));
|
||||
}
|
||||
|
@ -78,7 +78,7 @@ public class PMDTaskTest {
|
||||
String actual = IOUtils.toString(in, StandardCharsets.UTF_8);
|
||||
// remove any trailing newline
|
||||
actual = actual.replaceAll("\n|\r", "");
|
||||
Assert.assertEquals("sample.dummy:0:\tTest Rule 2", actual);
|
||||
Assert.assertEquals("sample.dummy:1:\tTest Rule 2", actual);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -6,8 +6,9 @@ package net.sourceforge.pmd.lang;
|
||||
|
||||
import java.io.Reader;
|
||||
|
||||
import org.checkerframework.checker.nullness.qual.NonNull;
|
||||
|
||||
import net.sourceforge.pmd.Rule;
|
||||
import net.sourceforge.pmd.RuleContext;
|
||||
import net.sourceforge.pmd.RuleViolation;
|
||||
import net.sourceforge.pmd.lang.ast.DummyAstStages;
|
||||
import net.sourceforge.pmd.lang.ast.DummyRoot;
|
||||
@ -64,15 +65,10 @@ public class DummyLanguageModule extends BaseLanguageModule {
|
||||
}
|
||||
|
||||
public static class RuleViolationFactory extends DefaultRuleViolationFactory {
|
||||
@Override
|
||||
protected RuleViolation createRuleViolation(Rule rule, RuleContext ruleContext, Node node, String message) {
|
||||
return createRuleViolation(rule, ruleContext, node, message, 0, 0);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected RuleViolation createRuleViolation(Rule rule, RuleContext ruleContext, Node node, String message,
|
||||
int beginLine, int endLine) {
|
||||
ParametricRuleViolation<Node> rv = new ParametricRuleViolation<Node>(rule, ruleContext, node, message) {
|
||||
public RuleViolation createViolation(Rule rule, @NonNull Node location, @NonNull String filename, @NonNull String formattedMessage) {
|
||||
return new ParametricRuleViolation<Node>(rule, filename, location, formattedMessage) {
|
||||
{
|
||||
this.packageName = "foo"; // just for testing variable expansion
|
||||
}
|
||||
@ -82,8 +78,6 @@ public class DummyLanguageModule extends BaseLanguageModule {
|
||||
return super.getPackageName();
|
||||
}
|
||||
};
|
||||
rv.setLines(beginLine, endLine);
|
||||
return rv;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -130,7 +130,7 @@ public class SummaryHTMLRendererTest extends AbstractRendererTest {
|
||||
+ PMD.EOL + "<th>#</th><th>File</th><th>Line</th><th>Problem</th></tr>" + PMD.EOL
|
||||
+ "</table><hr/><center><h3>Suppressed warnings</h3></center><table align=\"center\" cellspacing=\"0\" cellpadding=\"3\"><tr>"
|
||||
+ PMD.EOL + "<th>File</th><th>Line</th><th>Rule</th><th>NOPMD or Annotation</th><th>Reason</th></tr>"
|
||||
+ PMD.EOL + "<tr bgcolor=\"lightgrey\"> " + PMD.EOL + "<td align=\"left\"><a href=\"link_prefix.html#line_prefix1\"></a></td>" + PMD.EOL
|
||||
+ PMD.EOL + "<tr bgcolor=\"lightgrey\"> " + PMD.EOL + "<td align=\"left\"><a href=\"link_prefixtest.html#line_prefix1\">test</a></td>" + PMD.EOL
|
||||
+ "<td align=\"center\">1</td>" + PMD.EOL + "<td align=\"center\">Foo</td>" + PMD.EOL
|
||||
+ "<td align=\"center\">//NOPMD</td>" + PMD.EOL + "<td align=\"center\">test</td>" + PMD.EOL
|
||||
+ "</tr>"
|
||||
|
@ -49,7 +49,7 @@ public final class JavaRuleViolationFactory extends DefaultRuleViolationFactory
|
||||
|
||||
@Override
|
||||
public RuleViolation createViolation(Rule rule, @NonNull Node location, @NonNull String filename, @NonNull String formattedMessage) {
|
||||
return new JavaRuleViolation(rule, filename, (JavaNode) location, formattedMessage);
|
||||
return new JavaRuleViolation(rule, (JavaNode) location, filename, formattedMessage);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -8,7 +8,6 @@ import static net.sourceforge.pmd.lang.ParserOptionsTest.verifyOptionsEqualsHash
|
||||
import static net.sourceforge.pmd.util.CollectionUtil.listOf;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import java.util.List;
|
||||
@ -64,8 +63,6 @@ public class EcmascriptParserOptionsTest {
|
||||
|
||||
options.setSuppressMarker("foo");
|
||||
assertEquals("foo", options.getSuppressMarker());
|
||||
options.setSuppressMarker(null);
|
||||
assertNull(options.getSuppressMarker());
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -6,8 +6,9 @@ package net.sourceforge.pmd.test.lang;
|
||||
|
||||
import java.io.Reader;
|
||||
|
||||
import org.checkerframework.checker.nullness.qual.NonNull;
|
||||
|
||||
import net.sourceforge.pmd.Rule;
|
||||
import net.sourceforge.pmd.RuleContext;
|
||||
import net.sourceforge.pmd.RuleViolation;
|
||||
import net.sourceforge.pmd.lang.AbstractParser;
|
||||
import net.sourceforge.pmd.lang.AbstractPmdLanguageVersionHandler;
|
||||
@ -70,22 +71,14 @@ public class DummyLanguageModule extends BaseLanguageModule {
|
||||
|
||||
public static class RuleViolationFactory extends DefaultRuleViolationFactory {
|
||||
@Override
|
||||
protected RuleViolation createRuleViolation(Rule rule, RuleContext ruleContext, Node node, String message) {
|
||||
return createRuleViolation(rule, ruleContext, node, message, 0, 0);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected RuleViolation createRuleViolation(Rule rule, RuleContext ruleContext, Node node, String message,
|
||||
int beginLine, int endLine) {
|
||||
ParametricRuleViolation<Node> rv = new ParametricRuleViolation<Node>(rule, ruleContext, node, message) {
|
||||
public RuleViolation createViolation(Rule rule, @NonNull Node location, @NonNull String filename, @NonNull String formattedMessage) {
|
||||
return new ParametricRuleViolation<Node>(rule, filename, location, formattedMessage) {
|
||||
@Override
|
||||
public String getPackageName() {
|
||||
this.packageName = "foo"; // just for testing variable expansion
|
||||
return super.getPackageName();
|
||||
}
|
||||
};
|
||||
rv.setLines(beginLine, endLine);
|
||||
return rv;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user