Class ExamplesTable

java.lang.Object
org.jbehave.core.model.ExamplesTable
Direct Known Subclasses:
ImmutableExamplesTable

public class ExamplesTable extends Object
Represents a tabular structure that holds rows of example data for parameters named via the column headers:
 |header 1|header 2| .... |header n|
 |value 11|value 12| .... |value 1n|
 ...
 |value m1|value m2| .... |value mn|
 

Different header and value column separators can be specified to replace the default separator "|":

 !!header 1!!header 2!! .... !!header n!!
 !value 11!value 12! .... !value 1n!
 ...
 !value m1!value m2| .... !value mn!
 

Rows starting with an ignorable separator are allowed and ignored:

 |header 1|header 2| .... |header n|
 |-- A commented row --|
 |value 11|value 12| .... |value 1n|
 ...
 |-- Another commented row --|
 |value m1|value m2| .... |value mn|
 

Ignorable separator is configurable and defaults to "|--".

The separators are also configurable via inlined properties:

 {ignorableSeparator=!--,headerSeparator=!,valueSeparator=!}
 !header 1!header 2! .... !header n!
 !-- A commented row --!
 !value 11!value 12! .... !value 1n!
 ...
 !-- Another commented row --!
 !value m1!value m2! .... !value mn!
 

By default all column values are trimmed. To avoid trimming the values, use the "trim" inlined property:

 {trim=false}
 | header 1 | header 2 | .... | header n |
 | value 11 | value 12 | .... | value 1n |
 

Comments is column values are supported via the "commentSeparator" inlined property:

 {commentSeparator=#}
 | header 1#comment | header 2 | .... | header n |
 | value 11#comment | value 12 | .... | value 1n |
 
Comments including the separator are stripped.

Line break is a default separator for rows in ExamplesTable, that's why they can't be added as is to the data. In order to put the value with line breaks to ExamplesTable escape sequences (a character preceded by a backslash (\) is an escape sequence) must be used.

Escape SequenceDescription
\nInsert a newline in the value at this point.
\rInsert a carriage return in the text at this point.
\\Insert a backslash character in the text at this point.
Inlined property processEscapeSequences defines whether escape sequences should be replaced in the data. It’s false by default (no property is declared explicitly). The allowed values are true and false, any other values are considered invalid and will lead to exception thrown at parsing.
 {processEscapeSequences=true, commentSeparator=#}
 |header          |
 |line 1\nline 2  |# The value with a newline
 |line 1\r\nline 2|# The value with a carriage return and a newline
 |line 1\\nline 2 |# The value with an escaped escape sequence, the result will be "line 1\nline 2"
 

The table allows the retrieval of row values as converted parameters. Use getRowAsParameters(int) and invoke Parameters.valueAs(String, Type) specifying the header and the class type of the parameter.

The table allows the transformation of its string representation via the "transformer" inlined property (multiple transformers will be applied in chain-mode):

 {transformer=myTransformerName}
 {transformer=myOtherTransformerName}
 |header 1|header 2| .... |header n|
 |value 11|value 12| .... |value 1n|
 ...
 |value m1|value m2| .... |value mn|
 
The transformers need to be registered by name via the TableTransformers.useTransformer(String, TableTransformer). A few transformers are already registered by default in TableTransformers.

The table allow filtering on meta by row via the "metaByRow" inlined property:

 {metaByRow=true}
 | Meta:       | header 1 | .... | header n |
 | @name=value | value 11 | .... | value 1n |
 

Once created, the table row can be modified, via the withRowValues(int, Map) method, by specifying the map of row values to be changed.

A table can also be created by providing the entire data content, via the withRows(List) method.

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.

  • Field Details

    • INLINED_PROPERTIES_PATTERN

      public static final Pattern INLINED_PROPERTIES_PATTERN
    • EMPTY

      public static final ExamplesTable EMPTY
  • Constructor Details

    • ExamplesTable

      public ExamplesTable(String tableAsString)
  • Method Details

    • withDefaults

      public ExamplesTable withDefaults(Parameters defaults)
    • withNamedParameters

      public ExamplesTable withNamedParameters(Map<String,String> namedParameters)
    • withRowValues

      public ExamplesTable withRowValues(int rowIndex, Map<String,String> values)
    • withRows

      public ExamplesTable withRows(List<Map<String,String>> rows)
    • getProperties

      public Properties getProperties()
    • getPropertiesAsString

      public String getPropertiesAsString()
    • getHeaders

      public List<String> getHeaders()
    • getRow

      public Map<String,String> getRow(int rowIndex)
    • getRow

      public Map<String,String> getRow(int rowIndex, boolean replaceNamedParameters)
    • getRowValues

      public List<String> getRowValues(int rowIndex)
    • getRowValues

      public List<String> getRowValues(int rowIndex, boolean replaceNamedParameters)
    • getRowAsParameters

      public Parameters getRowAsParameters(int rowIndex)
    • getRowAsParameters

      public Parameters getRowAsParameters(int rowIndex, boolean replaceNamedParameters)
    • getRowCount

      public int getRowCount()
    • metaByRow

      public boolean metaByRow()
    • getRows

      public List<Map<String,String>> getRows()
    • getRowsAsParameters

      public List<Parameters> getRowsAsParameters()
    • getRowsAsParameters

      public List<Parameters> getRowsAsParameters(boolean replaceNamedParameters)
    • getRowsAs

      public <T> List<T> getRowsAs(Class<T> type)
    • getRowsAs

      public <T> List<T> getRowsAs(Class<T> type, Map<String,String> fieldNameMapping)
    • getColumn

      public List<String> getColumn(String columnName)
    • getColumn

      public List<String> getColumn(String columnName, boolean replaceNamedParameters)
    • getHeaderSeparator

      public String getHeaderSeparator()
    • getValueSeparator

      public String getValueSeparator()
    • asString

      public String asString()
    • isEmpty

      public boolean isEmpty()
    • outputTo

      public void outputTo(PrintStream output)
    • empty

      public static ExamplesTable empty()
    • toString

      public String toString()
      Overrides:
      toString in class Object