View Javadoc
1   /**
2    * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
3    */
4   package net.sourceforge.pmd.renderers;
5   
6   import static org.junit.Assert.assertEquals;
7   import net.sourceforge.pmd.FooRule;
8   import net.sourceforge.pmd.Report;
9   import net.sourceforge.pmd.Report.ProcessingError;
10  import net.sourceforge.pmd.ReportTest;
11  import net.sourceforge.pmd.RuleContext;
12  import net.sourceforge.pmd.RuleViolation;
13  import net.sourceforge.pmd.lang.ast.DummyNode;
14  import net.sourceforge.pmd.lang.ast.Node;
15  import net.sourceforge.pmd.lang.rule.ParametricRuleViolation;
16  
17  import org.junit.Test;
18  
19  
20  public abstract class AbstractRendererTst {
21  
22      public abstract Renderer getRenderer();
23  
24      public abstract String getExpected();
25  
26      public abstract String getExpectedEmpty();
27  
28      public abstract String getExpectedMultiple();
29  
30      public String getExpectedError(ProcessingError error) {
31          return "";
32      }
33  
34      public String filter(String expected) {
35          return expected;
36      }
37  
38      @Test(expected = NullPointerException.class)
39      public void testNullPassedIn() throws Throwable {
40          getRenderer().renderFileReport(null);
41      }
42  
43      private static Report reportOneViolation() {
44          Report report = new Report();
45          report.addRuleViolation(newRuleViolation(1));
46          return report;
47      }
48  
49      private static Report reportTwoViolations() {
50          Report report = new Report();
51          report.addRuleViolation(newRuleViolation(1));
52          report.addRuleViolation(newRuleViolation(2));
53          return report;
54      }
55  
56      private static RuleViolation newRuleViolation(int endColumn) {
57          DummyNode node = new DummyNode(1);
58          node.testingOnly__setBeginLine(1);
59          node.testingOnly__setBeginColumn(1);
60          node.testingOnly__setEndLine(1);
61          node.testingOnly__setEndColumn(endColumn);
62          RuleContext ctx = new RuleContext();
63          ctx.setSourceCodeFilename("n/a");
64          return new ParametricRuleViolation<Node>(new FooRule(), ctx, node, "blah");
65      }
66  
67      @Test
68      public void testRenderer() throws Throwable {
69          Report rep = reportOneViolation();
70          String actual = ReportTest.render(getRenderer(), rep);
71          assertEquals(filter(getExpected()), filter(actual));
72      }
73  
74      @Test
75      public void testRendererEmpty() throws Throwable {
76          Report rep = new Report();
77          String actual = ReportTest.render(getRenderer(), rep);
78          assertEquals(filter(getExpectedEmpty()), filter(actual));
79      }
80  
81      @Test
82      public void testRendererMultiple() throws Throwable {
83          Report rep = reportTwoViolations();
84          String actual = ReportTest.render(getRenderer(), rep);
85          assertEquals(filter(getExpectedMultiple()), filter(actual));
86      }
87  
88      @Test
89      public void testError() throws Throwable {
90          Report rep = new Report();
91          Report.ProcessingError err = new Report.ProcessingError("Error", "file");
92          rep.addError(err);
93          String actual = ReportTest.render(getRenderer(), rep);
94          assertEquals(filter(getExpectedError(err)), filter(actual));
95      }
96  }