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