Skip to content

Commit

Permalink
JBEHAVE-1531 Disallow definition of table properties outside of table…
Browse files Browse the repository at this point in the history
… they belong to (#79)
  • Loading branch information
uarlouski authored Nov 5, 2024
1 parent 3a9c82c commit 4443513
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 4 deletions.
16 changes: 13 additions & 3 deletions distribution/src/site/content/tabular-parameters.html
Original file line number Diff line number Diff line change
Expand Up @@ -372,16 +372,27 @@ <h2>Loading tabular parameter from an external resource</h2>
</script>

<p>Inlined properties can be applied to the tabular parameter loaded from an external
resource</p>
resource, though keep in mind that parsing related properties like <b>headerSeparator</b>,
<b>valueSeparator</b> and <b>ignorableSeparator</b> must be placed inside the external
resource they belong to.</p>
<script type="syntaxhighlighter" class="brush: bdd">
<![CDATA[
Given that Larry has done <trades> trades
Examples:
{headerSeparator=!,valueSeparator=!}
{transformer=FROM_LANDSCAPE}
org/jbehave/examples/trader/stories/traders.table
]]>
</script>

<h2>org/jbehave/examples/trader/stories/traders.table</h2>
<pre class="brush: plain">
{headerSeparator=!,valueSeparator=!}
!name !rank !
!Larry!Stooge 3!
!Moe !Stooge 1!
!Curly!Stooge 2!
</pre>

<p>If the external tabular parameters contains <a href="#transformers">transformers</a>, they will be applied immediately after the resource is loaded. Consequently, transformers from the current story/scenario will be applied subsequently.</p>
Scenario:
<pre class="brush: bdd">
Expand All @@ -395,7 +406,6 @@ <h2>Loading tabular parameter from an external resource</h2>
{transformer=REPLACING, replacing=url1, replacement=url2}
|urls |
|https://www.url1.com |
</pre>

<p>We need to enable theExamplesTable parameter converter to find
the resource with the appropriate resource loader configured via the <a
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,4 @@ When the stock is traded with <price>
Then the trader is alerted with <status>

Examples:
{transfomer=LANDSCAPE, headerSeparator=!, valueSeparator=!}
org/jbehave/examples/core/stories/trades_transposed.table
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{transformer=FROM_LANDSCAPE, headerSeparator=!, valueSeparator=!}
!symbol !STK1!STK1!STK1!
!threshold!15.0!15.0!15.0!
!price !5.0 !11.0!16.0!
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.jbehave.core.model;

import static org.apache.commons.lang3.Validate.isTrue;

import java.util.Deque;

import org.jbehave.core.configuration.Configuration;
Expand Down Expand Up @@ -92,6 +94,7 @@ public ExamplesTable createExamplesTable(String input) {
Deque<TableProperties> properties = tablePropertiesQueue.getProperties();

if (!isTable(tableAsString, properties.peekFirst()) && !tableAsString.isEmpty()) {
checkResourceProperties(input, properties.peekFirst());
String loadedTable = resourceLoader.loadResourceAsText(tableAsString.trim());
tablePropertiesQueue = tableParsers.parseProperties(loadedTable);
Deque<TableProperties> target = tablePropertiesQueue.getProperties();
Expand All @@ -110,6 +113,14 @@ public ExamplesTable createExamplesTable(String input) {
tableTransformers, tableTransformerMonitor);
}

private void checkResourceProperties(String table, TableProperties properties) {
isTrue(keywords.examplesTableHeaderSeparator().equals(properties.getHeaderSeparator())
&& keywords.examplesTableValueSeparator().equals(properties.getValueSeparator())
&& keywords.examplesTableIgnorableSeparator().equals(properties.getIgnorableSeparator()),
"Examples table parsing separators are not allowed to be applied outside of external table they "
+ "belong to:%n%s", table);
}

protected boolean isTable(String table, TableProperties properties) {
String headerSeparator = properties == null ? keywords.examplesTableHeaderSeparator()
: properties.getHeaderSeparator();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,16 @@

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import org.jbehave.core.io.LoadFromClasspath;
import org.jbehave.core.io.ResourceLoader;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;

class ExamplesTableFactoryBehaviour {

Expand Down Expand Up @@ -142,4 +146,25 @@ void shouldLoadAndProperlyApplyTransformersForExamplesTableFromResourceInput() {
+ lineFromFirstInnerTransformer
+ lineFromSecondInnerTransformer));
}

@ValueSource(strings = {
"headerSeparator",
"valueSeparator",
"ignorableSeparator"
})
@ParameterizedTest
void shouldFailIfExamplesTableParsingSeparatorsArePlacesOutsideOfExternalTableTheyBelongTo(String separator) {
// Given
ExamplesTableFactory factory = new ExamplesTableFactory(new LoadFromClasspath(), new TableTransformers());

// When
String tableAsString = String.format("{%s=!}\ndata.table", separator);
IllegalArgumentException thrown = assertThrows(IllegalArgumentException.class,
() -> factory.createExamplesTable(tableAsString));

// Then
String message = String.format("Examples table parsing separators are not allowed to be applied outside of "
+ "external table they belong to:%n{%s=!}\ndata.table", separator);
assertEquals(message, thrown.getMessage());
}
}

0 comments on commit 4443513

Please sign in to comment.