|
19 | 19 | import net.sourceforge.pmd.lang.LanguageRegistry; |
20 | 20 | import net.sourceforge.pmd.lang.ast.DummyNode; |
21 | 21 | import net.sourceforge.pmd.lang.ast.DummyNodeWithDeprecatedAttribute; |
| 22 | +import net.sourceforge.pmd.lang.ast.DummyNodeWithListAndEnum; |
22 | 23 | import net.sourceforge.pmd.lang.ast.xpath.Attribute; |
23 | 24 | import net.sourceforge.pmd.lang.rule.xpath.XPathVersion; |
24 | 25 |
|
@@ -56,6 +57,54 @@ public void testAttributeDeprecation20() { |
56 | 57 | testDeprecation(XPathVersion.XPATH_2_0); |
57 | 58 | } |
58 | 59 |
|
| 60 | + @Test |
| 61 | + public void testListAttributeDeprecation20() { |
| 62 | + XPathRule xpr = makeRuleWithList("TestRuleWithListAccess"); |
| 63 | + loggingRule.clear(); |
| 64 | + |
| 65 | + RuleContext ctx = new RuleContext(); |
| 66 | + ctx.setLanguageVersion(LanguageRegistry.getLanguage(DummyLanguageModule.NAME).getDefaultVersion()); |
| 67 | + DummyNode firstNode = newNodeWithList(); |
| 68 | + eval(ctx, xpr, firstNode); |
| 69 | + assertEquals(1, ctx.getReport().size()); |
| 70 | + |
| 71 | + String log = loggingRule.getLog(); |
| 72 | + assertThat(log, Matchers.containsString("Use of deprecated attribute 'dummyNode/@List' by XPath rule 'TestRuleWithListAccess'")); |
| 73 | + |
| 74 | + loggingRule.clear(); |
| 75 | + eval(ctx, xpr, newNodeWithList()); // with another node |
| 76 | + assertEquals(2, ctx.getReport().size()); |
| 77 | + assertEquals("", loggingRule.getLog()); // no addtional warnings |
| 78 | + |
| 79 | + // with another rule forked from the same one (in multithreaded processor) |
| 80 | + eval(ctx, xpr.deepCopy(), newNodeWithList()); |
| 81 | + assertEquals(3, ctx.getReport().size()); |
| 82 | + assertEquals("", loggingRule.getLog()); // no addtional warnings |
| 83 | + |
| 84 | + // with another rule on the same node, new warnings |
| 85 | + XPathRule otherRule = makeRuleWithList("OtherTestRuleWithListAccess"); |
| 86 | + otherRule.setRuleSetName("rset.xml"); |
| 87 | + eval(ctx, otherRule, firstNode); |
| 88 | + assertEquals(4, ctx.getReport().size()); |
| 89 | + log = loggingRule.getLog(); |
| 90 | + assertThat(log, Matchers.containsString("Use of deprecated attribute 'dummyNode/@List' by XPath rule 'OtherTestRuleWithListAccess' (in ruleset 'rset.xml')")); |
| 91 | + } |
| 92 | + |
| 93 | + |
| 94 | + private XPathRule makeRuleWithList(String name) { |
| 95 | + XPathRule xpr = new XPathRule(XPathVersion.XPATH_2_0, "//dummyNode[@List = 'A']"); |
| 96 | + xpr.setName(name); |
| 97 | + xpr.setMessage("list is 'a'"); |
| 98 | + return xpr; |
| 99 | + } |
| 100 | + |
| 101 | + |
| 102 | + private DummyNode newNodeWithList() { |
| 103 | + DummyNode firstNode = new DummyNodeWithListAndEnum(0); |
| 104 | + firstNode.setCoords(1, 1, 1, 2); |
| 105 | + return firstNode; |
| 106 | + } |
| 107 | + |
59 | 108 | public void testDeprecation(XPathVersion version) { |
60 | 109 | XPathRule xpr = makeRule(version, "SomeRule"); |
61 | 110 |
|
|