|
1 | | -package cucumber.api.groovy; |
2 | | - |
3 | | -import cucumber.runtime.CucumberException; |
4 | | -import cucumber.runtime.filter.TagPredicate; |
5 | | -import cucumber.runtime.groovy.GroovyBackend; |
6 | | -import groovy.lang.Closure; |
7 | | - |
8 | | -import java.util.ArrayList; |
9 | | -import java.util.List; |
10 | | - |
11 | | -public class Hooks { |
12 | | - private static final int DEFAULT_ORDER = 10000; |
13 | | - private static final long DEFAULT_TIMEOUT = 0; |
14 | | - |
15 | | - public static void World(Closure body) throws Throwable { |
16 | | - GroovyBackend.getInstance().registerWorld(body); |
17 | | - } |
18 | | - |
19 | | - /** |
20 | | - * Registers a before hook, which is executed before specific, or all, scenarios. |
21 | | - * |
22 | | - * Following values are expected as hook parameters. |
23 | | - * - Long timeoutMillis: max amount of milliseconds this is allowed to run for. The default is 0 which means no restriction. |
24 | | - * - Integer order: the order in which this hook should run. Lower numbers are run first. The default is 10000. |
25 | | - * - String(s) tags: one or more tag expression to filter the certain scenarios. The default is empty. |
26 | | - * - Closure body: hook body which is executed before scenario. Not null. |
27 | | - * |
28 | | - * @param args the hook parameters |
29 | | - */ |
30 | | - public static void Before(Object... args) { |
31 | | - addHook(args, true, false); |
32 | | - } |
33 | | - |
34 | | - /** |
35 | | - * Registers an after hook, which is executed after specific, or all, scenarios. |
36 | | - * |
37 | | - * Following values are expected as hook parameters. |
38 | | - * - Long timeoutMillis: max amount of milliseconds this is allowed to run for. The default is 0 which means no restriction. |
39 | | - * - Integer order: the order in which this hook should run. Higher numbers are run first. The default is 10000. |
40 | | - * - String(s) tags: one or more tag expression to filter the certain scenarios. The default is empty. |
41 | | - * - Closure body: hook body which is executed after scenario. Not null. |
42 | | - * |
43 | | - * @param args the hook parameters |
44 | | - */ |
45 | | - public static void After(Object... args) { |
46 | | - addHook(args, false, false); |
47 | | - } |
48 | | - |
49 | | - public static void AfterStep(Object... args) { |
50 | | - addHook(args, false, true); |
51 | | - } |
52 | | - |
53 | | - public static void BeforeStep(Object... args) { |
54 | | - addHook(args, true, true); |
55 | | - } |
56 | | - |
57 | | - private static void addHook(Object[] tagsExpressionsAndBody, boolean before, boolean forStep) { |
58 | | - long timeoutMillis = DEFAULT_TIMEOUT; |
59 | | - int order = DEFAULT_ORDER; |
60 | | - boolean timeoutSet = false; |
61 | | - boolean orderSet = false; |
62 | | - Closure body = null; |
63 | | - List<String> tagExpressions = new ArrayList<String>(); |
64 | | - |
65 | | - for (Object o : tagsExpressionsAndBody) { |
66 | | - if (o instanceof String) { |
67 | | - tagExpressions.add((String) o); |
68 | | - } else if (o instanceof Long) { |
69 | | - if (timeoutSet) { |
70 | | - throw new CucumberException("Two timeout (Long) arguments found; " + |
71 | | - Long.toString(timeoutMillis) + ", and; " + |
72 | | - Long.toString((Long) o)); |
73 | | - } |
74 | | - timeoutMillis = (Long) o; |
75 | | - timeoutSet = true; |
76 | | - } else if (o instanceof Integer) { |
77 | | - if (orderSet) { |
78 | | - throw new CucumberException("Two order (Integer) arguments found; " + |
79 | | - Integer.toString(order) + ", and; " + |
80 | | - Integer.toString((Integer) o)); |
81 | | - } |
82 | | - order = (Integer) o; |
83 | | - orderSet = true; |
84 | | - } else if (o instanceof Closure) { |
85 | | - body = (Closure) o; |
86 | | - } else { |
87 | | - throw new CucumberException("An argument of the type " + o.getClass().getName() + " found, " + |
88 | | - (before ? "Before" : "After") + " only allows the argument types " + |
89 | | - "String - Tag, Long - timeout, Integer - order, and Closure"); |
90 | | - } |
91 | | - } |
92 | | - |
93 | | - TagPredicate tagPredicate = new TagPredicate(tagExpressions); |
94 | | - if (before) { |
95 | | - if(forStep) { |
96 | | - GroovyBackend.getInstance().addBeforeStepHook(tagPredicate, timeoutMillis, order, body); |
97 | | - }else{ |
98 | | - GroovyBackend.getInstance().addBeforeHook(tagPredicate, timeoutMillis, order, body); |
99 | | - } |
100 | | - } else { |
101 | | - if(forStep) { |
102 | | - GroovyBackend.getInstance().addAfterStepHook(tagPredicate, timeoutMillis, order, body); |
103 | | - }else{ |
104 | | - GroovyBackend.getInstance().addAfterHook(tagPredicate, timeoutMillis, order, body); |
105 | | - } |
106 | | - } |
107 | | - } |
108 | | -} |
| 1 | +package cucumber.api.groovy; |
| 2 | + |
| 3 | +import cucumber.runtime.CucumberException; |
| 4 | +import cucumber.runtime.filter.TagPredicate; |
| 5 | +import cucumber.runtime.groovy.GroovyBackend; |
| 6 | +import groovy.lang.Closure; |
| 7 | + |
| 8 | +import java.util.ArrayList; |
| 9 | +import java.util.List; |
| 10 | + |
| 11 | +public class Hooks { |
| 12 | + private static final int DEFAULT_ORDER = 10000; |
| 13 | + private static final long DEFAULT_TIMEOUT = 0; |
| 14 | + |
| 15 | + public static void World(Closure body) { |
| 16 | + GroovyBackend.getInstance().registerWorld(body); |
| 17 | + } |
| 18 | + |
| 19 | + /** |
| 20 | + * Registers a before hook, which is executed before specific, or all, scenarios. |
| 21 | + * |
| 22 | + * Following values are expected as hook parameters. |
| 23 | + * - Long timeoutMillis: max amount of milliseconds this is allowed to run for. The default is 0 which means no restriction. |
| 24 | + * - Integer order: the order in which this hook should run. Lower numbers are run first. The default is 10000. |
| 25 | + * - String(s) tags: one or more tag expression to filter the certain scenarios. The default is empty. |
| 26 | + * - Closure body: hook body which is executed before scenario. Not null. |
| 27 | + * |
| 28 | + * @param args the hook parameters |
| 29 | + */ |
| 30 | + public static void Before(Object... args) { |
| 31 | + addHook(args, true, false); |
| 32 | + } |
| 33 | + |
| 34 | + /** |
| 35 | + * Registers an after hook, which is executed after specific, or all, scenarios. |
| 36 | + * |
| 37 | + * Following values are expected as hook parameters. |
| 38 | + * - Long timeoutMillis: max amount of milliseconds this is allowed to run for. The default is 0 which means no restriction. |
| 39 | + * - Integer order: the order in which this hook should run. Higher numbers are run first. The default is 10000. |
| 40 | + * - String(s) tags: one or more tag expression to filter the certain scenarios. The default is empty. |
| 41 | + * - Closure body: hook body which is executed after scenario. Not null. |
| 42 | + * |
| 43 | + * @param args the hook parameters |
| 44 | + */ |
| 45 | + public static void After(Object... args) { |
| 46 | + addHook(args, false, false); |
| 47 | + } |
| 48 | + |
| 49 | + public static void AfterStep(Object... args) { |
| 50 | + addHook(args, false, true); |
| 51 | + } |
| 52 | + |
| 53 | + public static void BeforeStep(Object... args) { |
| 54 | + addHook(args, true, true); |
| 55 | + } |
| 56 | + |
| 57 | + private static void addHook(Object[] tagsExpressionsAndBody, boolean before, boolean forStep) { |
| 58 | + long timeoutMillis = DEFAULT_TIMEOUT; |
| 59 | + int order = DEFAULT_ORDER; |
| 60 | + boolean timeoutSet = false; |
| 61 | + boolean orderSet = false; |
| 62 | + Closure body = null; |
| 63 | + List<String> tagExpressions = new ArrayList<String>(); |
| 64 | + |
| 65 | + for (Object o : tagsExpressionsAndBody) { |
| 66 | + if (o instanceof String) { |
| 67 | + tagExpressions.add((String) o); |
| 68 | + } else if (o instanceof Long) { |
| 69 | + if (timeoutSet) { |
| 70 | + throw new CucumberException("Two timeout (Long) arguments found; " + |
| 71 | + Long.toString(timeoutMillis) + ", and; " + |
| 72 | + Long.toString((Long) o)); |
| 73 | + } |
| 74 | + timeoutMillis = (Long) o; |
| 75 | + timeoutSet = true; |
| 76 | + } else if (o instanceof Integer) { |
| 77 | + if (orderSet) { |
| 78 | + throw new CucumberException("Two order (Integer) arguments found; " + |
| 79 | + Integer.toString(order) + ", and; " + |
| 80 | + Integer.toString((Integer) o)); |
| 81 | + } |
| 82 | + order = (Integer) o; |
| 83 | + orderSet = true; |
| 84 | + } else if (o instanceof Closure) { |
| 85 | + body = (Closure) o; |
| 86 | + } else { |
| 87 | + throw new CucumberException("An argument of the type " + o.getClass().getName() + " found, " + |
| 88 | + (before ? "Before" : "After") + " only allows the argument types " + |
| 89 | + "String - Tag, Long - timeout, Integer - order, and Closure"); |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + TagPredicate tagPredicate = new TagPredicate(tagExpressions); |
| 94 | + if (before) { |
| 95 | + if(forStep) { |
| 96 | + GroovyBackend.getInstance().addBeforeStepHook(tagPredicate, timeoutMillis, order, body); |
| 97 | + }else{ |
| 98 | + GroovyBackend.getInstance().addBeforeHook(tagPredicate, timeoutMillis, order, body); |
| 99 | + } |
| 100 | + } else { |
| 101 | + if(forStep) { |
| 102 | + GroovyBackend.getInstance().addAfterStepHook(tagPredicate, timeoutMillis, order, body); |
| 103 | + }else{ |
| 104 | + GroovyBackend.getInstance().addAfterHook(tagPredicate, timeoutMillis, order, body); |
| 105 | + } |
| 106 | + } |
| 107 | + } |
| 108 | +} |
0 commit comments