-
Notifications
You must be signed in to change notification settings - Fork 713
/
SuiteHtmlFormatter.java
185 lines (143 loc) · 6.28 KB
/
SuiteHtmlFormatter.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
// Copyright (C) 2003-2009 by Object Mentor, Inc. All rights reserved.
// Released under the terms of the CPL Common Public License version 1.0.
package fitnesse.reporting;
import static fitnesse.testsystems.ExecutionResult.getExecutionResult;
import java.io.Closeable;
import java.io.IOException;
import java.io.Writer;
import fitnesse.testrunner.WikiTestPage;
import fitnesse.testsystems.*;
import fitnesse.util.TimeMeasurement;
import fitnesse.html.HtmlTag;
import fitnesse.html.HtmlUtil;
import fitnesse.wiki.PathParser;
import fitnesse.wiki.WikiPage;
public class SuiteHtmlFormatter extends InteractiveFormatter implements Closeable {
private static final String TEST_SUMMARIES_ID = "test-summaries";
private TestSummary pageCounts = new TestSummary();
private int currentTest = 0;
private final String testBasePathName;
private String testSystemName = null;
private int totalTests = 1;
private TimeMeasurement latestTestTime;
private String testSummariesId = TEST_SUMMARIES_ID;
private TimeMeasurement totalTimeMeasurement;
public SuiteHtmlFormatter(WikiPage page, Writer writer) {
super(page, writer);
totalTimeMeasurement = new TimeMeasurement().start();
testBasePathName = PathParser.render(page.getPageCrawler().getFullPath());
}
@Override
public void announceNumberTestsToRun(int testsToRun) {
super.announceNumberTestsToRun(testsToRun);
totalTests = (testsToRun != 0) ? testsToRun : 1;
}
public void announceStartNewTest(String relativeName, String fullPathName) throws IOException {
currentTest++;
updateSummaryDiv(getProgressHtml(relativeName));
maybeWriteTestSystem();
writeTestOutputDiv(relativeName, fullPathName);
}
private void writeTestOutputDiv(String relativeName, String fullPathName) throws IOException {
if (!testBasePathName.equals(fullPathName)) {
HtmlTag pageNameBar = HtmlUtil.makeDivTag("test_output_name");
HtmlTag anchor = HtmlUtil.makeLink(fullPathName, relativeName);
anchor.addAttribute("id", relativeName + currentTest);
anchor.addAttribute("class", "test_name");
HtmlTag title = new HtmlTag("h3", anchor);
HtmlTag topLink = HtmlUtil.makeLink("#" + TEST_SUMMARIES_ID, "Top");
topLink.addAttribute("class", "top_of_page");
pageNameBar.add(title);
pageNameBar.add(topLink);
writeData(pageNameBar.html());
}
writeData("<div class=\"alternating_block\">");
}
private void maybeWriteTestSystem() throws IOException {
if (testSystemName != null) {
HtmlTag systemTitle = new HtmlTag("h2", String.format("Test System: %s", testSystemName));
writeData(systemTitle.html());
// once we write it out we don't need it any more
testSystemName = null;
}
}
@Override
public void testStarted(WikiTestPage testPage) throws IOException {
latestTestTime = new TimeMeasurement().start();
super.testStarted(testPage);
String fullPathName = testPage.getFullPath();
announceStartNewTest(getRelativeName(), fullPathName);
}
private String getProgressHtml(String relativeName) {
float percentFinished = (currentTest - 1) * 100f / totalTests;
String text = "Running tests ... (" + currentTest + "/" + totalTests + ")";
text = text.replaceAll(" ", " ");
HtmlTag progressDiv = new HtmlTag("div", text);
// need some results before we can check pageCounts for results
ExecutionResult cssClass = (currentTest == 1) ? ExecutionResult.PASS : getExecutionResult(relativeName, this.pageCounts);
progressDiv.addAttribute("id", "progressBar");
progressDiv.addAttribute("class", cssClass.toString());
progressDiv.addAttribute("style", "width:" + percentFinished + "%");
return progressDiv.html();
}
public void processTestResults(String relativeName, TestSummary testSummary) throws IOException {
finishOutputForTest();
getAssertionCounts().add(testSummary);
HtmlTag tag = new HtmlTag("li");
tag.add(HtmlUtil.makeSpanTag("results " + getExecutionResult(relativeName, testSummary), testSummary.toString()));
HtmlTag link = HtmlUtil.makeLink("#" + relativeName + currentTest, relativeName);
link.addAttribute("class", "link");
tag.add(link);
if (latestTestTime != null) {
tag.add(HtmlUtil.makeSpanTag("", String.format("(%.03f seconds)", latestTestTime.elapsedSeconds())));
}
pageCounts.tallyPageCounts(getExecutionResult(relativeName, testSummary, wasInterrupted()));
HtmlTag insertScript = HtmlUtil.makeAppendElementScript(testSummariesId, tag.html());
writeData(insertScript.html());
}
private void finishOutputForTest() throws IOException {
writeData("</div>" + HtmlTag.endl);
}
@Override
public void close() throws IOException {
// Todo: why assign it to this variable, looks inconsistent.
latestTestTime = totalTimeMeasurement.stop();
removeStopTestLink();
AddLogLink();
maybeMakeErrorNavigatorVisible();
finishWritingOutput();
}
@Override
public void testOutputChunk(String output) throws IOException {
writeData(output);
}
@Override
public void testComplete(WikiTestPage testPage, TestSummary testSummary) throws IOException {
latestTestTime.stop();
super.testComplete(testPage, testSummary);
processTestResults(getRelativeName(testPage), testSummary);
latestTestTime = null;
}
@Override
public void errorOccurred(Throwable cause) {
latestTestTime = null;
super.errorOccurred(cause);
}
@Override
public void testSystemStarted(TestSystem testSystem) throws IOException {
testSystemName = testSystem.getName();
testSummariesId = "test-system-" + testSystemName;
String tag = String.format("<h3>%s</h3>\n<ul id=\"%s\"></ul>", testSystemName, testSummariesId);
HtmlTag insertScript = HtmlUtil.makeAppendElementScript(TEST_SUMMARIES_ID, tag);
writeData(insertScript.html());
}
protected String makeSummaryContent() {
String summaryContent = "<strong>Test Pages:</strong> " + pageCounts.toString() + " ";
if (latestTestTime != null) {
summaryContent += String.format("<strong>Assertions:</strong> %s (%.03f seconds)", getAssertionCounts(), latestTestTime.elapsedSeconds());
} else {
summaryContent += String.format("<strong>Assertions:</strong> %s ", getAssertionCounts());
}
return summaryContent;
}
}