forked from kutzi/test-stability-plugin
-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathCircularStabilityHistory.java
232 lines (182 loc) · 5.57 KB
/
CircularStabilityHistory.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
/*
* The MIT License
*
* Copyright (c) 2013, eSailors IT Solutions GmbH
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package de.esailors.jenkins.teststability;
import jenkins.model.Jenkins;
import com.thoughtworks.xstream.converters.Converter;
import com.thoughtworks.xstream.converters.MarshallingContext;
import com.thoughtworks.xstream.converters.UnmarshallingContext;
import com.thoughtworks.xstream.io.HierarchicalStreamReader;
import com.thoughtworks.xstream.io.HierarchicalStreamWriter;
import de.esailors.jenkins.teststability.StabilityTestData.Result;
import javax.annotation.Nullable;
/**
* Circular history of test results.
* <p>
* Old records are dropped when <code>maxSize</code> is exceeded.
*
* @author ckutz
*/
public class CircularStabilityHistory {
private Result[] data;
private int head;
private int tail;
// number of elements in queue
private int size = 0;
private CircularStabilityHistory() {}
public CircularStabilityHistory(int maxSize) {
data = new Result[maxSize];
head = 0;
tail = 0;
}
public boolean add(Result value) {
data[tail] = value;
tail++;
if (tail == data.length) {
tail = 0;
}
if (size == data.length) {
head = (head + 1) % data.length;
} else {
size++;
}
return true;
}
public Result[] getData() {
Result[] copy = new Result[size];
for (int i = 0; i < size; i++) {
copy[i] = data[(head + i) % data.length];
}
return copy;
}
public boolean isEmpty() {
return data.length == 0;
}
public int getMaxSize() {
return this.data.length;
}
static {
Jenkins.XSTREAM2.registerConverter(new ConverterImpl());
}
public static class ConverterImpl implements Converter {
@Override
public boolean canConvert(@SuppressWarnings("rawtypes") @Nullable Class type) {
return type != null && CircularStabilityHistory.class.isAssignableFrom(type);
}
@Override
public void marshal(Object source, HierarchicalStreamWriter writer,
MarshallingContext context) {
CircularStabilityHistory b = (CircularStabilityHistory) source;
writer.startNode("head");
writer.setValue(Integer.toString(b.head));
writer.endNode();
writer.startNode("tail");
writer.setValue(Integer.toString(b.tail));
writer.endNode();
writer.startNode("size");
writer.setValue(Integer.toString(b.size));
writer.endNode();
writer.startNode("data");
writer.setValue(dataToString(b.data));
writer.endNode();
}
private String dataToString(Result[] data) {
StringBuilder buf = new StringBuilder();
for (Result d : data) {
if(d == null) {
buf.append(",");
continue;
}
if (d.passed) {
buf.append(d.buildNumber).append(";").append("1,");
} else {
buf.append(d.buildNumber).append(";").append("0,");
}
}
if (buf.length() > 0) {
buf.deleteCharAt(buf.length() - 1);
}
return buf.toString();
}
@Override
public CircularStabilityHistory unmarshal(HierarchicalStreamReader r,
UnmarshallingContext context) {
r.moveDown();
int head = Integer.parseInt(r.getValue());
r.moveUp();
r.moveDown();
int tail = Integer.parseInt(r.getValue());
r.moveUp();
r.moveDown();
int size = Integer.parseInt(r.getValue());
r.moveUp();
r.moveDown();
String data = r.getValue();
r.moveUp();
CircularStabilityHistory buf = new CircularStabilityHistory();
Result[] b = stringToData(data);
buf.data = b;
buf.head = head;
buf.size = size;
buf.tail = tail;
return buf;
}
private Result[] stringToData(String s) {
String[] split = s.split(",", -1);
Result d[] = new Result[split.length];
int i = 0;
for(String testResult : split) {
if (testResult.isEmpty()) {
i++;
continue;
}
String[] split2 = testResult.split(";");
int buildNumber = Integer.parseInt(split2[0]);
// TODO: check that '0' is the only other allowed value:
boolean buildResult = "1".equals(split2[1]) ? true : false;
d[i] = new Result(buildNumber, buildResult);
i++;
}
return d;
}
}
public void addAll(Result[] results) {
for (Result b : results) {
add(b);
}
}
public void add(int buildNumber, boolean passed) {
add(new Result(buildNumber, passed));
}
public boolean isAllPassed() {
if (size == 0) {
return true;
}
for (Result r : data) {
if (r != null && !r.passed) {
return false;
}
}
return true;
}
}