001: /*
002: * Jacareto Copyright (c) 2002-2005
003: * Applied Computer Science Research Group, Darmstadt University of
004: * Technology, Institute of Mathematics & Computer Science,
005: * Ludwigsburg University of Education, and Computer Based
006: * Learning Research Group, Aachen University. All rights reserved.
007: *
008: * Jacareto is free software; you can redistribute it and/or
009: * modify it under the terms of the GNU General Public
010: * License as published by the Free Software Foundation; either
011: * version 2 of the License, or (at your option) any later version.
012: *
013: * Jacareto is distributed in the hope that it will be useful,
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
016: * General Public License for more details.
017: *
018: * You should have received a copy of the GNU General Public
019: * License along with Jacareto; if not, write to the Free
020: * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
021: *
022: */
023:
024: package jacareto.test;
025:
026: import org.jdom.Document;
027: import org.jdom.Element;
028: import org.jdom.output.XMLOutputter;
029:
030: import java.io.FileOutputStream;
031: import java.io.IOException;
032:
033: import java.util.Iterator;
034:
035: /**
036: * Stores a test report as xml file.
037: *
038: * @author <a href="mailto:cspannagel@web.de">Christian Spannagel</a>
039: * @version 1.00
040: */
041: public class TestReportXMLBuilder {
042: /**
043: * Stores the given test report as xml file.
044: *
045: * @param testReport DOCUMENT ME!
046: * @param filename DOCUMENT ME!
047: *
048: * @return whether or not the test report could be stored
049: */
050: public static boolean store(TestReport testReport, String filename) {
051: if (!filename.toLowerCase().endsWith(".xml")) {
052: filename += ".xml";
053: }
054:
055: // The statistics element
056: Element statistics = new Element("Statistics");
057: Element successes = new Element("Successes");
058: successes.setAttribute("value", ""
059: + testReport.getNumberOfSuccesses());
060: statistics.addContent(successes);
061:
062: Element failures = new Element("Failures");
063: failures.setAttribute("value", ""
064: + testReport.getNumberOfFailures());
065: statistics.addContent(failures);
066:
067: Element corrections = new Element("Corrections");
068: corrections.setAttribute("value", ""
069: + testReport.getNumberOfCorrections());
070: statistics.addContent(corrections);
071:
072: Element ignored = new Element("IgnoredFailures");
073: ignored.setAttribute("value", ""
074: + testReport.getNumberOfIgnoredFailures());
075: statistics.addContent(ignored);
076:
077: // Add all elements
078: Element overview = new Element("Overview");
079: Iterator it = testReport.iterator();
080:
081: while (it.hasNext()) {
082: TestReportElement trElement = (TestReportElement) it.next();
083: Element xmlElement = new Element("TestReportElement");
084: xmlElement.setAttribute("success", ""
085: + trElement.getResult());
086: xmlElement.setAttribute("corrected", ""
087: + trElement.getCorrected());
088: xmlElement.setAttribute("ignored", ""
089: + trElement.getIgnored());
090: xmlElement.setAttribute("component", ""
091: + trElement.getComponentName());
092:
093: Element msg = new Element("Message");
094: msg.addContent(trElement.getEvaluationMessage());
095: xmlElement.addContent(msg);
096: overview.addContent(xmlElement);
097: }
098:
099: // The root element
100: Element rootElement = new Element("TestReport");
101: rootElement.addContent(statistics);
102: rootElement.addContent(overview);
103:
104: // Save
105: try {
106: FileOutputStream out = new FileOutputStream(filename);
107: XMLOutputter xmlOutputter = new XMLOutputter(" ", true,
108: "iso-8859-1");
109: Document xmlDocument = new Document(rootElement);
110: xmlOutputter.output(xmlDocument, out);
111: out.close();
112: } catch (IOException i) {
113: return false;
114: }
115:
116: return true;
117: }
118: }
|