• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

pmd / pmd / 353

16 Jan 2026 01:01PM UTC coverage: 78.957% (-0.02%) from 78.976%
353

push

github

adangel
[core] Fix #6184: More consistent enum properties (#6233)

18529 of 24342 branches covered (76.12%)

Branch coverage included in aggregate %.

146 of 164 new or added lines in 20 files covered. (89.02%)

6 existing lines in 5 files now uncovered.

40349 of 50228 relevant lines covered (80.33%)

0.81 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

45.05
/pmd-test/src/main/java/net/sourceforge/pmd/test/RuleTst.java
1
/*
2
 * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
3
 */
4

5
package net.sourceforge.pmd.test;
6

7
import static org.junit.jupiter.api.Assertions.assertEquals;
8
import static org.junit.jupiter.api.Assertions.fail;
9

10
import java.io.File;
11
import java.io.IOException;
12
import java.io.InputStream;
13
import java.io.StringWriter;
14
import java.net.URI;
15
import java.net.URISyntaxException;
16
import java.net.URL;
17
import java.nio.file.Path;
18
import java.nio.file.Paths;
19
import java.security.CodeSource;
20
import java.util.ArrayList;
21
import java.util.Collection;
22
import java.util.Collections;
23
import java.util.Comparator;
24
import java.util.List;
25
import java.util.Map;
26

27
import org.apache.commons.lang3.StringUtils;
28
import org.junit.jupiter.api.DynamicTest;
29
import org.junit.jupiter.api.TestFactory;
30
import org.xml.sax.InputSource;
31

32
import net.sourceforge.pmd.PMDConfiguration;
33
import net.sourceforge.pmd.PmdAnalysis;
34
import net.sourceforge.pmd.internal.util.ClasspathClassLoader;
35
import net.sourceforge.pmd.lang.LanguageVersion;
36
import net.sourceforge.pmd.lang.document.FileId;
37
import net.sourceforge.pmd.lang.document.TextFile;
38
import net.sourceforge.pmd.lang.rule.Rule;
39
import net.sourceforge.pmd.lang.rule.RuleSet;
40
import net.sourceforge.pmd.lang.rule.RuleSetLoadException;
41
import net.sourceforge.pmd.lang.rule.RuleSetLoader;
42
import net.sourceforge.pmd.properties.PropertyDescriptor;
43
import net.sourceforge.pmd.renderers.TextRenderer;
44
import net.sourceforge.pmd.reporting.GlobalAnalysisListener;
45
import net.sourceforge.pmd.reporting.Report;
46
import net.sourceforge.pmd.reporting.RuleViolation;
47
import net.sourceforge.pmd.test.schema.RuleTestCollection;
48
import net.sourceforge.pmd.test.schema.RuleTestDescriptor;
49
import net.sourceforge.pmd.test.schema.TestSchemaParser;
50

51
/**
52
 * Advanced methods for test cases
53
 */
54
public abstract class RuleTst {
1✔
55

56
    protected void setUp() {
57
        // This method is intended to be overridden by subclasses.
58
    }
×
59

60
    /**
61
     * Return the rules that will be tested. Each rule must have a corresponding XML file containing a test collection.
62
     * Test collections for all these rules are run separately.
63
     */
64
    protected List<Rule> getRules() {
65
        return Collections.emptyList();
×
66
    }
67

68
    /** Return extra rules that will be run while running the tests. */
69
    protected Collection<? extends Rule> getExtraRules() {
70
        return Collections.emptyList();
1✔
71
    }
72

73
    /**
74
     * Find a rule in a certain ruleset by name.
75
     */
76
    public static Rule findRule(String ruleSet, String ruleName) {
77
        try {
78
            RuleSet parsedRset = new RuleSetLoader().warnDeprecated(false).loadFromResource(ruleSet);
×
79
            Rule rule = parsedRset.getRuleByName(ruleName);
×
80
            if (rule == null) {
×
81
                fail("Rule " + ruleName + " not found in ruleset " + ruleSet);
×
82
            } else {
83
                rule.setRuleSetName(ruleSet);
×
84
            }
85
            return rule;
×
86
        } catch (RuleSetLoadException e) {
×
87
            e.printStackTrace();
×
88
            fail("Couldn't find ruleset " + ruleSet);
×
89
            return null;
×
90
        }
91
    }
92

93
    /**
94
     * Run the rule on the given code, and check the expected number of violations.
95
     */
96
    void runTest(RuleTestDescriptor test) {
97
        Rule rule = test.getRule();
1✔
98

99
        // always reinitialize the rule, regardless of test.getReinitializeRule() (#3976 / #3302)
100
        rule = reinitializeRule(rule);
1✔
101

102
        Map<PropertyDescriptor<?>, Object> oldProperties = rule.getPropertiesByPropertyDescriptor();
1✔
103
        Report report = null;
1✔
104
        try {
105
            int res;
106
            try {
107
                // Set test specific properties onto the Rule
108
                if (test.getProperties() != null) {
1!
109
                    for (Map.Entry<Object, Object> entry : test.getProperties().entrySet()) {
1!
110
                        String propertyName = (String) entry.getKey();
×
111
                        PropertyDescriptor propertyDescriptor = rule.getPropertyDescriptor(propertyName);
×
112
                        if (propertyDescriptor == null) {
×
113
                            throw new IllegalArgumentException(
×
114
                                    "No such property '" + propertyName + "' on Rule " + rule.getName());
×
115
                        }
116

117
                        Object value = propertyDescriptor.serializer().fromString((String) entry.getValue());
×
NEW
118
                        if (propertyDescriptor.serializer().isFromStringDeprecated((String) entry.getValue())) {
×
NEW
119
                            System.err.println(rule.getName() + ":" + test.getDescription() + ": Deprecated property value used! " + entry);
×
120
                        }
121
                        rule.setProperty(propertyDescriptor, value);
×
122
                    }
×
123
                }
124

125
                String dysfunctionReason = rule.dysfunctionReason();
1✔
126
                if (StringUtils.isNotBlank(dysfunctionReason)) {
1!
127
                    throw new RuntimeException("Rule is not configured correctly: " + dysfunctionReason);
×
128
                }
129

130
                report = processUsingStringReader(test, rule);
1✔
131
                res = report.getViolations().size();
1✔
132
            } catch (Exception e) {
×
133
                e.printStackTrace();
×
134
                throw new RuntimeException('"' + test.getDescription() + "\" failed", e);
×
135
            }
1✔
136
            assertEquals(test.getExpectedProblems(), res,
1✔
137
                    '"' + test.getDescription() + "\" resulted in wrong number of failures,");
1✔
138
            assertMessages(report, test);
1✔
139
            assertLineNumbers(report, test);
1✔
140
            assertSuppressions(report, test);
1✔
141
        } catch (AssertionError e) {
1✔
142
            printReport(test, report);
1✔
143
            throw e;
1✔
144
        } finally {
145
            // Restore old properties
146
            for (Map.Entry<PropertyDescriptor<?>, Object> entry : oldProperties.entrySet()) {
1✔
147
                rule.setProperty((PropertyDescriptor) entry.getKey(), entry.getValue());
1✔
148
            }
1✔
149
        }
150
    }
1✔
151

152

153
    /**
154
     * Code to be executed if the rule is reinitialised.
155
     *
156
     * @param rule The rule to reinitialise
157
     *
158
     * @return The rule once it has been reinitialised
159
     */
160
    private Rule reinitializeRule(Rule rule) {
161
        return rule.deepCopy();
1✔
162
    }
163

164
    private void assertSuppressions(Report report, RuleTestDescriptor test) {
165
        if (!test.hasExpectedSuppressions()) {
1✔
166
            return;
1✔
167
        }
168
        List<RuleTestDescriptor.SuppressionDescriptor> expectedSuppressions = test.getExpectedSuppressions();
1✔
169
        assertEquals(expectedSuppressions.size(), report.getSuppressedViolations().size(), "wrong number of suppressed violations");
1✔
170
        for (int i = 0; i < expectedSuppressions.size(); i++) {
1✔
171
            RuleTestDescriptor.SuppressionDescriptor expectedSuppression = expectedSuppressions.get(i);
1✔
172
            Report.SuppressedViolation actualSuppression = report.getSuppressedViolations().get(i);
1✔
173
            assertEquals(expectedSuppression.getLine(), actualSuppression.getRuleViolation().getBeginLine(), "wrong line for suppression");
1✔
174
            if (StringUtils.isNotBlank(expectedSuppression.getSuppressorId())) {
1✔
175
                assertEquals(expectedSuppression.getSuppressorId(), actualSuppression.getSuppressor().getId(), "wrong suppressor id");
1✔
176
            }
177
        }
178
    }
1✔
179

180
    private void assertMessages(Report report, RuleTestDescriptor test) {
181
        if (report == null || test.getExpectedMessages().isEmpty()) {
1!
182
            return;
1✔
183
        }
184

185
        List<String> expectedMessages = test.getExpectedMessages();
×
186
        if (report.getViolations().size() != expectedMessages.size()) {
×
187
            throw new RuntimeException("Test setup error: number of expected messages doesn't match "
×
188
                                           + "number of violations for test case '" + test.getDescription() + "'");
×
189
        }
190

191
        int index = 0;
×
192
        for (RuleViolation violation : report.getViolations()) {
×
193
            String actual = violation.getDescription();
×
194
            assertEquals(expectedMessages.get(index), actual,
×
195
                         '"' + test.getDescription() + "\" produced wrong message on violation number " + (index + 1)
×
196
                             + ".");
197
            index++;
×
198
        }
×
199
    }
×
200

201
    private void assertLineNumbers(Report report, RuleTestDescriptor test) {
202
        if (report == null || test.getExpectedLineNumbers().isEmpty()) {
1!
203
            return;
1✔
204
        }
205

206
        List<Integer> expected = test.getExpectedLineNumbers();
1✔
207
        List<Integer> expectedEndLines = test.getExpectedEndLineNumbers();
1✔
208
        if (report.getViolations().size() != expected.size()) {
1!
209
            throw new RuntimeException("Test setup error: number of expected line numbers " + expected.size()
×
210
                                           + " doesn't match number of violations " + report.getViolations().size()
×
211
                                           + " for test case '"
212
                                           + test.getDescription() + "'");
×
213
        }
214

215
        int index = 0;
1✔
216
        for (RuleViolation violation : report.getViolations()) {
1✔
217
            Integer actualBeginLine = violation.getBeginLine();
1✔
218
            Integer actualEndLine = violation.getEndLine();
1✔
219

220
            assertEquals(expected.get(index), actualBeginLine,
1✔
221
                         '"' + test.getDescription() + "\" violation on wrong line number: violation number "
1✔
222
                             + (index + 1) + ".");
223
            if (!expectedEndLines.isEmpty()) {
1!
224
                assertEquals(expectedEndLines.get(index), actualEndLine,
1✔
225
                        '"' + test.getDescription() + "\" violation on wrong end line number: violation number "
1✔
226
                            + (index + 1) + ".");
227
            }
228
            index++;
1✔
229
        }
1✔
230
    }
1✔
231

232
    private void printReport(RuleTestDescriptor test, Report report) {
233
        final String separator = "--------------------------------------------------------------";
1✔
234

235
        System.out.println(separator);
1✔
236
        System.out.println("Test Failure: " + test.getDescription());
1✔
237

238
        if (report == null) {
1!
239
            System.out.println("There is no report!");
×
240
            System.out.println(separator);
×
241
            return;
×
242
        }
243

244
        System.out.println(
1✔
245
            " -> Expected " + test.getExpectedProblems() + " problem(s), " + report.getViolations().size()
1✔
246
                + " problem(s) found.");
247
        System.out.println(" -> Expected messages: " + test.getExpectedMessages());
1✔
248
        System.out.println(" -> Expected begin line numbers: " + test.getExpectedLineNumbers());
1✔
249
        if (!test.getExpectedEndLineNumbers().isEmpty()) {
1!
250
            System.out.println(" -> Expected   end line numbers: " + test.getExpectedEndLineNumbers());
×
251
        }
252
        if (test.hasExpectedSuppressions()) {
1!
253
            System.out.println(" -> Expected " + test.getExpectedSuppressions().size() + " suppression(s), "
1✔
254
                    + report.getSuppressedViolations().size() + " found.");
1✔
255
        }
256
        System.out.println();
1✔
257
        StringWriter reportOutput = new StringWriter();
1✔
258
        TextRenderer renderer = new TextRenderer();
1✔
259
        renderer.setWriter(reportOutput);
1✔
260
        try {
261
            renderer.start();
1✔
262
            renderer.renderFileReport(report);
1✔
263
            renderer.end();
1✔
264
        } catch (IOException e) {
×
265
            throw new RuntimeException(e);
×
266
        }
1✔
267
        System.out.println(reportOutput);
1✔
268
        System.out.println(separator);
1✔
269
    }
1✔
270

271
    private Report processUsingStringReader(RuleTestDescriptor test, Rule rule) {
272
        return runTestFromString(test.getCode(), rule, test.getLanguageVersion());
1✔
273
    }
274

275
    private static final ClassLoader TEST_AUXCLASSPATH_CLASSLOADER;
276

277
    static {
278
        final Path PATH_TO_JRT_FS_JAR;
279
        // find jrt-fs.jar to be added to auxclasspath
280
        // Similar logic like jdk.internal.jrtfs.SystemImage
281
        CodeSource codeSource = Object.class.getProtectionDomain().getCodeSource();
1✔
282
        if (codeSource == null) {
1!
283
            PATH_TO_JRT_FS_JAR = Paths.get(System.getProperty("java.home"), "lib", "jrt-fs.jar");
1✔
284
        } else {
285
            URL location = codeSource.getLocation();
×
286
            if (!"file".equalsIgnoreCase(location.getProtocol())) {
×
287
                throw new IllegalStateException("Object.class loaded in unexpected way from " + location);
×
288
            }
289
            try {
290
                PATH_TO_JRT_FS_JAR = Paths.get(location.toURI());
×
291
            } catch (URISyntaxException e) {
×
292
                throw new IllegalStateException(e);
×
293
            }
×
294
        }
295

296
        try {
297
            TEST_AUXCLASSPATH_CLASSLOADER = new ClasspathClassLoader(PATH_TO_JRT_FS_JAR.toString(), PMDConfiguration.class.getClassLoader());
1✔
298
        } catch (IOException e) {
×
299
            throw new RuntimeException(e);
×
300
        }
1✔
301
    }
1✔
302

303
    /**
304
     * Run the rule on the given code and put the violations in the report.
305
     */
306
    Report runTestFromString(String code, Rule rule, LanguageVersion languageVersion) {
307
        PMDConfiguration configuration = new PMDConfiguration();
1✔
308
        configuration.setIgnoreIncrementalAnalysis(true);
1✔
309
        configuration.setDefaultLanguageVersion(languageVersion);
1✔
310
        configuration.setThreads(0); // don't use separate threads
1✔
311
        configuration.setClassLoader(TEST_AUXCLASSPATH_CLASSLOADER);
1✔
312

313
        try (PmdAnalysis pmd = PmdAnalysis.create(configuration)) {
1✔
314
            pmd.files().addFile(TextFile.forCharSeq(code, FileId.fromPathLikeString("file"), languageVersion));
1✔
315
            Collection<? extends Rule> extraRules = getExtraRules();
1✔
316
            if (!extraRules.isEmpty()) {
1!
317
                pmd.addRuleSet(RuleSet.create("extra rules", "description", "file.xml", Collections.emptyList(), Collections.emptyList(), extraRules));
×
318
            }
319
            pmd.addRuleSet(RuleSet.forSingleRule(rule));
1✔
320
            pmd.addListener(GlobalAnalysisListener.exceptionThrower());
1✔
321
            return pmd.performAnalysisAndCollectReport();
1✔
322
        }
323
    }
324

325
    /**
326
     * getResourceAsStream tries to find the XML file in weird locations if the
327
     * ruleName includes the package, so we strip it here.
328
     */
329
    private String getCleanRuleName(Rule rule) {
330
        String fullClassName = rule.getClass().getName();
×
331
        if (fullClassName.equals(rule.getName())) {
×
332
            // We got the full class name, so we'll use the stripped name
333
            // instead
334
            String packageName = rule.getClass().getPackage().getName();
×
335
            return fullClassName.substring(packageName.length() + 1);
×
336
        } else {
337
            return rule.getName(); // Test is using findRule, smart!
×
338
        }
339
    }
340

341
    /**
342
     * Extract a set of tests from an XML file. The file should be
343
     * ./xml/RuleName.xml relative to the test class. The format is defined in
344
     * rule-tests_1_1_0.xsd in pmd-test-schema.
345
     */
346
    RuleTestCollection parseTestCollection(Rule rule) {
347
        String testsFileName = getCleanRuleName(rule);
×
348
        return parseTestCollection(rule, testsFileName);
×
349
    }
350

351
    private RuleTestCollection parseTestCollection(Rule rule, String testsFileName) {
352
        return parseTestXml(rule, testsFileName, "xml/");
×
353
    }
354

355
    /**
356
     * Extract a set of tests from an XML file with the given name. The file
357
     * should be ./xml/[testsFileName].xml relative to the test class. The
358
     * format is defined in test-data.xsd.
359
     */
360
    private RuleTestCollection parseTestXml(Rule rule, String testsFileName, String baseDirectory) {
361
        String testXmlFileName = baseDirectory + testsFileName + ".xml";
×
362
        String absoluteUriToTestXmlFile = new File(".").getAbsoluteFile().toURI() + "/src/test/resources/"
×
363
                + this.getClass().getPackage().getName().replaceAll("\\.", "/")
×
364
                + "/" + testXmlFileName;
365

366
        try (InputStream inputStream = getClass().getResourceAsStream(testXmlFileName)) {
×
367
            if (inputStream == null) {
×
368
                throw new RuntimeException("Couldn't find " + testXmlFileName);
×
369
            }
370
            InputSource source = new InputSource();
×
371
            source.setByteStream(inputStream);
×
372
            source.setSystemId(testXmlFileName);
×
373
            TestSchemaParser parser = new TestSchemaParser();
×
374
            RuleTestCollection ruleTestCollection = parser.parse(rule, source);
×
375
            ruleTestCollection.setAbsoluteUriToTestXmlFile(absoluteUriToTestXmlFile);
×
376
            return ruleTestCollection;
×
377
        } catch (Exception e) {
×
378
            throw new RuntimeException("Couldn't parse " + testXmlFileName + ", due to: " + e, e);
×
379
        }
380
    }
381

382
    /**
383
     * Run a set of tests defined in an XML test-data file for a rule. The file
384
     * should be ./xml/RuleName.xml relative to the test-class. The format is
385
     * defined in test-data.xsd.
386
     */
387
    public void runTests(Rule rule) {
388
        runTests(parseTestCollection(rule));
×
389
    }
×
390

391
    /**
392
     * Run a set of tests defined in a XML test-data file. The file should be
393
     * ./xml/[testsFileName].xml relative to the test-class. The format is
394
     * defined in test-data.xsd.
395
     */
396
    public void runTests(Rule rule, String testsFileName) {
397
        runTests(parseTestCollection(rule, testsFileName));
×
398
    }
×
399

400
    private void runTests(RuleTestCollection tests) {
401
        for (RuleTestDescriptor test : tests.getTests()) {
×
402
            runTest(test);
×
403
        }
×
404
    }
×
405

406
    @TestFactory
407
    Collection<DynamicTest> ruleTests() {
408
        setUp();
×
409
        final List<Rule> rules = new ArrayList<>(getRules());
×
410
        rules.sort(Comparator.comparing(Rule::getName));
×
411

412
        List<DynamicTest> tests = new ArrayList<>();
×
413
        for (Rule r : rules) {
×
414
            RuleTestCollection ruleTests = parseTestCollection(r);
×
415
            RuleTestDescriptor focused = ruleTests.getFocusedTestOrNull();
×
416
            for (RuleTestDescriptor t : ruleTests.getTests()) {
×
417
                if (focused != null && !focused.equals(t)) {
×
418
                    t.setDisabled(true); // disable it
×
419
                }
420
                tests.add(toDynamicTest(ruleTests, t));
×
421
            }
×
422
        }
×
423
        return tests;
×
424
    }
425

426
    private DynamicTest toDynamicTest(RuleTestCollection collection, RuleTestDescriptor testDescriptor) {
427
        URI testSourceUri = URI.create(
×
428
            collection.getAbsoluteUriToTestXmlFile() + "?line=" + testDescriptor.getLineNumber());
×
429
        if (testDescriptor.isDisabled()) {
×
430
            return DynamicTest.dynamicTest("[IGNORED] " + testDescriptor.getDescription(),
×
431
                                           testSourceUri,
432
                                           () -> { });
×
433
        }
434
        return DynamicTest.dynamicTest(testDescriptor.getDescription(),
×
435
                                       testSourceUri,
436
                                       () -> runTest(testDescriptor));
×
437
    }
438
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc