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.PMD;
    8   import net.sourceforge.pmd.Report;
    9   import net.sourceforge.pmd.ReportTest;
    10  import net.sourceforge.pmd.Report.ProcessingError;
    11  import net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceDeclaration;
    12  import net.sourceforge.pmd.lang.java.rule.AbstractJavaRule;
    13  import net.sourceforge.pmd.renderers.Renderer;
    14  import net.sourceforge.pmd.testframework.RuleTst;
    15
    16  import org.junit.Test;
    17
    18
    19  public abstract class AbstractRendererTst extends RuleTst {
    20
    21      private static class FooRule extends AbstractJavaRule {
    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, c.jjtGetChild(0));
    38              }
    39              return ctx;
    40          }
    41      }
    42
    43      public abstract Renderer 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() throws Throwable {
    57  	ReportTest.render(getRenderer(), 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 = ReportTest.render(getRenderer(), rep);
    65          assertEquals(getExpected(), actual);
    66      }
    67
    68      @Test
    69      public void testRendererEmpty() throws Throwable {
    70          Report rep = new Report();
    71          String actual = ReportTest.render(getRenderer(), 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 = ReportTest.render(getRenderer(), 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 = ReportTest.render(getRenderer(), rep);
    89          assertEquals(getExpectedError(err), actual);
    90      }
    91
    92      private static final String TEST1 = "public class Foo {}" + PMD.EOL;
    93  }