Skip to content

Commit

Permalink
Update java/PMDTaskTest to use mocked rules
Browse files Browse the repository at this point in the history
  • Loading branch information
oowekyala committed Apr 15, 2022
1 parent be2618b commit 715442b
Show file tree
Hide file tree
Showing 7 changed files with 185 additions and 42 deletions.
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
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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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"/>
Expand All @@ -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"/>
Expand All @@ -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"/>
Expand All @@ -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"/>
Expand All @@ -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"/>
Expand All @@ -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"/>
Expand All @@ -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"/>
Expand All @@ -79,15 +79,15 @@
</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>
</pmd>
</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"/>
Expand All @@ -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"/>
-->
Expand All @@ -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.
Expand All @@ -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"/>
Expand All @@ -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"/>
Expand All @@ -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"/>
Expand Down
40 changes: 40 additions & 0 deletions pmd-java/src/test/resources/rulesets/testing/test-rset-1.xml
Original file line number Diff line number Diff line change
@@ -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>
25 changes: 25 additions & 0 deletions pmd-java/src/test/resources/rulesets/testing/test-rset-2.xml
Original file line number Diff line number Diff line change
@@ -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>
25 changes: 25 additions & 0 deletions pmd-java/src/test/resources/rulesets/testing/test-rset-3.xml
Original file line number Diff line number Diff line change
@@ -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>
Loading

0 comments on commit 715442b

Please sign in to comment.