1
2 package net.sourceforge.pmd;
3
4 import static org.junit.Assert.assertEquals;
5 import static org.junit.Assert.assertTrue;
6
7 import java.io.StringReader;
8
9 import junit.framework.JUnit4TestAdapter;
10 import net.sourceforge.pmd.PMD;
11 import net.sourceforge.pmd.Report;
12 import net.sourceforge.pmd.Rule;
13 import net.sourceforge.pmd.RuleContext;
14 import net.sourceforge.pmd.RuleSet;
15 import net.sourceforge.pmd.RuleSets;
16 import net.sourceforge.pmd.testframework.RuleTst;
17 import net.sourceforge.pmd.testframework.TestDescriptor;
18
19 import org.junit.Before;
20 import org.junit.Test;
21
22
23 public class ExcludeLinesTest extends RuleTst {
24 private Rule rule;
25
26 @Before
27 public void setUp() {
28 rule = findRule("java-unusedcode", "UnusedLocalVariable");
29 }
30
31 @Test
32 public void testAcceptance() {
33 runTest(new TestDescriptor(TEST1, "NOPMD should work", 0, rule));
34 runTest(new TestDescriptor(TEST2, "Should fail without exclude marker", 1, rule));
35 }
36
37 @Test
38 public void testAlternateMarker() throws Throwable {
39 PMD p = new PMD();
40 p.getConfiguration().setSuppressMarker("FOOBAR");
41 RuleContext ctx = new RuleContext();
42 Report r = new Report();
43 ctx.setReport(r);
44 ctx.setSourceCodeFilename("n/a");
45 ctx.setLanguageVersion(DEFAULT_LANGUAGE_VERSION);
46 RuleSet rules = new RuleSet();
47 rules.addRule(rule);
48 p.getSourceCodeProcessor().processSourceCode(new StringReader(TEST3), new RuleSets(rules), ctx);
49 assertTrue(r.isEmpty());
50 assertEquals(r.getSuppressedRuleViolations().size(), 1);
51 }
52
53 private static final String TEST1 =
54 "public class Foo {" + PMD.EOL +
55 " void foo() {" + PMD.EOL +
56 " int x; //NOPMD " + PMD.EOL +
57 " } " + PMD.EOL +
58 "}";
59
60 private static final String TEST2 =
61 "public class Foo {" + PMD.EOL +
62 " void foo() {" + PMD.EOL +
63 " int x;" + PMD.EOL +
64 " } " + PMD.EOL +
65 "}";
66
67 private static final String TEST3 =
68 "public class Foo {" + PMD.EOL +
69 " void foo() {" + PMD.EOL +
70 " int x; // FOOBAR" + PMD.EOL +
71 " } " + PMD.EOL +
72 "}";
73
74 public static junit.framework.Test suite() {
75 return new JUnit4TestAdapter(ExcludeLinesTest.class);
76 }
77 }