commit 465bf95ded8e94c73b5b6a2d2c4227cedd84d80d
Author:     Alex Lehmann <alexl...@gmail.com>
AuthorDate: Sat Feb 25 14:49:59 2012 +0100
Commit:     Alex Lehmann <alexl...@gmail.com>
CommitDate: Sat Feb 25 14:49:59 2012 +0100

    JBEHAVE-724: Too many columns in an example table give 
IndexOutOfBoundsException
    check for header array size and ignore the rest of the columns

diff --git 
a/jbehave-core/src/main/java/org/jbehave/core/model/ExamplesTable.java 
b/jbehave-core/src/main/java/org/jbehave/core/model/ExamplesTable.java
index ba2ffcd..8987ca3 100755
--- a/jbehave-core/src/main/java/org/jbehave/core/model/ExamplesTable.java
+++ b/jbehave-core/src/main/java/org/jbehave/core/model/ExamplesTable.java
@@ -104,6 +104,12 @@ import static java.util.regex.Pattern.compile;
  * <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();
@@ -176,8 +182,10 @@ public class ExamplesTable {
                 List<String> columns = columnsFor(rowAsString, 
properties.getProperty("valueSeparator", valueSeparator));
                 Map<String, String> map = createRowMap();
                 for (int column = 0; column < columns.size(); column++) {
+                    if(column<headers.size()) {
                     map.put(headers.get(column), columns.get(column));
                 }
+                }
                 data.add(map);
             }
         }
diff --git 
a/jbehave-core/src/test/java/org/jbehave/core/model/ExamplesTableBehaviour.java 
b/jbehave-core/src/test/java/org/jbehave/core/model/ExamplesTableBehaviour.java
index d67ccd5..88910e5 100755
--- 
a/jbehave-core/src/test/java/org/jbehave/core/model/ExamplesTableBehaviour.java
+++ 
b/jbehave-core/src/test/java/org/jbehave/core/model/ExamplesTableBehaviour.java
@@ -373,6 +373,21 @@ public class ExamplesTableBehaviour {
       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);
     }



Reply via email to