diff --git a/jbehave-core/src/main/java/org/jbehave/core/model/ExamplesTableFactory.java b/jbehave-core/src/main/java/org/jbehave/core/model/ExamplesTableFactory.java index 2126841e0..c9c8cda73 100755 --- a/jbehave-core/src/main/java/org/jbehave/core/model/ExamplesTableFactory.java +++ b/jbehave-core/src/main/java/org/jbehave/core/model/ExamplesTableFactory.java @@ -4,6 +4,7 @@ import java.util.Deque; +import org.apache.commons.lang3.StringUtils; import org.jbehave.core.configuration.Configuration; import org.jbehave.core.configuration.Keywords; import org.jbehave.core.i18n.LocalizedKeywords; @@ -104,9 +105,19 @@ public ExamplesTable createExamplesTable(String input) { loadedTable = TableTransformersExecutor.applyTransformers(tableTransformers, tablePropertiesQueue.getTable(), tableParsers, target, tableTransformerMonitor); tablePropertiesQueue = tableParsers.parseProperties(loadedTable); - target = tablePropertiesQueue.getProperties(); + TableProperties parsedProperties = tablePropertiesQueue.getProperties().getFirst(); + if (StringUtils.isEmpty(parsedProperties.getPropertiesAsString())) { + TableProperties newProperties = new TableProperties("", keywords, parameterConverters); + newProperties.overrideSeparatorsFrom(target.peekLast()); + target.clear(); + target.addFirst(newProperties); + } else { + target = tablePropertiesQueue.getProperties(); + } } properties.descendingIterator().forEachRemaining(target::addFirst); + + tablePropertiesQueue = new TablePropertiesQueue(tablePropertiesQueue.getTable(), target); } return new ExamplesTable(tablePropertiesQueue, parameterConverters, parameterControls, tableParsers, diff --git a/jbehave-core/src/test/java/org/jbehave/core/model/ExamplesTableFactoryBehaviour.java b/jbehave-core/src/test/java/org/jbehave/core/model/ExamplesTableFactoryBehaviour.java index a1f4ac70f..533d4e9cc 100755 --- a/jbehave-core/src/test/java/org/jbehave/core/model/ExamplesTableFactoryBehaviour.java +++ b/jbehave-core/src/test/java/org/jbehave/core/model/ExamplesTableFactoryBehaviour.java @@ -1,5 +1,6 @@ package org.jbehave.core.model; +import static java.util.Arrays.asList; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.equalTo; import static org.junit.jupiter.api.Assertions.assertEquals; @@ -7,6 +8,10 @@ import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; + import org.jbehave.core.io.LoadFromClasspath; import org.jbehave.core.io.ResourceLoader; import org.junit.jupiter.api.Test; @@ -167,4 +172,25 @@ void shouldFailIfExamplesTableParsingSeparatorsArePlacesOutsideOfExternalTableTh + "external table they belong to:%n{%s=!}\ndata.table", separator); assertEquals(message, thrown.getMessage()); } + + @Test + void shouldLoadTableFromPathAndPreserveSeparators() { + // Given + ExamplesTableFactory factory = new ExamplesTableFactory(new LoadFromClasspath(), new TableTransformers()); + + // When + ExamplesTable table = factory.createExamplesTable("data.table"); + + // Then + assertThat(table.getHeaders(), equalTo(asList("symbol", "threshold", "price", "status"))); + List> rows = table.getRows(); + assertThat(rows.size(), equalTo(3)); + ensureRowContentIs(rows, 0, asList("STK1", "15.0", "5.0", "OFF")); + ensureRowContentIs(rows, 1, asList("STK1", "15.0", "10.0", "OFF")); + ensureRowContentIs(rows, 2, asList("STK1", "15.0", "16.0", "ON")); + } + + private void ensureRowContentIs(List> rows, int row, List expected) { + assertThat(new ArrayList<>(rows.get(row).values()), equalTo(expected)); + } } diff --git a/jbehave-core/src/test/resources/data.table b/jbehave-core/src/test/resources/data.table new file mode 100644 index 000000000..b64b3a280 --- /dev/null +++ b/jbehave-core/src/test/resources/data.table @@ -0,0 +1,6 @@ +{transformer=REPLACING, replacing=11.0, replacement=10.0, headerSeparator=!, valueSeparator=!} +{transformer=FROM_LANDSCAPE, headerSeparator=!, valueSeparator=!} +!symbol !STK1!STK1!STK1! +!threshold!15.0!15.0!15.0! +!price !5.0 !11.0!16.0! +!status !OFF !OFF !ON !