Skip to content

Commit 9b26a5e

Browse files
committed
added support for execution order for "Before" and "After" hook. closes #807
1 parent cce054f commit 9b26a5e

File tree

3 files changed

+58
-17
lines changed

3 files changed

+58
-17
lines changed

groovy/src/main/java/cucumber/api/groovy/Hooks.java

+40-8
Original file line numberDiff line numberDiff line change
@@ -8,38 +8,70 @@
88
import java.util.List;
99

1010
public class Hooks {
11+
private static final int DEFAULT_ORDER = 10000;
12+
private static final long DEFAULT_TIMEOUT = 0;
13+
1114
public static void World(Closure body) throws Throwable {
1215
GroovyBackend.getInstance().registerWorld(body);
1316
}
1417

15-
public static void Before(Object... args) throws Throwable {
18+
/**
19+
* Registers a before hook, which is executed before specific, or all, scenarios.
20+
* <p/>
21+
* Following values are expected as hook parameters.
22+
* - timeout: max amount of milliseconds this is allowed to run for. The default is 0 which means no restriction.
23+
* - order: the order in which this hook should run. Lower numbers are run first. The default is 10000.
24+
* - tag expressions: one or more tag expression to filter the certain scenarios. The default is empty.
25+
* - Closure: hook body which is executed before scenario. Not null.
26+
*
27+
* @param args the hook parameters
28+
*/
29+
public static void Before(Object... args) {
1630
addHook(args, true);
1731
}
1832

19-
public static void After(Object... args) throws Throwable {
33+
/**
34+
* Registers an after hook, which is executed after specific, or all, scenarios.
35+
* <p/>
36+
* Following values are expected as hook parameters.
37+
* - timeout: max amount of milliseconds this is allowed to run for. The default is 0 which means no restriction.
38+
* - order: the order in which this hook should run. Higher numbers are run first. The default is 10000.
39+
* - tag expressions: one or more tag expression to filter the certain scenarios. The default is empty.
40+
* - Closure: hook body which is executed after scenario. Not null.
41+
*
42+
* @param args the hook parameters
43+
*/
44+
public static void After(Object... args) {
2045
addHook(args, false);
2146
}
2247

2348
private static void addHook(Object[] tagsExpressionsAndBody, boolean before) {
24-
List<String> tagExpressions = new ArrayList<String>();
25-
int timeoutMillis = 0;
49+
long timeout = DEFAULT_TIMEOUT;
50+
int order = DEFAULT_ORDER;
2651
Closure body = null;
52+
List<String> tagExpressions = new ArrayList<String>();
2753

54+
boolean isDefaultTimeout = true;
55+
boolean isDefaultOrder = true;
2856
for (Object o : tagsExpressionsAndBody) {
2957
if (o instanceof String) {
3058
tagExpressions.add((String) o);
31-
} else if (o instanceof Integer) {
32-
timeoutMillis = (Integer) o;
59+
} else if (o instanceof Number && isDefaultTimeout) {
60+
timeout = ((Number) o).longValue();
61+
isDefaultTimeout = false;
62+
} else if (o instanceof Number && isDefaultOrder) {
63+
order = ((Number) o).intValue();
64+
isDefaultOrder = false;
3365
} else if (o instanceof Closure) {
3466
body = (Closure) o;
3567
}
3668
}
3769

3870
TagExpression tagExpression = new TagExpression(tagExpressions);
3971
if (before) {
40-
GroovyBackend.getInstance().addBeforeHook(tagExpression, timeoutMillis, body);
72+
GroovyBackend.getInstance().addBeforeHook(tagExpression, timeout, order, body);
4173
} else {
42-
GroovyBackend.getInstance().addAfterHook(tagExpression, timeoutMillis, body);
74+
GroovyBackend.getInstance().addAfterHook(tagExpression, timeout, order, body);
4375
}
4476
}
4577
}

groovy/src/main/java/cucumber/runtime/groovy/GroovyBackend.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -140,12 +140,12 @@ public void registerWorld(Closure closure) {
140140
worldClosures.add(closure);
141141
}
142142

143-
public void addBeforeHook(TagExpression tagExpression, long timeoutMillis, Closure body) {
144-
glue.addBeforeHook(new GroovyHookDefinition(tagExpression, timeoutMillis, body, currentLocation(), this));
143+
public void addBeforeHook(TagExpression tagExpression, long timeout, int order, Closure body) {
144+
glue.addBeforeHook(new GroovyHookDefinition(tagExpression, timeout, order, body, currentLocation(), this));
145145
}
146146

147-
public void addAfterHook(TagExpression tagExpression, long timeoutMillis, Closure body) {
148-
glue.addAfterHook(new GroovyHookDefinition(tagExpression, timeoutMillis, body, currentLocation(), this));
147+
public void addAfterHook(TagExpression tagExpression, long timeout, int order, Closure body) {
148+
glue.addAfterHook(new GroovyHookDefinition(tagExpression, timeout, order, body, currentLocation(), this));
149149
}
150150

151151
public void invoke(Closure body, Object[] args) throws Throwable {

groovy/src/main/java/cucumber/runtime/groovy/GroovyHookDefinition.java

+14-5
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,23 @@
1111

1212
public class GroovyHookDefinition implements HookDefinition {
1313
private final TagExpression tagExpression;
14-
private final long timeoutMillis;
14+
private final long timeout;
15+
private final int order;
1516
private final Closure body;
1617
private final GroovyBackend backend;
1718
private final StackTraceElement location;
1819

19-
public GroovyHookDefinition(TagExpression tagExpression, long timeoutMillis, Closure body, StackTraceElement location, GroovyBackend backend) {
20+
public GroovyHookDefinition(
21+
TagExpression tagExpression,
22+
long timeout,
23+
int order,
24+
Closure body,
25+
StackTraceElement location,
26+
GroovyBackend backend) {
27+
2028
this.tagExpression = tagExpression;
21-
this.timeoutMillis = timeoutMillis;
29+
this.timeout = timeout;
30+
this.order = order;
2231
this.body = body;
2332
this.location = location;
2433
this.backend = backend;
@@ -37,7 +46,7 @@ public Object call() throws Throwable {
3746
backend.invoke(body, new Object[]{scenario});
3847
return null;
3948
}
40-
}, timeoutMillis);
49+
}, timeout);
4150
}
4251

4352
@Override
@@ -47,7 +56,7 @@ public boolean matches(Collection<Tag> tags) {
4756

4857
@Override
4958
public int getOrder() {
50-
return location.getFileName() == "env.groovy" ? -1 : 0;
59+
return order;
5160
}
5261
}
5362

0 commit comments

Comments
 (0)