forked from cucumber/cucumber-jvm
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implemented fix for issue cucumber#433 - DataTable.toTable( List<Stri…
…ng[]> ), toTable( List<Map<String,Object>> ). Added ArrayOfSingleValueWriter and MapWriter.
- Loading branch information
Nicholas Albion
committed
Dec 5, 2012
1 parent
97b099c
commit bc5c6d9
Showing
4 changed files
with
201 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
core/src/main/java/cucumber/runtime/xstream/ArrayOfSingleValueWriter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package cucumber.runtime.xstream; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import cucumber.runtime.CucumberException; | ||
|
||
/** | ||
* Based on {@link ListOfSingleValueWriter} but supports {@link #getHeader()} | ||
* @author Nicholas Albion | ||
*/ | ||
public class ArrayOfSingleValueWriter extends CellWriter { | ||
private int nodeDepth; | ||
private final List<String> columnNames; | ||
private final List<String> values = new ArrayList<String>(); | ||
|
||
public ArrayOfSingleValueWriter(List<String> columnNames) { | ||
this.columnNames = columnNames; | ||
} | ||
|
||
@Override | ||
public List<String> getHeader() { | ||
return columnNames; | ||
} | ||
|
||
@Override | ||
public List<String> getValues() { | ||
return values; | ||
} | ||
|
||
@Override | ||
public void startNode(String name) { | ||
if (nodeDepth > 1) { | ||
throw new CucumberException("Can only convert List<List<T>> to a table when T is a single value (primitive, string, date etc)."); | ||
} | ||
nodeDepth++; | ||
} | ||
|
||
@Override | ||
public void addAttribute(String name, String value) { | ||
} | ||
|
||
@Override | ||
public void setValue(String value) { | ||
values.add(value == null ? "" : value); | ||
} | ||
|
||
@Override | ||
public void endNode() { | ||
nodeDepth--; | ||
} | ||
|
||
@Override | ||
public void flush() { | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
@Override | ||
public void close() { | ||
throw new UnsupportedOperationException(); | ||
} | ||
} |
75 changes: 75 additions & 0 deletions
75
core/src/main/java/cucumber/runtime/xstream/MapWriter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package cucumber.runtime.xstream; | ||
|
||
import java.util.ArrayList; | ||
import java.util.LinkedHashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
/** | ||
* Supports Map<String, Object> as the List item | ||
* | ||
* @author Nicholas Albion | ||
*/ | ||
public class MapWriter extends CellWriter { | ||
private String key; | ||
private final List<String> columnNames; | ||
private final Map<String, Object> values = new LinkedHashMap<String, Object>(); | ||
private final List<String> fieldValues = new ArrayList<String>(); | ||
|
||
public MapWriter(List<String> columnNames) { | ||
this.columnNames = columnNames; | ||
} | ||
|
||
@Override | ||
public List<String> getHeader() { | ||
return columnNames; | ||
} | ||
|
||
@Override | ||
public List<String> getValues() { | ||
if (columnNames.size() > 0) { | ||
List<String> fieldValues = new ArrayList<String>(columnNames.size()); | ||
for (String columnName : columnNames) { | ||
Object value = values.get(columnName); | ||
fieldValues.add( value == null ? "" : value.toString() ); | ||
} | ||
|
||
return fieldValues; | ||
} else { | ||
return fieldValues; | ||
} | ||
} | ||
|
||
@Override | ||
public void setValue(String value) { | ||
if( key == null ) { | ||
key = value; | ||
} else { | ||
values.put(key, value); | ||
fieldValues.add(value == null ? "" : 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() { | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters