Skip to content

Commit

Permalink
Implement DataTable.toTable(List<Map<String,Object>>). Closes #433.
Browse files Browse the repository at this point in the history
  • Loading branch information
DFUK authored and DFUK committed Dec 5, 2012
1 parent f83316c commit f79d5d6
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 8 deletions.
3 changes: 3 additions & 0 deletions core/src/main/java/cucumber/runtime/table/TableConverter.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import cucumber.runtime.xstream.ListOfComplexTypeReader;
import cucumber.runtime.xstream.ListOfSingleValueWriter;
import cucumber.runtime.xstream.LocalizedXStreams;
import cucumber.runtime.xstream.MapWriter;
import gherkin.formatter.model.Comment;
import gherkin.formatter.model.DataTableRow;
import gherkin.util.Mapper;
Expand Down Expand Up @@ -180,6 +181,8 @@ public DataTable toTable(List<?> objects, String... columnNames) {
// XStream needs an instance of ArrayList
object = new ArrayList<Object>(asList((Object[]) object));
writer = new ListOfSingleValueWriter();
} else if (object instanceof Map) {
writer = new MapWriter(asList(columnNames));
} else {
writer = new ComplexTypeWriter(asList(columnNames));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@ public ComplexTypeWriter(List<String> columnNames) {

@Override
public List<String> getHeader() {
return columnNames.size() > 0 ? columnNames : fieldNames;
return columnNames.isEmpty() ? fieldNames : columnNames;
}

@Override
public List<String> getValues() {
CamelCaseStringConverter converter = new CamelCaseStringConverter();
if (columnNames.size() > 0) {
if (!columnNames.isEmpty()) {
String[] explicitFieldValues = new String[columnNames.size()];
int n = 0;
for (String columnName : columnNames) {
Expand Down
67 changes: 67 additions & 0 deletions core/src/main/java/cucumber/runtime/xstream/MapWriter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package cucumber.runtime.xstream;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
* Supports Map&lt;String, Object&gt; as the List item
*/
public class MapWriter extends CellWriter {
private final List<String> columnNames;
private final Map<String, Object> values = new LinkedHashMap<String, Object>();

private String key;

public MapWriter(List<String> columnNames) {
this.columnNames = columnNames;
}

@Override
public List<String> getHeader() {
return columnNames.isEmpty() ? new ArrayList<String>(values.keySet()) : columnNames;
}

@Override
public List<String> getValues() {
List<String> values = new ArrayList<String>(columnNames.size());
for (String columnName : getHeader()) {
Object value = this.values.get(columnName);
values.add(value == null ? "" : value.toString());
}
return values;
}

@Override
public void setValue(String value) {
if (key == null) {
key = value;
} else {
values.put(key, value);
key = null;
}
}

@Override
public void flush() {
throw new UnsupportedOperationException();
}

@Override
public void close() {
throw new UnsupportedOperationException();
}

@Override
public void startNode(String name) {
}

@Override
public void addAttribute(String name, String value) {
}

@Override
public void endNode() {
}
}
40 changes: 34 additions & 6 deletions core/src/test/java/cucumber/runtime/table/ToDataTableTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,11 @@

import java.lang.reflect.Type;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import static java.util.Arrays.asList;
import static org.junit.Assert.assertEquals;
Expand Down Expand Up @@ -127,20 +130,45 @@ public void converts_list_of_list_of_number_to_table() {
}

@Test
public void converts_list_of_array_of_string_to_table() {
public void converts_list_of_array_of_string_or_number_to_table_with_number_formatting() {
List<Object[]> arrays = asList(
new Object[]{"name", "birthDate", "credits"},
new Object[]{"Sid Vicious", "10/05/1957", "1000"},
new Object[]{"Frank Zappa", "21/12/1940", "3000"}
new Object[]{"Sid Vicious", "10/05/1957", 1000},
new Object[]{"Frank Zappa", "21/12/1940", 3000}
);
DataTable table = tc.toTable(arrays, "name", "credits", "birthDate");
DataTable table = tc.toTable(arrays);
assertEquals("" +
" | name | birthDate | credits |\n" +
" | Sid Vicious | 10/05/1957 | 1000 |\n" +
" | Frank Zappa | 21/12/1940 | 3000 |\n" +
" | Sid Vicious | 10/05/1957 | 1,000 |\n" +
" | Frank Zappa | 21/12/1940 | 3,000 |\n" +
"", table.toString());
}

@Test
public void convert_list_of_maps_to_table() {
Map<String, Object> vicious = new LinkedHashMap<String, Object>();
vicious.put("name", "Sid Vicious");
vicious.put("birthDate", "10/05/1957");
vicious.put("credits", 1000);
Map<String, Object> zappa = new LinkedHashMap<String, Object>();
zappa.put("name", "Frank Zappa");
zappa.put("birthDate", "21/12/1940");
zappa.put("credits", 3000);
List<Map<String, Object>> maps = asList(vicious, zappa);

assertEquals("" +
" | name | credits | birthDate |\n" +
" | Sid Vicious | 1,000 | 10/05/1957 |\n" +
" | Frank Zappa | 3,000 | 21/12/1940 |\n" +
"", tc.toTable(maps, "name", "credits", "birthDate").toString());

assertEquals("" +
" | name | birthDate | credits |\n" +
" | Sid Vicious | 10/05/1957 | 1,000 |\n" +
" | Frank Zappa | 21/12/1940 | 3,000 |\n" +
"", tc.toTable(maps).toString());
}

// No setters
public static class UserPojo {
public Integer credits;
Expand Down

0 comments on commit f79d5d6

Please sign in to comment.