1
1
package cucumber .runtime ;
2
2
3
- import cucumber .api .Pending ;
4
- import cucumber .api .Result ;
5
3
import cucumber .api .StepDefinitionReporter ;
6
4
import cucumber .api .SummaryPrinter ;
7
- import cucumber .api .event .EventHandler ;
8
- import cucumber .api .event .TestCaseFinished ;
9
5
import cucumber .api .event .TestRunFinished ;
10
- import cucumber .api .event .TestStepFinished ;
11
- import cucumber .api .formatter .Formatter ;
12
6
import cucumber .runner .EventBus ;
13
7
import cucumber .runner .Runner ;
14
8
import cucumber .runner .TimeService ;
22
16
import java .io .IOException ;
23
17
import java .io .PrintStream ;
24
18
import java .util .ArrayList ;
25
- import java .util .Arrays ;
26
19
import java .util .Collection ;
27
20
import java .util .List ;
28
21
import java .util .Map ;
33
26
*/
34
27
public class Runtime {
35
28
36
- private static final String [] ASSUMPTION_VIOLATED_EXCEPTIONS = {
37
- "org.junit.AssumptionViolatedException" ,
38
- "org.junit.internal.AssumptionViolatedException"
39
- };
40
-
41
- static {
42
- Arrays .sort (ASSUMPTION_VIOLATED_EXCEPTIONS );
43
- }
44
-
45
- private static final byte ERRORS = 0x1 ;
46
-
47
- private final Stats stats ;
48
- UndefinedStepsTracker undefinedStepsTracker = new UndefinedStepsTracker (); // package private to be avaiable for tests.
29
+ final Stats stats ; // package private to be avaiable for tests.
30
+ private final UndefinedStepsTracker undefinedStepsTracker = new UndefinedStepsTracker ();
49
31
50
32
private final RuntimeOptions runtimeOptions ;
51
33
52
- private final List <Throwable > errors = new ArrayList <Throwable >();
53
34
private final ResourceLoader resourceLoader ;
54
35
private final ClassLoader classLoader ;
55
36
private final Runner runner ;
56
37
private final List <PicklePredicate > filters ;
57
38
private final EventBus bus ;
58
39
private final Compiler compiler = new Compiler ();
59
- private final EventHandler <TestStepFinished > stepFinishedHandler = new EventHandler <TestStepFinished >() {
60
- @ Override
61
- public void receive (TestStepFinished event ) {
62
- Result result = event .result ;
63
- if (result .getError () != null ) {
64
- addError (result .getError ());
65
- }
66
- if (event .testStep .isHook ()) {
67
- addHookToCounterAndResult (result );
68
- } else {
69
- addStepToCounterAndResult (result );
70
- }
71
- }
72
- };
73
- private final EventHandler <TestCaseFinished > testCaseFinishedHandler = new EventHandler <TestCaseFinished >() {
74
- @ Override
75
- public void receive (TestCaseFinished event ) {
76
- stats .addScenario (event .result .getStatus (), event .testCase .getScenarioDesignation ());
77
- }
78
- };
79
-
80
40
public Runtime (ResourceLoader resourceLoader , ClassFinder classFinder , ClassLoader classLoader , RuntimeOptions runtimeOptions ) {
81
41
this (resourceLoader , classLoader , loadBackends (resourceLoader , classFinder ), runtimeOptions );
82
42
}
@@ -116,8 +76,7 @@ public Runtime(ResourceLoader resourceLoader, ClassLoader classLoader, Collectio
116
76
this .filters .add (new LinePredicate (lineFilters ));
117
77
}
118
78
119
- bus .registerHandlerFor (TestStepFinished .class , stepFinishedHandler );
120
- bus .registerHandlerFor (TestCaseFinished .class , testCaseFinishedHandler );
79
+ stats .setEventPublisher (bus );
121
80
undefinedStepsTracker .setEventPublisher (bus );
122
81
runtimeOptions .setEventBus (bus );
123
82
}
@@ -127,10 +86,6 @@ private static Collection<? extends Backend> loadBackends(ResourceLoader resourc
127
86
return reflections .instantiateSubclasses (Backend .class , "cucumber.runtime" , new Class []{ResourceLoader .class }, new Object []{resourceLoader });
128
87
}
129
88
130
- public void addError (Throwable error ) {
131
- errors .add (error );
132
- }
133
-
134
89
/**
135
90
* This is the main entry point. Used from CLI, but not from JUnit.
136
91
*/
@@ -192,40 +147,11 @@ void printStats(PrintStream out) {
192
147
}
193
148
194
149
public List <Throwable > getErrors () {
195
- return errors ;
150
+ return stats . getErrors () ;
196
151
}
197
152
198
153
public byte exitStatus () {
199
- byte result = 0x0 ;
200
- if (hasErrors () || hasUndefinedOrPendingStepsAndIsStrict ()) {
201
- result |= ERRORS ;
202
- }
203
- return result ;
204
- }
205
-
206
- private boolean hasUndefinedOrPendingStepsAndIsStrict () {
207
- return runtimeOptions .isStrict () && hasUndefinedOrPendingSteps ();
208
- }
209
-
210
- private boolean hasUndefinedOrPendingSteps () {
211
- return hasUndefinedSteps () || hasPendingSteps ();
212
- }
213
-
214
- private boolean hasUndefinedSteps () {
215
- return undefinedStepsTracker .hasUndefinedSteps ();
216
- }
217
-
218
- private boolean hasPendingSteps () {
219
- return !errors .isEmpty () && !hasErrors ();
220
- }
221
-
222
- private boolean hasErrors () {
223
- for (Throwable error : errors ) {
224
- if (!isPending (error ) && !isAssumptionViolated (error )) {
225
- return true ;
226
- }
227
- }
228
- return false ;
154
+ return stats .exitStatus (runtimeOptions .isStrict ());
229
155
}
230
156
231
157
public List <String > getSnippets () {
@@ -236,28 +162,6 @@ public Glue getGlue() {
236
162
return runner .getGlue ();
237
163
}
238
164
239
- public static boolean isPending (Throwable t ) {
240
- if (t == null ) {
241
- return false ;
242
- }
243
- return t .getClass ().isAnnotationPresent (Pending .class );
244
- }
245
-
246
- public static boolean isAssumptionViolated (Throwable t ) {
247
- if (t == null ) {
248
- return false ;
249
- }
250
- return Arrays .binarySearch (ASSUMPTION_VIOLATED_EXCEPTIONS , t .getClass ().getName ()) >= 0 ;
251
- }
252
-
253
- private void addStepToCounterAndResult (Result result ) {
254
- stats .addStep (result );
255
- }
256
-
257
- private void addHookToCounterAndResult (Result result ) {
258
- stats .addHookTime (result .getDuration ());
259
- }
260
-
261
165
public EventBus getEventBus () {
262
166
return bus ;
263
167
}
0 commit comments