Skip to content

Commit 22e9a6b

Browse files
committed
adapt LocaleParameterByTypeTransformer of cucucmber-expressions
1 parent 4abdc5f commit 22e9a6b

File tree

4 files changed

+109
-13
lines changed

4 files changed

+109
-13
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1919
- [Core] Update dependency io.cucumber:html-formatter to v22.1.0
2020
- [Core] Update dependency io.cucumber:junit-xml-formatter to v0.11.0
2121
- [Core] Update dependency io.cucumber:pretty-formatter to v2.4.1
22+
- [Java] Any custom method declared as `@DefaultParameterTransformer` can now have a `Locale` argument to optionally consider the locale declaration of the current Feature ([cucumber/cucumber-expressions#376](https://github.com/cucumber/cucumber-expressions/issues/376) Stefan Gasterstädt)
2223

2324
### Fixed
2425
- [Core] Add OS version to `Meta` message ([#3108](https://github.com/cucumber/cucumber-jvm/pull/3108))

cucumber-java/src/main/java/io/cucumber/java/DefaultParameterTransformer.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,11 @@
1414
* <ul>
1515
* <li>{@code String, Type -> Object}</li>
1616
* <li>{@code Object, Type -> Object}</li>
17+
* <li>{@code String, Type, Locale -> Object}</li>
18+
* <li>{@code Object, Type, Locale -> Object}</li>
1719
* </ul>
1820
*
21+
* @see io.cucumber.cucumberexpressions.LocaleParameterByTypeTransformer
1922
* @see io.cucumber.cucumberexpressions.ParameterByTypeTransformer
2023
* @see io.cucumber.cucumberexpressions.ParameterType
2124
*/

cucumber-java/src/main/java/io/cucumber/java/JavaDefaultParameterTransformerDefinition.java

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,34 @@
22

33
import io.cucumber.core.backend.DefaultParameterTransformerDefinition;
44
import io.cucumber.core.backend.Lookup;
5-
import io.cucumber.cucumberexpressions.ParameterByTypeTransformer;
5+
import io.cucumber.cucumberexpressions.LocaleParameterByTypeTransformer;
66

77
import java.lang.reflect.Method;
88
import java.lang.reflect.Type;
9+
import java.util.Locale;
910

1011
import static io.cucumber.java.InvalidMethodSignatureException.builder;
1112

1213
class JavaDefaultParameterTransformerDefinition extends AbstractGlueDefinition
1314
implements DefaultParameterTransformerDefinition {
1415

1516
private final Lookup lookup;
16-
private final ParameterByTypeTransformer transformer;
17+
private final LocaleParameterByTypeTransformer transformer;
1718

1819
JavaDefaultParameterTransformerDefinition(Method method, Lookup lookup) {
1920
super(requireValidMethod(method), lookup);
2021
this.lookup = lookup;
21-
this.transformer = this::execute;
22+
this.transformer = new LocaleParameterByTypeTransformer() {
23+
@Override
24+
public Object transform(String fromValue, Type toValueType) throws Throwable {
25+
return this.transform(fromValue, toValueType, null);
26+
}
27+
28+
@Override
29+
public Object transform(String fromValue, Type toValueType, Locale locale) throws Throwable {
30+
return JavaDefaultParameterTransformerDefinition.this.execute(fromValue, toValueType, locale);
31+
}
32+
};
2233
}
2334

2435
private static Method requireValidMethod(Method method) {
@@ -28,7 +39,7 @@ private static Method requireValidMethod(Method method) {
2839
}
2940

3041
Class<?>[] parameterTypes = method.getParameterTypes();
31-
if (parameterTypes.length != 2) {
42+
if ((parameterTypes.length != 2) && (parameterTypes.length != 3)) {
3243
throw createInvalidSignatureException(method);
3344
}
3445

@@ -40,23 +51,33 @@ private static Method requireValidMethod(Method method) {
4051
throw createInvalidSignatureException(method);
4152
}
4253

54+
if ((parameterTypes.length == 3) && !Locale.class.equals(parameterTypes[2])) {
55+
throw createInvalidSignatureException(method);
56+
}
4357
return method;
4458
}
4559

46-
private Object execute(String fromValue, Type toValueType) {
47-
return Invoker.invoke(this, lookup.getInstance(method.getDeclaringClass()), method, fromValue, toValueType);
60+
private Object execute(String fromValue, Type toValueType, Locale locale) {
61+
if (method.getParameterTypes().length == 2) {
62+
return Invoker.invoke(this, lookup.getInstance(method.getDeclaringClass()), method, fromValue, toValueType);
63+
} else {
64+
return Invoker.invoke(this, lookup.getInstance(method.getDeclaringClass()), method, fromValue, toValueType,
65+
locale);
66+
}
4867
}
4968

5069
private static InvalidMethodSignatureException createInvalidSignatureException(Method method) {
5170
return builder(method)
5271
.addAnnotation(DefaultParameterTransformer.class)
5372
.addSignature("public Object defaultDataTableEntry(String fromValue, Type toValueType)")
5473
.addSignature("public Object defaultDataTableEntry(Object fromValue, Type toValueType)")
74+
.addSignature("public Object defaultDataTableEntry(String fromValue, Type toValueType, Locale locale)")
75+
.addSignature("public Object defaultDataTableEntry(Object fromValue, Type toValueType, Locale locale)")
5576
.build();
5677
}
5778

5879
@Override
59-
public ParameterByTypeTransformer parameterByTypeTransformer() {
80+
public LocaleParameterByTypeTransformer parameterByTypeTransformer() {
6081
return transformer;
6182
}
6283

cucumber-java/src/test/java/io/cucumber/java/JavaDefaultParameterTransformerDefinitionTest.java

Lines changed: 77 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
import java.lang.reflect.Method;
77
import java.lang.reflect.Type;
8+
import java.util.Locale;
89
import java.util.Map;
910

1011
import static org.hamcrest.CoreMatchers.startsWith;
@@ -33,10 +34,36 @@ void can_transform_string_to_type() throws Throwable {
3334
assertThat(transformed, is("transform_string_to_type"));
3435
}
3536

37+
@Test
38+
void can_transform_string_to_type_ignoring_locale() throws Throwable {
39+
Method method = JavaDefaultParameterTransformerDefinitionTest.class.getMethod("transform_string_to_type",
40+
String.class, Type.class);
41+
JavaDefaultParameterTransformerDefinition definition = new JavaDefaultParameterTransformerDefinition(method,
42+
lookup);
43+
Object transformed = definition.parameterByTypeTransformer().transform("something", String.class,
44+
Locale.ENGLISH);
45+
assertThat(transformed, is("transform_string_to_type"));
46+
}
47+
3648
public Object transform_string_to_type(String fromValue, Type toValueType) {
3749
return "transform_string_to_type";
3850
}
3951

52+
@Test
53+
void can_transform_string_to_type_using_locale() throws Throwable {
54+
Method method = JavaDefaultParameterTransformerDefinitionTest.class.getMethod(
55+
"transform_string_to_type_with_locale", String.class, Type.class, Locale.class);
56+
JavaDefaultParameterTransformerDefinition definition = new JavaDefaultParameterTransformerDefinition(method,
57+
lookup);
58+
Object transformed = definition.parameterByTypeTransformer().transform("something", String.class,
59+
Locale.ENGLISH);
60+
assertThat(transformed, is("transform_string_to_type_with_locale_en"));
61+
}
62+
63+
public Object transform_string_to_type_with_locale(String fromValue, Type toValueType, Locale locale) {
64+
return "transform_string_to_type_with_locale_" + locale.getLanguage();
65+
}
66+
4067
@Test
4168
void can_transform_object_to_type() throws Throwable {
4269
Method method = JavaDefaultParameterTransformerDefinitionTest.class.getMethod("transform_object_to_type",
@@ -47,10 +74,36 @@ void can_transform_object_to_type() throws Throwable {
4774
assertThat(transformed, is("transform_object_to_type"));
4875
}
4976

77+
@Test
78+
void can_transform_object_to_type_ignoring_locale() throws Throwable {
79+
Method method = JavaDefaultParameterTransformerDefinitionTest.class.getMethod("transform_object_to_type",
80+
Object.class, Type.class);
81+
JavaDefaultParameterTransformerDefinition definition = new JavaDefaultParameterTransformerDefinition(method,
82+
lookup);
83+
String transformed = (String) definition.parameterByTypeTransformer().transform("something", String.class,
84+
Locale.ENGLISH);
85+
assertThat(transformed, is("transform_object_to_type"));
86+
}
87+
5088
public Object transform_object_to_type(Object fromValue, Type toValueType) {
5189
return "transform_object_to_type";
5290
}
5391

92+
@Test
93+
void can_transform_object_to_type_using_locale() throws Throwable {
94+
Method method = JavaDefaultParameterTransformerDefinitionTest.class.getMethod(
95+
"transform_object_to_type_with_locale", Object.class, Type.class, Locale.class);
96+
JavaDefaultParameterTransformerDefinition definition = new JavaDefaultParameterTransformerDefinition(method,
97+
lookup);
98+
String transformed = (String) definition.parameterByTypeTransformer().transform("something", String.class,
99+
Locale.ENGLISH);
100+
assertThat(transformed, is("transform_object_to_type_with_locale_en"));
101+
}
102+
103+
public Object transform_object_to_type_with_locale(Object fromValue, Type toValueType, Locale locale) {
104+
return "transform_object_to_type_with_locale_" + locale.getLanguage();
105+
}
106+
54107
@Test
55108
void must_have_non_void_return() throws Throwable {
56109
Method method = JavaDefaultParameterTransformerDefinitionTest.class.getMethod("transforms_string_to_void",
@@ -61,29 +114,31 @@ void must_have_non_void_return() throws Throwable {
61114
"A @DefaultParameterTransformer annotated method must have one of these signatures:\n" +
62115
" * public Object defaultDataTableEntry(String fromValue, Type toValueType)\n" +
63116
" * public Object defaultDataTableEntry(Object fromValue, Type toValueType)\n" +
117+
" * public Object defaultDataTableEntry(String fromValue, Type toValueType, Locale locale)\n" +
118+
" * public Object defaultDataTableEntry(Object fromValue, Type toValueType, Locale locale)\n" +
64119
"at io.cucumber.java.JavaDefaultParameterTransformerDefinitionTest.transforms_string_to_void(java.lang.String,java.lang.reflect.Type)"));
65120
}
66121

67122
public void transforms_string_to_void(String fromValue, Type toValueType) {
68123
}
69124

70125
@Test
71-
void must_have_two_arguments() throws Throwable {
126+
void must_have_two_or_three_arguments() throws Throwable {
72127
Method oneArg = JavaDefaultParameterTransformerDefinitionTest.class.getMethod("one_argument", String.class);
73128
assertThrows(InvalidMethodSignatureException.class,
74129
() -> new JavaDefaultParameterTransformerDefinition(oneArg, lookup));
75-
Method threeArg = JavaDefaultParameterTransformerDefinitionTest.class.getMethod("three_arguments", String.class,
76-
Type.class, Object.class);
130+
Method fourArg = JavaDefaultParameterTransformerDefinitionTest.class.getMethod("four_arguments", String.class,
131+
Type.class, Locale.class, Object.class);
77132
assertThrows(InvalidMethodSignatureException.class,
78-
() -> new JavaDefaultParameterTransformerDefinition(threeArg, lookup));
133+
() -> new JavaDefaultParameterTransformerDefinition(fourArg, lookup));
79134
}
80135

81136
public Object one_argument(String fromValue) {
82137
return "one_argument";
83138
}
84139

85-
public Object three_arguments(String fromValue, Type toValueType, Object extra) {
86-
return "three_arguments";
140+
public Object four_arguments(String fromValue, Type toValueType, Locale locale, Object extra) {
141+
return "four_arguments";
87142
}
88143

89144
@Test
@@ -92,22 +147,38 @@ void must_have_string_or_object_as_from_value() throws Throwable {
92147
Type.class);
93148
assertThrows(InvalidMethodSignatureException.class,
94149
() -> new JavaDefaultParameterTransformerDefinition(twoArg, lookup));
150+
Method threeArg = JavaDefaultParameterTransformerDefinitionTest.class.getMethod("map_as_from_value_with_locale",
151+
Map.class, Type.class, Locale.class);
152+
assertThrows(InvalidMethodSignatureException.class,
153+
() -> new JavaDefaultParameterTransformerDefinition(threeArg, lookup));
95154
}
96155

97156
public Object map_as_from_value(Map<String, String> fromValue, Type toValueType) {
98157
return "map_as_from_value";
99158
}
100159

160+
public Object map_as_from_value_with_locale(Map<String, String> fromValue, Type toValueType, Locale locale) {
161+
return "map_as_from_value_with_locale";
162+
}
163+
101164
@Test
102165
void must_have_type_as_to_value_type() throws Throwable {
103166
Method twoArg = JavaDefaultParameterTransformerDefinitionTest.class.getMethod("object_as_to_value_type",
104167
String.class, Object.class);
105168
assertThrows(InvalidMethodSignatureException.class,
106169
() -> new JavaDefaultParameterTransformerDefinition(twoArg, lookup));
170+
Method threeArg = JavaDefaultParameterTransformerDefinitionTest.class.getMethod(
171+
"object_as_to_value_type_with_locale", String.class, Object.class, Locale.class);
172+
assertThrows(InvalidMethodSignatureException.class,
173+
() -> new JavaDefaultParameterTransformerDefinition(threeArg, lookup));
107174
}
108175

109176
public Object object_as_to_value_type(String fromValue, Object toValueType) {
110177
return "object_as_to_value_type";
111178
}
112179

180+
public Object object_as_to_value_type_with_locale(String fromValue, Object toValueType, Locale locale) {
181+
return "object_as_to_value_type_with_locale";
182+
}
183+
113184
}

0 commit comments

Comments
 (0)