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 static org.junit.Assert.assertNull;
007: import net.sourceforge.pmd.AbstractRule;
008: import net.sourceforge.pmd.PMD;
009: import net.sourceforge.pmd.Report;
010: import net.sourceforge.pmd.RuleContext;
011: import net.sourceforge.pmd.RuleSet;
012: import net.sourceforge.pmd.SourceType;
013: import net.sourceforge.pmd.ast.ASTClassOrInterfaceDeclaration;
014: import net.sourceforge.pmd.renderers.XMLRenderer;
015:
016: import org.junit.Test;
017: import org.w3c.dom.Element;
018: import org.xml.sax.InputSource;
019: import org.xml.sax.SAXException;
020:
021: import test.net.sourceforge.pmd.testframework.RuleTst;
022:
023: import java.io.IOException;
024: import java.io.StringReader;
025:
026: import javax.xml.parsers.DocumentBuilderFactory;
027: import javax.xml.parsers.ParserConfigurationException;
028:
029: public class XMLRendererTest extends RuleTst {
030:
031: private static class FooRule extends AbstractRule {
032: public Object visit(ASTClassOrInterfaceDeclaration c, Object ctx) {
033: if (c.getImage().equals("Foo"))
034: addViolation(ctx, c);
035: return ctx;
036: }
037:
038: public String getMessage() {
039: return "blah";
040: }
041:
042: public String getName() {
043: return "Foo";
044: }
045:
046: public String getRuleSetName() {
047: return "RuleSet";
048: }
049:
050: public String getDescription() {
051: return "desc";
052: }
053: }
054:
055: @Test
056: public void testEmptyReport() throws Throwable {
057: Element root = parseRootElement(new Report());
058: assertEquals("pmd", root.getNodeName());
059: assertNull(root.getFirstChild().getNextSibling()); // only one child, it's whitespace
060: }
061:
062: @Test
063: public void testErrorReport() throws Throwable {
064: Report report = new Report();
065: report.addError(new Report.ProcessingError("test_msg",
066: "test_filename"));
067: Element root = parseRootElement(report);
068: assertEquals("test_msg", root.getFirstChild().getNextSibling()
069: .getAttributes().getNamedItem("msg").getNodeValue());
070: }
071:
072: @Test
073: public void testSingleReport() throws Throwable {
074: Report report = new Report();
075: runTestFromString(TEST1, new FooRule(), report);
076: Element root = parseRootElement(report);
077: assertEquals("n/a", root.getFirstChild().getNextSibling()
078: .getAttributes().getNamedItem("name").getNodeValue());
079: assertEquals("Foo", root.getFirstChild().getNextSibling()
080: .getFirstChild().getNextSibling().getAttributes()
081: .getNamedItem("rule").getNodeValue());
082: assertEquals("RuleSet", root.getFirstChild().getNextSibling()
083: .getFirstChild().getNextSibling().getAttributes()
084: .getNamedItem("ruleset").getNodeValue());
085: assertEquals("1", root.getFirstChild().getNextSibling()
086: .getFirstChild().getNextSibling().getAttributes()
087: .getNamedItem("beginline").getNodeValue());
088: }
089:
090: private static final String TEST1 = "public class Foo {}" + PMD.EOL;
091:
092: private static final String TEST2 = "public class Foo {" + PMD.EOL
093: + " public class Foo {}" + PMD.EOL + "}" + PMD.EOL;
094:
095: @Test
096: public void testDoubleReport() throws Throwable {
097: Report report = new Report();
098: runTestFromString(TEST2, new FooRule(), report);
099: runTestFromString(TEST2, new FooRule(), report);
100: Element root = parseRootElement(report);
101: assertEquals("Foo", root.getFirstChild().getNextSibling()
102: .getFirstChild().getNextSibling().getAttributes()
103: .getNamedItem("rule").getNodeValue());
104: assertEquals("Foo", root.getFirstChild().getNextSibling()
105: .getFirstChild().getNextSibling().getNextSibling()
106: .getNextSibling().getAttributes().getNamedItem("rule")
107: .getNodeValue());
108: }
109:
110: @Test
111: public void testTwoFiles() throws Throwable {
112: Report report = new Report();
113: FooRule rule = new FooRule();
114: runTestFromString(TEST2, rule, report);
115: PMD p = new PMD();
116: p.setJavaVersion(SourceType.JAVA_14);
117: RuleContext ctx = new RuleContext();
118: ctx.setReport(report);
119: ctx.setSourceCodeFilename("bar");
120: RuleSet rules = new RuleSet();
121: rules.addRule(rule);
122: p.processFile(new StringReader(TEST2), rules, ctx);
123: Element root = parseRootElement(report);
124: assertEquals("bar", root.getFirstChild().getNextSibling()
125: .getAttributes().getNamedItem("name").getNodeValue());
126: assertEquals("n/a", root.getFirstChild().getNextSibling()
127: .getNextSibling().getNextSibling().getAttributes()
128: .getNamedItem("name").getNodeValue());
129: }
130:
131: private Element parseRootElement(Report rpt) throws SAXException,
132: IOException, ParserConfigurationException {
133: return DocumentBuilderFactory.newInstance()
134: .newDocumentBuilder().parse(
135: new InputSource(new StringReader(
136: new XMLRenderer().render(rpt))))
137: .getDocumentElement();
138: }
139:
140: public static junit.framework.Test suite() {
141: return new junit.framework.JUnit4TestAdapter(
142: XMLRendererTest.class);
143: }
144: }
|