10
10
import static org .hamcrest .MatcherAssert .assertThat ;
11
11
import static org .hamcrest .Matchers .equalTo ;
12
12
import static org .hamcrest .Matchers .equalToCompressingWhiteSpace ;
13
+ import static org .junit .jupiter .api .Assertions .assertAll ;
13
14
import static org .junit .jupiter .api .Assertions .assertThrows ;
14
15
15
16
class DocStringTypeRegistryDocStringConverterTest {
16
17
17
- private final DocStringTypeRegistry registry = new DocStringTypeRegistry ();
18
- private final DocStringTypeRegistryDocStringConverter converter = new DocStringTypeRegistryDocStringConverter (
19
- registry );
18
+ static final DocStringType jsonNodeForJson = new DocStringType (
19
+ JsonNode .class ,
20
+ "json" ,
21
+ (String s ) -> new ObjectMapper ().readTree (s ));
22
+ static final DocStringType stringForText = new DocStringType (
23
+ String .class ,
24
+ "text" ,
25
+ (String s ) -> s );
26
+ static final DocStringType stringForXml = new DocStringType (
27
+ String .class ,
28
+ "xml" ,
29
+ (String s ) -> s );
30
+ static final DocStringType stringForYaml = new DocStringType (
31
+ String .class ,
32
+ "yml" ,
33
+ (String s ) -> s );
34
+ public static final DocStringType jsonNodeForXml = new DocStringType (
35
+ JsonNode .class ,
36
+ "xml" ,
37
+ (String s ) -> new ObjectMapper ().readTree (s ));
38
+ public static final DocStringType stringForJson = new DocStringType (
39
+ String .class ,
40
+ "json" ,
41
+ (String s ) -> s );
42
+
43
+ final DocStringTypeRegistry registry = new DocStringTypeRegistry ();
44
+ final DocStringTypeRegistryDocStringConverter converter = new DocStringTypeRegistryDocStringConverter (registry );
20
45
21
46
@ Test
22
47
void throws_when_uses_doc_string_type_but_downcast_conversion () {
23
- registry .defineDocStringType (new DocStringType (
24
- JsonNode .class ,
25
- "json" ,
26
- (String s ) -> new ObjectMapper ().readTree (s )));
48
+ registry .defineDocStringType (jsonNodeForJson );
27
49
28
50
DocString docString = DocString .create (
29
51
"{\" hello\" :\" world\" }" ,
@@ -39,10 +61,7 @@ void throws_when_uses_doc_string_type_but_downcast_conversion() {
39
61
40
62
@ Test
41
63
void uses_target_type_when_available () {
42
- registry .defineDocStringType (new DocStringType (
43
- JsonNode .class ,
44
- "json" ,
45
- (String s ) -> new ObjectMapper ().readTree (s )));
64
+ registry .defineDocStringType (jsonNodeForJson );
46
65
47
66
DocString docString = DocString .create (
48
67
"{\" hello\" :\" world\" }" );
@@ -62,10 +81,7 @@ void target_type_to_string_is_predefined() {
62
81
63
82
@ Test
64
83
void default_converter_is_used_if_registered_converter_does_not_match_type () {
65
- registry .defineDocStringType (new DocStringType (
66
- JsonNode .class ,
67
- "json" ,
68
- (String s ) -> new ObjectMapper ().readTree (s )));
84
+ registry .defineDocStringType (jsonNodeForJson );
69
85
70
86
DocString docString = DocString .create (
71
87
"hello world" , "json" );
@@ -76,10 +92,7 @@ void default_converter_is_used_if_registered_converter_does_not_match_type() {
76
92
77
93
@ Test
78
94
void default_converter_never_conflicts_with_registered_converter_for_string () {
79
- registry .defineDocStringType (new DocStringType (
80
- String .class ,
81
- "text" ,
82
- (String s ) -> s ));
95
+ registry .defineDocStringType (stringForText );
83
96
84
97
DocString docString = DocString .create ("hello world" );
85
98
@@ -89,25 +102,20 @@ void default_converter_never_conflicts_with_registered_converter_for_string() {
89
102
90
103
@ Test
91
104
void throws_if_converter_type_conflicts_with_type () {
92
- registry .defineDocStringType (new DocStringType (
93
- JsonNode .class ,
94
- "json" ,
95
- (String s ) -> new ObjectMapper ().readTree (s )));
96
-
97
- registry .defineDocStringType (new DocStringType (
98
- String .class ,
99
- "text" ,
100
- (String s ) -> s ));
105
+ registry .defineDocStringType (jsonNodeForJson );
106
+ registry .defineDocStringType (stringForText );
101
107
102
108
DocString docString = DocString .create ("hello world" , "json" );
103
109
104
110
CucumberDocStringException exception = assertThrows (
105
111
CucumberDocStringException .class ,
106
112
() -> converter .convert (docString , String .class ));
107
113
114
+ // TODO: This exception should explain,
115
+
108
116
assertThat (exception .getMessage (),
109
117
is ("Multiple converters found for type java.lang.String, and the content type 'json' " +
110
- "did not match any of the registered types [text]. Change the content type of the docstring " +
118
+ "did not match any of the registered types [[anonymous], text]. Change the content type of the docstring " +
111
119
"or register a docstring type for 'json'" ));
112
120
}
113
121
@@ -187,15 +195,9 @@ void converts_no_content_type_doc_string_to_registered_matching_convertor() {
187
195
188
196
@ Test
189
197
void throws_when_multiple_convertors_available () {
190
- registry .defineDocStringType (new DocStringType (
191
- JsonNode .class ,
192
- "json" ,
193
- (String s ) -> new ObjectMapper ().readTree (s )));
198
+ registry .defineDocStringType (jsonNodeForJson );
194
199
195
- registry .defineDocStringType (new DocStringType (
196
- JsonNode .class ,
197
- "xml" ,
198
- (String s ) -> new ObjectMapper ().readTree (s )));
200
+ registry .defineDocStringType (jsonNodeForXml );
199
201
200
202
DocString docString = DocString .create (
201
203
"{\" hello\" :\" world\" }" );
@@ -211,20 +213,9 @@ void throws_when_multiple_convertors_available() {
211
213
212
214
@ Test
213
215
void different_docstring_content_types_convert_to_matching_doc_string_types () {
214
- registry .defineDocStringType (new DocStringType (
215
- String .class ,
216
- "json" ,
217
- (String s ) -> s ));
218
-
219
- registry .defineDocStringType (new DocStringType (
220
- String .class ,
221
- "xml" ,
222
- (String s ) -> s ));
223
-
224
- registry .defineDocStringType (new DocStringType (
225
- String .class ,
226
- "yml" ,
227
- (String s ) -> s ));
216
+ registry .defineDocStringType (stringForJson );
217
+ registry .defineDocStringType (stringForXml );
218
+ registry .defineDocStringType (stringForYaml );
228
219
229
220
DocString docStringJson = DocString .create (
230
221
"{\" content\" :\" hello world\" }" , "json" );
@@ -237,9 +228,11 @@ void different_docstring_content_types_convert_to_matching_doc_string_types() {
237
228
String convertXml = converter .convert (docStringXml , String .class );
238
229
String convertYml = converter .convert (docStringYml , String .class );
239
230
240
- assertThat (docStringJson .getContent (), equalTo (convertJson ));
241
- assertThat (docStringXml .getContent (), equalTo (convertXml ));
242
- assertThat (docStringYml .getContent (), equalTo (convertYml ));
231
+ assertAll (
232
+ () -> assertThat (docStringJson .getContent (), equalTo (convertJson )),
233
+ () -> assertThat (docStringXml .getContent (), equalTo (convertXml )),
234
+ () -> assertThat (docStringYml .getContent (), equalTo (convertYml ))
235
+ );
243
236
}
244
237
245
238
@ Test
0 commit comments