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