1   /***
2    * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
3    */
4   package test.net.sourceforge.pmd.renderers;
5   
6   import net.sourceforge.pmd.AbstractRule;
7   import net.sourceforge.pmd.PMD;
8   import net.sourceforge.pmd.Report;
9   import net.sourceforge.pmd.Report.ProcessingError;
10  import net.sourceforge.pmd.ast.ASTClassOrInterfaceDeclaration;
11  import net.sourceforge.pmd.ast.SimpleNode;
12  import net.sourceforge.pmd.renderers.AbstractRenderer;
13  import test.net.sourceforge.pmd.testframework.RuleTst;
14  
15  public abstract class AbstractRendererTst extends RuleTst {
16  
17      private static class FooRule extends AbstractRule {
18          public Object visit(ASTClassOrInterfaceDeclaration c, Object ctx) {
19              if (c.getImage().equals("Foo"))
20                  addViolation(ctx, c);
21              return ctx;
22          }
23          public String getMessage() { return "msg";  }
24          public String getName() { return "Foo"; }
25          public String getRuleSetName() { return "RuleSet"; }
26          public String getDescription() { return "desc"; }
27      }
28  
29      private static class FooRule2 extends FooRule {
30          public Object visit(ASTClassOrInterfaceDeclaration c, Object ctx) {
31              if (c.getImage().equals("Foo")) {
32                  addViolation(ctx, c);
33                  addViolation(ctx, (SimpleNode) c.jjtGetChild(0));
34              }
35              return ctx;
36          }
37      }
38  
39      public abstract AbstractRenderer getRenderer();
40  
41      public abstract String getExpected();
42  
43      public abstract String getExpectedEmpty();
44  
45      public abstract String getExpectedMultiple();
46  
47      public String getExpectedError(ProcessingError error) {
48          return "";
49      }
50  
51      public void testNullPassedIn() {
52          try {
53              getRenderer().render(null);
54              fail("Providing a render(null) should throw an npx");
55          } catch (NullPointerException npx) {
56              // cool
57          }
58      }
59  
60      public void testRenderer() throws Throwable {
61          Report rep = new Report();
62          runTestFromString(TEST1, new FooRule(), rep);
63          String actual = getRenderer().render(rep);
64          assertEquals(actual, getExpected());
65      }
66  
67      public void testRendererEmpty() throws Throwable {
68          Report rep = new Report();
69          String actual = getRenderer().render(rep);
70          assertEquals(actual, getExpectedEmpty());
71      }
72  
73      public void testRendererMultiple() throws Throwable {
74          Report rep = new Report();
75          runTestFromString(TEST1, new FooRule2(), rep);
76          String actual = getRenderer().render(rep);
77          assertEquals(actual, getExpectedMultiple());
78      }
79  
80      public void testError() throws Throwable {
81          Report rep = new Report();
82          Report.ProcessingError err = new Report.ProcessingError("Error", "file");
83          rep.addError(err);
84          String actual = getRenderer().render(rep);
85          assertEquals(actual, getExpectedError(err));
86      }
87  
88      private static final String TEST1 = "public class Foo {}" + PMD.EOL;
89  }