forked from phoedos/pmd
Merge branch 'bug-1506' into pmd/5.4.x
This commit is contained in:
@@ -27,5 +27,12 @@
|
|||||||
<groupId>org.apache.ant</groupId>
|
<groupId>org.apache.ant</groupId>
|
||||||
<artifactId>ant-testutil</artifactId>
|
<artifactId>ant-testutil</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.mockito</groupId>
|
||||||
|
<artifactId>mockito-core</artifactId>
|
||||||
|
<version>1.10.19</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
</project>
|
</project>
|
||||||
|
@@ -0,0 +1,119 @@
|
|||||||
|
/**
|
||||||
|
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html
|
||||||
|
*/
|
||||||
|
package net.sourceforge.pmd.test.lang;
|
||||||
|
|
||||||
|
import java.io.Reader;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.Collections;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
import net.sourceforge.pmd.Rule;
|
||||||
|
import net.sourceforge.pmd.RuleContext;
|
||||||
|
import net.sourceforge.pmd.RuleViolation;
|
||||||
|
import net.sourceforge.pmd.lang.AbstractLanguageVersionHandler;
|
||||||
|
import net.sourceforge.pmd.lang.AbstractParser;
|
||||||
|
import net.sourceforge.pmd.lang.BaseLanguageModule;
|
||||||
|
import net.sourceforge.pmd.lang.Parser;
|
||||||
|
import net.sourceforge.pmd.lang.ParserOptions;
|
||||||
|
import net.sourceforge.pmd.lang.TokenManager;
|
||||||
|
import net.sourceforge.pmd.lang.ast.Node;
|
||||||
|
import net.sourceforge.pmd.lang.ast.ParseException;
|
||||||
|
import net.sourceforge.pmd.lang.rule.AbstractRuleChainVisitor;
|
||||||
|
import net.sourceforge.pmd.lang.rule.AbstractRuleViolationFactory;
|
||||||
|
import net.sourceforge.pmd.lang.rule.ParametricRuleViolation;
|
||||||
|
import net.sourceforge.pmd.test.lang.ast.DummyNode;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Dummy language used for testing PMD.
|
||||||
|
*/
|
||||||
|
public class DummyLanguageModule extends BaseLanguageModule {
|
||||||
|
|
||||||
|
public static final String NAME = "Dummy";
|
||||||
|
public static final String TERSE_NAME = "dummy";
|
||||||
|
|
||||||
|
public DummyLanguageModule() {
|
||||||
|
super(NAME, null, TERSE_NAME, DummyRuleChainVisitor.class, "dummy");
|
||||||
|
addVersion("1.0", new Handler(), false);
|
||||||
|
addVersion("1.1", new Handler(), false);
|
||||||
|
addVersion("1.2", new Handler(), false);
|
||||||
|
addVersion("1.3", new Handler(), false);
|
||||||
|
addVersion("1.4", new Handler(), false);
|
||||||
|
addVersion("1.5", new Handler(), false);
|
||||||
|
addVersion("1.6", new Handler(), false);
|
||||||
|
addVersion("1.7", new Handler(), true);
|
||||||
|
addVersion("1.8", new Handler(), false);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static class DummyRuleChainVisitor extends AbstractRuleChainVisitor {
|
||||||
|
@Override
|
||||||
|
protected void visit(Rule rule, Node node, RuleContext ctx) {
|
||||||
|
rule.apply(Arrays.asList(node), ctx);
|
||||||
|
}
|
||||||
|
@Override
|
||||||
|
protected void indexNodes(List<Node> nodes, RuleContext ctx) {
|
||||||
|
for (Node n : nodes) {
|
||||||
|
indexNode(n);
|
||||||
|
List<Node> childs = new ArrayList<Node>();
|
||||||
|
for (int i = 0; i < n.jjtGetNumChildren(); i++) {
|
||||||
|
childs.add(n.jjtGetChild(i));
|
||||||
|
}
|
||||||
|
indexNodes(childs, ctx);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static class Handler extends AbstractLanguageVersionHandler {
|
||||||
|
@Override
|
||||||
|
public RuleViolationFactory getRuleViolationFactory() {
|
||||||
|
return new RuleViolationFactory();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Parser getParser(ParserOptions parserOptions) {
|
||||||
|
return new AbstractParser(parserOptions) {
|
||||||
|
@Override
|
||||||
|
public Node parse(String fileName, Reader source) throws ParseException {
|
||||||
|
DummyNode node = new DummyNode(1);
|
||||||
|
node.testingOnly__setBeginLine(1);
|
||||||
|
node.testingOnly__setBeginColumn(1);
|
||||||
|
node.setImage("Foo");
|
||||||
|
return node;
|
||||||
|
}
|
||||||
|
@Override
|
||||||
|
public Map<Integer, String> getSuppressMap() {
|
||||||
|
return Collections.emptyMap();
|
||||||
|
}
|
||||||
|
@Override
|
||||||
|
public boolean canParse() {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
@Override
|
||||||
|
protected TokenManager createTokenManager(Reader source) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static class RuleViolationFactory extends AbstractRuleViolationFactory {
|
||||||
|
@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) {
|
||||||
|
{
|
||||||
|
this.packageName = "foo"; // just for testing variable expansion
|
||||||
|
}
|
||||||
|
};
|
||||||
|
rv.setLines(beginLine, endLine);
|
||||||
|
return rv;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@@ -0,0 +1,17 @@
|
|||||||
|
/**
|
||||||
|
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html
|
||||||
|
*/
|
||||||
|
package net.sourceforge.pmd.test.lang.ast;
|
||||||
|
|
||||||
|
import net.sourceforge.pmd.lang.ast.AbstractNode;
|
||||||
|
|
||||||
|
public class DummyNode extends AbstractNode {
|
||||||
|
public DummyNode(int id) {
|
||||||
|
super(id);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "dummyNode";
|
||||||
|
}
|
||||||
|
}
|
@@ -216,7 +216,9 @@ public abstract class RuleTst {
|
|||||||
ctx.setIgnoreExceptions(false);
|
ctx.setIgnoreExceptions(false);
|
||||||
RuleSet rules = new RuleSet();
|
RuleSet rules = new RuleSet();
|
||||||
rules.addRule(rule);
|
rules.addRule(rule);
|
||||||
|
rules.start(ctx);
|
||||||
p.getSourceCodeProcessor().processSourceCode(new StringReader(code), new RuleSets(rules), ctx);
|
p.getSourceCodeProcessor().processSourceCode(new StringReader(code), new RuleSets(rules), ctx);
|
||||||
|
rules.end(ctx);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
throw new RuntimeException(e);
|
throw new RuntimeException(e);
|
||||||
}
|
}
|
||||||
|
@@ -0,0 +1 @@
|
|||||||
|
net.sourceforge.pmd.test.lang.DummyLanguageModule
|
36
pmd-test/src/main/resources/rulesets/dummy/basic.xml
Normal file
36
pmd-test/src/main/resources/rulesets/dummy/basic.xml
Normal file
@@ -0,0 +1,36 @@
|
|||||||
|
<?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 http://pmd.sourceforge.net/ruleset_2_0_0.xsd">
|
||||||
|
|
||||||
|
<description>
|
||||||
|
Ruleset used by test RuleSetReferenceIdTest
|
||||||
|
</description>
|
||||||
|
|
||||||
|
<rule name="DummyBasicMockRule" language="dummy" since="1.0" message="Test Rule 1" class="net.sourceforge.pmd.lang.rule.MockRule"
|
||||||
|
externalInfoUrl="${pmd.website.baseurl}/rules/dummy/basic.xml#DummyBasicMockRule">
|
||||||
|
<description>
|
||||||
|
Just for test
|
||||||
|
</description>
|
||||||
|
<priority>3</priority>
|
||||||
|
<example>
|
||||||
|
<![CDATA[
|
||||||
|
]]>
|
||||||
|
</example>
|
||||||
|
</rule>
|
||||||
|
|
||||||
|
<rule deprecated="true" name="OldNameOfDummyBasicMockRule" ref="DummyBasicMockRule"/>
|
||||||
|
|
||||||
|
<rule name="SampleXPathRule" language="dummy" since="1.1" message="Test Rule 2" class="net.sourceforge.pmd.lang.rule.XPathRule"
|
||||||
|
externalInfoUrl="${pmd.website.baseurl}/rules/dummy/basic.xml#SampleXPathRule">
|
||||||
|
<description>Test</description>
|
||||||
|
<priority>3</priority>
|
||||||
|
<example> </example>
|
||||||
|
<properties>
|
||||||
|
<property name="xpath">
|
||||||
|
<value><![CDATA[
|
||||||
|
//DummyNode
|
||||||
|
]]></value>
|
||||||
|
</property>
|
||||||
|
</properties>
|
||||||
|
</rule>"
|
||||||
|
</ruleset>
|
@@ -0,0 +1,47 @@
|
|||||||
|
/**
|
||||||
|
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html
|
||||||
|
*/
|
||||||
|
package net.sourceforge.pmd.testframework;
|
||||||
|
|
||||||
|
import static org.mockito.Matchers.any;
|
||||||
|
import static org.mockito.Matchers.anyList;
|
||||||
|
import static org.mockito.Mockito.mock;
|
||||||
|
import static org.mockito.Mockito.times;
|
||||||
|
import static org.mockito.Mockito.verify;
|
||||||
|
import static org.mockito.Mockito.verifyNoMoreInteractions;
|
||||||
|
import static org.mockito.Mockito.when;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import net.sourceforge.pmd.Report;
|
||||||
|
import net.sourceforge.pmd.Rule;
|
||||||
|
import net.sourceforge.pmd.RuleContext;
|
||||||
|
import net.sourceforge.pmd.lang.LanguageRegistry;
|
||||||
|
import net.sourceforge.pmd.lang.LanguageVersion;
|
||||||
|
|
||||||
|
public class RuleTstTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void shouldCallStartAndEnd() {
|
||||||
|
RuleTst ruleTester = new RuleTst() {};
|
||||||
|
LanguageVersion languageVersion = LanguageRegistry.findLanguageByTerseName("dummy").getDefaultVersion();
|
||||||
|
Report report = new Report();
|
||||||
|
Rule rule = mock(Rule.class);
|
||||||
|
when(rule.getLanguage()).thenReturn(languageVersion.getLanguage());
|
||||||
|
when(rule.getName()).thenReturn("test rule");
|
||||||
|
|
||||||
|
ruleTester.runTestFromString("the code", rule, report, languageVersion, false);
|
||||||
|
|
||||||
|
verify(rule).start(any(RuleContext.class));
|
||||||
|
verify(rule).end(any(RuleContext.class));
|
||||||
|
verify(rule, times(4)).getLanguage();
|
||||||
|
verify(rule).usesDFA();
|
||||||
|
verify(rule).usesTypeResolution();
|
||||||
|
verify(rule, times(2)).usesRuleChain();
|
||||||
|
verify(rule).getMinimumLanguageVersion();
|
||||||
|
verify(rule).getMaximumLanguageVersion();
|
||||||
|
verify(rule).apply(anyList(), any(RuleContext.class));
|
||||||
|
verify(rule).getName();
|
||||||
|
verifyNoMoreInteractions(rule);
|
||||||
|
}
|
||||||
|
}
|
@@ -19,6 +19,7 @@
|
|||||||
* [#1501](https://sourceforge.net/p/pmd/bugs/1501/): \[java] \[apex] CyclomaticComplexity rule causes OOM when class reporting is disabled
|
* [#1501](https://sourceforge.net/p/pmd/bugs/1501/): \[java] \[apex] CyclomaticComplexity rule causes OOM when class reporting is disabled
|
||||||
* General
|
* General
|
||||||
* [#1499](https://sourceforge.net/p/pmd/bugs/1499/): \[core] CPD test break PMD 5.5.1 build on Windows
|
* [#1499](https://sourceforge.net/p/pmd/bugs/1499/): \[core] CPD test break PMD 5.5.1 build on Windows
|
||||||
|
* [#1506](https://sourceforge.net/p/pmd/bugs/1506/): \[core] When runing any RuleTst, start/end methods not called
|
||||||
* [#1508](https://sourceforge.net/p/pmd/bugs/1508/): \[core] \[java] PMD is leaking file handles
|
* [#1508](https://sourceforge.net/p/pmd/bugs/1508/): \[core] \[java] PMD is leaking file handles
|
||||||
|
|
||||||
**API Changes:**
|
**API Changes:**
|
||||||
|
Reference in New Issue
Block a user