Skip to content

Commit

Permalink
JBEHAVE-724: Too many columns in an example table give IndexOutOfBoun…
Browse files Browse the repository at this point in the history
…dsException

check for header array size and ignore the rest of the columns
  • Loading branch information
alexlehm committed Feb 25, 2012
1 parent 295b1a1 commit 465bf95
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,12 @@
* <p>
* A table can also be created by providing the entire data content, via the
* {@link #withRows(List<Map<String,String>>)} method.
*
* </p>
* The parsing code assumes that the number of columns for data rows is the same
* as in the header, if a row has less fields, the remaining are filled with
* empty values, if it has more, the fields are ignored.
* <p>
*/
public class ExamplesTable {
private static final Map<String, String> EMPTY_MAP = Collections.emptyMap();
Expand Down Expand Up @@ -176,7 +182,9 @@ private void parse(String tableAsString) {
List<String> columns = columnsFor(rowAsString, properties.getProperty("valueSeparator", valueSeparator));
Map<String, String> map = createRowMap();
for (int column = 0; column < columns.size(); column++) {
map.put(headers.get(column), columns.get(column));
if(column<headers.size()) {
map.put(headers.get(column), columns.get(column));
}
}
data.add(map);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,21 @@ public void shouldIgnoreEmptyLines() throws Exception {
assertThat(table.asString(), equalTo("|one|two|\n|a|b|\n|c|d|\n"));
}

@Test
public void shouldHandleWrongNumberOfColumns() {
// When
String tableTooFewColumns = "|a|b|\n|a|\n";
ExamplesTable shortTable = new ExamplesTable(tableTooFewColumns);
// Then
assertThat(shortTable.asString(), equalTo("|a|b|\n|a||\n"));

// When
String tableTooManyColumns = "|a|b|\n|a|b|c|\n";
ExamplesTable longTable = new ExamplesTable(tableTooManyColumns);
// Then
assertThat(longTable.asString(), equalTo("|a|b|\n|a|b|\n"));
}

public Date convertDate(String value) throws ParseException {
return new SimpleDateFormat("dd/MM/yyyy").parse(value);
}
Expand Down

0 comments on commit 465bf95

Please sign in to comment.