001: /**
002: * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
003: */package test.net.sourceforge.pmd.renderers;
004:
005: import static org.junit.Assert.assertEquals;
006: import net.sourceforge.pmd.AbstractRule;
007: import net.sourceforge.pmd.PMD;
008: import net.sourceforge.pmd.Report;
009: import net.sourceforge.pmd.Report.ProcessingError;
010: import net.sourceforge.pmd.ast.ASTClassOrInterfaceDeclaration;
011: import net.sourceforge.pmd.ast.SimpleNode;
012: import net.sourceforge.pmd.renderers.AbstractRenderer;
013:
014: import org.junit.Test;
015:
016: import test.net.sourceforge.pmd.testframework.RuleTst;
017:
018: public abstract class AbstractRendererTst extends RuleTst {
019:
020: private static class FooRule extends AbstractRule {
021: public Object visit(ASTClassOrInterfaceDeclaration c, Object ctx) {
022: if (c.getImage().equals("Foo"))
023: addViolation(ctx, c);
024: return ctx;
025: }
026:
027: public String getMessage() {
028: return "msg";
029: }
030:
031: public String getName() {
032: return "Foo";
033: }
034:
035: public String getRuleSetName() {
036: return "RuleSet";
037: }
038:
039: public String getDescription() {
040: return "desc";
041: }
042: }
043:
044: private static class FooRule2 extends FooRule {
045: public Object visit(ASTClassOrInterfaceDeclaration c, Object ctx) {
046: if (c.getImage().equals("Foo")) {
047: addViolation(ctx, c);
048: addViolation(ctx, (SimpleNode) c.jjtGetChild(0));
049: }
050: return ctx;
051: }
052: }
053:
054: public abstract AbstractRenderer getRenderer();
055:
056: public abstract String getExpected();
057:
058: public abstract String getExpectedEmpty();
059:
060: public abstract String getExpectedMultiple();
061:
062: public String getExpectedError(ProcessingError error) {
063: return "";
064: }
065:
066: @Test(expected=NullPointerException.class)
067: public void testNullPassedIn() {
068: getRenderer().render(null);
069: }
070:
071: @Test
072: public void testRenderer() throws Throwable {
073: Report rep = new Report();
074: runTestFromString(TEST1, new FooRule(), rep);
075: String actual = getRenderer().render(rep);
076: assertEquals(getExpected(), actual);
077: }
078:
079: @Test
080: public void testRendererEmpty() throws Throwable {
081: Report rep = new Report();
082: String actual = getRenderer().render(rep);
083: assertEquals(getExpectedEmpty(), actual);
084: }
085:
086: @Test
087: public void testRendererMultiple() throws Throwable {
088: Report rep = new Report();
089: runTestFromString(TEST1, new FooRule2(), rep);
090: String actual = getRenderer().render(rep);
091: assertEquals(getExpectedMultiple(), actual);
092: }
093:
094: @Test
095: public void testError() throws Throwable {
096: Report rep = new Report();
097: Report.ProcessingError err = new Report.ProcessingError(
098: "Error", "file");
099: rep.addError(err);
100: String actual = getRenderer().render(rep);
101: assertEquals(getExpectedError(err), actual);
102: }
103:
104: private static final String TEST1 = "public class Foo {}" + PMD.EOL;
105: }
|