Update java/PMDTaskTest to use mocked rules

This commit is contained in:
Clément Fournier
2022-04-15 19:31:56 +02:00
parent be2618b66b
commit 715442b66b
7 changed files with 185 additions and 42 deletions

View File

@ -27,21 +27,20 @@ public class PMDTaskTest extends AbstractAntTestHelper {
@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");
}
@ -49,42 +48,40 @@ public class PMDTaskTest extends AbstractAntTestHelper {
@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
@ -166,14 +163,14 @@ public class PMDTaskTest extends AbstractAntTestHelper {
@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());
@ -183,7 +180,7 @@ public class PMDTaskTest extends AbstractAntTestHelper {
@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());

View File

@ -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);
}
}
}

View File

@ -6,8 +6,8 @@
<target name="testNestedRuleset">
<pmd>
<ruleset>${pmd.home}/src/main/resources/category/java/codestyle.xml</ruleset>
<ruleset>${pmd.home}/src/main/resources/category/java/design.xml</ruleset>
<ruleset>${pmd.home}/src/test/resources/rulesets/testing/test-rset-1.xml</ruleset>
<ruleset>${pmd.home}/src/test/resources/rulesets/testing/test-rset-2.xml</ruleset>
<formatter type="text" toConsole="true"/>
<fileset dir="${pmd.home}/src/test/resources/ant/java">
<include name="*.java"/>
@ -17,8 +17,8 @@
<target name="testFormatterWithProperties">
<pmd>
<ruleset>${pmd.home}/src/main/resources/category/java/codestyle.xml</ruleset>
<ruleset>${pmd.home}/src/main/resources/category/java/design.xml</ruleset>
<ruleset>${pmd.home}/src/test/resources/rulesets/testing/test-rset-1.xml</ruleset>
<ruleset>${pmd.home}/src/test/resources/rulesets/testing/test-rset-2.xml</ruleset>
<formatter type="summaryhtml" toConsole="true">
<param name="linkPrefix" value="link_prefix"/>
<param name="linePrefix" value="line_prefix"/>
@ -30,7 +30,7 @@
</target>
<target name="testAbstractNames">
<pmd rulesetfiles="category/java/codestyle.xml,category/java/design.xml">
<pmd rulesetfiles="rulesets/testing/test-rset-1.xml,rulesets/testing/test-rset-2.xml">
<formatter type="text" toConsole="true"/>
<fileset dir="${pmd.home}/src/test/resources/ant/java">
<include name="*.java"/>
@ -40,8 +40,8 @@
<target name="testAbstractNamesInNestedRuleset">
<pmd>
<ruleset>category/java/codestyle.xml</ruleset>
<ruleset>category/java/design.xml</ruleset>
<ruleset>rulesets/testing/test-rset-1.xml</ruleset>
<ruleset>rulesets/testing/test-rset-2.xml</ruleset>
<formatter type="text" toConsole="true"/>
<fileset dir="${pmd.home}/src/test/resources/ant/java">
<include name="*.java"/>
@ -50,7 +50,7 @@
</target>
<target name="testCommaInRulesetfiles">
<pmd rulesetfiles="${pmd.home}/src/main/resources/category/java/codestyle.xml,${pmd.home}/src/main/resources/category/java/design.xml">
<pmd rulesetfiles="${pmd.home}/src/test/resources/rulesets/testing/test-rset-1.xml,${pmd.home}/src/test/resources/rulesets/testing/test-rset-2.xml">
<formatter type="text" toConsole="true"/>
<fileset dir="${pmd.home}/src/test/resources/ant/java">
<include name="*.java"/>
@ -61,7 +61,7 @@
<target name="testRelativeRulesets">
<pmd>
<ruleset>custom_ruleset.xml</ruleset>
<ruleset>category/java/codestyle.xml</ruleset>
<ruleset>rulesets/testing/test-rset-1.xml</ruleset>
<formatter type="text" toConsole="true"/>
<fileset dir="${pmd.home}/src/test/resources/ant/java">
<include name="*.java"/>
@ -70,7 +70,7 @@
</target>
<target name="testRelativeRulesetsInRulesetfiles">
<pmd rulesetfiles="custom_ruleset.xml,src/main/resources/category/java/codestyle.xml">
<pmd rulesetfiles="custom_ruleset.xml,src/test/resources/rulesets/testing/test-rset-1.xml">
<formatter type="text" toConsole="true"/>
<fileset dir="${pmd.home}/src/test/resources/ant/java">
<include name="*.java"/>
@ -79,7 +79,7 @@
</target>
<target name="testNoFormattersValidation">
<pmd rulesetfiles="${pmd.home}/src/main/resources/category/java/codestyle.xml">
<pmd rulesetfiles="${pmd.home}/src/test/resources/rulesets/testing/test-rset-1.xml">
<fileset dir="${pmd.home}/src/test/resources/ant/java">
<include name="*.java"/>
</fileset>
@ -87,7 +87,7 @@
</target>
<target name="testExplicitRuleInRuleSet">
<pmd rulesetfiles="src/main/resources/category/java/design.xml/ExcessiveMethodLength">
<pmd rulesetfiles="src/test/resources/rulesets/testing/test-rset-1.xml/DummyRuleWithAViolationPerFile">
<formatter type="text" toConsole="true"/>
<fileset dir="${pmd.home}/src/test/resources/ant/java">
<include name="*.java"/>
@ -111,7 +111,7 @@
<target name="testFormatterEncodingWithXML">
<!-- source encoding is cp1252 -->
<pmd encoding="cp1252">
<ruleset>category/java/bestpractices.xml</ruleset>
<ruleset>rulesets/testing/test-rset-3.xml</ruleset>
<!--
<formatter type="xml" toConsole="true"/>
-->
@ -128,7 +128,7 @@
<target name="testFormatterEncodingWithXMLConsole">
<!-- source encoding is cp1252 -->
<pmd encoding="cp1252">
<ruleset>category/java/bestpractices.xml</ruleset>
<ruleset>rulesets/testing/test-rset-3.xml</ruleset>
<formatter type="xml" toConsole="true">
<!-- specifying a encoding here will override the console encoding.
The output might have junk characters in it.
@ -145,7 +145,7 @@
</target>
<target name="testAnalysisCache">
<pmd rulesetfiles="src/main/resources/category/java/design.xml/ExcessiveMethodLength" cacheLocation="${tmpfile}">
<pmd rulesetfiles="src/test/resources/rulesets/testing/test-rset-1.xml/DummyRuleWithAViolationPerFile" cacheLocation="${tmpfile}">
<formatter type="xml" toConsole="true"/>
<fileset dir="${pmd.home}/src/test/resources/ant/java">
<include name="*.java"/>
@ -154,7 +154,7 @@
</target>
<target name="testMissingCacheLocation">
<pmd rulesetfiles="src/main/resources/category/java/design.xml/ExcessiveMethodLength">
<pmd rulesetfiles="src/test/resources/rulesets/testing/test-rset-1.xml/DummyRuleWithAViolationPerFile">
<formatter type="xml" toConsole="true"/>
<fileset dir="${pmd.home}/src/test/resources/ant/java">
<include name="*.java"/>
@ -163,7 +163,7 @@
</target>
<target name="testDisableIncrementalAnalysis">
<pmd rulesetfiles="src/main/resources/category/java/design.xml/ExcessiveMethodLength" noCache="true" cacheLocation="${tmpfile}">
<pmd rulesetfiles="src/test/resources/rulesets/testing/test-rset-1.xml/DummyRuleWithAViolationPerFile" noCache="true" cacheLocation="${tmpfile}">
<formatter type="xml" toConsole="true"/>
<fileset dir="${pmd.home}/src/test/resources/ant/java">
<include name="*.java"/>

View File

@ -0,0 +1,40 @@
<?xml version="1.0"?>
<ruleset name="Test Ruleset" xmlns="http://pmd.sourceforge.net/ruleset/2.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://pmd.sourceforge.net/ruleset/2.0.0 https://pmd.sourceforge.io/ruleset_2_0_0.xsd">
<description>
Ruleset used by test RuleSetReferenceIdTest
</description>
<rule name="DummyRuleNoViolation"
language="java"
since="1.0"
message="Test Rule 1"
class="net.sourceforge.pmd.lang.java.rule.DummyJavaRule">
<description>
Just for test
</description>
<priority>3</priority>
<example>
<![CDATA[
]]>
</example>
</rule>
<rule name="DummyRuleWithAViolationPerFile"
language="java"
since="1.0"
message="Violation from test-rset-1.xml"
class="net.sourceforge.pmd.lang.java.rule.DummyJavaRule$DummyRuleOneViolationPerFile">
<description>
Just for test
</description>
<priority>3</priority>
<example>
<![CDATA[
]]>
</example>
</rule>
</ruleset>

View File

@ -0,0 +1,25 @@
<?xml version="1.0"?>
<ruleset name="Test Ruleset" xmlns="http://pmd.sourceforge.net/ruleset/2.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://pmd.sourceforge.net/ruleset/2.0.0 https://pmd.sourceforge.io/ruleset_2_0_0.xsd">
<description>
Ruleset used by test RuleSetReferenceIdTest
</description>
<rule name="DummyRuleWithAViolationPerFile"
language="java"
since="1.0"
message="Violation from test-rset-2.xml"
class="net.sourceforge.pmd.lang.java.rule.DummyJavaRule$DummyRuleOneViolationPerFile">
<description>
Just for test
</description>
<priority>3</priority>
<example>
<![CDATA[
]]>
</example>
</rule>
</ruleset>

View File

@ -0,0 +1,25 @@
<?xml version="1.0"?>
<ruleset name="Test Ruleset" xmlns="http://pmd.sourceforge.net/ruleset/2.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://pmd.sourceforge.net/ruleset/2.0.0 https://pmd.sourceforge.io/ruleset_2_0_0.xsd">
<description>
Ruleset used by test RuleSetReferenceIdTest
</description>
<rule name="DummyRuleWithAViolationPerFile"
language="java"
since="1.0"
message="Violation from test-rset-3.xml: name {0}"
class="net.sourceforge.pmd.lang.java.rule.DummyJavaRule$DummyRulePrintsVars">
<description>
Just for test
</description>
<priority>3</priority>
<example>
<![CDATA[
]]>
</example>
</rule>
</ruleset>

View File

@ -5,15 +5,18 @@
package net.sourceforge.pmd.ant;
import static java.io.File.separator;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.not;
import java.io.File;
import java.io.IOException;
import org.apache.tools.ant.BuildFileRule;
import org.apache.tools.ant.Project;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Rule;
import org.junit.contrib.java.lang.system.SystemErrRule;
import org.junit.rules.TemporaryFolder;
@ -33,6 +36,9 @@ public abstract class AbstractAntTestHelper {
@Rule
public final BuildFileRule buildRule = new BuildFileRule();
@Rule
public final SystemErrRule systemErrRule = new SystemErrRule().muteForSuccessfulTests();
protected String pathToTestScript;
protected String antTestScriptFilename;
public String mvnWorkaround;
@ -85,21 +91,20 @@ public abstract class AbstractAntTestHelper {
public void executeTarget(String target) {
buildRule.executeTarget(target);
System.err.println(buildRule.getLog());
}
public void assertOutputContaining(String text) {
assertContains(buildRule.getOutput(), text);
assertThat(buildRule.getOutput(), containsString(text));
}
public void assertContains(String text, String toFind) {
Assert.assertTrue("Expected to find \"" + toFind + "\", but it's missing",
text.contains(toFind));
assertThat(text, containsString(toFind));
}
public void assertDoesntContain(String text, String toFind) {
Assert.assertTrue("Expected no occurrence of \"" + toFind + "\", but found at least one",
!text.contains(toFind));
assertThat(text, not(containsString(toFind)));
}
}