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

pmd / pmd / 23

30 May 2025 04:25PM UTC coverage: 78.377% (-0.2%) from 78.601%
23

push

github

adangel
[core] Add rule to report unnecessary suppression comments/annotations (#5609)

Merge pull request #5609 from oowekyala:new-rule-UnnecessarySuppression

17712 of 23434 branches covered (75.58%)

Branch coverage included in aggregate %.

159 of 328 new or added lines in 22 files covered. (48.48%)

36 existing lines in 4 files now uncovered.

38902 of 48799 relevant lines covered (79.72%)

0.81 hits per line

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

32.16
/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
        try {
104
            int res;
105
            Report report;
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
            if (test.getExpectedProblems() != res) {
1!
134
                printReport(test, report);
×
135
            }
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
        } finally {
141
            // Restore old properties
142
            for (Map.Entry<PropertyDescriptor<?>, Object> entry : oldProperties.entrySet()) {
1✔
143
                rule.setProperty((PropertyDescriptor) entry.getKey(), entry.getValue());
1✔
144
            }
1✔
145
        }
146
    }
1✔
147

148

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

160

161
    private void assertMessages(Report report, RuleTestDescriptor test) {
162
        if (report == null || test.getExpectedMessages().isEmpty()) {
1!
163
            return;
1✔
164
        }
165

166
        List<String> expectedMessages = test.getExpectedMessages();
×
167
        if (report.getViolations().size() != expectedMessages.size()) {
×
168
            throw new RuntimeException("Test setup error: number of expected messages doesn't match "
×
169
                                           + "number of violations for test case '" + test.getDescription() + "'");
×
170
        }
171

172
        int index = 0;
×
173
        for (RuleViolation violation : report.getViolations()) {
×
174
            String actual = violation.getDescription();
×
175
            if (!expectedMessages.get(index).equals(actual)) {
×
176
                printReport(test, report);
×
177
            }
178
            assertEquals(expectedMessages.get(index), actual,
×
179
                         '"' + test.getDescription() + "\" produced wrong message on violation number " + (index + 1)
×
180
                             + ".");
181
            index++;
×
182
        }
×
183
    }
×
184

185
    private void assertLineNumbers(Report report, RuleTestDescriptor test) {
186
        if (report == null || test.getExpectedLineNumbers().isEmpty()) {
1!
187
            return;
×
188
        }
189

190
        List<Integer> expected = test.getExpectedLineNumbers();
1✔
191
        List<Integer> expectedEndLines = test.getExpectedEndLineNumbers();
1✔
192
        if (report.getViolations().size() != expected.size()) {
1!
193
            throw new RuntimeException("Test setup error: number of expected line numbers " + expected.size()
×
194
                                           + " doesn't match number of violations " + report.getViolations().size()
×
195
                                           + " for test case '"
196
                                           + test.getDescription() + "'");
×
197
        }
198

199
        int index = 0;
1✔
200
        for (RuleViolation violation : report.getViolations()) {
1✔
201
            Integer actualBeginLine = violation.getBeginLine();
1✔
202
            Integer actualEndLine = violation.getEndLine();
1✔
203

204
            boolean isFailing = expected.get(index) != actualBeginLine.intValue();
1!
205
            isFailing |= (!expectedEndLines.isEmpty() && expectedEndLines.get(index) != actualEndLine.intValue());
1!
206

207
            if (isFailing) {
1!
208
                printReport(test, report);
×
209
            }
210
            assertEquals(expected.get(index), actualBeginLine,
1✔
211
                         '"' + test.getDescription() + "\" violation on wrong line number: violation number "
1✔
212
                             + (index + 1) + ".");
213
            if (!expectedEndLines.isEmpty()) {
1!
214
                assertEquals(expectedEndLines.get(index), actualEndLine,
1✔
215
                        '"' + test.getDescription() + "\" violation on wrong end line number: violation number "
1✔
216
                            + (index + 1) + ".");
217
            }
218
            index++;
1✔
219
        }
1✔
220
    }
1✔
221

222
    private void printReport(RuleTestDescriptor test, Report report) {
223
        System.out.println("--------------------------------------------------------------");
×
224
        System.out.println("Test Failure: " + test.getDescription());
×
225
        System.out.println(
×
226
            " -> Expected " + test.getExpectedProblems() + " problem(s), " + report.getViolations().size()
×
227
                + " problem(s) found.");
228
        System.out.println(" -> Expected messages: " + test.getExpectedMessages());
×
229
        System.out.println(" -> Expected begin line numbers: " + test.getExpectedLineNumbers());
×
230
        if (!test.getExpectedEndLineNumbers().isEmpty()) {
×
231
            System.out.println(" -> Expected   end line numbers: " + test.getExpectedEndLineNumbers());
×
232
        }
233
        System.out.println();
×
234
        StringWriter reportOutput = new StringWriter();
×
235
        TextRenderer renderer = new TextRenderer();
×
236
        renderer.setWriter(reportOutput);
×
237
        try {
238
            renderer.start();
×
239
            renderer.renderFileReport(report);
×
240
            renderer.end();
×
241
        } catch (IOException e) {
×
242
            throw new RuntimeException(e);
×
243
        }
×
244
        System.out.println(reportOutput);
×
245
        System.out.println("--------------------------------------------------------------");
×
246
    }
×
247

248
    private Report processUsingStringReader(RuleTestDescriptor test, Rule rule) {
249
        return runTestFromString(test.getCode(), rule, test.getLanguageVersion());
1✔
250
    }
251

252
    private static final ClassLoader TEST_AUXCLASSPATH_CLASSLOADER;
253

254
    static {
255
        final Path PATH_TO_JRT_FS_JAR;
256
        // find jrt-fs.jar to be added to auxclasspath
257
        // Similar logic like jdk.internal.jrtfs.SystemImage
258
        CodeSource codeSource = Object.class.getProtectionDomain().getCodeSource();
1✔
259
        if (codeSource == null) {
1!
260
            PATH_TO_JRT_FS_JAR = Paths.get(System.getProperty("java.home"), "lib", "jrt-fs.jar");
1✔
261
        } else {
262
            URL location = codeSource.getLocation();
×
263
            if (!"file".equalsIgnoreCase(location.getProtocol())) {
×
264
                throw new IllegalStateException("Object.class loaded in unexpected way from " + location);
×
265
            }
266
            try {
267
                PATH_TO_JRT_FS_JAR = Paths.get(location.toURI());
×
268
            } catch (URISyntaxException e) {
×
269
                throw new IllegalStateException(e);
×
270
            }
×
271
        }
272

273
        try {
274
            TEST_AUXCLASSPATH_CLASSLOADER = new ClasspathClassLoader(PATH_TO_JRT_FS_JAR.toString(), PMDConfiguration.class.getClassLoader());
1✔
275
        } catch (IOException e) {
×
276
            throw new RuntimeException(e);
×
277
        }
1✔
278
    }
1✔
279

280
    /**
281
     * Run the rule on the given code and put the violations in the report.
282
     */
283
    Report runTestFromString(String code, Rule rule, LanguageVersion languageVersion) {
284
        PMDConfiguration configuration = new PMDConfiguration();
1✔
285
        configuration.setIgnoreIncrementalAnalysis(true);
1✔
286
        configuration.setDefaultLanguageVersion(languageVersion);
1✔
287
        configuration.setThreads(0); // don't use separate threads
1✔
288
        configuration.setClassLoader(TEST_AUXCLASSPATH_CLASSLOADER);
1✔
289

290
        try (PmdAnalysis pmd = PmdAnalysis.create(configuration)) {
1✔
291
            pmd.files().addFile(TextFile.forCharSeq(code, FileId.fromPathLikeString("file"), languageVersion));
1✔
292
            Collection<? extends Rule> extraRules = getExtraRules();
1✔
293
            if (!extraRules.isEmpty()) {
1!
NEW
294
                pmd.addRuleSet(RuleSet.create("extra rules", "description", "file.xml", Collections.emptyList(), Collections.emptyList(), extraRules));
×
295
            }
296
            pmd.addRuleSet(RuleSet.forSingleRule(rule));
1✔
297
            pmd.addListener(GlobalAnalysisListener.exceptionThrower());
1✔
298
            return pmd.performAnalysisAndCollectReport();
1✔
299
        }
300
    }
301

302
    /**
303
     * getResourceAsStream tries to find the XML file in weird locations if the
304
     * ruleName includes the package, so we strip it here.
305
     */
306
    private String getCleanRuleName(Rule rule) {
307
        String fullClassName = rule.getClass().getName();
×
308
        if (fullClassName.equals(rule.getName())) {
×
309
            // We got the full class name, so we'll use the stripped name
310
            // instead
311
            String packageName = rule.getClass().getPackage().getName();
×
312
            return fullClassName.substring(packageName.length() + 1);
×
313
        } else {
314
            return rule.getName(); // Test is using findRule, smart!
×
315
        }
316
    }
317

318
    /**
319
     * Extract a set of tests from an XML file. The file should be
320
     * ./xml/RuleName.xml relative to the test class. The format is defined in
321
     * rule-tests_1_0_0.xsd in pmd-test-schema.
322
     */
323
    RuleTestCollection parseTestCollection(Rule rule) {
324
        String testsFileName = getCleanRuleName(rule);
×
325
        return parseTestCollection(rule, testsFileName);
×
326
    }
327

328
    private RuleTestCollection parseTestCollection(Rule rule, String testsFileName) {
329
        return parseTestXml(rule, testsFileName, "xml/");
×
330
    }
331

332
    /**
333
     * Extract a set of tests from an XML file with the given name. The file
334
     * should be ./xml/[testsFileName].xml relative to the test class. The
335
     * format is defined in test-data.xsd.
336
     */
337
    private RuleTestCollection parseTestXml(Rule rule, String testsFileName, String baseDirectory) {
338
        String testXmlFileName = baseDirectory + testsFileName + ".xml";
×
339
        String absoluteUriToTestXmlFile = new File(".").getAbsoluteFile().toURI() + "/src/test/resources/"
×
340
                + this.getClass().getPackage().getName().replaceAll("\\.", "/")
×
341
                + "/" + testXmlFileName;
342

343
        try (InputStream inputStream = getClass().getResourceAsStream(testXmlFileName)) {
×
344
            if (inputStream == null) {
×
345
                throw new RuntimeException("Couldn't find " + testXmlFileName);
×
346
            }
347
            InputSource source = new InputSource();
×
348
            source.setByteStream(inputStream);
×
349
            source.setSystemId(testXmlFileName);
×
350
            TestSchemaParser parser = new TestSchemaParser();
×
351
            RuleTestCollection ruleTestCollection = parser.parse(rule, source);
×
352
            ruleTestCollection.setAbsoluteUriToTestXmlFile(absoluteUriToTestXmlFile);
×
353
            return ruleTestCollection;
×
354
        } catch (Exception e) {
×
355
            throw new RuntimeException("Couldn't parse " + testXmlFileName + ", due to: " + e, e);
×
356
        }
357
    }
358

359
    /**
360
     * Run a set of tests defined in an XML test-data file for a rule. The file
361
     * should be ./xml/RuleName.xml relative to the test-class. The format is
362
     * defined in test-data.xsd.
363
     */
364
    public void runTests(Rule rule) {
365
        runTests(parseTestCollection(rule));
×
366
    }
×
367

368
    /**
369
     * Run a set of tests defined in a XML test-data file. The file should be
370
     * ./xml/[testsFileName].xml relative to the test-class. The format is
371
     * defined in test-data.xsd.
372
     */
373
    public void runTests(Rule rule, String testsFileName) {
374
        runTests(parseTestCollection(rule, testsFileName));
×
375
    }
×
376

377
    private void runTests(RuleTestCollection tests) {
378
        for (RuleTestDescriptor test : tests.getTests()) {
×
379
            runTest(test);
×
380
        }
×
381
    }
×
382

383
    @TestFactory
384
    Collection<DynamicTest> ruleTests() {
385
        setUp();
×
386
        final List<Rule> rules = new ArrayList<>(getRules());
×
387
        rules.sort(Comparator.comparing(Rule::getName));
×
388

389
        List<DynamicTest> tests = new ArrayList<>();
×
390
        for (Rule r : rules) {
×
391
            RuleTestCollection ruleTests = parseTestCollection(r);
×
392
            RuleTestDescriptor focused = ruleTests.getFocusedTestOrNull();
×
393
            for (RuleTestDescriptor t : ruleTests.getTests()) {
×
394
                if (focused != null && !focused.equals(t)) {
×
395
                    t.setDisabled(true); // disable it
×
396
                }
397
                tests.add(toDynamicTest(ruleTests, t));
×
398
            }
×
399
        }
×
400
        return tests;
×
401
    }
402

403
    private DynamicTest toDynamicTest(RuleTestCollection collection, RuleTestDescriptor testDescriptor) {
404
        URI testSourceUri = URI.create(
×
405
            collection.getAbsoluteUriToTestXmlFile() + "?line=" + testDescriptor.getLineNumber());
×
406
        if (testDescriptor.isDisabled()) {
×
407
            return DynamicTest.dynamicTest("[IGNORED] " + testDescriptor.getDescription(),
×
408
                                           testSourceUri,
409
                                           () -> { });
×
410
        }
411
        return DynamicTest.dynamicTest(testDescriptor.getDescription(),
×
412
                                       testSourceUri,
413
                                       () -> runTest(testDescriptor));
×
414
    }
415
}
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