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