001: /*
002: * @(#)XMLFileServer.java
003: *
004: * Copyright (C) 2002-2003 Matt Albrecht
005: * groboclown@users.sourceforge.net
006: * http://groboutils.sourceforge.net
007: *
008: * Part of the GroboUtils package at:
009: * http://groboutils.sourceforge.net
010: *
011: * Permission is hereby granted, free of charge, to any person obtaining a
012: * copy of this software and associated documentation files (the "Software"),
013: * to deal in the Software without restriction, including without limitation
014: * the rights to use, copy, modify, merge, publish, distribute, sublicense,
015: * and/or sell copies of the Software, and to permit persons to whom the
016: * Software is furnished to do so, subject to the following conditions:
017: *
018: * The above copyright notice and this permission notice shall be included in
019: * all copies or substantial portions of the Software.
020: *
021: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
022: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
023: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
024: * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
025: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
026: * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
027: * DEALINGS IN THE SOFTWARE.
028: */
029: package net.sourceforge.groboutils.pmti.v1.autodoc.v1.xml;
030:
031: import java.io.Writer;
032: import java.io.StringWriter;
033: import java.io.PrintWriter;
034: import java.io.IOException;
035:
036: import net.sourceforge.groboutils.autodoc.v1.testserver.TestData;
037: import net.sourceforge.groboutils.autodoc.v1.testserver.TestInfo;
038:
039: import net.sourceforge.groboutils.pmti.v1.autodoc.v1.server.AbstractNewFileServer;
040:
041: import net.sourceforge.groboutils.pmti.v1.autodoc.v1.ITFTestData;
042:
043: import junit.framework.AssertionFailedError;
044:
045: import net.sourceforge.groboutils.util.xml.v1.XMLUtil;
046:
047: /**
048: * An interface which corresponds to a part of the framework that knows how
049: * to deal with the framework's <tt>TestData</tt>. It may directly deal with
050: * the data, or pass it off to a remote server.
051: *
052: * @author Matt Albrecht <a href="mailto:groboclown@users.sourceforge.net">groboclown@users.sourceforge.net</a>
053: * @since March 17, 2002
054: * @version $Date: 2003/02/10 22:51:57 $
055: */
056: public class XMLFileServer extends AbstractNewFileServer {
057: public XMLFileServer(String prefix) {
058: this (prefix, ".xml");
059: }
060:
061: public XMLFileServer(String prefix, String postfix) {
062: super (prefix, postfix, false);
063: }
064:
065: /**
066: * Write the BugTestResult with all the result's encountered BugTestLog
067: * events.
068: */
069: protected void writeTestData(TestData td, Writer w)
070: throws IOException {
071: if (td == null || !(td instanceof ITFTestData)) {
072: return;
073: }
074: ITFTestData itd = (ITFTestData) td;
075: TestInfo ti = td.getTestInfo();
076: String newline = System.getProperty("line.separator");
077: if (newline == null) {
078: newline = "\n";
079: }
080:
081: AssertionFailedError failures[] = itd.getFailures();
082: Throwable errors[] = itd.getErrors();
083: String logs[] = itd.getIssues();
084:
085: StringBuffer sb = new StringBuffer("<?xml version=\"1.0\"?>");
086: sb.append(newline).append("<testresult suite=\"");
087: escapeString(sb, ti.getSuite());
088: sb.append("\" test=\"");
089: escapeString(sb, ti.getMethod());
090: sb.append("\" tests=\"").append(itd.getTestCount())
091: .append("\" time=\"")
092: .
093: // These are just numbers, so we shouldn't need to escape them.
094: // Heh... famous last words...
095: append(((double) itd.getRunTime() / 1000.0)).append(
096: "\" failures=\"").append(
097: (failures == null ? 0 : failures.length))
098: .append("\" errors=\"").append(
099: (errors == null ? 0 : errors.length)).append(
100: "\" >").append(newline);
101: if (failures != null) {
102: for (int i = 0; i < failures.length; ++i) {
103: sb.append(" <failure message=\"");
104: escapeString(sb, failures[i].getMessage());
105: sb.append("\" >").append(newline);
106: escapeString(sb, toString(failures[i]));
107: sb.append(" </failure>").append(newline);
108: }
109: }
110: if (errors != null) {
111: for (int i = 0; i < errors.length; ++i) {
112: sb.append(" <error message=\"");
113: escapeString(sb, errors[i].getMessage());
114: sb.append("\" >").append(newline);
115: escapeString(sb, toString(errors[i]));
116: sb.append(" </error>").append(newline);
117: }
118: }
119: for (int i = 0; i < logs.length; ++i) {
120: sb.append(" <issue issueid=\"");
121: escapeString(sb, logs[i]);
122: sb.append("\" />").append(newline);
123: }
124: sb.append("</testresult>").append(newline);
125:
126: w.write(sb.toString());
127: w.flush();
128: }
129:
130: protected void escapeString(StringBuffer sb, String raw) {
131: XMLUtil.getInstance().utf2xml(raw, sb);
132: }
133:
134: protected String toString(Throwable t) {
135: StringWriter sw = new StringWriter();
136: t.printStackTrace(new PrintWriter(sw));
137: return sw.toString();
138: }
139: }
|