Skip to content

Commit e44cc9c

Browse files
committed
Merge branch 'master' into android
2 parents b86999f + b0496b3 commit e44cc9c

File tree

24 files changed

+388
-56
lines changed

24 files changed

+388
-56
lines changed

History.md

+2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
## [1-1-5-SNAPSHOT (Git master)](https://github.com/cucumber/cucumber-jvm/compare/v1.1.4...master)
22

3+
* [Core] Step Definition and Hook timeout is now a `long` instead of an `int`. (Aslak Hellesøy)
4+
* [Rhino] Before and After hooks support ([#587](https://github.com/cucumber/cucumber-jvm/pull/587) Rui Figueira)
35
* [Android] Separate CI job for Android. ([#581](https://github.com/cucumber/cucumber-jvm/issues/581), [#584](https://github.com/cucumber/cucumber-jvm/pull/584) Björn Rasmusson)
46
* [Android] Add support for Dependency Injection via cucumber-picocontainer, cucumber-guice, cucumber-spring etx. (Aslak Hellesøy)
57
* [TestNG] Java Calculator TestNG example project ([#579](https://github.com/cucumber/cucumber-jvm/pull/579) Dmytro Chyzhykov)

core/src/main/java/cucumber/runtime/Timeout.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import java.util.concurrent.atomic.AtomicBoolean;
88

99
public class Timeout {
10-
public static <T> T timeout(Callback<T> callback, int timeoutMillis) throws Throwable {
10+
public static <T> T timeout(Callback<T> callback, long timeoutMillis) throws Throwable {
1111
if (timeoutMillis == 0) {
1212
return callback.call();
1313
} else {

core/src/main/java/cucumber/runtime/Utils.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public static boolean isInstantiable(Class<?> clazz) {
2727
return Modifier.isPublic(clazz.getModifiers()) && !Modifier.isAbstract(clazz.getModifiers()) && !isNonStaticInnerClass;
2828
}
2929

30-
public static Object invoke(final Object target, final Method method, int timeoutMillis, final Object... args) throws Throwable {
30+
public static Object invoke(final Object target, final Method method, long timeoutMillis, final Object... args) throws Throwable {
3131
return Timeout.timeout(new Timeout.Callback<Object>() {
3232
@Override
3333
public Object call() throws Throwable {

examples/java-helloworld/README.md

+39-5
Original file line numberDiff line numberDiff line change
@@ -31,16 +31,50 @@ The Cucumber runtime parses command line options to know what features to run, w
3131
When you use the JUnit runner, these options are generated from the `@Cucumber.Options` annotation on your test.
3232

3333
Sometimes it can be useful to override these options without changing or recompiling the JUnit class. This can be done with the
34-
`cucumber.options` system property. Here are a couple of examples:
34+
`cucumber.options` system property. The general form is:
3535

36-
With Maven:
36+
Using Maven:
37+
```
38+
mvn -Dcucumber.options="..." test
39+
```
40+
41+
Using Ant:
42+
```
43+
JAVA_OPTIONS='-Dcucumber.options="..."' ant runcukes
44+
```
45+
46+
Let's look at some things you can do with `cucumber.options`. Try this:
47+
```
48+
-Dcucumber.options="--help"
49+
```
50+
51+
That should list all the available options.
52+
53+
#### Run a subset of Features or Scenarios
54+
55+
Specify a particular scenario by *line* (and use the pretty format)
56+
57+
```
58+
-Dcucumber.options="classpath:cucumber/examples/java/helloworld/helloworld.feature:4 --format pretty"
59+
```
60+
61+
This works because Maven puts `./src/test/resources` on your `classpath`.
62+
You can also specify files to run by filesystem path:
3763

3864
```
39-
mvn -Dcucumber.options="--format junit:target/cucumber-junit-report.xml" test
65+
-Dcucumber.options="src/test/resources/cucumber/examples/java/helloworld/helloworld.feature:4 --format pretty"
4066
```
4167

42-
Or with Ant:
68+
You can also specify what to run by *tag*:
4369

4470
```
45-
_JAVA_OPTIONS='-Dcucumber.options="--format json-pretty:target/cucumber-json-report.json"' ant runcukes
71+
-Dcucumber.options="--tags @bar --format pretty"
4672
```
73+
74+
#### Specify a different formatter:
75+
76+
For example a JUnit formatter:
77+
```
78+
-Dcucumber.options="--format junit:target/cucumber-junit-report.xml"
79+
```
80+

groovy/src/main/code_generator/I18n.groovy.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ public class ${i18n.underscoredIsoCode.toUpperCase()} {
1111
GroovyBackend.instance.addStepDefinition(regexp, 0, body);
1212
}
1313

14-
public static void ${java.text.Normalizer.normalize(kw, java.text.Normalizer.Form.NFD)}(Pattern regexp, int timeoutMillis, Closure body) throws Throwable {
14+
public static void ${java.text.Normalizer.normalize(kw, java.text.Normalizer.Form.NFD)}(Pattern regexp, long timeoutMillis, Closure body) throws Throwable {
1515
GroovyBackend.instance.addStepDefinition(regexp, timeoutMillis, body);
1616
}
1717
<% } %>

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ public String getSnippet(Step step, FunctionNameSanitizer functionNameSanitizer)
123123
return snippetGenerator.getSnippet(step, null);
124124
}
125125

126-
public void addStepDefinition(Pattern regexp, int timeoutMillis, Closure body) {
126+
public void addStepDefinition(Pattern regexp, long timeoutMillis, Closure body) {
127127
glue.addStepDefinition(new GroovyStepDefinition(regexp, timeoutMillis, body, currentLocation(), instance));
128128
}
129129

@@ -132,11 +132,11 @@ public void registerWorld(Closure closure) {
132132
worldClosure = closure;
133133
}
134134

135-
public void addBeforeHook(TagExpression tagExpression, int timeoutMillis, Closure body) {
135+
public void addBeforeHook(TagExpression tagExpression, long timeoutMillis, Closure body) {
136136
glue.addBeforeHook(new GroovyHookDefinition(tagExpression, timeoutMillis, body, currentLocation(), instance));
137137
}
138138

139-
public void addAfterHook(TagExpression tagExpression, int timeoutMillis, Closure body) {
139+
public void addAfterHook(TagExpression tagExpression, long timeoutMillis, Closure body) {
140140
glue.addAfterHook(new GroovyHookDefinition(tagExpression, timeoutMillis, body, currentLocation(), instance));
141141
}
142142

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@
1111

1212
public class GroovyHookDefinition implements HookDefinition {
1313
private final TagExpression tagExpression;
14-
private final int timeoutMillis;
14+
private final long timeoutMillis;
1515
private final Closure body;
1616
private final GroovyBackend backend;
1717
private final StackTraceElement location;
1818

19-
public GroovyHookDefinition(TagExpression tagExpression, int timeoutMillis, Closure body, StackTraceElement location, GroovyBackend backend) {
19+
public GroovyHookDefinition(TagExpression tagExpression, long timeoutMillis, Closure body, StackTraceElement location, GroovyBackend backend) {
2020
this.tagExpression = tagExpression;
2121
this.timeoutMillis = timeoutMillis;
2222
this.body = body;

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,13 @@
1717
public class GroovyStepDefinition implements StepDefinition {
1818
private final Pattern pattern;
1919
private final JdkPatternArgumentMatcher argumentMatcher;
20-
private final int timeoutMillis;
20+
private final long timeoutMillis;
2121
private final Closure body;
2222
private final StackTraceElement location;
2323
private final GroovyBackend backend;
2424
private List<ParameterInfo> parameterInfos;
2525

26-
public GroovyStepDefinition(Pattern pattern, int timeoutMillis, Closure body, StackTraceElement location, GroovyBackend backend) {
26+
public GroovyStepDefinition(Pattern pattern, long timeoutMillis, Closure body, StackTraceElement location, GroovyBackend backend) {
2727
this.pattern = pattern;
2828
this.timeoutMillis = timeoutMillis;
2929
this.backend = backend;

java/src/main/code_generator/I18n.java.txt

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ public @interface ${kw} {
1717
String value();
1818

1919
/**
20-
* @return max amount of time this is allowed to run for. 0 (default) means no restriction.
20+
* @return max amount of milliseconds this is allowed to run for. 0 (default) means no restriction.
2121
*/
22-
int timeout() default 0;
22+
long timeout() default 0;
2323
}
2424

java/src/main/java/cucumber/api/java/After.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@
1414
String[] value() default {};
1515

1616
/**
17-
* @return max amount of time this is allowed to run for. 0 (default) means no restriction.
17+
* @return max amount of milliseconds this is allowed to run for. 0 (default) means no restriction.
1818
*/
19-
int timeout() default 0;
19+
long timeout() default 0;
2020

2121
/**
2222
* The order in which this hook should run. Higher numbers are run first.

java/src/main/java/cucumber/api/java/Before.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@
1414
String[] value() default {};
1515

1616
/**
17-
* @return max amount of time this is allowed to run for. 0 (default) means no restriction.
17+
* @return max amount of milliseconds this is allowed to run for. 0 (default) means no restriction.
1818
*/
19-
int timeout() default 0;
19+
long timeout() default 0;
2020

2121
/**
2222
* The order in which this hook should run. Lower numbers are run first.

java/src/main/java/cucumber/runtime/java/JavaBackend.java

+5-5
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ public String getSnippet(Step step, FunctionNameSanitizer functionNameSanitizer)
109109
void addStepDefinition(Annotation annotation, Method method) {
110110
try {
111111
objectFactory.addClass(method.getDeclaringClass());
112-
glue.addStepDefinition(new JavaStepDefinition(method, pattern(annotation), timeout(annotation), objectFactory));
112+
glue.addStepDefinition(new JavaStepDefinition(method, pattern(annotation), timeoutMillis(annotation), objectFactory));
113113
} catch (DuplicateStepDefinitionException e) {
114114
throw e;
115115
} catch (Throwable e) {
@@ -123,21 +123,21 @@ private Pattern pattern(Annotation annotation) throws Throwable {
123123
return Pattern.compile(regexpString);
124124
}
125125

126-
private int timeout(Annotation annotation) throws Throwable {
126+
private long timeoutMillis(Annotation annotation) throws Throwable {
127127
Method regexpMethod = annotation.getClass().getMethod("timeout");
128-
return (Integer) Utils.invoke(annotation, regexpMethod, 0);
128+
return (Long) Utils.invoke(annotation, regexpMethod, 0);
129129
}
130130

131131
void addHook(Annotation annotation, Method method) {
132132
objectFactory.addClass(method.getDeclaringClass());
133133

134134
if (annotation.annotationType().equals(Before.class)) {
135135
String[] tagExpressions = ((Before) annotation).value();
136-
int timeout = ((Before) annotation).timeout();
136+
long timeout = ((Before) annotation).timeout();
137137
glue.addBeforeHook(new JavaHookDefinition(method, tagExpressions, ((Before) annotation).order(), timeout, objectFactory));
138138
} else {
139139
String[] tagExpressions = ((After) annotation).value();
140-
int timeout = ((After) annotation).timeout();
140+
long timeout = ((After) annotation).timeout();
141141
glue.addAfterHook(new JavaHookDefinition(method, tagExpressions, ((After) annotation).order(), timeout, objectFactory));
142142
}
143143
}

java/src/main/java/cucumber/runtime/java/JavaHookDefinition.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,14 @@
1616
class JavaHookDefinition implements HookDefinition {
1717

1818
private final Method method;
19-
private final int timeout;
19+
private final long timeoutMillis;
2020
private final TagExpression tagExpression;
2121
private final int order;
2222
private final ObjectFactory objectFactory;
2323

24-
public JavaHookDefinition(Method method, String[] tagExpressions, int order, int timeout, ObjectFactory objectFactory) {
24+
public JavaHookDefinition(Method method, String[] tagExpressions, int order, long timeoutMillis, ObjectFactory objectFactory) {
2525
this.method = method;
26-
this.timeout = timeout;
26+
this.timeoutMillis = timeoutMillis;
2727
tagExpression = new TagExpression(asList(tagExpressions));
2828
this.order = order;
2929
this.objectFactory = objectFactory;
@@ -56,7 +56,7 @@ public void execute(Scenario scenario) throws Throwable {
5656
throw new CucumberException("Hooks must declare 0 or 1 arguments. " + method.toString());
5757
}
5858

59-
Utils.invoke(objectFactory.getInstance(method.getDeclaringClass()), method, timeout, args);
59+
Utils.invoke(objectFactory.getInstance(method.getDeclaringClass()), method, timeoutMillis, args);
6060
}
6161

6262
@Override

java/src/main/java/cucumber/runtime/java/JavaStepDefinition.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,17 @@
1717
class JavaStepDefinition implements StepDefinition {
1818
private final Method method;
1919
private final Pattern pattern;
20-
private final int timeout;
20+
private final long timeout;
2121
private final JdkPatternArgumentMatcher argumentMatcher;
2222
private final ObjectFactory objectFactory;
2323
private List<ParameterInfo> parameterInfos;
2424

25-
public JavaStepDefinition(Method method, Pattern pattern, int timeout, ObjectFactory objectFactory) {
25+
public JavaStepDefinition(Method method, Pattern pattern, long timeoutMillis, ObjectFactory objectFactory) {
2626
this.method = method;
2727
this.parameterInfos = ParameterInfo.fromMethod(method);
2828
this.pattern = pattern;
2929
this.argumentMatcher = new JdkPatternArgumentMatcher(pattern);
30-
this.timeout = timeout;
30+
this.timeout = timeoutMillis;
3131
this.objectFactory = objectFactory;
3232
}
3333

rhino/pom.xml

+5
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,11 @@
4343
<artifactId>junit</artifactId>
4444
<scope>test</scope>
4545
</dependency>
46+
<dependency>
47+
<groupId>org.mockito</groupId>
48+
<artifactId>mockito-all</artifactId>
49+
<scope>test</scope>
50+
</dependency>
4651
<dependency>
4752
<groupId>net.sourceforge.cobertura</groupId>
4853
<artifactId>cobertura</artifactId>

rhino/src/main/java/cucumber/runtime/rhino/RhinoBackend.java

+29-15
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,19 @@
11
package cucumber.runtime.rhino;
22

3+
import static cucumber.runtime.io.MultiLoader.packageName;
4+
import gherkin.formatter.model.Step;
5+
6+
import java.io.IOException;
7+
import java.io.InputStreamReader;
8+
import java.util.List;
9+
10+
import org.mozilla.javascript.Context;
11+
import org.mozilla.javascript.Function;
12+
import org.mozilla.javascript.NativeFunction;
13+
import org.mozilla.javascript.Scriptable;
14+
import org.mozilla.javascript.regexp.NativeRegExp;
15+
import org.mozilla.javascript.tools.shell.Global;
16+
317
import cucumber.runtime.Backend;
418
import cucumber.runtime.CucumberException;
519
import cucumber.runtime.Glue;
@@ -8,18 +22,6 @@
822
import cucumber.runtime.io.ResourceLoader;
923
import cucumber.runtime.snippets.FunctionNameSanitizer;
1024
import cucumber.runtime.snippets.SnippetGenerator;
11-
import gherkin.formatter.model.Step;
12-
import org.mozilla.javascript.Context;
13-
import org.mozilla.javascript.NativeFunction;
14-
import org.mozilla.javascript.Scriptable;
15-
import org.mozilla.javascript.regexp.NativeRegExp;
16-
import org.mozilla.javascript.tools.shell.Global;
17-
18-
import java.io.IOException;
19-
import java.io.InputStreamReader;
20-
import java.util.List;
21-
22-
import static cucumber.runtime.io.MultiLoader.packageName;
2325

2426
public class RhinoBackend implements Backend {
2527
private static final String JS_DSL = "/cucumber/runtime/rhino/dsl.js";
@@ -56,7 +58,7 @@ public void loadGlue(Glue glue, List<String> gluePaths) {
5658

5759
@Override
5860
public void setUnreportedStepExecutor(UnreportedStepExecutor executor) {
59-
//Not used yet
61+
// Not used yet
6062
}
6163

6264
@Override
@@ -72,7 +74,7 @@ public String getSnippet(Step step, FunctionNameSanitizer functionNameSanitizer)
7274
return snippetGenerator.getSnippet(step, functionNameSanitizer);
7375
}
7476

75-
private StackTraceElement stepDefLocation() {
77+
private StackTraceElement jsLocation() {
7678
Throwable t = new Throwable();
7779
StackTraceElement[] stackTraceElements = t.getStackTrace();
7880
for (StackTraceElement stackTraceElement : stackTraceElements) {
@@ -89,8 +91,20 @@ private StackTraceElement stepDefLocation() {
8991
}
9092

9193
public void addStepDefinition(Global jsStepDefinition, NativeRegExp regexp, NativeFunction bodyFunc, NativeFunction argumentsFromFunc) throws Throwable {
92-
StackTraceElement stepDefLocation = stepDefLocation();
94+
StackTraceElement stepDefLocation = jsLocation();
9395
RhinoStepDefinition stepDefinition = new RhinoStepDefinition(cx, scope, jsStepDefinition, regexp, bodyFunc, stepDefLocation, argumentsFromFunc);
9496
glue.addStepDefinition(stepDefinition);
9597
}
98+
99+
public void addBeforeHook(Function fn, String[] tags, int order, long timeoutMillis) {
100+
StackTraceElement stepDefLocation = jsLocation();
101+
RhinoHookDefinition hookDefinition = new RhinoHookDefinition(cx, scope, fn, tags, order, timeoutMillis, stepDefLocation);
102+
glue.addBeforeHook(hookDefinition);
103+
}
104+
105+
public void addAfterHook(Function fn, String[] tags, int order, long timeoutMillis) {
106+
StackTraceElement stepDefLocation = jsLocation();
107+
RhinoHookDefinition hookDefinition = new RhinoHookDefinition(cx, scope, fn, tags, order, timeoutMillis, stepDefLocation);
108+
glue.addAfterHook(hookDefinition);
109+
}
96110
}

0 commit comments

Comments
 (0)