1
- package cucumber .runtime . java ;
1
+ package cucumber .table ;
2
2
3
3
import com .thoughtworks .xstream .annotations .XStreamConverter ;
4
+ import com .thoughtworks .xstream .converters .Converter ;
5
+ import com .thoughtworks .xstream .converters .MarshallingContext ;
6
+ import com .thoughtworks .xstream .converters .UnmarshallingContext ;
4
7
import com .thoughtworks .xstream .converters .javabean .JavaBeanConverter ;
5
- import cucumber .annotation .DateFormat ;
8
+ import com .thoughtworks .xstream .io .HierarchicalStreamReader ;
9
+ import com .thoughtworks .xstream .io .HierarchicalStreamWriter ;
10
+ import cucumber .DateFormat ;
6
11
import cucumber .runtime .CucumberException ;
12
+ import cucumber .runtime .ParameterType ;
7
13
import cucumber .runtime .StepDefinition ;
8
14
import cucumber .runtime .StepDefinitionMatch ;
9
15
import cucumber .runtime .converters .LocalizedXStreams ;
10
- import cucumber .table .DataTable ;
11
16
import gherkin .I18n ;
12
17
import gherkin .formatter .Argument ;
13
18
import gherkin .formatter .model .Comment ;
24
29
import java .util .List ;
25
30
import java .util .Map ;
26
31
import java .util .TimeZone ;
27
- import java .util .regex .Pattern ;
28
32
29
33
import static java .util .Arrays .asList ;
30
34
import static java .util .Collections .emptyList ;
31
35
import static junit .framework .Assert .assertEquals ;
32
36
33
- public class JavaTableConversionTest {
37
+ public class FromDataTableTest {
34
38
@ Rule
35
39
public ExpectedException thrown = ExpectedException .none ();
36
40
@@ -40,6 +44,7 @@ public class JavaTableConversionTest {
40
44
public static class StepDefs {
41
45
public List <UserPojo > listOfPojos ;
42
46
public List <UserBean > listOfBeans ;
47
+ public List <UserWithNameField > listOfUsersWithNameField ;
43
48
public List <List <Double >> listOfListOfDoubles ;
44
49
public List <Map <String , String >> listOfMapsOfStringToString ;
45
50
public List <Map <String , Object >> listOfMapsOfStringToObject ;
@@ -54,6 +59,10 @@ public void listOfBeans(@DateFormat("yyyy-MM-dd") List<UserBean> listOfBeans) {
54
59
this .listOfBeans = listOfBeans ;
55
60
}
56
61
62
+ public void listOfUsersWithNameField (@ DateFormat ("yyyy-MM-dd" ) List <UserWithNameField > listOfUsersWithNameField ) {
63
+ this .listOfUsersWithNameField = listOfUsersWithNameField ;
64
+ }
65
+
57
66
public void listOfListOfDoubles (List <List <Double >> listOfListOfDoubles ) {
58
67
this .listOfListOfDoubles = listOfListOfDoubles ;
59
68
}
@@ -94,6 +103,15 @@ public void transforms_to_list_of_beans() throws Throwable {
94
103
assertEquals (sidsBirthday (), stepDefs .listOfBeans .get (0 ).getBirthDate ());
95
104
}
96
105
106
+ @ Test
107
+ public void converts_table_to_list_of_class_with_special_fields () throws Throwable {
108
+ Method m = StepDefs .class .getMethod ("listOfUsersWithNameField" , List .class );
109
+ StepDefs stepDefs = runStepDef (m , listOfDatesAndNamesWithHeader ());
110
+ assertEquals (sidsBirthday (), stepDefs .listOfUsersWithNameField .get (0 ).birthDate );
111
+ assertEquals ("Sid" , stepDefs .listOfUsersWithNameField .get (0 ).name .first );
112
+ assertEquals ("Vicious" , stepDefs .listOfUsersWithNameField .get (0 ).name .last );
113
+ }
114
+
97
115
@ Test
98
116
public void transforms_to_list_of_single_values () throws Throwable {
99
117
Method m = StepDefs .class .getMethod ("listOfListOfDoubles" , List .class );
@@ -152,7 +170,7 @@ public void does_not_transform_to_list_of_non_generic_map() throws Throwable {
152
170
153
171
private StepDefs runStepDef (Method method , List <DataTableRow > rows ) throws Throwable {
154
172
StepDefs stepDefs = new StepDefs ();
155
- StepDefinition stepDefinition = new JavaStepDefinition ( method , Pattern . compile ( "whatever" ), new SingletonFactory ( stepDefs ) );
173
+ StepDefinition stepDefinition = new DirectStepDef ( stepDefs , method );
156
174
157
175
Step stepWithRows = new Step (NO_COMMENTS , "Given " , "something" , 10 , rows , null );
158
176
@@ -169,6 +187,13 @@ private List<DataTableRow> listOfDatesWithHeader() {
169
187
return rows ;
170
188
}
171
189
190
+ private List <DataTableRow > listOfDatesAndNamesWithHeader () {
191
+ List <DataTableRow > rows = new ArrayList <DataTableRow >();
192
+ rows .add (new DataTableRow (NO_COMMENTS , asList ("Birth Date" , "Name" ), 1 ));
193
+ rows .add (new DataTableRow (NO_COMMENTS , asList ("1957-05-10" , "Sid Vicious" ), 2 ));
194
+ return rows ;
195
+ }
196
+
172
197
private List <DataTableRow > listOfDoublesWithoutHeader () {
173
198
List <DataTableRow > rows = new ArrayList <DataTableRow >();
174
199
rows .add (new DataTableRow (NO_COMMENTS , asList ("100.5" , "99.5" ), 2 ));
@@ -201,4 +226,76 @@ public void setBirthDate(Date birthDate) {
201
226
this .birthDateX = birthDate ;
202
227
}
203
228
}
229
+
230
+ public static class UserWithNameField {
231
+ public Name name ;
232
+ public Date birthDate ;
233
+ }
234
+
235
+ @ XStreamConverter (NameConverter .class )
236
+ public static class Name {
237
+ public String first ;
238
+ public String last ;
239
+ }
240
+
241
+ public static class NameConverter implements Converter {
242
+ @ Override
243
+ public void marshal (Object source , HierarchicalStreamWriter writer , MarshallingContext context ) {
244
+ throw new UnsupportedOperationException ();
245
+ }
246
+
247
+ @ Override
248
+ public Object unmarshal (HierarchicalStreamReader reader , UnmarshallingContext context ) {
249
+ Name name = new Name ();
250
+ String [] firstLast = reader .getValue ().split (" " );
251
+ name .first = firstLast [0 ];
252
+ name .last = firstLast [1 ];
253
+ return name ;
254
+ }
255
+
256
+ @ Override
257
+ public boolean canConvert (Class type ) {
258
+ return type .equals (Name .class );
259
+ }
260
+ }
261
+
262
+ private class DirectStepDef implements StepDefinition {
263
+ private final Object target ;
264
+ private final Method method ;
265
+
266
+ public DirectStepDef (Object target , Method method ) {
267
+ this .target = target ;
268
+ this .method = method ;
269
+ }
270
+
271
+ @ Override
272
+ public List <Argument > matchedArguments (Step step ) {
273
+ throw new UnsupportedOperationException ();
274
+ }
275
+
276
+ @ Override
277
+ public String getLocation () {
278
+ return getClass ().getName ();
279
+ }
280
+
281
+ @ Override
282
+ public List <ParameterType > getParameterTypes () {
283
+ return ParameterType .fromMethod (method );
284
+ }
285
+
286
+ @ Override
287
+ public void execute (I18n i18n , Object [] args ) throws Throwable {
288
+ method .invoke (target , args );
289
+ }
290
+
291
+ @ Override
292
+ public boolean isDefinedAt (StackTraceElement stackTraceElement ) {
293
+ return false ;
294
+ }
295
+
296
+ @ Override
297
+ public String getPattern () {
298
+ throw new UnsupportedOperationException ();
299
+ }
300
+ }
204
301
}
0 commit comments