-
Notifications
You must be signed in to change notification settings - Fork 713
/
InteractiveFormatter.java
153 lines (118 loc) · 4.66 KB
/
InteractiveFormatter.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
package fitnesse.reporting;
import java.io.IOException;
import java.io.Writer;
import fitnesse.testrunner.TestsRunnerListener;
import fitnesse.testrunner.WikiTestPage;
import fitnesse.html.HtmlTag;
import fitnesse.html.HtmlUtil;
import fitnesse.html.RawHtml;
import fitnesse.testsystems.ExecutionResult;
import fitnesse.testsystems.TestSummary;
import fitnesse.wiki.PageCrawler;
import fitnesse.wiki.WikiPage;
public abstract class InteractiveFormatter extends BaseFormatter implements TestsRunnerListener {
private static final String TESTING_INTERRUPTED = "<strong>Testing was interrupted and results are incomplete.</strong> ";
private final Writer writer;
private boolean wasInterrupted = false;
private TestSummary assertionCounts = new TestSummary();
private String relativeName;
protected InteractiveFormatter(WikiPage page, Writer writer) {
super(page);
this.writer = writer;
}
protected void writeData(String output) throws IOException {
writer.write(output);
}
protected void updateSummaryDiv(String html) throws IOException {
writeData(HtmlUtil.makeReplaceElementScript("test-summary", html).html());
}
protected String getRelativeName() {
return relativeName;
}
protected String getRelativeName(WikiTestPage testPage) {
PageCrawler pageCrawler = getPage().getPageCrawler();
String relativeName = pageCrawler.getRelativeName(testPage.getSourcePage());
if ("".equals(relativeName)) {
relativeName = String.format("(%s)", testPage.getName());
}
return relativeName;
}
protected void addStopLink(String stopResponderId) throws IOException {
String link = "?responder=stoptest&id=" + stopResponderId;
HtmlTag status = HtmlUtil.makeSilentLink(link, new RawHtml("Stop Test"));
status.addAttribute("class", "stop");
writeData(HtmlUtil.makeReplaceElementScript("test-action", status.html()).html());
}
protected void removeStopTestLink() throws IOException {
HtmlTag script = HtmlUtil.makeReplaceElementScript("test-action", "");
writeData(script.html());
}
public TestSummary getAssertionCounts() {
return assertionCounts;
}
@Override
public int getErrorCount() {
return getAssertionCounts().getWrong() + getAssertionCounts().getExceptions();
}
public boolean wasInterrupted() {
return wasInterrupted;
}
@Override
public void errorOccurred(Throwable cause) {
wasInterrupted = true;
super.errorOccurred(cause);
}
@Override
public void testStarted(WikiTestPage testPage) throws IOException {
relativeName = getRelativeName(testPage);
}
public String testSummary() {
String summaryContent = wasInterrupted ? TESTING_INTERRUPTED : "";
summaryContent += makeSummaryContent();
HtmlTag script = HtmlUtil.makeReplaceElementScript("test-summary", summaryContent);
script.add("document.getElementById(\"test-summary\").className = \""
+ (wasInterrupted ? ExecutionResult.ERROR : ExecutionResult.getExecutionResult(relativeName, getAssertionCounts())) + "\";");
return script.html();
}
protected abstract String makeSummaryContent();
public void finishWritingOutput() throws IOException {
writeData(testSummary());
}
@Override
public void announceNumberTestsToRun(int testsToRun) {
}
@Override
public void unableToStartTestSystem(String testSystemName, Throwable cause) throws IOException {
writeData(String.format("<span class=\"error\">Unable to start test system '%s': %s</span>", testSystemName, cause.toString()));
}
public void setTrackingId(String stopResponderId) throws IOException {
addStopLink(stopResponderId);
}
protected void AddLogLink() throws IOException {
writeData(HtmlUtil.makeReplaceElementScript("test-action", executionStatus()).html());
}
protected void maybeMakeErrorNavigatorVisible() throws IOException {
if(exceptionsOrErrorsExist()){
writeData(initErroMetadata());
}
}
private boolean exceptionsOrErrorsExist() {
return (assertionCounts.getExceptions() + assertionCounts.getWrong()) > 0;
}
public String executionStatus() {
if (wasInterrupted)
return makeExecutionStatusLink(ExecutionStatus.ERROR);
// if (log.hasCapturedOutput())
// return makeExecutionStatusLink(ExecutionStatus.OUTPUT);
return makeExecutionStatusLink(ExecutionStatus.OK);
}
private String initErroMetadata() {
HtmlTag init = HtmlUtil.makeInitErrorMetadataScript();
return init.html();
}
public static String makeExecutionStatusLink(ExecutionStatus executionStatus) {
HtmlTag status = HtmlUtil.makeLink("?executionLog", executionStatus.getMessage());
status.addAttribute("class", executionStatus.getStyle());
return status.html();
}
}