01: package test.net.sourceforge.pmd;
02:
03: import static org.junit.Assert.assertEquals;
04: import static org.junit.Assert.assertTrue;
05: import net.sourceforge.pmd.PMD;
06: import net.sourceforge.pmd.Report;
07: import net.sourceforge.pmd.Rule;
08: import net.sourceforge.pmd.RuleContext;
09: import net.sourceforge.pmd.RuleSet;
10: import net.sourceforge.pmd.RuleSets;
11: import net.sourceforge.pmd.SourceTypeToRuleLanguageMapper;
12:
13: import org.junit.Before;
14: import org.junit.Test;
15:
16: import test.net.sourceforge.pmd.testframework.RuleTst;
17: import test.net.sourceforge.pmd.testframework.TestDescriptor;
18:
19: import java.io.StringReader;
20:
21: import junit.framework.JUnit4TestAdapter;
22:
23: public class ExcludeLinesTest extends RuleTst {
24: private Rule rule;
25:
26: @Before
27: public void setUp() {
28: rule = findRule("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,
35: "Should fail without exclude marker", 1, rule));
36: }
37:
38: @Test
39: public void testAlternateMarker() throws Throwable {
40: PMD p = new PMD();
41: p.setExcludeMarker("FOOBAR");
42: RuleContext ctx = new RuleContext();
43: Report r = new Report();
44: ctx.setReport(r);
45: ctx.setSourceCodeFilename("n/a");
46: RuleSet rules = new RuleSet();
47: rules.addRule(rule);
48: rules.setLanguage(SourceTypeToRuleLanguageMapper
49: .getMappedLanguage(DEFAULT_SOURCE_TYPE));
50: p.processFile(new StringReader(TEST3), new RuleSets(rules),
51: ctx, DEFAULT_SOURCE_TYPE);
52: assertTrue(r.isEmpty());
53: assertEquals(r.getSuppressedRuleViolations().size(), 1);
54: }
55:
56: private static final String TEST1 = "public class Foo {" + PMD.EOL
57: + " void foo() {" + PMD.EOL + " int x; //NOPMD " + PMD.EOL
58: + " } " + PMD.EOL + "}";
59:
60: private static final String TEST2 = "public class Foo {" + PMD.EOL
61: + " void foo() {" + PMD.EOL + " int x;" + PMD.EOL + " } "
62: + PMD.EOL + "}";
63:
64: private static final String TEST3 = "public class Foo {" + PMD.EOL
65: + " void foo() {" + PMD.EOL + " int x; // FOOBAR"
66: + PMD.EOL + " } " + PMD.EOL + "}";
67:
68: public static junit.framework.Test suite() {
69: return new JUnit4TestAdapter(ExcludeLinesTest.class);
70: }
71: }
|