001 /******************************************************************************
002 * Copyright (C) MActor Developers. 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 ******************************************************************************/
020 package org.mactor.ui;
021
022 import java.io.File;
023 import java.io.FileOutputStream;
024 import java.io.IOException;
025 import java.text.SimpleDateFormat;
026 import java.util.Calendar;
027 import java.util.List;
028 import java.util.Map.Entry;
029
030 import org.dom4j.Document;
031 import org.dom4j.DocumentFactory;
032 import org.dom4j.Element;
033 import org.dom4j.io.OutputFormat;
034 import org.dom4j.io.XMLWriter;
035 import org.mactor.framework.MactorException;
036 import org.mactor.framework.TestSuiteSummary_old;
037 import org.mactor.framework.TestSummary_old;
038 import org.mactor.framework.TestSummary_old.MessageInfo;
039
040 public class ReportUtil {
041 private static final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
042 public static void writeReportToFile(File file, TestSuiteSummary_old summary, List<TestSummary_old> result) throws MactorException {
043 Document doc = buildReport(summary, result);
044 write(file, doc);
045 }
046 private static void write(File file, Document doc) throws MactorException {
047 try {
048 OutputFormat format = OutputFormat.createPrettyPrint();
049 FileOutputStream fos = new FileOutputStream(file);
050 XMLWriter writer = new XMLWriter(fos, format);
051 writer.write(doc);
052 writer.flush();
053 fos.close();
054 } catch (IOException ioe) {
055 throw new MactorException("Failed to write the file '" + file.getAbsolutePath() + "'. Error:" + ioe.getMessage(), ioe);
056 }
057 }
058 private static Document buildReport(TestSuiteSummary_old summary, List<TestSummary_old> result) {
059 Document doc = DocumentFactory.getInstance().createDocument();
060 Element rootEl = doc.addElement("report");
061 Element summaryEl = rootEl.addElement("summary");
062 addEl(summaryEl, "start_time", summary.getTestStart());
063 addEl(summaryEl, "complete_time", summary.getTestStart());
064 addEl(summaryEl, "success_count", summary.getSuccessCount());
065 addEl(summaryEl, "failed_count", summary.getFailedCount());
066 addEl(summaryEl, "avg_success_time_ms", summary.getAverageSuccessCompleteTimeMs());
067 addEl(summaryEl, "avg_failed_time_ms", summary.getAverageFailedCompleteTimeMs());
068 addEl(summaryEl, "message_sent_count", summary.getMessageSentCount());
069 addEl(summaryEl, "message_received_count", summary.getMessageReceivedCount());
070 Element testsEl = rootEl.addElement("tests");
071 for (TestSummary_old test : result) {
072 Element tEl = testsEl.addElement("test");
073 addEl(tEl, "start_time", test.getTestStartTime());
074 addEl(tEl, "complete_time", test.getTestCompleteTime());
075 addEl(tEl, "succesful", test.isSuccess());
076 if (!test.isSuccess())
077 addEl(tEl, "failure_message", test.getFailedMessage());
078 Element valsEl = tEl.addElement("context_values");
079 for (Entry<String, String> val : test.getContextValues().entrySet()) {
080 Element vEl = valsEl.addElement("context_value");
081 vEl.addAttribute("name", val.getKey());
082 vEl.setText(val.getValue());
083 }
084 Element messagesEl = tEl.addElement("messages");
085 for (MessageInfo mi : test.getMessageHistory()) {
086 Element mEl = messagesEl.addElement("message");
087 addEl(mEl, "time", mi.getCreatedTime());
088 addEl(mEl, "incoming", mi.isIncoming() + "");
089 addEl(mEl, "response", mi.isResponse() + "");
090 addEl(mEl, "archive_path", mi.getArchivePath());
091 if (mi.getChannel() != null)
092 addEl(mEl, "channel", mi.getChannel());
093 }
094 }
095 return doc;
096 }
097 private static void addEl(Element el, String name, Object value) {
098 String formatted = null;
099 if (value == null)
100 formatted = "";
101 else if (value instanceof Calendar)
102 formatted = sdf.format(((Calendar) value).getTime());
103 else
104 formatted = value.toString();
105 Element newEl = el.addElement(name);
106 newEl.setText(formatted);
107 }
108 }