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.responders.run;
004:
005: import fitnesse.wiki.*;
006: import fitnesse.components.*;
007: import fitnesse.responders.ErrorResponder;
008: import fitnesse.html.*;
009: import java.util.*;
010: import java.text.SimpleDateFormat;
011:
012: public class ExecutionLog {
013: public static final String ErrorLogName = "ErrorLogs";
014: private PageCrawler crawler;
015:
016: public static SimpleDateFormat makeDateFormat() {
017: //SimpleDateFormat is not thread safe, so we need to create each instance independently.
018: return new SimpleDateFormat(
019: "h:mm:ss a (z) 'on' EEEE, MMMM d, yyyy");
020: }
021:
022: private String errorLogPageName;
023: private WikiPagePath errorLogPagePath;
024: private WikiPage root;
025:
026: private CommandRunner runner;
027: private List reasons = new LinkedList();
028: private List exceptions = new LinkedList();
029:
030: public ExecutionLog(WikiPage testPage, CommandRunner client)
031: throws Exception {
032: runner = client;
033:
034: crawler = testPage.getPageCrawler();
035: crawler.setDeadEndStrategy(new VirtualEnabledPageCrawler());
036: root = crawler.getRoot(testPage);
037: errorLogPagePath = crawler.getFullPath(testPage)
038: .addNameToFront(ErrorLogName);
039: errorLogPageName = PathParser.render(errorLogPagePath);
040: }
041:
042: public void addException(Exception e) {
043: exceptions.add(e);
044: }
045:
046: public void addReason(String reason) {
047: if (!reasons.contains(reason))
048: reasons.add(reason);
049: }
050:
051: public void publish() throws Exception {
052: String content = buildLogContent();
053:
054: WikiPage errorLogPage = crawler.addPage(root, errorLogPagePath);
055: PageData data = errorLogPage.getData();
056: data.setContent(content);
057: errorLogPage.commit(data);
058: }
059:
060: public String buildLogContent() {
061: StringBuffer buffer = new StringBuffer();
062: addEntry(buffer, "Date", makeDateFormat().format(new Date()));
063: addEntry(buffer, "Command", runner.getCommand());
064: addEntry(buffer, "Exit code", String.valueOf(runner
065: .getExitCode()));
066: addEntry(buffer, "Time elapsed", (double) runner
067: .getExecutionTime()
068: / 1000.0 + " seconds");
069: if (runner.wroteToOutputStream())
070: addOutputBlock(buffer);
071: if (runner.wroteToErrorStream())
072: addErrorBlock(buffer);
073: if (runner.hasExceptions() || exceptions.size() > 0)
074: addExceptionBlock(buffer);
075: String content = buffer.toString();
076: return content;
077: }
078:
079: private void addEntry(StringBuffer buffer, String key, String value) {
080: buffer.append("|'''").append(key).append(": '''|").append("!-")
081: .append(value).append("-!").append("|\n");
082: }
083:
084: private void addOutputBlock(StringBuffer buffer) {
085: buffer.append("----");
086: buffer.append("'''Standard Output:'''").append("\n");
087: buffer.append("{{{").append(runner.getOutput()).append("}}}");
088: }
089:
090: private void addErrorBlock(StringBuffer buffer) {
091: buffer.append("----");
092: buffer.append("'''Standard Error:'''").append("\n");
093: buffer.append("{{{").append(runner.getError()).append("}}}");
094: }
095:
096: private void addExceptionBlock(StringBuffer buffer) {
097: exceptions.addAll(runner.getExceptions());
098: buffer.append("----");
099: buffer.append("'''Internal Exception");
100: if (exceptions.size() > 1)
101: buffer.append("s");
102: buffer.append(":'''").append("\n");
103: for (Iterator iterator = exceptions.iterator(); iterator
104: .hasNext();) {
105: Exception exception = (Exception) iterator.next();
106: buffer.append("{{{ ").append(
107: ErrorResponder.makeExceptionString(exception))
108: .append("}}}");
109: }
110: }
111:
112: public int exceptionCount() {
113: return exceptions.size();
114: }
115:
116: public String getErrorLogPageName() {
117: return errorLogPageName;
118: }
119:
120: public boolean hasCapturedOutput() {
121: return runner.wroteToErrorStream()
122: || runner.wroteToOutputStream();
123: }
124:
125: public String executionStatusHtml() throws Exception {
126: String linkHref = getErrorLogPageName();
127: return executionStatusHtml(linkHref, "");
128: }
129:
130: public String executionStatusHtml(String linkHref,
131: String imageUrlBase) throws Exception {
132: ExecutionStatus executionStatus;
133:
134: if (exceptionCount() > 0)
135: executionStatus = ExecutionStatus.ERROR;
136: else if (hasCapturedOutput())
137: executionStatus = ExecutionStatus.OUTPUT;
138: else
139: executionStatus = ExecutionStatus.OK;
140:
141: HtmlTag status = new HtmlTag("div");
142: status.addAttribute("id", "execution-status");
143: HtmlTag image = new HtmlTag("img");
144: image.addAttribute("src", imageUrlBase
145: + "/files/images/executionStatus/"
146: + executionStatus.getIconFilename());
147: status.add(HtmlUtil.makeLink(linkHref, image.html()));
148: status.add(HtmlUtil.BR);
149: status.add(HtmlUtil.makeLink(linkHref, executionStatus
150: .getMessage()));
151: return status.html();
152: }
153: }
|