Skip to content

Commit

Permalink
Merge pull request pmd#3921 from oowekyala:issue3068-mock-rules-for-c…
Browse files Browse the repository at this point in the history
…li-tests

[java] Make cli tests use dummy rules pmd#3921
  • Loading branch information
adangel committed Apr 21, 2022
2 parents ab1cb6d + 455cd96 commit 9b2a92b
Show file tree
Hide file tree
Showing 15 changed files with 334 additions and 90 deletions.
1 change: 1 addition & 0 deletions docs/pages/release_notes.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ This is a {{ site.pmd.release_type }} release.
* doc
* [#2505](https://github.com/pmd/pmd/issues/2505): \[doc] Improve side bar to show release date
* java
* [#3068](https://github.com/pmd/pmd/issues/3068): \[java] Some tests should not depend on real rules
* [#3889](https://github.com/pmd/pmd/pull/3889): \[java] Catch LinkageError in UselessOverridingMethodRule
* [#3910](https://github.com/pmd/pmd/pull/3910): \[java] UnusedPrivateField - Allow the ignored fieldnames to be configurable
* plsql
Expand Down
25 changes: 17 additions & 8 deletions pmd-java/src/test/java/net/sourceforge/pmd/ExcludeLinesTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public class ExcludeLinesTest extends RuleTst {

@Before
public void setUp() {
rule = findRule("java-unusedcode", "UnusedLocalVariable");
rule = findRule("rulesets/testing/test-rset-3.xml", "PrintsVariableNames");
}

@Test
Expand All @@ -42,19 +42,28 @@ public void testAlternateMarker() throws Exception {
ctx.setSourceCodeFile(new File("n/a"));
ctx.setLanguageVersion(LanguageRegistry.getLanguage(JavaLanguageModule.NAME).getDefaultVersion());
RuleSet rules = RuleSet.forSingleRule(rule);

SourceCodeProcessor sourceCodeProcessor = new SourceCodeProcessor(configuration);
sourceCodeProcessor.processSourceCode(new StringReader(TEST3), new RuleSets(rules), ctx);
assertTrue(r.isEmpty());
assertEquals(r.getSuppressedRuleViolations().size(), 1);
}

private static final String TEST1 = "public class Foo {" + PMD.EOL + " void foo() {" + PMD.EOL + " int x; //NOPMD "
+ PMD.EOL + " } " + PMD.EOL + "}";
private static final String TEST1 = "public class Foo {\n"
+ " void foo() {\n"
+ " int x; //NOPMD \n"
+ " } \n"
+ "}";

private static final String TEST2 = "public class Foo {" + PMD.EOL + " void foo() {" + PMD.EOL + " int x;"
+ PMD.EOL + " } " + PMD.EOL + "}";
private static final String TEST2 = "public class Foo {\n"
+ " void foo() {\n"
+ " int x;\n"
+ " } \n"
+ "}";

private static final String TEST3 = "public class Foo {" + PMD.EOL + " void foo() {" + PMD.EOL
+ " int x; // FOOBAR" + PMD.EOL + " } " + PMD.EOL + "}";
private static final String TEST3 = "public class Foo {\n"
+ " void foo() {\n"
+ " int x; // FOOBAR\n"
+ " } \n"
+ "}";
}
35 changes: 16 additions & 19 deletions pmd-java/src/test/java/net/sourceforge/pmd/ant/PMDTaskTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,64 +27,61 @@ public PMDTaskTest() {
@Test
public void testNoFormattersValidation() {
executeTarget("testNoFormattersValidation");
assertOutputContaining("Fields should be declared at the top of the class");
assertOutputContaining("Violation from test-rset-1.xml");
}

@Test
public void testNestedRuleset() {
executeTarget("testNestedRuleset");
assertOutputContaining("Avoid really long methods");
assertOutputContaining("Fields should be declared at the");
assertOutputContaining("Violation from test-rset-1.xml");
assertOutputContaining("Violation from test-rset-2.xml");
}

@Test
public void testFormatterWithProperties() {
executeTarget("testFormatterWithProperties");
assertOutputContaining("Avoid really long methods");
assertOutputContaining("Fields should be declared at the");
assertOutputContaining("Violation from test-rset-1.xml");
assertOutputContaining("link_prefix");
assertOutputContaining("line_prefix");
}

@Test
public void testAbstractNames() {
executeTarget("testAbstractNames");
assertOutputContaining("Avoid really long methods");
assertOutputContaining("Fields should be declared at the");
assertOutputContaining("Violation from test-rset-1.xml");
assertOutputContaining("Violation from test-rset-2.xml");
}

@Test
public void testAbstractNamesInNestedRuleset() {
executeTarget("testAbstractNamesInNestedRuleset");
assertOutputContaining("Avoid really long methods");
assertOutputContaining("Fields should be declared at the");
assertOutputContaining("Violation from test-rset-1.xml");
assertOutputContaining("Violation from test-rset-2.xml");
}

@Test
public void testCommaInRulesetfiles() {
executeTarget("testCommaInRulesetfiles");
assertOutputContaining("Avoid really long methods");
assertOutputContaining("Fields should be declared at the");
assertOutputContaining("Violation from test-rset-1.xml");
assertOutputContaining("Violation from test-rset-2.xml");
}

@Test
public void testRelativeRulesets() {
executeTarget("testRelativeRulesets");
assertOutputContaining("Avoid really long methods");
assertOutputContaining("Fields should be declared at the");
assertOutputContaining("Violation from test-rset-1.xml");
}

@Test
public void testRelativeRulesetsInRulesetfiles() {
executeTarget("testRelativeRulesetsInRulesetfiles");
assertOutputContaining("Avoid really long methods");
assertOutputContaining("Fields should be declared at");
assertOutputContaining("Violation from test-rset-1.xml");
}

@Test
public void testExplicitRuleInRuleSet() {
executeTarget("testExplicitRuleInRuleSet");
assertOutputContaining("Avoid really long methods");
assertOutputContaining("Violation from test-rset-1.xml");
}

@Test
Expand Down Expand Up @@ -166,14 +163,14 @@ public void testFormatterEncodingWithXMLConsole() throws UnsupportedEncodingExce
@Test
public void testMissingCacheLocation() {
executeTarget("testMissingCacheLocation");
assertOutputContaining("Avoid really long methods");
assertOutputContaining("Violation from test-rset-1.xml");
assertContains(buildRule.getLog(), "This analysis could be faster");
}

@Test
public void testAnalysisCache() {
executeTarget("testAnalysisCache");
assertOutputContaining("Avoid really long methods");
assertOutputContaining("Violation from test-rset-1.xml");
assertDoesntContain(buildRule.getLog(), "This analysis could be faster");

assertTrue(currentTempFile().exists());
Expand All @@ -183,7 +180,7 @@ public void testAnalysisCache() {
@Test
public void testDisableIncrementalAnalysis() {
executeTarget("testDisableIncrementalAnalysis");
assertOutputContaining("Avoid really long methods");
assertOutputContaining("Violation from test-rset-1.xml");
assertDoesntContain(buildRule.getLog(), "This analysis could be faster");

assertFalse(currentTempFile().exists());
Expand Down
34 changes: 18 additions & 16 deletions pmd-java/src/test/java/net/sourceforge/pmd/cli/CLITest.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,62 +13,64 @@

/**
* @author Romain Pelisse <belaran@gmail.com>
*
*/
public class CLITest extends BaseCLITest {

private static final String RSET_NO_VIOLATION = "rulesets/testing/rset-without-violations.xml";
private static final String RSET_WITH_VIOLATION = "rulesets/testing/test-rset-1.xml";

@Test
public void minimalArgs() {
runTest("-d", SOURCE_FOLDER, "-f", "text", "-R", "category/java/bestpractices.xml,category/java/design.xml");
runTest("-d", SOURCE_FOLDER, "-f", "text", "-R", RSET_NO_VIOLATION);
}

@Test
public void minimumPriority() {
String[] args = { "-d", SOURCE_FOLDER, "-f", "text", "-R", "category/java/design.xml", "-min", "1", };
String[] args = { "-d", SOURCE_FOLDER, "-f", "text", "-R", RSET_WITH_VIOLATION, "-min", "1", };
runTest(args);
}

@Test
public void usingDebug() {
runTest("-d", SOURCE_FOLDER, "-f", "text", "-R", "category/java/design.xml", "-debug");
runTest("-d", SOURCE_FOLDER, "-f", "text", "-R", RSET_NO_VIOLATION, "-debug");
}

@Test
public void usingDebugLongOption() {
runTest("-d", SOURCE_FOLDER, "-f", "text", "-R", "category/java/design.xml", "--debug");
runTest("-d", SOURCE_FOLDER, "-f", "text", "-R", RSET_NO_VIOLATION, "--debug");
}

@Test
public void changeJavaVersion() {
String[] args = { "-d", SOURCE_FOLDER, "-f", "text", "-R", "category/java/design.xml", "-version", "1.5", "-language",
"java", "--debug", };
String[] args = { "-d", SOURCE_FOLDER, "-f", "text", "-R", RSET_NO_VIOLATION, "-version", "1.5", "-language", "java", "--debug" };
String log = runTest(args);
assertThat(log, containsPattern("Adding file .*\\.java \\(lang: java 1\\.5\\)"));
}

@Test
public void exitStatusNoViolations() {
runTest("-d", SOURCE_FOLDER, "-f", "text", "-R", "category/java/design.xml");
runTest("-d", SOURCE_FOLDER, "-f", "text", "-R", "rulesets/testing/rset-without-violations.xml");
}

@Test
public void exitStatusWithViolations() {
String[] args = { "-d", SOURCE_FOLDER, "-f", "text", "-R", "category/java/errorprone.xml", };
String[] args = { "-d", SOURCE_FOLDER, "-f", "text", "-R", RSET_WITH_VIOLATION, };
String log = runTest(StatusCode.VIOLATIONS_FOUND, args);
assertThat(log, containsString("Avoid empty if"));
assertThat(log, containsString("Violation from test-rset-1.xml"));
}

@Test
public void exitStatusWithViolationsAndWithoutFailOnViolations() {
String[] args = { "-d", SOURCE_FOLDER, "-f", "text", "-R", "category/java/errorprone.xml", "-failOnViolation", "false", };
String[] args = { "-d", SOURCE_FOLDER, "-f", "text", "-R", RSET_WITH_VIOLATION, "-failOnViolation", "false", };
String log = runTest(StatusCode.OK, args);
assertThat(log, containsString("Avoid empty if"));
assertThat(log, containsString("Violation from test-rset-1.xml"));
}

@Test
public void exitStatusWithViolationsAndWithoutFailOnViolationsLongOption() {
String[] args = { "-d", SOURCE_FOLDER, "-f", "text", "-R", "category/java/errorprone.xml", "--fail-on-violation", "false", };
String[] args = { "-d", SOURCE_FOLDER, "-f", "text", "-R", RSET_WITH_VIOLATION, "--fail-on-violation", "false", };
String log = runTest(StatusCode.OK, args);
assertThat(log, containsString("Avoid empty if"));
assertThat(log, containsString("Violation from test-rset-1.xml"));
}

/**
Expand Down Expand Up @@ -98,9 +100,9 @@ public void testWrongRulesetWithRulename() {
*/
@Test
public void testWrongRulename() {
String[] args = { "-d", SOURCE_FOLDER, "-f", "text", "-R", "category/java/design.xml/ThisRuleDoesNotExist", };
String[] args = { "-d", SOURCE_FOLDER, "-f", "text", "-R", RSET_WITH_VIOLATION + "/ThisRuleDoesNotExist", };
String log = runTest(StatusCode.OK, args);
assertThat(log, containsString("No rules found. Maybe you misspelled a rule name?"
+ " (category/java/design.xml/ThisRuleDoesNotExist)"));
+ " (" + RSET_WITH_VIOLATION + "/ThisRuleDoesNotExist)"));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html
*/

package net.sourceforge.pmd.lang.java.rule;

import java.util.List;

import net.sourceforge.pmd.RuleContext;
import net.sourceforge.pmd.lang.ast.Node;
import net.sourceforge.pmd.lang.java.ast.ASTVariableDeclaratorId;
import net.sourceforge.pmd.lang.java.ast.JavaNode;

/**
* @author Clément Fournier
*/
public class DummyJavaRule extends AbstractJavaRule {

@Override
public void apply(List<? extends Node> nodes, RuleContext ctx) {
for (Node node : nodes) {
apply(node, ctx);
}
}

public void apply(Node node, RuleContext ctx) {

}

public static class DummyRuleOneViolationPerFile extends DummyJavaRule {

@Override
public void apply(Node node, RuleContext ctx) {
ctx.addViolation(node);
}
}

public static class DummyRulePrintsVars extends DummyJavaRule {

@Override
public void apply(Node node, RuleContext ctx) {
((JavaNode) node).jjtAccept(this, ctx);
}

@Override
public Object visit(ASTVariableDeclaratorId node, Object data) {
asCtx(data).addViolation(node, node.getName());
return super.visit(node, data);
}
}
}
Loading

0 comments on commit 9b2a92b

Please sign in to comment.