Skip to content

Commit 97f14d5

Browse files
committed
Make it easier to create a DataTable
1 parent fffcca7 commit 97f14d5

File tree

10 files changed

+115
-27
lines changed

10 files changed

+115
-27
lines changed

History.md

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## [Git master](https://github.com/cucumber/cucumber-jvm/compare/v1.0.1...master)
2+
3+
* [Core] Added DataTable.toTable(List<?> other) for creating a new table. Handy for printing a table when diffing isn't helpful. (Aslak Hellesøy)
4+
15
## [1.0.2](https://github.com/cucumber/cucumber-jvm/compare/v1.0.1...v1.0.2)
26

37
* [Java] Snippets using a table have a hint about how to use List<YourClass>. (Aslak Hellesøy)

core/src/main/java/cucumber/runtime/StepDefinitionMatch.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ public List<String> map(DataTableRow row) {
134134
}
135135

136136
private Object tableArgument(Step step, int argIndex, XStream xStream, String dateFormat) {
137-
DataTable table = new DataTable(step.getRows(), new TableConverter(xStream, dateFormat));
137+
DataTable table = new DataTable(step.getRows(), new TableConverter(xStream));
138138

139139
Type listType = getGenericListType(argIndex);
140140
if (listType != null) {

core/src/main/java/cucumber/table/DataTable.java

+27-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package cucumber.table;
22

3+
import cucumber.runtime.converters.LocalizedXStreams;
4+
import gherkin.I18n;
35
import gherkin.formatter.PrettyFormatter;
46
import gherkin.formatter.model.DataTableRow;
57
import gherkin.formatter.model.Row;
@@ -14,6 +16,11 @@ public class DataTable {
1416
private final List<DataTableRow> gherkinRows;
1517
private final TableConverter tableConverter;
1618

19+
public static DataTable create(List<?> raw) {
20+
TableConverter tableConverter = new TableConverter(new LocalizedXStreams(Thread.currentThread().getContextClassLoader()).get(new I18n("en")));
21+
return tableConverter.toTable(raw);
22+
}
23+
1724
public DataTable(List<DataTableRow> gherkinRows, TableConverter tableConverter) {
1825
this.gherkinRows = gherkinRows;
1926
this.tableConverter = tableConverter;
@@ -54,11 +61,28 @@ private List<String> toStrings(Row row) {
5461
return strings;
5562
}
5663

57-
public void diff(List<List<String>> other) {
58-
diff(tableConverter.toTable(other));
64+
public DataTable toTable(List<?> raw) {
65+
return tableConverter.toTable(raw);
66+
}
67+
68+
/**
69+
* Diffs this table with {@code other}, which can be a {@code List<List<String>>} or a
70+
* {@code List<YourType>}.
71+
*
72+
* @param other the other table to diff with.
73+
* @throws TableDiffException if the tables are different.
74+
*/
75+
public void diff(List<?> other) throws TableDiffException {
76+
diff(toTable(other));
5977
}
6078

61-
private void diff(DataTable other) {
79+
/**
80+
* Diffs this table with {@code other}.
81+
*
82+
* @param other the other table to diff with.
83+
* @throws TableDiffException if the tables are different.
84+
*/
85+
public void diff(DataTable other) throws TableDiffException {
6286
new TableDiffer(this, other).calculateDiffs();
6387
}
6488

core/src/main/java/cucumber/table/TableConverter.java

+4-5
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,9 @@
2323

2424
public class TableConverter {
2525
private final XStream xStream;
26-
private final String dateFormat;
2726

28-
public TableConverter(XStream xStream, String dateFormat) {
27+
public TableConverter(XStream xStream) {
2928
this.xStream = xStream;
30-
this.dateFormat = dateFormat;
3129
}
3230

3331
/**
@@ -51,10 +49,11 @@ public <T> List<T> toList(Type itemType, DataTable dataTable) {
5149
/**
5250
* Converts a List of objects to a DataTable.
5351
*
54-
* @param objects the objects to convers
52+
* @param objects the objects to convert
53+
* @param columnNames an explicit list of column names (currently not used)
5554
* @return a DataTable
5655
*/
57-
public DataTable toTable(List<?> objects) {
56+
public DataTable toTable(List<?> objects, String... columnNames) {
5857
DataTableWriter writer;
5958
if (isListOfListOfSingleValue(objects)) {
6059
objects = wrapLists((List<List<?>>) objects);

core/src/main/java/cucumber/table/TableDiffer.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ private void checkColumns(DataTable a, DataTable b) {
2828
}
2929
}
3030

31-
public void calculateDiffs() {
31+
public void calculateDiffs() throws TableDiffException {
3232
Patch patch = DiffUtils.diff(orig.diffableRows(), other.diffableRows());
3333
List<Delta> deltas = patch.getDeltas();
3434
if (!deltas.isEmpty()) {

core/src/test/java/cucumber/table/DataTableTest.java

+30-14
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import gherkin.I18n;
66
import gherkin.formatter.model.Comment;
77
import gherkin.formatter.model.DataTableRow;
8-
import org.junit.Before;
98
import org.junit.Test;
109

1110
import java.util.ArrayList;
@@ -16,24 +15,41 @@
1615

1716
public class DataTableTest {
1817

19-
private DataTable simpleTable;
20-
21-
@Before
22-
public void initSimpleTable() {
23-
List<DataTableRow> simpleRows = new ArrayList<DataTableRow>();
24-
simpleRows.add(new DataTableRow(new ArrayList<Comment>(), asList("one", "four", "seven"), 1));
25-
simpleRows.add(new DataTableRow(new ArrayList<Comment>(), asList("4444", "55555", "666666"), 2));
26-
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
27-
XStream xStream = new LocalizedXStreams(classLoader).get(new I18n("en"));
28-
simpleTable = new DataTable(simpleRows, new TableConverter(xStream, null));
29-
}
30-
3118
@Test
3219
public void rawShouldHaveThreeColumnsAndTwoRows() {
33-
List<List<String>> raw = simpleTable.raw();
20+
List<List<String>> raw = createSimpleTable().raw();
3421
assertEquals("Rows size", 2, raw.size());
3522
for (List<String> list : raw) {
3623
assertEquals("Cols size: " + list, 3, list.size());
3724
}
3825
}
26+
27+
@Test
28+
public void canCreateTableFromListOfListOfString() {
29+
DataTable dataTable = createSimpleTable();
30+
DataTable other = dataTable.toTable(dataTable.raw());
31+
assertEquals("" +
32+
" | one | four | seven |\n" +
33+
" | 4444 | 55555 | 666666 |\n",
34+
other.toString());
35+
}
36+
37+
@Test
38+
public void canCreateTableFromListOfListOfStringWithoutOtherTable() {
39+
DataTable dataTable = createSimpleTable();
40+
DataTable other = DataTable.create(dataTable.raw());
41+
assertEquals("" +
42+
" | one | four | seven |\n" +
43+
" | 4444 | 55555 | 666666 |\n",
44+
other.toString());
45+
}
46+
47+
public DataTable createSimpleTable() {
48+
List<DataTableRow> simpleRows = new ArrayList<DataTableRow>();
49+
simpleRows.add(new DataTableRow(new ArrayList<Comment>(), asList("one", "four", "seven"), 1));
50+
simpleRows.add(new DataTableRow(new ArrayList<Comment>(), asList("4444", "55555", "666666"), 2));
51+
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
52+
XStream xStream = new LocalizedXStreams(classLoader).get(new I18n("en"));
53+
return new DataTable(simpleRows, new TableConverter(xStream));
54+
}
3955
}

core/src/test/java/cucumber/table/TableParser.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,6 @@ public void eof() {
7474
});
7575
l.scan(source);
7676
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
77-
return new DataTable(rows, new TableConverter(new LocalizedXStreams(classLoader).get(new I18n("en")), null));
77+
return new DataTable(rows, new TableConverter(new LocalizedXStreams(classLoader).get(new I18n("en"))));
7878
}
7979
}

core/src/test/java/cucumber/table/ToDataTableTest.java

+25-2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import cucumber.runtime.converters.TimeConverter;
77
import gherkin.I18n;
88
import org.junit.Before;
9+
import org.junit.Ignore;
910
import org.junit.Test;
1011

1112
import java.lang.reflect.Type;
@@ -22,7 +23,7 @@ public class ToDataTableTest {
2223
@Before
2324
public void createTableConverterWithDateFormat() {
2425
XStream xStream = new LocalizedXStreams(Thread.currentThread().getContextClassLoader()).get(new I18n("en"));
25-
tc = new TableConverter(xStream, null);
26+
tc = new TableConverter(xStream);
2627
SingleValueConverterWrapperExt converterWrapper = (SingleValueConverterWrapperExt) xStream.getConverterLookup().lookupConverterForType(Date.class);
2728
TimeConverter timeConverter = (TimeConverter) converterWrapper.getConverter();
2829
timeConverter.setOnlyFormat("dd/MM/yyyy", Locale.UK);
@@ -32,13 +33,35 @@ public void createTableConverterWithDateFormat() {
3233
public void converts_list_of_beans_to_table() {
3334
List<UserPojo> users = tc.toList(UserPojo.class, personTable());
3435
DataTable table = tc.toTable(users);
36+
assertEquals("" +
37+
" | credits | name | birthDate |\n" +
38+
" | 1,000 | Sid Vicious | 10/05/1957 |\n" +
39+
" | 3,000 | Frank Zappa | 21/12/1940 |\n" +
40+
"", table.toString());
41+
}
42+
43+
@Test
44+
@Ignore
45+
public void converts_list_of_beans_to_table_with_explicit_columns() {
46+
List<UserPojo> users = tc.toList(UserPojo.class, personTable());
47+
DataTable table = tc.toTable(users, "name", "birthDate", "credits");
3548
assertEquals("" +
3649
" | name | birthDate | credits |\n" +
3750
" | Sid Vicious | 10/05/1957 | 1,000 |\n" +
3851
" | Frank Zappa | 21/12/1940 | 3,000 |\n" +
3952
"", table.toString());
4053
}
4154

55+
/**
56+
* TODO: To make this pass we have to make sure the columns are the same.
57+
*/
58+
@Test
59+
@Ignore
60+
public void diffs_round_trip() {
61+
List<UserPojo> users = tc.toList(UserPojo.class, personTable());
62+
personTable().diff(users);
63+
}
64+
4265
private DataTable personTable() {
4366
return TableParser.parse("" +
4467
"| name | birthDate | credits |\n" +
@@ -63,9 +86,9 @@ public void converts_list_of_single_value_to_table() {
6386

6487
// No setters
6588
public static class UserPojo {
89+
public Integer credits;
6690
public String name;
6791
public Date birthDate;
68-
public Integer credits;
6992

7093
public UserPojo(int foo) {
7194
}

examples/groovy-calculator/pom.xml

+11
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,17 @@
3737
<artifactId>groovy-all</artifactId>
3838
<scope>provided</scope>
3939
</dependency>
40+
<!-- These dependencies are required in order to find classes when running in an IDE - they haven't been jar-jarred -->
41+
<dependency>
42+
<groupId>com.thoughtworks.xstream</groupId>
43+
<artifactId>xstream</artifactId>
44+
<scope>test</scope>
45+
</dependency>
46+
<dependency>
47+
<groupId>com.googlecode.java-diff-utils</groupId>
48+
<artifactId>diffutils</artifactId>
49+
<scope>test</scope>
50+
</dependency>
4051
</dependencies>
4152

4253
<build>

examples/java-webbit-websockets-selenium/pom.xml

+11
Original file line numberDiff line numberDiff line change
@@ -48,5 +48,16 @@
4848
<artifactId>junit</artifactId>
4949
<scope>test</scope>
5050
</dependency>
51+
<!-- These dependencies are required in order to find classes when running in an IDE - they haven't been jar-jarred -->
52+
<dependency>
53+
<groupId>com.thoughtworks.xstream</groupId>
54+
<artifactId>xstream</artifactId>
55+
<scope>test</scope>
56+
</dependency>
57+
<dependency>
58+
<groupId>com.googlecode.java-diff-utils</groupId>
59+
<artifactId>diffutils</artifactId>
60+
<scope>test</scope>
61+
</dependency>
5162
</dependencies>
5263
</project>

0 commit comments

Comments
 (0)