Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit bd0c0ac

Browse files
aslakhellesoybrasmusson
authored andcommitted
Some more hacks to fix recent java8 bugs. Needs cleanup.
1 parent 93a833a commit bd0c0ac

File tree

5 files changed

+88
-87
lines changed

5 files changed

+88
-87
lines changed

History.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
## [1.2.5-SNAPSHOT](https://github.com/cucumber/cucumber-jvm/compare/v1.2.4...master) (In Git)
22

3+
* [Java8]
34
* [Java8] Fix IllegalArgumentException on JDK 1.8.0_60 ([#912](https://github.com/cucumber/cucumber-jvm/issues/912), [#914](https://github.com/cucumber/cucumber-jvm/pull/914) Michael Wilkerson)
45
* [Core] Double-check for directory exists in the ensureParentDirExists(File) ([#978](https://github.com/cucumber/cucumber-jvm/pull/978) Pavel Ordenko)
56
* [picocontainer] Picocontainer lifecycle support([#994](https://github.com/cucumber/cucumber-jvm/pull/994), [#993](https://github.com/cucumber/cucumber-jvm/issues/993), [#992](https://github.com/cucumber/cucumber-jvm/pull/992) Richard Bradley)

java/src/main/java/cucumber/runtime/java/Java8StepDefinition.java

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import gherkin.formatter.model.Step;
1212

1313
import java.lang.reflect.Method;
14+
import java.lang.reflect.ParameterizedType;
1415
import java.lang.reflect.Type;
1516
import java.util.ArrayList;
1617
import java.util.List;
@@ -39,7 +40,27 @@ public Java8StepDefinition(Pattern pattern, long timeoutMillis, StepdefBody body
3940

4041
Class<? extends StepdefBody> bodyClass = body.getClass();
4142

42-
ArrayList<Method> acceptMethods = new ArrayList<Method>();
43+
this.method = getAcceptMethod(bodyClass);
44+
this.parameterInfos = getParameterInfos(bodyClass, typeIntrospector, method.getParameterTypes().length);
45+
}
46+
47+
private List<ParameterInfo> getParameterInfos(Class<? extends StepdefBody> bodyClass, TypeIntrospector typeIntrospector, int parameterCount) throws Exception {
48+
Type genericInterface = bodyClass.getGenericInterfaces()[0];
49+
Type[] argumentTypes;
50+
if (genericInterface instanceof ParameterizedType) {
51+
argumentTypes = ((ParameterizedType) genericInterface).getActualTypeArguments();
52+
} else {
53+
argumentTypes = typeIntrospector.getGenericTypes(bodyClass);
54+
}
55+
Type[] argumentTypesOfCorrectLength = new Type[parameterCount];
56+
System.arraycopy(argumentTypes, argumentTypes.length - parameterCount, argumentTypesOfCorrectLength, 0, parameterCount);
57+
verifyNotListOrMap(argumentTypesOfCorrectLength);
58+
59+
return ParameterInfo.fromTypes(argumentTypesOfCorrectLength);
60+
}
61+
62+
private Method getAcceptMethod(Class<? extends StepdefBody> bodyClass) {
63+
List<Method> acceptMethods = new ArrayList<Method>();
4364
for (Method method : bodyClass.getDeclaredMethods()) {
4465
if (!method.isBridge() && !method.isSynthetic() && "accept".equals(method.getName())) {
4566
acceptMethods.add(method);
@@ -49,25 +70,22 @@ public Java8StepDefinition(Pattern pattern, long timeoutMillis, StepdefBody body
4970
throw new IllegalStateException(String.format("Expected single 'accept' method on body class, found " +
5071
"'%s'", acceptMethods));
5172
}
52-
this.method = acceptMethods.get(0);
53-
Type[] argumentTypes = method.getGenericParameterTypes();
54-
verifyNotListOrMap(argumentTypes);
55-
this.parameterInfos = ParameterInfo.fromTypes(argumentTypes);
73+
return acceptMethods.get(0);
5674
}
5775

5876
private void verifyNotListOrMap(Type[] argumentTypes) {
5977
for (Type argumentType : argumentTypes) {
60-
if(argumentType instanceof Class) {
78+
if (argumentType instanceof Class) {
6179
Class<?> argumentClass = (Class<?>) argumentType;
62-
if(List.class.isAssignableFrom(argumentClass) || Map.class.isAssignableFrom(argumentClass)) {
80+
if (List.class.isAssignableFrom(argumentClass) || Map.class.isAssignableFrom(argumentClass)) {
6381
throw withLocation(new CucumberException("Can't use " + argumentClass.getName() + " in lambda step definition. Declare a DataTable argument instead and convert manually with asList/asLists/asMap/asMaps"));
6482
}
6583
}
6684
}
6785
}
6886

6987
private CucumberException withLocation(CucumberException exception) {
70-
exception.setStackTrace(new StackTraceElement[] {this.location});
88+
exception.setStackTrace(new StackTraceElement[]{this.location});
7189
return exception;
7290
}
7391

java/src/test/java/cucumber/runtime/java/Java8StepDefinitionTest.java

Lines changed: 0 additions & 76 deletions
This file was deleted.
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package cucumber.runtime.java8.test;
2+
3+
import cucumber.api.java8.StepdefBody;
4+
import cucumber.runtime.CucumberException;
5+
import cucumber.runtime.java.Java8StepDefinition;
6+
import cucumber.runtime.java.TypeIntrospector;
7+
import cucumber.runtime.java8.ConstantPoolTypeIntrospector;
8+
import org.junit.Test;
9+
10+
import java.util.List;
11+
import java.util.regex.Pattern;
12+
13+
import static org.junit.Assert.assertEquals;
14+
15+
public class Java8StepDefinitionTest {
16+
private final TypeIntrospector typeIntrospector = new ConstantPoolTypeIntrospector();
17+
18+
@Test
19+
public void should_calculate_parameters_count_from_body_with_one_param() throws Exception {
20+
StepdefBody body = (StepdefBody.A1<String>) p1 -> {
21+
};
22+
Java8StepDefinition def = new Java8StepDefinition(Pattern.compile("^I have (\\d) some step (.*)$"), 0, body, typeIntrospector);
23+
assertEquals(new Integer(1), def.getParameterCount());
24+
}
25+
26+
@Test
27+
public void should_calculate_parameters_count_from_body_with_two_params() throws Exception {
28+
StepdefBody body = (StepdefBody.A2<String, String>) (p1, p2) -> {
29+
};
30+
Java8StepDefinition def = new Java8StepDefinition(Pattern.compile("^I have some step $"), 0, body, typeIntrospector);
31+
assertEquals(new Integer(2), def.getParameterCount());
32+
}
33+
34+
@Test
35+
public void should_fail_for_param_with_non_generic_list() throws Exception {
36+
try {
37+
StepdefBody body = (StepdefBody.A1<List>) p1 -> {
38+
};
39+
new Java8StepDefinition(Pattern.compile("^I have (\\d) some step (.*)$"), 0, body, typeIntrospector);
40+
} catch (CucumberException expected) {
41+
assertEquals("Can't use java.util.List in lambda step definition. Declare a DataTable argument instead and convert manually with asList/asLists/asMap/asMaps", expected.getMessage());
42+
}
43+
}
44+
45+
@Test
46+
public void should_pass_for_param_with_generic_list() throws Exception {
47+
try {
48+
StepdefBody body = (StepdefBody.A1<List<String>>) p1 -> {
49+
};
50+
new Java8StepDefinition(Pattern.compile("^I have (\\d) some step (.*)$"), 0, body, typeIntrospector);
51+
} catch (CucumberException expected) {
52+
assertEquals("Can't use java.util.List in lambda step definition. Declare a DataTable argument instead and convert manually with asList/asLists/asMap/asMaps", expected.getMessage());
53+
}
54+
}
55+
56+
}

java8/src/test/java/cucumber/runtime/java8/test/LambdaStepdefs.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
package cucumber.runtime.java8.test;
22

33
import cucumber.api.DataTable;
4-
import cucumber.api.PendingException;
54
import cucumber.api.Scenario;
65
import cucumber.api.java8.En;
7-
import cucumber.api.java8.StepdefBody;
86

97
import java.util.List;
108

@@ -19,13 +17,17 @@ public LambdaStepdefs() {
1917
assertNotSame(this, lastInstance);
2018
lastInstance = this;
2119
});
20+
2221
Given("^this data table:$", (DataTable peopleTable) -> {
2322
List<Person> people = peopleTable.asList(Person.class);
2423
assertEquals("Hellesøy", people.get(0).last);
2524
});
25+
2626
Integer alreadyHadThisManyCukes = 1;
27-
Given("^I have 42 cukes in my belly$", () -> {
27+
28+
Given("^I have (\\d+) cukes in my belly$", (Long n) -> {
2829
assertEquals((Integer) 1, alreadyHadThisManyCukes);
30+
assertEquals((Long) 42L, n);
2931
});
3032
}
3133

0 commit comments

Comments
 (0)