View Javadoc
1   /**
2    * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
3    */
4   package net.sourceforge.pmd;
5   
6   import static org.junit.Assert.assertEquals;
7   import static org.junit.Assert.assertFalse;
8   import static org.junit.Assert.assertTrue;
9   import net.sourceforge.pmd.lang.LanguageRegistry;
10  import net.sourceforge.pmd.lang.LanguageVersion;
11  import net.sourceforge.pmd.lang.java.JavaLanguageModule;
12  import net.sourceforge.pmd.testframework.RuleTst;
13  
14  import org.junit.Test;
15  
16  
17  public class ReportTest extends RuleTst {
18  
19      private LanguageVersion defaultLanguage = LanguageRegistry.getLanguage(JavaLanguageModule.NAME).getDefaultVersion();
20  
21      @Test
22      public void testBasic() throws Throwable {
23          Report r = new Report();
24          runTestFromString(TEST1, new FooRule(), r, defaultLanguage);
25          assertFalse(r.isEmpty());
26      }
27  
28      @Test
29      public void testExclusionsInReportWithRuleViolationSuppressRegex() throws Throwable {
30          Report rpt = new Report();
31          Rule rule =  new FooRule();
32          rule.setProperty(Rule.VIOLATION_SUPPRESS_REGEX_DESCRIPTOR, ".*No Foo.*");
33          runTestFromString(TEST1, rule, rpt, defaultLanguage);
34          assertTrue(rpt.isEmpty());
35          assertEquals(1, rpt.getSuppressedRuleViolations().size());
36      }
37  
38      @Test
39      public void testExclusionsInReportWithRuleViolationSuppressXPath() throws Throwable {
40          Report rpt = new Report();
41          Rule rule =  new FooRule();
42          rule.setProperty(Rule.VIOLATION_SUPPRESS_XPATH_DESCRIPTOR, ".[@Image = 'Foo']");
43          runTestFromString(TEST1, rule, rpt, defaultLanguage);
44          assertTrue(rpt.isEmpty());
45          assertEquals(1, rpt.getSuppressedRuleViolations().size());
46      }
47  
48      @Test
49      public void testExclusionsInReportWithAnnotations() throws Throwable {
50          Report rpt = new Report();
51          runTestFromString(TEST2, new FooRule(), rpt, LanguageRegistry.getLanguage(JavaLanguageModule.NAME).getVersion("1.5"));
52          assertTrue(rpt.isEmpty());
53          assertEquals(1, rpt.getSuppressedRuleViolations().size());
54      }
55  
56      @Test
57      public void testExclusionsInReportWithAnnotationsFullName() throws Throwable {
58          Report rpt = new Report();
59          runTestFromString(TEST2_FULL, new FooRule(), rpt, LanguageRegistry.getLanguage(JavaLanguageModule.NAME).getVersion("1.5"));
60          assertTrue(rpt.isEmpty());
61          assertEquals(1, rpt.getSuppressedRuleViolations().size());
62      }
63  
64      @Test
65      public void testExclusionsInReportWithNOPMD() throws Throwable {
66          Report rpt = new Report();
67          runTestFromString(TEST3, new FooRule(), rpt, defaultLanguage);
68          assertTrue(rpt.isEmpty());
69          assertEquals(1, rpt.getSuppressedRuleViolations().size());
70      }
71  
72      private static final String TEST1 =
73              "public class Foo {}" + PMD.EOL;
74  
75      private static final String TEST2 =
76              "@SuppressWarnings(\"PMD\")" + PMD.EOL +
77              "public class Foo {}";
78      private static final String TEST2_FULL =
79              "@java.lang.SuppressWarnings(\"PMD\")" + PMD.EOL +
80              "public class Foo {}";
81  
82      private static final String TEST3 =
83              "public class Foo {} // NOPMD";
84  }