|
2 | 2 |
|
3 | 3 | import cucumber.api.DataTable;
|
4 | 4 | import cucumber.deps.com.thoughtworks.xstream.annotations.XStreamConverter;
|
| 5 | +import cucumber.deps.com.thoughtworks.xstream.converters.SingleValueConverter; |
5 | 6 | import cucumber.deps.com.thoughtworks.xstream.converters.javabean.JavaBeanConverter;
|
| 7 | +import cucumber.runtime.CucumberException; |
6 | 8 | import cucumber.runtime.ParameterInfo;
|
7 | 9 | import org.junit.Test;
|
8 | 10 |
|
9 | 11 | import java.util.Arrays;
|
10 | 12 | import java.util.Calendar;
|
| 13 | +import java.util.Collections; |
11 | 14 | import java.util.Date;
|
12 | 15 | import java.util.HashMap;
|
13 | 16 | import java.util.List;
|
14 | 17 | import java.util.Locale;
|
15 | 18 | import java.util.Map;
|
16 | 19 |
|
17 | 20 | import static java.util.Arrays.asList;
|
| 21 | +import static java.util.Collections.emptyList; |
18 | 22 | import static org.junit.Assert.assertEquals;
|
| 23 | +import static org.junit.Assert.fail; |
19 | 24 |
|
20 | 25 | public class TableConverterTest {
|
21 | 26 |
|
@@ -184,6 +189,141 @@ public void converts_to_list_of_java_bean_and_almost_back() {
|
184 | 189 | assertEquals(" | birthDate | deathCal |\n | 1957-05-10 | 1979-02-02 |\n", table.toTable(converted).toString());
|
185 | 190 | }
|
186 | 191 |
|
| 192 | + public static class BlogBean { |
| 193 | + private String author; |
| 194 | + private List<String> tags; |
| 195 | + private String post; |
| 196 | + |
| 197 | + public String getPost() { |
| 198 | + return post; |
| 199 | + } |
| 200 | + |
| 201 | + public void setPost(String post) { |
| 202 | + this.post = post; |
| 203 | + } |
| 204 | + |
| 205 | + public String getAuthor() { |
| 206 | + return author; |
| 207 | + } |
| 208 | + |
| 209 | + public void setAuthor(String author) { |
| 210 | + this.author = author; |
| 211 | + } |
| 212 | + |
| 213 | + public List<String> getTags() { |
| 214 | + return tags; |
| 215 | + } |
| 216 | + |
| 217 | + public void setTags(List<String> tags) { |
| 218 | + this.tags = tags; |
| 219 | + } |
| 220 | + } |
| 221 | + |
| 222 | + @Test |
| 223 | + public void throws_cucumber_exception_for_complex_types() { |
| 224 | + BlogBean blog = new BlogBean(); |
| 225 | + blog.setAuthor("Tom Scott"); |
| 226 | + blog.setTags(asList("Language", "Linguistics", " Mycenaean Greek")); |
| 227 | + blog.setPost("Linear B is a syllabic script that was used for writing Mycenaean Greek..."); |
| 228 | + try { |
| 229 | + DataTable.create(Collections.singletonList(blog)); |
| 230 | + fail(); |
| 231 | + } catch (CucumberException expected) { |
| 232 | + assertEquals("" + |
| 233 | + "Don't know how to convert \"cucumber.runtime.table.TableConverterTest$BlogBean.tags\" into a table entry.\n" + |
| 234 | + "Either exclude tags from the table by selecting the fields to include:\n" + |
| 235 | + "\n" + |
| 236 | + "DataTable.create(entries, \"Field\", \"Other Field\")\n" + |
| 237 | + "\n" + |
| 238 | + "Or try writing your own converter:\n" + |
| 239 | + "\n" + |
| 240 | + "@cucumber.deps.com.thoughtworks.xstream.annotations.XStreamConverter(TagsConverter.class)\n" + |
| 241 | + "private List tags;\n", |
| 242 | + expected.getMessage()); |
| 243 | + } |
| 244 | + } |
| 245 | + |
| 246 | + @Test |
| 247 | + public void converts_empty_complex_types_and_almost_back() { |
| 248 | + DataTable table = TableParser.parse("" + |
| 249 | + "|Author |Tags |Post |\n" + |
| 250 | + "|Tom Scott| |Linear B is a...|\n", PARAMETER_INFO); |
| 251 | + List<BlogBean> converted = table.asList(BlogBean.class); |
| 252 | + BlogBean blog = converted.get(0); |
| 253 | + assertEquals("Tom Scott", blog.getAuthor()); |
| 254 | + assertEquals(emptyList(), blog.getTags()); |
| 255 | + assertEquals("Linear B is a...", blog.getPost()); |
| 256 | + assertEquals("" + |
| 257 | + " | author | tags | post |\n" + |
| 258 | + " | Tom Scott | | Linear B is a... |\n", |
| 259 | + table.toTable(converted).toString()); |
| 260 | + } |
| 261 | + |
| 262 | + public static class AnnotatedBlogBean { |
| 263 | + private String author; |
| 264 | + @XStreamConverter(TagsConverter.class) |
| 265 | + private List<String> tags; |
| 266 | + private String post; |
| 267 | + |
| 268 | + public String getPost() { |
| 269 | + return post; |
| 270 | + } |
| 271 | + |
| 272 | + public void setPost(String post) { |
| 273 | + this.post = post; |
| 274 | + } |
| 275 | + |
| 276 | + public String getAuthor() { |
| 277 | + return author; |
| 278 | + } |
| 279 | + |
| 280 | + public void setAuthor(String author) { |
| 281 | + this.author = author; |
| 282 | + } |
| 283 | + |
| 284 | + public List<String> getTags() { |
| 285 | + return tags; |
| 286 | + } |
| 287 | + |
| 288 | + public void setTags(List<String> tags) { |
| 289 | + this.tags = tags; |
| 290 | + } |
| 291 | + } |
| 292 | + |
| 293 | + public static class TagsConverter implements SingleValueConverter { |
| 294 | + |
| 295 | + @Override |
| 296 | + public String toString(Object o) { |
| 297 | + return o.toString().replace("[", "").replace("]", ""); |
| 298 | + } |
| 299 | + |
| 300 | + @Override |
| 301 | + public Object fromString(String s) { |
| 302 | + return asList(s.split(", ")); |
| 303 | + } |
| 304 | + |
| 305 | + @Override |
| 306 | + public boolean canConvert(Class type) { |
| 307 | + return List.class.isAssignableFrom(type); |
| 308 | + } |
| 309 | + } |
| 310 | + |
| 311 | + @Test |
| 312 | + public void converts_annotated_complex_types_and_almost_back() { |
| 313 | + DataTable table = TableParser.parse("" + |
| 314 | + "|Author |Tags |Post |\n" + |
| 315 | + "|Tom Scott|Language, Linguistics, Mycenaean Greek|Linear B is a...|\n", PARAMETER_INFO); |
| 316 | + List<AnnotatedBlogBean> converted = table.asList(AnnotatedBlogBean.class); |
| 317 | + AnnotatedBlogBean blog = converted.get(0); |
| 318 | + assertEquals("Tom Scott", blog.getAuthor()); |
| 319 | + assertEquals(asList("Language", "Linguistics", "Mycenaean Greek"), blog.getTags()); |
| 320 | + assertEquals("Linear B is a...", blog.getPost()); |
| 321 | + assertEquals("" + |
| 322 | + " | author | tags | post |\n" + |
| 323 | + " | Tom Scott | Language, Linguistics, Mycenaean Greek | Linear B is a... |\n", |
| 324 | + table.toTable(converted).toString()); |
| 325 | + } |
| 326 | + |
187 | 327 | @Test
|
188 | 328 | public void converts_to_list_of_map_of_date() {
|
189 | 329 | DataTable table = TableParser.parse("|Birth Date|Death Cal|\n|1957-05-10|1979-02-02|\n", PARAMETER_INFO);
|
|
0 commit comments