forked from cucumber/cucumber-jvm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRuntime.java
289 lines (243 loc) · 10.2 KB
/
Runtime.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
package cucumber.runtime;
import cucumber.api.Pending;
import cucumber.runtime.io.ResourceLoader;
import cucumber.runtime.io.ResourceLoaderReflections;
import cucumber.runtime.model.CucumberFeature;
import cucumber.runtime.snippets.SummaryPrinter;
import cucumber.runtime.xstream.LocalizedXStreams;
import gherkin.I18n;
import gherkin.formatter.Argument;
import gherkin.formatter.Formatter;
import gherkin.formatter.Reporter;
import gherkin.formatter.model.*;
import java.io.IOException;
import java.util.*;
/**
* This is the main entry point for running Cucumber features.
*/
public class Runtime implements UnreportedStepExecutor {
private static final String[] PENDING_EXCEPTIONS = new String[]{
"org.junit.internal.AssumptionViolatedException"
};
static {
Arrays.sort(PENDING_EXCEPTIONS);
}
private static final Object DUMMY_ARG = new Object();
private static final byte ERRORS = 0x1;
final UndefinedStepsTracker undefinedStepsTracker = new UndefinedStepsTracker();
private final Glue glue;
private final RuntimeOptions runtimeOptions;
private final List<Throwable> errors = new ArrayList<Throwable>();
private final Collection<? extends Backend> backends;
private final ResourceLoader resourceLoader;
private final ClassLoader classLoader;
//TODO: These are really state machine variables, and I'm not sure the runtime is the best place for this state machine
//They really should be created each time a scenario is run, not in here
private boolean skipNextStep = false;
private ScenarioImpl scenarioResult = null;
public Runtime(ResourceLoader resourceLoader, ClassLoader classLoader, RuntimeOptions runtimeOptions) {
this(resourceLoader, classLoader, loadBackends(resourceLoader, classLoader), runtimeOptions);
}
public Runtime(ResourceLoader resourceLoader, ClassLoader classLoader, Collection<? extends Backend> backends, RuntimeOptions runtimeOptions) {
this(resourceLoader, classLoader, backends, runtimeOptions, null);
}
public Runtime(ResourceLoader resourceLoader, ClassLoader classLoader, Collection<? extends Backend> backends,
RuntimeOptions runtimeOptions, RuntimeGlue optionalGlue) {
if (backends.isEmpty()) {
throw new CucumberException("No backends were found. Please make sure you have a backend module on your CLASSPATH.");
}
this.resourceLoader = resourceLoader;
this.classLoader = classLoader;
this.backends = backends;
this.runtimeOptions = runtimeOptions;
this.glue = optionalGlue != null ? optionalGlue : new RuntimeGlue(undefinedStepsTracker, new LocalizedXStreams(classLoader));
for (Backend backend : backends) {
backend.loadGlue(glue, runtimeOptions.getGlue());
backend.setUnreportedStepExecutor(this);
}
}
private static Collection<? extends Backend> loadBackends(ResourceLoader resourceLoader, ClassLoader classLoader) {
return new ResourceLoaderReflections(resourceLoader, classLoader).instantiateSubclasses(Backend.class, "cucumber.runtime", new Class[]{ResourceLoader.class}, new Object[]{resourceLoader});
}
public void addError(Throwable error) {
errors.add(error);
}
/**
* This is the main entry point. Used from CLI, but not from JUnit.
*/
public void run() {
for (CucumberFeature cucumberFeature : runtimeOptions.cucumberFeatures(resourceLoader)) {
run(cucumberFeature);
}
Formatter formatter = runtimeOptions.formatter(classLoader);
formatter.done();
printSummary();
formatter.close();
}
private void run(CucumberFeature cucumberFeature) {
Formatter formatter = runtimeOptions.formatter(classLoader);
Reporter reporter = runtimeOptions.reporter(classLoader);
cucumberFeature.run(formatter, reporter, this);
}
private void printSummary() {
// TODO: inject a SummaryPrinter in the ctor
new SummaryPrinter(System.out).print(this);
}
public void buildBackendWorlds(Reporter reporter, Set<Tag> tags) {
for (Backend backend : backends) {
backend.buildWorld();
}
undefinedStepsTracker.reset();
//TODO: this is the initial state of the state machine, it should not go here, but into something else
skipNextStep = false;
scenarioResult = new ScenarioImpl(reporter, tags);
}
public void disposeBackendWorlds() {
for (Backend backend : backends) {
backend.disposeWorld();
}
}
public List<Throwable> getErrors() {
return errors;
}
public byte exitStatus() {
byte result = 0x0;
if (hasErrors() || hasUndefinedOrPendingStepsAndIsStrict()) {
result |= ERRORS;
}
return result;
}
private boolean hasUndefinedOrPendingStepsAndIsStrict() {
return runtimeOptions.isStrict() && hasUndefinedOrPendingSteps();
}
private boolean hasUndefinedOrPendingSteps() {
return hasUndefinedSteps() || hasPendingSteps();
}
private boolean hasUndefinedSteps() {
return undefinedStepsTracker.hasUndefinedSteps();
}
private boolean hasPendingSteps() {
return !errors.isEmpty() && !hasErrors();
}
private boolean hasErrors() {
for (Throwable error : errors) {
if (!isPending(error)) {
return true;
}
}
return false;
}
public List<String> getSnippets() {
return undefinedStepsTracker.getSnippets(backends);
}
public Glue getGlue() {
return glue;
}
public void runBeforeHooks(Reporter reporter, Set<Tag> tags) {
runHooks(glue.getBeforeHooks(), reporter, tags, true);
}
public void runAfterHooks(Reporter reporter, Set<Tag> tags) {
runHooks(glue.getAfterHooks(), reporter, tags, false);
}
private void runHooks(List<HookDefinition> hooks, Reporter reporter, Set<Tag> tags, boolean isBefore) {
if (!runtimeOptions.isDryRun()) {
for (HookDefinition hook : hooks) {
runHookIfTagsMatch(hook, reporter, tags, isBefore);
}
}
}
private void runHookIfTagsMatch(HookDefinition hook, Reporter reporter, Set<Tag> tags, boolean isBefore) {
if (hook.matches(tags)) {
String status = Result.PASSED;
Throwable error = null;
Match match = new Match(Collections.<Argument>emptyList(), hook.getLocation(false));
long start = System.nanoTime();
try {
hook.execute(scenarioResult);
} catch (Throwable t) {
error = t;
status = isPending(t) ? "pending" : Result.FAILED;
addError(t);
skipNextStep = true;
} finally {
long duration = System.nanoTime() - start;
Result result = new Result(status, duration, error, DUMMY_ARG);
scenarioResult.add(result);
if (isBefore) {
reporter.before(match, result);
} else {
reporter.after(match, result);
}
}
}
}
//TODO: Maybe this should go into the cucumber step execution model and it should return the result of that execution!
@Override
public void runUnreportedStep(String uri, I18n i18n, String stepKeyword, String stepName, int line, List<DataTableRow> dataTableRows, DocString docString) throws Throwable {
Step step = new Step(Collections.<Comment>emptyList(), stepKeyword, stepName, line, dataTableRows, docString);
StepDefinitionMatch match = glue.stepDefinitionMatch(uri, step, i18n);
if (match == null) {
UndefinedStepException error = new UndefinedStepException(step);
StackTraceElement[] originalTrace = error.getStackTrace();
StackTraceElement[] newTrace = new StackTraceElement[originalTrace.length + 1];
newTrace[0] = new StackTraceElement("✽", "StepDefinition", uri, line);
System.arraycopy(originalTrace, 0, newTrace, 1, originalTrace.length);
error.setStackTrace(newTrace);
throw error;
}
match.runStep(i18n);
}
public void runStep(String uri, Step step, Reporter reporter, I18n i18n) {
StepDefinitionMatch match;
try {
match = glue.stepDefinitionMatch(uri, step, i18n);
} catch (AmbiguousStepDefinitionsException e) {
reporter.match(e.getMatches().get(0));
reporter.result(new Result(Result.FAILED, 0L, e, DUMMY_ARG));
addError(e);
skipNextStep = true;
return;
}
if (match != null) {
reporter.match(match);
} else {
reporter.match(Match.UNDEFINED);
reporter.result(Result.UNDEFINED);
skipNextStep = true;
return;
}
if (runtimeOptions.isDryRun()) {
skipNextStep = true;
}
if (skipNextStep) {
scenarioResult.add(Result.SKIPPED);
reporter.result(Result.SKIPPED);
} else {
String status = Result.PASSED;
Throwable error = null;
long start = System.nanoTime();
try {
match.runStep(i18n);
} catch (Throwable t) {
error = t;
status = isPending(t) ? "pending" : Result.FAILED;
addError(t);
skipNextStep = true;
} finally {
long duration = System.nanoTime() - start;
Result result = new Result(status, duration, error, DUMMY_ARG);
scenarioResult.add(result);
reporter.result(result);
}
}
}
public static boolean isPending(Throwable t) {
if (t == null) {
return false;
}
return t.getClass().isAnnotationPresent(Pending.class) || Arrays.binarySearch(PENDING_EXCEPTIONS, t.getClass().getName()) >= 0;
}
public void writeStepdefsJson() throws IOException {
glue.writeStepdefsJson(runtimeOptions.getFeaturePaths(), runtimeOptions.getDotCucumber());
}
}