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

pmd / pmd / 46

26 Jun 2025 09:17AM UTC coverage: 78.43% (+0.05%) from 78.379%
46

push

github

adangel
[test] Verify suppressed violations in rule tests (#5806)

Merge pull request #5806 from adangel:test/assert-suppressions

17746 of 23468 branches covered (75.62%)

Branch coverage included in aggregate %.

55 of 62 new or added lines in 4 files covered. (88.71%)

38998 of 48882 relevant lines covered (79.78%)

0.81 hits per line

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

45.72
/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());
×
118
                        rule.setProperty(propertyDescriptor, value);
×
119
                    }
×
120
                }
121

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

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

149

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

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

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

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

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

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

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

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

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

229
    private void printReport(RuleTestDescriptor test, Report report) {
230
        final String separator = "--------------------------------------------------------------";
1✔
231

232
        System.out.println(separator);
1✔
233
        System.out.println("Test Failure: " + test.getDescription());
1✔
234

235
        if (report == null) {
1!
NEW
236
            System.out.println("There is no report!");
×
NEW
237
            System.out.println(separator);
×
NEW
238
            return;
×
239
        }
240

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

268
    private Report processUsingStringReader(RuleTestDescriptor test, Rule rule) {
269
        return runTestFromString(test.getCode(), rule, test.getLanguageVersion());
1✔
270
    }
271

272
    private static final ClassLoader TEST_AUXCLASSPATH_CLASSLOADER;
273

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

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

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

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

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

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

348
    private RuleTestCollection parseTestCollection(Rule rule, String testsFileName) {
349
        return parseTestXml(rule, testsFileName, "xml/");
×
350
    }
351

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

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

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

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

397
    private void runTests(RuleTestCollection tests) {
398
        for (RuleTestDescriptor test : tests.getTests()) {
×
399
            runTest(test);
×
400
        }
×
401
    }
×
402

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

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

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

© 2026 Coveralls, Inc