Skip to content

Commit fd25cdc

Browse files
author
Roy Jacobs
committed
ComplexTypeWriter should handle cases where startNode is called more often than setValue
1 parent 65f8e59 commit fd25cdc

File tree

1 file changed

+13
-10
lines changed

1 file changed

+13
-10
lines changed

core/src/main/java/cucumber/runtime/xstream/ComplexTypeWriter.java

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,16 @@
33
import cucumber.runtime.table.CamelCaseStringConverter;
44

55
import java.util.ArrayList;
6+
import java.util.LinkedHashMap;
67
import java.util.List;
8+
import java.util.Map;
79

810
import static java.util.Arrays.asList;
911

1012
public class ComplexTypeWriter extends CellWriter {
1113
private final List<String> columnNames;
12-
private final List<String> fieldNames = new ArrayList<String>();
13-
private final List<String> fieldValues = new ArrayList<String>();
14+
private Map<String, String> fields = new LinkedHashMap<String, String>();
15+
private String currentKey;
1416

1517
private int nodeDepth = 0;
1618

@@ -20,7 +22,7 @@ public ComplexTypeWriter(List<String> columnNames) {
2022

2123
@Override
2224
public List<String> getHeader() {
23-
return columnNames.isEmpty() ? fieldNames : columnNames;
25+
return columnNames.isEmpty() ? new ArrayList<String>(fields.keySet()) : columnNames;
2426
}
2527

2628
@Override
@@ -30,24 +32,24 @@ public List<String> getValues() {
3032
String[] explicitFieldValues = new String[columnNames.size()];
3133
int n = 0;
3234
for (String columnName : columnNames) {
33-
int index = fieldNames.indexOf(converter.map(columnName));
34-
if (index == -1) {
35-
explicitFieldValues[n] = "";
35+
final String convertedColumnName = converter.map(columnName);
36+
if (fields.containsKey(convertedColumnName)) {
37+
explicitFieldValues[n] = fields.get(convertedColumnName);
3638
} else {
37-
explicitFieldValues[n] = fieldValues.get(index);
39+
explicitFieldValues[n] = "";
3840
}
3941
n++;
4042
}
4143
return asList(explicitFieldValues);
4244
} else {
43-
return fieldValues;
45+
return new ArrayList<String>(fields.values());
4446
}
4547
}
4648

4749
@Override
4850
public void startNode(String name) {
4951
if (nodeDepth == 1) {
50-
this.fieldNames.add(name);
52+
currentKey = name;
5153
}
5254
nodeDepth++;
5355
}
@@ -58,12 +60,13 @@ public void addAttribute(String name, String value) {
5860

5961
@Override
6062
public void setValue(String value) {
61-
fieldValues.add(value == null ? "" : value);
63+
fields.put(currentKey, value == null ? "" : value);
6264
}
6365

6466
@Override
6567
public void endNode() {
6668
nodeDepth--;
69+
currentKey = null;
6770
}
6871

6972
@Override

0 commit comments

Comments
 (0)