001: /******************************************************************************
002: * Copyright (C) Lars Ivar Almli. All rights reserved. *
003: * ---------------------------------------------------------------------------*
004: * This file is part of MActor. *
005: * *
006: * MActor is free software; you can redistribute it and/or modify *
007: * it under the terms of the GNU General Public License as published by *
008: * the Free Software Foundation; either version 2 of the License, or *
009: * (at your option) any later version. *
010: * *
011: * MActor is distributed in the hope that it will be useful, *
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of *
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
014: * GNU General Public License for more details. *
015: * *
016: * You should have received a copy of the GNU General Public License *
017: * along with MActor; if not, write to the Free Software *
018: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA *
019: ******************************************************************************/package org.mactor.ui;
020:
021: import java.io.File;
022: import java.io.FileOutputStream;
023: import java.io.IOException;
024: import java.text.SimpleDateFormat;
025: import java.util.Calendar;
026: import java.util.List;
027: import java.util.Map.Entry;
028: import org.dom4j.Document;
029: import org.dom4j.DocumentFactory;
030: import org.dom4j.Element;
031: import org.dom4j.io.OutputFormat;
032: import org.dom4j.io.XMLWriter;
033: import org.mactor.framework.MactorException;
034: import org.mactor.framework.TestSuiteSummary;
035: import org.mactor.framework.TestSummary;
036: import org.mactor.framework.TestSummary.MessageInfo;
037:
038: public class ReportUtil {
039: private static final SimpleDateFormat sdf = new SimpleDateFormat(
040: "yyyy-MM-dd'T'HH:mm:ss.SSSZ");
041:
042: public static void writeReportToFile(File file,
043: TestSuiteSummary summary, List<TestSummary> result)
044: throws MactorException {
045: Document doc = buildReport(summary, result);
046: write(file, doc);
047: }
048:
049: private static void write(File file, Document doc)
050: throws MactorException {
051: try {
052: OutputFormat format = OutputFormat.createPrettyPrint();
053: FileOutputStream fos = new FileOutputStream(file);
054: XMLWriter writer = new XMLWriter(fos, format);
055: writer.write(doc);
056: writer.flush();
057: fos.close();
058: } catch (IOException ioe) {
059: throw new MactorException("Failed to write the file '"
060: + file.getAbsolutePath() + "'. Error:"
061: + ioe.getMessage(), ioe);
062: }
063: }
064:
065: private static Document buildReport(TestSuiteSummary summary,
066: List<TestSummary> result) {
067: Document doc = DocumentFactory.getInstance().createDocument();
068: Element rootEl = doc.addElement("report");
069: Element summaryEl = rootEl.addElement("summary");
070: addEl(summaryEl, "start_time", summary.getTestStart());
071: addEl(summaryEl, "complete_time", summary.getTestStart());
072: addEl(summaryEl, "success_count", summary.getSuccessCount());
073: addEl(summaryEl, "failed_count", summary.getFailedCount());
074: addEl(summaryEl, "avg_success_time_ms", summary
075: .getAverageSuccessCompleteTimeMs());
076: addEl(summaryEl, "avg_failed_time_ms", summary
077: .getAverageFailedCompleteTimeMs());
078: addEl(summaryEl, "message_sent_count", summary
079: .getMessageSentCount());
080: addEl(summaryEl, "message_received_count", summary
081: .getMessageReceivedCount());
082:
083: Element testsEl = rootEl.addElement("tests");
084: for (TestSummary test : result) {
085: Element tEl = testsEl.addElement("test");
086: addEl(tEl, "start_time", test.getTestStartTime());
087: addEl(tEl, "complete_time", test.getTestCompleteTime());
088: addEl(tEl, "succesful", test.isSuccess());
089: if (!test.isSuccess())
090: addEl(tEl, "failure_message", test.getFailedMessage());
091: Element valsEl = tEl.addElement("context_values");
092: for (Entry<String, String> val : test.getContextValues()
093: .entrySet()) {
094: Element vEl = valsEl.addElement("context_value");
095: vEl.addAttribute("name", val.getKey());
096: vEl.setText(val.getValue());
097: }
098: Element messagesEl = tEl.addElement("messages");
099: for (MessageInfo mi : test.getMessageHistory()) {
100: Element mEl = messagesEl.addElement("message");
101: addEl(mEl, "time", mi.getCreatedTime());
102: addEl(mEl, "incoming", mi.isIncoming() + "");
103: addEl(mEl, "response", mi.isResponse() + "");
104: addEl(mEl, "archive_path", mi.getArchivePath());
105: if (mi.getChannel() != null)
106: addEl(mEl, "channel", mi.getChannel());
107: }
108: }
109: return doc;
110: }
111:
112: private static void addEl(Element el, String name, Object value) {
113: String formatted = null;
114: if (value == null)
115: formatted = "";
116: else if (value instanceof Calendar)
117: formatted = sdf.format(((Calendar) value).getTime());
118: else
119: formatted = value.toString();
120: Element newEl = el.addElement(name);
121: newEl.setText(formatted);
122: }
123: }
|