From 154d4589b2f4ea21a45a5c02466a58895d797a4a Mon Sep 17 00:00:00 2001 From: Martin Busik Date: Sun, 2 Sep 2012 01:34:17 +0200 Subject: [PATCH] TableDiff with list of pojos: camelcase convert of column names to field names --- .../table/xstream/ComplexTypeWriter.java | 5 +++- .../java/cucumber/table/TableDifferTest.java | 30 +++++++++++++++++++ 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/core/src/main/java/cucumber/table/xstream/ComplexTypeWriter.java b/core/src/main/java/cucumber/table/xstream/ComplexTypeWriter.java index d15d770a77..1d937c4e48 100644 --- a/core/src/main/java/cucumber/table/xstream/ComplexTypeWriter.java +++ b/core/src/main/java/cucumber/table/xstream/ComplexTypeWriter.java @@ -3,6 +3,8 @@ import java.util.ArrayList; import java.util.List; +import cucumber.table.CamelCaseStringConverter; + import static java.util.Arrays.asList; public class ComplexTypeWriter extends CellWriter { @@ -23,11 +25,12 @@ public List getHeader() { @Override public List getValues() { + CamelCaseStringConverter converter = new CamelCaseStringConverter(); if (columnNames.size() > 0) { String[] explicitFieldValues = new String[columnNames.size()]; int n = 0; for (String columnName : columnNames) { - int index = fieldNames.indexOf(columnName); + int index = fieldNames.indexOf(converter.map(columnName)); if (index == -1) { explicitFieldValues[n] = ""; } else { diff --git a/core/src/test/java/cucumber/table/TableDifferTest.java b/core/src/test/java/cucumber/table/TableDifferTest.java index 08d8f7770c..ce727d4899 100755 --- a/core/src/test/java/cucumber/table/TableDifferTest.java +++ b/core/src/test/java/cucumber/table/TableDifferTest.java @@ -2,6 +2,7 @@ import org.junit.Test; +import java.io.Serializable; import java.util.ArrayList; import java.util.List; @@ -120,4 +121,33 @@ public void should_not_fail_with_out_of_memory() { throw e; } } + @Test + public void diff_with_list_of_pojos_and_camelcase_header_mapping() { + String source = "" + + "| id | Given Name |\n" + + "| 1 | me |\n" + + "| 2 | you |\n" + + "| 3 | jdoe |\n"; + + DataTable expected = TableParser.parse(source, null); + + List actual = new ArrayList(); + actual.add(new TestPojo(1,"me",123)); + actual.add(new TestPojo(2,"you",222)); + actual.add(new TestPojo(3,"jdoe",34545)); + expected.diff(actual); + } } + +class TestPojo { + Integer id; + String givenName; + int decisionCriteria; + + public TestPojo(Integer id, String givenName , int decisionCriteria) { + this.id = id; + this.givenName = givenName; + this.decisionCriteria = decisionCriteria; + } +} +