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