|
8 | 8 | import java.util.List;
|
9 | 9 |
|
10 | 10 | public class Hooks {
|
| 11 | + private static final int DEFAULT_ORDER = 10000; |
| 12 | + private static final long DEFAULT_TIMEOUT = 0; |
| 13 | + |
11 | 14 | public static void World(Closure body) throws Throwable {
|
12 | 15 | GroovyBackend.getInstance().registerWorld(body);
|
13 | 16 | }
|
14 | 17 |
|
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) { |
16 | 30 | addHook(args, true);
|
17 | 31 | }
|
18 | 32 |
|
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) { |
20 | 45 | addHook(args, false);
|
21 | 46 | }
|
22 | 47 |
|
23 | 48 | 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; |
26 | 51 | Closure body = null;
|
| 52 | + List<String> tagExpressions = new ArrayList<String>(); |
27 | 53 |
|
| 54 | + boolean isDefaultTimeout = true; |
| 55 | + boolean isDefaultOrder = true; |
28 | 56 | for (Object o : tagsExpressionsAndBody) {
|
29 | 57 | if (o instanceof String) {
|
30 | 58 | 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; |
33 | 65 | } else if (o instanceof Closure) {
|
34 | 66 | body = (Closure) o;
|
35 | 67 | }
|
36 | 68 | }
|
37 | 69 |
|
38 | 70 | TagExpression tagExpression = new TagExpression(tagExpressions);
|
39 | 71 | if (before) {
|
40 |
| - GroovyBackend.getInstance().addBeforeHook(tagExpression, timeoutMillis, body); |
| 72 | + GroovyBackend.getInstance().addBeforeHook(tagExpression, timeout, order, body); |
41 | 73 | } else {
|
42 |
| - GroovyBackend.getInstance().addAfterHook(tagExpression, timeoutMillis, body); |
| 74 | + GroovyBackend.getInstance().addAfterHook(tagExpression, timeout, order, body); |
43 | 75 | }
|
44 | 76 | }
|
45 | 77 | }
|
0 commit comments