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