001: // Copyright (C) 2003,2004,2005 by Object Mentor, Inc. All rights reserved.
002: // Released under the terms of the GNU General Public License version 2 or later.
003: package fitnesse.runner;
004:
005: import java.io.InputStream;
006: import fitnesse.util.*;
007: import fitnesse.testutil.AbstractRegex;
008: import org.w3c.dom.*;
009: import fit.Counts;
010:
011: public class XmlResultFormatterTest extends AbstractRegex {
012: private XmlResultFormatter formatter;
013:
014: private PageResult result1;
015:
016: private PageResult result2;
017:
018: private Counts finalCounts;
019:
020: public void setUp() throws Exception {
021: formatter = new XmlResultFormatter("localhost:8081", "RootPath");
022: result1 = new PageResult("ResultOne", new Counts(1, 2, 3, 4),
023: "result one content");
024: result2 = new PageResult("ResultTwo", new Counts(4, 3, 2, 1),
025: "result two content");
026: finalCounts = new Counts(5, 5, 5, 5);
027: }
028:
029: public void tearDown() throws Exception {
030: formatter = null;
031: System.gc();
032: }
033:
034: public void testValidXml() throws Exception {
035: Document doc = getXmlDoc();
036: Element documentElement = doc.getDocumentElement();
037: assertEquals("testResults", documentElement.getNodeName());
038: assertEquals("localhost:8081", XmlUtil.getTextValue(
039: documentElement, "host"));
040: assertEquals("RootPath", XmlUtil.getTextValue(documentElement,
041: "rootPath"));
042: }
043:
044: public void testOneResult() throws Exception {
045: formatter.acceptResult(result1);
046: Document doc = getXmlDoc();
047: NodeList results = doc.getElementsByTagName("result");
048: assertEquals(1, results.getLength());
049: Element result = (Element) results.item(0);
050: checkResultElement(result, result1);
051: }
052:
053: public void testTwoResults() throws Exception {
054: formatter.acceptResult(result1);
055: formatter.acceptResult(result2);
056: Document doc = getXmlDoc();
057: NodeList results = doc.getElementsByTagName("result");
058: assertEquals(2, results.getLength());
059: Element resultElement1 = (Element) results.item(0);
060: Element resultElement2 = (Element) results.item(1);
061:
062: checkResultElement(resultElement1, result1);
063: checkResultElement(resultElement2, result2);
064: }
065:
066: public void testFinalCounts() throws Exception {
067: formatter.acceptFinalCount(finalCounts);
068: Document doc = getXmlDoc();
069: NodeList finalCountsList = doc
070: .getElementsByTagName("finalCounts");
071: assertEquals(1, finalCountsList.getLength());
072: Element finalCountElement = (Element) finalCountsList.item(0);
073: checkCounts(finalCounts, finalCountElement);
074: }
075:
076: private void checkResultElement(Element resultElement,
077: PageResult result) throws Exception {
078: assertEquals(result.title(), XmlUtil.getTextValue(
079: resultElement, "relativePageName"));
080: assertEquals(result.content(), XmlUtil.getTextValue(
081: resultElement, "content"));
082: Element countsElement = XmlUtil.getElementByTagName(
083: resultElement, "counts");
084: Counts counts = result.counts();
085: checkCounts(counts, countsElement);
086: }
087:
088: private void checkCounts(Counts counts, Element countsElement)
089: throws Exception {
090: assertEquals(counts.right + "", XmlUtil.getTextValue(
091: countsElement, "right"));
092: assertEquals(counts.wrong + "", XmlUtil.getTextValue(
093: countsElement, "wrong"));
094: assertEquals(counts.ignores + "", XmlUtil.getTextValue(
095: countsElement, "ignores"));
096: assertEquals(counts.exceptions + "", XmlUtil.getTextValue(
097: countsElement, "exceptions"));
098: }
099:
100: private String getXml() throws Exception {
101: InputStream input = formatter.getResultStream();
102: assertNotNull(input);
103:
104: int bytes = formatter.getByteCount();
105: String xml = new StreamReader(input).read(bytes);
106: return xml;
107: }
108:
109: private Document getXmlDoc() throws Exception {
110: return XmlUtil.newDocument(getXml());
111: }
112: }
|