1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.surefire.api.suite;
20
21 import java.io.ByteArrayOutputStream;
22 import java.io.PrintWriter;
23
24
25
26
27
28
29
30
31
32 public class RunResult {
33 private final int completedCount;
34
35 private final int errors;
36
37 private final int failures;
38
39 private final int skipped;
40
41 private final int flakes;
42
43 private final String failure;
44
45 private final boolean timeout;
46
47 public static final int SUCCESS = 0;
48
49 public static final int FAILURE = 255;
50
51 private static final int NO_TESTS = 254;
52
53 public static RunResult timeout(RunResult accumulatedAtTimeout) {
54 return errorCode(accumulatedAtTimeout, accumulatedAtTimeout.getFailure(), true);
55 }
56
57 public static RunResult failure(RunResult accumulatedAtTimeout, Exception cause) {
58 return errorCode(accumulatedAtTimeout, getStackTrace(cause), accumulatedAtTimeout.isTimeout());
59 }
60
61 private static RunResult errorCode(RunResult other, String failure, boolean timeout) {
62 return new RunResult(
63 other.getCompletedCount(),
64 other.getErrors(),
65 other.getFailures(),
66 other.getSkipped(),
67 failure,
68 timeout);
69 }
70
71 public RunResult(int completedCount, int errors, int failures, int skipped) {
72 this(completedCount, errors, failures, skipped, null, false);
73 }
74
75 public RunResult(int completedCount, int errors, int failures, int skipped, int flakes) {
76 this(completedCount, errors, failures, skipped, flakes, null, false);
77 }
78
79 public RunResult(int completedCount, int errors, int failures, int skipped, String failure, boolean timeout) {
80 this(completedCount, errors, failures, skipped, 0, failure, timeout);
81 }
82
83 public RunResult(
84 int completedCount, int errors, int failures, int skipped, int flakes, String failure, boolean timeout) {
85 this.completedCount = completedCount;
86 this.errors = errors;
87 this.failures = failures;
88 this.skipped = skipped;
89 this.flakes = flakes;
90 this.failure = failure;
91 this.timeout = timeout;
92 }
93
94 private static String getStackTrace(Exception e) {
95 if (e == null) {
96 return null;
97 }
98 ByteArrayOutputStream out = new ByteArrayOutputStream();
99 try (PrintWriter pw = new PrintWriter(out)) {
100 e.printStackTrace(pw);
101 }
102 return new String(out.toByteArray());
103 }
104
105 public int getCompletedCount() {
106 return completedCount;
107 }
108
109 public int getErrors() {
110 return errors;
111 }
112
113 public int getFlakes() {
114 return flakes;
115 }
116
117 public int getFailures() {
118 return failures;
119 }
120
121 public int getSkipped() {
122 return skipped;
123 }
124
125 public Integer getFailsafeCode()
126 {
127 if (completedCount == 0) {
128 return NO_TESTS;
129 }
130 if (!isErrorFree()) {
131 return FAILURE;
132 }
133 return null;
134 }
135
136
137 public boolean isErrorFree() {
138 return getFailures() == 0 && getErrors() == 0 && !isFailure();
139 }
140
141 public boolean isInternalError() {
142 return getFailures() == 0 && getErrors() == 0 && isFailure();
143 }
144
145
146 public boolean isFailureOrTimeout() {
147 return isTimeout() || isFailure();
148 }
149
150 public boolean isFailure() {
151 return failure != null;
152 }
153
154 public String getFailure() {
155 return failure;
156 }
157
158 public boolean isTimeout() {
159 return timeout;
160 }
161
162 public RunResult aggregate(RunResult other) {
163 String failureMessage = getFailure() != null ? getFailure() : other.getFailure();
164 boolean timeout = isTimeout() || other.isTimeout();
165 int completed = getCompletedCount() + other.getCompletedCount();
166 int fail = getFailures() + other.getFailures();
167 int ign = getSkipped() + other.getSkipped();
168 int err = getErrors() + other.getErrors();
169 int flakes = getFlakes() + other.getFlakes();
170 return new RunResult(completed, err, fail, ign, flakes, failureMessage, timeout);
171 }
172
173 public static RunResult noTestsRun() {
174 return new RunResult(0, 0, 0, 0);
175 }
176
177 public boolean isNoTestsRun() {
178 return completedCount == 0 && errors == 0 && skipped == 0 && flakes == 0 && failure == null && !timeout;
179 }
180
181 @Override
182 @SuppressWarnings("RedundantIfStatement")
183 public boolean equals(Object o) {
184 if (this == o) {
185 return true;
186 }
187 if (o == null || getClass() != o.getClass()) {
188 return false;
189 }
190
191 RunResult runResult = (RunResult) o;
192
193 if (completedCount != runResult.completedCount) {
194 return false;
195 }
196 if (errors != runResult.errors) {
197 return false;
198 }
199 if (failures != runResult.failures) {
200 return false;
201 }
202 if (skipped != runResult.skipped) {
203 return false;
204 }
205 if (timeout != runResult.timeout) {
206 return false;
207 }
208 if (failure != null ? !failure.equals(runResult.failure) : runResult.failure != null) {
209 return false;
210 }
211
212 return true;
213 }
214
215 @Override
216 public int hashCode() {
217 int result = completedCount;
218 result = 31 * result + errors;
219 result = 31 * result + failures;
220 result = 31 * result + skipped;
221 result = 31 * result + (failure != null ? failure.hashCode() : 0);
222 result = 31 * result + (timeout ? 1 : 0);
223 return result;
224 }
225 }