-
Notifications
You must be signed in to change notification settings - Fork 105
/
Copy pathTestNGTestResultBuildAction.java
234 lines (197 loc) · 6.95 KB
/
TestNGTestResultBuildAction.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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
package hudson.plugins.testng;
import hudson.FilePath;
import hudson.model.Action;
import hudson.model.Api;
import hudson.model.Run;
import hudson.plugins.testng.parser.ResultsParser;
import hudson.plugins.testng.results.MethodResult;
import hudson.plugins.testng.results.TestNGResult;
import hudson.tasks.junit.CaseResult;
import hudson.tasks.test.AbstractTestResultAction;
import java.io.IOException;
import java.io.PrintStream;
import java.io.Serial;
import java.io.Serializable;
import java.lang.ref.Reference;
import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import jenkins.tasks.SimpleBuildStep;
import org.kohsuke.stapler.StaplerRequest2;
import org.kohsuke.stapler.StaplerResponse2;
/**
* TestNG build action that exposes the results per build
*
* @author nullin
* @since v1.0
*/
public class TestNGTestResultBuildAction extends AbstractTestResultAction
implements Serializable, SimpleBuildStep.LastBuildAction {
private static final Logger LOGGER = Logger.getLogger(TestNGTestResultBuildAction.class.getName());
/** Unique identifier for this class. */
@Serial
private static final long serialVersionUID = 31415926L;
/**
* try and be good citizen. We don't want to hold this in memory. We also don't want to save
* this to build XML as we already save testng Reports
*/
private transient Reference<TestNGResult> testngResultRef;
/*
* Cache test counts to speed up loading of graphs
*/
protected Integer passCount; // null if uncomputed
protected int failCount;
protected int skipCount;
private final boolean escapeTestDescp;
private final boolean escapeExceptionMsg;
private final boolean showFailedBuilds;
public TestNGTestResultBuildAction(
TestNGResult testngResults, boolean escapeTestDescp, boolean escapeExceptionMsg, boolean showFailedBuilds) {
if (testngResults != null) {
this.testngResultRef = new WeakReference<TestNGResult>(testngResults);
// initialize the cached values when TestNGBuildAction is instantiated
count(testngResults);
}
this.escapeTestDescp = escapeTestDescp;
this.escapeExceptionMsg = escapeExceptionMsg;
this.showFailedBuilds = showFailedBuilds;
}
private void count(TestNGResult testngResults) {
this.passCount = testngResults.getPassCount();
this.failCount = testngResults.getFailCount();
this.skipCount = testngResults.getSkipCount();
}
private void countAndSave(TestNGResult testngResults) {
int savedPassCount = passCount != null ? passCount : -1;
int savedFailCount = failCount;
int savedSkipCount = skipCount;
count(testngResults);
if (passCount != savedPassCount || failCount != savedFailCount || skipCount != savedSkipCount) {
LOGGER.log(Level.FINE, "saving {0}", owner);
try {
owner.save();
} catch (IOException x) {
LOGGER.log(Level.WARNING, "failed to save " + owner, x);
}
}
}
private void countAsNeeded() {
if (passCount == null) {
countAndSave(getResult());
}
}
@Override
public TestNGResult getResult() {
return getResult(super.run);
}
public TestNGResult getResult(Run build) {
TestNGResult tr = testngResultRef != null ? testngResultRef.get() : null;
if (tr == null) {
tr = loadResults(build, null);
countAndSave(tr);
testngResultRef = new WeakReference<TestNGResult>(tr);
}
return tr;
}
static TestNGResult loadResults(Run<?, ?> owner, PrintStream logger) {
LOGGER.log(Level.FINE, "loading results for {0}", owner);
FilePath testngDir = Publisher.getTestNGReport(owner);
FilePath[] paths = null;
try {
paths = testngDir.list("testng-results*.xml");
} catch (Exception e) {
// do nothing
}
if (paths == null) {
TestNGResult tr = new TestNGResult();
tr.setRun(owner);
return tr;
}
ResultsParser parser = new ResultsParser(logger);
TestNGResult result = parser.parse(paths);
result.setRun(owner);
return result;
}
/** {@inheritDoc} */
@Override
public String getIconFileName() {
return PluginImpl.ICON_FILE_NAME;
}
@Override
public int getFailCount() {
countAsNeeded();
return failCount;
}
@Override
public int getSkipCount() {
countAsNeeded();
return skipCount;
}
@Override
public int getTotalCount() {
countAsNeeded();
return failCount + passCount + skipCount;
}
/** {@inheritDoc} */
@Override
public String getDisplayName() {
return PluginImpl.DISPLAY_NAME;
}
/** {@inheritDoc} */
@Override
public String getUrlName() {
return PluginImpl.URL;
}
public Object getDynamic(String token, StaplerRequest2 req, StaplerResponse2 rsp) {
return getResult().getDynamic(token, req, rsp);
}
@Override
public Api getApi() {
return new Api(getResult());
}
public List<CaseResult> getFailedTests() {
class HackyCaseResult extends CaseResult {
private MethodResult methodResult;
public HackyCaseResult(MethodResult methodResult) {
super(null, methodResult.getDisplayName(), methodResult.getErrorStackTrace());
this.methodResult = methodResult;
}
public Status getStatus() {
// We don't calculate age of results currently
// so, can't state if the failure is a regression or not
return Status.FAILED;
}
public String getClassName() {
return methodResult.getClassName();
}
public String getDisplayName() {
return methodResult.getDisplayName();
}
public String getErrorDetails() {
return methodResult.getErrorDetails();
}
@Override
public boolean equals(Object obj) {
return this == obj;
}
@Override
public int hashCode() {
return System.identityHashCode(this);
}
}
List<CaseResult> results = new ArrayList<CaseResult>(getFailCount());
for (MethodResult methodResult : getResult().getFailedTests()) {
results.add(new HackyCaseResult(methodResult));
}
return results;
}
@Override
public Collection<? extends Action> getProjectActions() {
return Collections.singleton(
new TestNGProjectAction(run.getParent(), escapeTestDescp, escapeExceptionMsg, showFailedBuilds));
}
}