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 static org.junit.Assert.assertNull;
8   
9   import java.io.IOException;
10  import java.io.StringReader;
11  
12  import javax.xml.parsers.DocumentBuilderFactory;
13  import javax.xml.parsers.ParserConfigurationException;
14  
15  import net.sourceforge.pmd.PMD;
16  import net.sourceforge.pmd.Report;
17  import net.sourceforge.pmd.ReportTest;
18  import net.sourceforge.pmd.RuleContext;
19  import net.sourceforge.pmd.RuleSet;
20  import net.sourceforge.pmd.RuleSets;
21  import net.sourceforge.pmd.lang.LanguageVersion;
22  import net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceDeclaration;
23  import net.sourceforge.pmd.lang.java.rule.AbstractJavaRule;
24  import net.sourceforge.pmd.testframework.RuleTst;
25  
26  import org.junit.Test;
27  import org.w3c.dom.Element;
28  import org.xml.sax.InputSource;
29  import org.xml.sax.SAXException;
30  
31  
32  public class XMLRendererTest extends RuleTst {
33  
34      private static class FooRule extends AbstractJavaRule {
35          public Object visit(ASTClassOrInterfaceDeclaration c, Object ctx) {
36              if (c.getImage().equals("Foo")) addViolation(ctx, c);
37              return ctx;
38          }
39  
40          public String getMessage() {
41              return "blah";
42          }
43  
44          public String getName() {
45              return "Foo";
46          }
47  
48          public String getRuleSetName() {
49              return "RuleSet";
50          }
51  
52          public String getDescription() {
53              return "desc";
54          }
55      }
56  
57      @Test
58      public void testEmptyReport() throws Throwable {
59          Element root = parseRootElement(new Report());
60          assertEquals("pmd", root.getNodeName());
61          assertNull(root.getFirstChild().getNextSibling()); // only one child, it's whitespace
62      }
63  
64      @Test
65      public void testErrorReport() throws Throwable {
66          Report report = new Report();
67          report.addError(new Report.ProcessingError("test_msg", "test_filename"));
68          Element root = parseRootElement(report);
69          assertEquals("test_msg", root.getFirstChild().getNextSibling().getAttributes().getNamedItem("msg").getNodeValue());
70      }
71  
72      @Test
73      public void testSingleReport() throws Throwable {
74          Report report = new Report();
75          runTestFromString(TEST1, new FooRule(), report);
76          Element root = parseRootElement(report);
77          assertEquals("n/a", root.getFirstChild().getNextSibling().getAttributes().getNamedItem("name").getNodeValue());
78          assertEquals("Foo", root.getFirstChild().getNextSibling().getFirstChild().getNextSibling().getAttributes().getNamedItem("rule").getNodeValue());
79          assertEquals("RuleSet", root.getFirstChild().getNextSibling().getFirstChild().getNextSibling().getAttributes().getNamedItem("ruleset").getNodeValue());
80          assertEquals("1", root.getFirstChild().getNextSibling().getFirstChild().getNextSibling().getAttributes().getNamedItem("beginline").getNodeValue());
81      }
82  
83      private static final String TEST1 =
84              "public class Foo {}" + PMD.EOL;
85  
86      private static final String TEST2 =
87              "public class Foo {" + PMD.EOL +
88              " public class Foo {}" + PMD.EOL +
89              "}" + PMD.EOL;
90  
91  
92      @Test
93      public void testDoubleReport() throws Throwable {
94          Report report = new Report();
95          runTestFromString(TEST2, new FooRule(), report);
96          runTestFromString(TEST2, new FooRule(), report);
97          Element root = parseRootElement(report);
98          assertEquals("Foo", root.getFirstChild().getNextSibling().getFirstChild().getNextSibling().getAttributes().getNamedItem("rule").getNodeValue());
99          assertEquals("Foo", root.getFirstChild().getNextSibling().getFirstChild().getNextSibling().getNextSibling().getNextSibling().getAttributes().getNamedItem("rule").getNodeValue());
100     }
101 
102     @Test
103     public void testTwoFiles() throws Throwable {
104         Report report = new Report();
105         FooRule rule = new FooRule();
106         runTestFromString(TEST2, rule, report);
107         PMD p = new PMD();
108         p.getConfiguration().setDefaultLanguageVersion(LanguageVersion.JAVA_14);
109         RuleContext ctx = new RuleContext();
110         ctx.setReport(report);
111         ctx.setSourceCodeFilename("bar");
112         RuleSet rules = new RuleSet();
113         rules.addRule(rule);
114         p.getSourceCodeProcessor().processSourceCode(new StringReader(TEST2), new RuleSets(rules), ctx);
115         Element root = parseRootElement(report);
116         assertEquals("bar", root.getFirstChild().getNextSibling().getAttributes().getNamedItem("name").getNodeValue());
117         assertEquals("n/a", root.getFirstChild().getNextSibling().getNextSibling().getNextSibling().getAttributes().getNamedItem("name").getNodeValue());
118     }
119 
120     private Element parseRootElement(Report rpt) throws SAXException, IOException, ParserConfigurationException {
121 	String result = ReportTest.render(new XMLRenderer(), rpt);
122         return DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new InputSource(new StringReader(result))).getDocumentElement();
123     }
124 
125     public static junit.framework.Test suite() {
126         return new junit.framework.JUnit4TestAdapter(XMLRendererTest.class);
127     }
128 }