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 static org.junit.Assert.assertEquals;
7 import net.sourceforge.pmd.AbstractRule;
8 import net.sourceforge.pmd.PMD;
9 import net.sourceforge.pmd.Report;
10 import net.sourceforge.pmd.Report.ProcessingError;
11 import net.sourceforge.pmd.ast.ASTClassOrInterfaceDeclaration;
12 import net.sourceforge.pmd.ast.SimpleNode;
13 import net.sourceforge.pmd.renderers.AbstractRenderer;
14
15 import org.junit.Test;
16
17 import test.net.sourceforge.pmd.testframework.RuleTst;
18
19 public abstract class AbstractRendererTst extends RuleTst {
20
21 private static class FooRule extends AbstractRule {
22 public Object visit(ASTClassOrInterfaceDeclaration c, Object ctx) {
23 if (c.getImage().equals("Foo"))
24 addViolation(ctx, c);
25 return ctx;
26 }
27 public String getMessage() { return "msg"; }
28 public String getName() { return "Foo"; }
29 public String getRuleSetName() { return "RuleSet"; }
30 public String getDescription() { return "desc"; }
31 }
32
33 private static class FooRule2 extends FooRule {
34 public Object visit(ASTClassOrInterfaceDeclaration c, Object ctx) {
35 if (c.getImage().equals("Foo")) {
36 addViolation(ctx, c);
37 addViolation(ctx, (SimpleNode) c.jjtGetChild(0));
38 }
39 return ctx;
40 }
41 }
42
43 public abstract AbstractRenderer getRenderer();
44
45 public abstract String getExpected();
46
47 public abstract String getExpectedEmpty();
48
49 public abstract String getExpectedMultiple();
50
51 public String getExpectedError(ProcessingError error) {
52 return "";
53 }
54
55 @Test(expected = NullPointerException.class)
56 public void testNullPassedIn() {
57 getRenderer().render(null);
58 }
59
60 @Test
61 public void testRenderer() throws Throwable {
62 Report rep = new Report();
63 runTestFromString(TEST1, new FooRule(), rep);
64 String actual = getRenderer().render(rep);
65 assertEquals(getExpected(), actual);
66 }
67
68 @Test
69 public void testRendererEmpty() throws Throwable {
70 Report rep = new Report();
71 String actual = getRenderer().render(rep);
72 assertEquals(getExpectedEmpty(), actual);
73 }
74
75 @Test
76 public void testRendererMultiple() throws Throwable {
77 Report rep = new Report();
78 runTestFromString(TEST1, new FooRule2(), rep);
79 String actual = getRenderer().render(rep);
80 assertEquals(getExpectedMultiple(), actual);
81 }
82
83 @Test
84 public void testError() throws Throwable {
85 Report rep = new Report();
86 Report.ProcessingError err = new Report.ProcessingError("Error", "file");
87 rep.addError(err);
88 String actual = getRenderer().render(rep);
89 assertEquals(getExpectedError(err), actual);
90 }
91
92 private static final String TEST1 = "public class Foo {}" + PMD.EOL;
93 }