Skip to content

Commit 907c072

Browse files
committed
Merge pull request pmd#4058 from oowekyala:pmd6-focused-test-attr
[test-schema] Add focused attr to the test schema pmd#4058
2 parents 4308aaf + 2f3f0d6 commit 907c072

File tree

18 files changed

+209
-100
lines changed

18 files changed

+209
-100
lines changed

docs/_plugins/jdoc_namespace_tag.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ def self.parse_fqcn(fqcn, var_ctx, allow_sym = true)
102102
RESERVED_NSPACES = ['apex', 'core', 'cpp', 'cs', 'dart', 'dist', 'doc', 'fortran', 'go', 'groovy', 'java',
103103
'javascript', 'jsp',
104104
'kotlin', 'lua', 'matlab', 'objectivec', 'perl', 'php', 'plsql', 'python', 'ruby', 'scala', 'swift',
105-
'test', 'ui',
105+
'test', 'test-schema', 'ui',
106106
'modelica', 'visualforce', 'vm', 'xml'].flat_map {|m| [m, "pmd-" + m]}
107107

108108
def self.make_base_namespaces

docs/pages/pmd/userdocs/extending/testing.md

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -135,14 +135,18 @@ between different test cases.
135135

136136
The `<test-code>` elements understands the following optional attributes:
137137

138-
* **reinitializeRule**: By default, it's `true`, so each test case starts with a fresh instantiated rule. Set it
139-
to `false` to reproduce cases, where the previous run has influences.
138+
* **disabled**: By default, it's `false`. Set it to `true`, to ignore and skip a test case.
140139

141-
* **disabled**: By default, it's `false`. Set ti to `true`, to ignore and skip a test case.
140+
* **focused**: By default, it's `false`. Set it to `true`, to ignore all other test cases.
142141

143-
* **useAuxClasspath**: By default, it's `true`. Set it to `false` to reproduce issues which only
142+
* **useAuxClasspath**: _deprecated since PMD 6.48.0: assumed true, has no effect anymore._
143+
By default, it's `true`. Set it to `false` to reproduce issues which only
144144
appear without type resolution.
145145

146+
* **reinitializeRule**: _deprecated since PMD 6.48.0: assumed true, has no effect anymore._
147+
By default, it's `true`, so each test case starts with a fresh instantiated rule. Set it
148+
to `false` to reproduce cases, where the previous run has influences.
149+
146150
* **regressionTest**: _deprecated since PMD 6.48.0: Use `disabled` instead. Note: It has the opposite boolean
147151
semantic._ By default, it's `true`. Set it to `false`, to ignore and skip a test case.
148152

docs/pages/release_notes.md

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ Being based on a proper Antlr grammar, CPD can:
5454
* java-performance
5555
* [#3625](https://github.com/pmd/pmd/issues/3625): \[java] AddEmptyString - false negative with empty var
5656
* test
57+
* [#3302](https://github.com/pmd/pmd/pull/3302): \[test] Improve xml test schema
5758
* [#3758](https://github.com/pmd/pmd/issues/3758): \[test] Move pmd-test to java 8
5859
* [#3976](https://github.com/pmd/pmd/pull/3976): \[test] Extract xml schema module
5960

@@ -65,8 +66,12 @@ Being based on a proper Antlr grammar, CPD can:
6566
this module for testing your own custom rules, you'll need to make sure to use at least Java 8.
6667
* The new module "pmd-test-schema" contains now the XSD schema and the code to parse the rule test XML files. The
6768
schema has been extracted in order to easily share it with other tools like the Rule Designer or IDE plugins.
68-
* The attribute `isRegressionTest` is deprecated and the new attribute `disabled` should be used instead for
69-
defining whether a rule test should be skipped or not.
69+
* Test schema changes:
70+
* The attribute `isRegressionTest` of `test-code` is deprecated. The new
71+
attribute `disabled` should be used instead for defining whether a rule test should be skipped or not.
72+
* The attributes `reinitializeRule` and `useAuxClasspath` of `test-code` are deprecated and assumed true.
73+
They will not be replaced.
74+
* The new attribute `focused` of `test-code` allows disabling all tests except the focused one temporarily.
7075
* More information about the rule test framework can be found in the documentation:
7176
[Testing your rules](pmd_userdocs_extending_testing.html)
7277

@@ -77,6 +82,8 @@ Being based on a proper Antlr grammar, CPD can:
7782
but it is no longer supported with Java 19 Preview.
7883
* The interface {% jdoc core::cpd.renderer.CPDRenderer %} is deprecated. For custom CPD renderers
7984
the new interface {% jdoc core::cpd.renderer.CPDReportRenderer %} should be used.
85+
* The class {% jdoc test::testframework.TestDescriptor %} is deprecated, replaced with {% jdoc test-schema::testframework.RuleTestDescriptor %}.
86+
* Many methods of {% jdoc test::testframework.RuleTst %} have been deprecated as internal API.
8087

8188
#### Experimental APIs
8289

pmd-java/src/test/java/net/sourceforge/pmd/jaxen/RegexpAcceptanceTest.java

Lines changed: 45 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,33 +4,64 @@
44

55
package net.sourceforge.pmd.jaxen;
66

7+
import static org.junit.Assert.assertEquals;
8+
9+
import java.util.List;
10+
11+
import org.junit.Before;
712
import org.junit.Test;
813

14+
import net.sourceforge.pmd.PMDConfiguration;
15+
import net.sourceforge.pmd.PmdAnalysis;
16+
import net.sourceforge.pmd.Report;
917
import net.sourceforge.pmd.Rule;
18+
import net.sourceforge.pmd.RuleSet;
19+
import net.sourceforge.pmd.RuleViolation;
1020
import net.sourceforge.pmd.lang.LanguageRegistry;
1121
import net.sourceforge.pmd.lang.java.JavaLanguageModule;
1222
import net.sourceforge.pmd.lang.rule.XPathRule;
13-
import net.sourceforge.pmd.testframework.SimpleAggregatorTst;
14-
import net.sourceforge.pmd.testframework.TestDescriptor;
23+
import net.sourceforge.pmd.lang.rule.xpath.XPathVersion;
1524

16-
public class RegexpAcceptanceTest extends SimpleAggregatorTst {
25+
public class RegexpAcceptanceTest {
1726

1827
private static final String XPATH = "//ClassOrInterfaceDeclaration[matches(@Image, 'F?o')]";
1928

20-
@Override
21-
protected void setUp() {
22-
// not registering any rule
29+
private Rule rule;
30+
31+
@Before
32+
public void setUp() {
33+
rule = new XPathRule(XPathVersion.XPATH_1_0, XPATH);
34+
rule.setLanguage(LanguageRegistry.getLanguage(JavaLanguageModule.NAME));
35+
rule.setMessage("F?o matched");
36+
}
37+
38+
@Test
39+
public void shouldMatchFoo() {
40+
List<RuleViolation> violations = eval("public class Foo {}");
41+
assertEquals(1, violations.size());
42+
}
43+
44+
@Test
45+
public void shouldNotMatchBar() {
46+
List<RuleViolation> violations = eval("public class Bar {}");
47+
assertEquals(0, violations.size());
2348
}
2449

2550
@Test
26-
public void testSimple() {
27-
Rule r = new XPathRule(XPATH);
28-
r.setLanguage(LanguageRegistry.getLanguage(JavaLanguageModule.NAME));
29-
r.setMessage("");
30-
TestDescriptor[] testDescriptors = extractTestsFromXml(r, "RegexpAcceptance");
31-
for (TestDescriptor testDescriptor : testDescriptors) {
32-
testDescriptor.setReinitializeRule(false);
51+
public void shouldMatchFlo() {
52+
List<RuleViolation> violations = eval("public class Flo {}");
53+
assertEquals(1, violations.size());
54+
}
55+
56+
private List<RuleViolation> eval(String code) {
57+
PMDConfiguration config = new PMDConfiguration();
58+
config.setIgnoreIncrementalAnalysis(true);
59+
try (PmdAnalysis pmd = PmdAnalysis.create(config)) {
60+
pmd.addRuleSet(RuleSet.forSingleRule(rule));
61+
pmd.files().addSourceFile(code, "RegexpAcceptanceTest.java");
62+
63+
Report report = pmd.performAnalysisAndCollectReport();
64+
return report.getViolations();
3365
}
34-
runTests(testDescriptors);
3566
}
3667
}

pmd-java/src/test/resources/net/sourceforge/pmd/jaxen/xml/RegexpAcceptance.xml

Lines changed: 0 additions & 33 deletions
This file was deleted.

pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/codestyle/xml/MethodNamingConventions.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,8 @@ public class TournamentTest extends TestCase {
178178
]]></code>
179179
</test-code>
180180

181-
<test-code useAuxClasspath="false">
181+
<!-- it's not possible anymore to test without auxclasspath. junit4 is always on the test auxclasspath -->
182+
<test-code disabled="true">
182183
<description>JUnit 4 test detection without proper auxclasspath</description>
183184
<expected-problems>1</expected-problems>
184185
<expected-linenumbers>8</expected-linenumbers>

pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/design/xml/CyclomaticComplexity.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ public class Test {
149149
<code-ref id="constructor-violation"/>
150150
</test-code>
151151

152-
<test-code reinitializeRule="true">
152+
<test-code>
153153
<description>#985 Suppressed methods shouldn't affect avg CyclomaticComplexity</description>
154154
<rule-property name="methodReportLevel">2</rule-property>
155155
<expected-problems>0</expected-problems>

pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/design/xml/StdCyclomaticComplexity.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ public class Test {
164164
<code-ref id="constructor-violation"/>
165165
</test-code>
166166

167-
<test-code reinitializeRule="true">
167+
<test-code>
168168
<description>#985 Suppressed methods shouldn't affect avg CyclomaticComplexity</description>
169169
<rule-property name="reportLevel">2</rule-property>
170170
<expected-problems>0</expected-problems>

pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/errorprone/xml/CloseResource.xml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,7 @@ public class Foo {
249249
]]></code>
250250
</test-code>
251251

252-
<test-code reinitializeRule="true">
252+
<test-code>
253253
<description>invoke an external method that close the resource: bug 2920057</description>
254254
<rule-property name="closeTargets">closeStatement,closeStatement,closeResultSet,closeConnexion</rule-property>
255255
<rule-property name="types">java.sql.Connection,java.sql.Statement,java.sql.ResultSet,java.sql.PreparedStatement</rule-property>
@@ -280,7 +280,7 @@ public class StructureFactory {
280280
]]></code>
281281
</test-code>
282282

283-
<test-code reinitializeRule="true">
283+
<test-code>
284284
<description>invoke an external method that closes the resource, but one is not the right method and an another is not the right variable: see bug 2920057</description>
285285
<rule-property name="closeTargets">closeStatement,closeStatement,closeResultSet,closeConnexion</rule-property>
286286
<rule-property name="types">java.sql.Connection,java.sql.Statement,java.sql.ResultSet,java.sql.PreparedStatement</rule-property>
@@ -312,7 +312,7 @@ public class StructureFactory {
312312
]]></code>
313313
</test-code>
314314

315-
<test-code reinitializeRule="true">
315+
<test-code>
316316
<description>#1011 CloseResource Rule ignores Constructors</description>
317317
<expected-problems>1</expected-problems>
318318
<code><![CDATA[
@@ -326,7 +326,7 @@ public class Test {
326326
]]></code>
327327
</test-code>
328328

329-
<test-code reinitializeRule="true">
329+
<test-code>
330330
<description>#1011 CloseResource Rule ignores Constructors - closed in finally</description>
331331
<expected-problems>0</expected-problems>
332332
<code><![CDATA[
@@ -345,7 +345,7 @@ public class Test {
345345
]]></code>
346346
</test-code>
347347

348-
<test-code reinitializeRule="true">
348+
<test-code>
349349
<description>#1011 CloseResource Rule ignores Constructors - not a problem - instance variable</description>
350350
<expected-problems>0</expected-problems>
351351
<code><![CDATA[
@@ -360,7 +360,7 @@ public class Test {
360360
]]></code>
361361
</test-code>
362362

363-
<test-code reinitializeRule="true">
363+
<test-code>
364364
<description>#1029 No instance level check in the close resource rule</description>
365365
<expected-problems>0</expected-problems>
366366
<code><![CDATA[

pmd-test-schema/src/main/java/net/sourceforge/pmd/test/schema/BaseTestParserImpl.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,13 @@ private void parseSingleTest(Element testCode,
100100

101101
descriptor.setDisabled(disabled);
102102

103+
104+
boolean focused = parseBoolAttribute(testCode, "focused", false, err,
105+
"Attribute focused is used, do not forget to remove it when checking in sources");
106+
107+
descriptor.setFocused(focused);
108+
109+
103110
Properties properties = parseRuleProperties(testCode, descriptor.getRule(), err);
104111
descriptor.getProperties().putAll(properties);
105112

0 commit comments

Comments
 (0)