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