-
Notifications
You must be signed in to change notification settings - Fork 80
/
RobotTestObject.java
297 lines (250 loc) · 8.33 KB
/
RobotTestObject.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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
/*
* Copyright 2008-2014 Nokia Solutions and Networks Oy
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package hudson.plugins.robot.model;
import hudson.model.AbstractBuild;
import hudson.model.AbstractModelObject;
import hudson.model.Run;
import hudson.plugins.robot.RobotBuildAction;
import hudson.plugins.robot.RobotConfig;
import hudson.plugins.robot.graph.RobotGraphHelper;
import hudson.util.ChartUtil;
import hudson.util.Graph;
import java.io.IOException;
import java.io.Serializable;
import java.util.Calendar;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.time.DurationFormatUtils;
import org.kohsuke.stapler.StaplerRequest;
import org.kohsuke.stapler.StaplerResponse;
public abstract class RobotTestObject extends AbstractModelObject implements Serializable{
private static final long serialVersionUID = -3191755290679194469L;
private transient RobotBuildAction parentAction;
public RobotBuildAction getParentAction() {
return parentAction;
}
public void setParentAction(RobotBuildAction parentAction) {
this.parentAction = parentAction;
}
public abstract String getName();
public abstract String getDescription();
public abstract RobotTestObject getParent();
private String duplicateSafeName;
protected transient long duration;
private String logFile;
private String reportFile;
private String id;
public String getId() {
return id != null ? id : "";
}
public void setId(String id) {
this.id = id;
}
public String getLogFile() {
return logFile;
}
public void setLogFile(String logFileName) {
this.logFile = logFileName;
}
public String getReportFile() {
return reportFile;
}
public void setReportFile(String reportFileName) {
this.reportFile = reportFileName;
}
public boolean getHasLog() {
return this.logFile != null && !"".equals(this.logFile);
}
public boolean getHasReport() {
return this.reportFile != null && !"".equals(this.reportFile);
}
/**
* Generates the full packagename
* @param thisObject Robot test object
* @return package name
*/
public String getRelativePackageName(RobotTestObject thisObject) {
/*
`name` attribute might be missing from suites, see more: https://issues.jenkins.io/browse/JENKINS-69807
*/
String name = getName();
if (name == null){
name = "";
}
StringBuilder sb = new StringBuilder(name);
String parentPackage = getRelativeParent(thisObject);
if (! "".equals(parentPackage)) {
sb.insert(0, parentPackage);
}
return sb.toString();
}
public String getRelativeParent(RobotTestObject thisObject) {
StringBuilder sb = new StringBuilder();
RobotTestObject parent = getParent();
if(parent != null && !parent.equals(thisObject)){
String parentPackage = parent.getRelativePackageName(thisObject);
if(StringUtils.isNotBlank(parentPackage)){
sb.insert(0, ".");
sb.insert(0, parentPackage);
}
}
return sb.toString();
}
/**
* Get path in tree relative to given TestObject
* @param thisObject The wanted testobject
* @return Path to TestObject
*/
public String getRelativeId(RobotTestObject thisObject){
StringBuilder sb = new StringBuilder(urlEncode(getDuplicateSafeName()));
RobotTestObject parent = getParent();
if(parent != null && !parent.equals(thisObject)){
String parentId = parent.getRelativeId(thisObject);
if(StringUtils.isNotBlank(parentId)){
sb.insert(0, "/");
sb.insert(0, parentId);
}
}
return sb.toString();
}
public String urlEncode(String name) {
return hudson.Util.rawEncode(name);
}
/**
* Return the build that this result belongs to.
* @return Build object. Null if no build.
*/
public Run<?,?> getOwner(){
return getParentAction() == null ? null : getParentAction().getOwner();
}
/**
* Get the corresponding result object from a given build
* @param build The Jenkins build
* @return TestObject for given build
*/
public RobotTestObject getResultInBuild(AbstractBuild<?,?> build) {
parentAction = build.getAction(RobotBuildAction.class);
String id = getRelativeId(getParentAction().getResult());
return (parentAction == null) ? null : parentAction.findObjectById(id);
}
public String getDuplicateSafeName() {
if (duplicateSafeName != null)
return duplicateSafeName;
return getName();
}
public void setDuplicateSafeName(String name) {
duplicateSafeName = name;
}
/**
* The name format used in case hashmap was changed in commit
* 59c8eec3e16f28baf1288848fccbac228bbe4748, July 2013. This method allows
* accessing the tests from old saved results.
* TODO: Remove this after a year or so?
* @return Name in old format
*/
protected String getOldFormatName() {
return getName().replaceAll("[/\\ :;#?]", "_");
}
/**
* Figure out if there's been changes since last request.
* @param req StaplerRequest
* @param rsp StaplerResponse
* @return true if modified, false otherwise
* @throws IOException thrown exception
*/
protected boolean isNeedToGenerate(StaplerRequest req, StaplerResponse rsp)
throws IOException {
if (ChartUtil.awtProblemCause != null) {
rsp.sendRedirect2(req.getContextPath() + "/images/headless.png");
return false;
}
Calendar t = getOwner().getTimestamp();
if (req.checkIfModified(t, rsp))
return false;
return true;
}
/**
* Get duration of this testobject run
* @return Duration of this testobject run
*/
public long getDuration() {
return duration;
}
/**
* Wrapper for calling formatting from jelly
* @return Duration in human readable form
*/
public String getHumanReadableDuration(){
return DurationFormatUtils.formatDurationHMS(getDuration());
}
/**
* Get difference in of duration to given test object
* @param comparable another testobject which to compare to
* @return time difference in human readable format
*/
public String getDurationDiff(RobotTestObject comparable){
long duration = getDuration();
long diff = duration;
if (comparable != null)
diff = duration - comparable.getDuration();
if (diff == 0) return "\u00B10";
else if (diff > 0) return "+" + DurationFormatUtils.formatDurationHMS(Math.abs(diff));
else return "-" + DurationFormatUtils.formatDurationHMS(Math.abs(diff));
}
public abstract RobotTestObject getPreviousResult();
public abstract int getFailed();
public abstract int getPassed();
public abstract int getSkipped();
public abstract long getCriticalPassed();
public abstract long getCriticalFailed();
/**
* Return robot trend graph in the request.
* @param req StaplerRequest
* @param rsp StaplerResponse
* @throws IOException thrown exception
*/
public void doGraph(StaplerRequest req, StaplerResponse rsp)
throws IOException {
if(!isNeedToGenerate(req, rsp)) return;
String label = parentAction.getxAxisLabel();
String labelFormat = StringUtils.isBlank(label) ? RobotConfig.getInstance().getXAxisLabelFormat() : label;
Graph g = RobotGraphHelper.createTestResultsGraphForTestObject(this,
Boolean.parseBoolean(req.getParameter("zoomSignificant")),
false, Boolean.parseBoolean(req.getParameter("hd")),
Boolean.parseBoolean(req.getParameter("failedOnly")),
Boolean.parseBoolean(req.getParameter("criticalOnly")),
labelFormat,
Integer.parseInt(req.getParameter("maxBuildsToShow")));
g.doPng(req, rsp);
}
/**
* Return duration graph of the case in the request.
* @param req StaplerRequest
* @param rsp StaplerResponse
* @throws IOException thrown exception
*/
public void doDurationGraph(StaplerRequest req, StaplerResponse rsp)
throws IOException {
if(!isNeedToGenerate(req, rsp)) return;
String label = parentAction.getxAxisLabel();
String labelFormat = StringUtils.isBlank(label) ? RobotConfig.getInstance().getXAxisLabelFormat() : label;
Graph g = RobotGraphHelper.createDurationGraphForTestObject(this,
req.hasParameter("hd"),
Integer.parseInt(req.getParameter("maxBuildsToShow")),
labelFormat,
req.hasParameter("preview"));
g.doPng(req, rsp);
}
}