Skip to content

Commit

Permalink
JBEHAVE-1604 Fix failure cause by chain of table loadings without use…
Browse files Browse the repository at this point in the history
…r properties
  • Loading branch information
uarlouski committed Nov 7, 2024
1 parent 8d420ff commit 7c4f44f
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ public Properties getProperties() {
}

public String getPropertiesAsString() {
return lastTableProperties().getPropertiesAsString();
return lastTableProperties().asString();
}

private String replaceNamedParameters(String text) {
Expand Down Expand Up @@ -369,12 +369,10 @@ public String asString() {
return EMPTY_VALUE;
}
StringBuilder sb = new StringBuilder();
for (TableProperties properties : tablePropertiesQueue) {
String propertiesAsString = properties.getPropertiesAsString();
if (!propertiesAsString.isEmpty()) {
sb.append(properties.asString()).append(lastTableProperties().getRowSeparator());
}
}
tablePropertiesQueue.stream().filter(t -> !t.isEmpty())
.map(TableProperties::asString)
.forEach(props -> sb.append(props)
.append(lastTableProperties().getRowSeparator()));
sb.append(ExamplesTableStringBuilder.buildExamplesTableString(lastTableProperties(), getHeaders(),
tableRows.getRows()));

Expand Down Expand Up @@ -409,6 +407,9 @@ public enum Decorator {
private static final String EQUAL = "=";
private static final String PIPE_REGEX = "\\|";

private static final String PROPERTIES_OPEN_CHAR = "{";
private static final String PROPERTIES_CLOSE_CHAR = "}";

private static final String DECORATORS_REGEX = Stream.of(Decorator.values())
.map(Decorator::name)
.collect(Collectors.joining("|", "(", ")"));
Expand All @@ -435,12 +436,12 @@ public TableProperties(String propertiesAsString, Keywords keywords, ParameterCo
properties.setProperty(HEADER_SEPARATOR_KEY, keywords.examplesTableHeaderSeparator());
properties.setProperty(VALUE_SEPARATOR_KEY, keywords.examplesTableValueSeparator());
properties.setProperty(IGNORABLE_SEPARATOR_KEY, keywords.examplesTableIgnorableSeparator());
properties.putAll(parseProperties(propertiesAsString));
properties.putAll(parseProperties());
}

private Map<String, String> parseProperties(String propertiesAsString) {
private Map<String, String> parseProperties() {
Map<String, String> result = new LinkedHashMap<>();
if (!StringUtils.isEmpty(propertiesAsString)) {
if (!isEmpty()) {
for (String propertyAsString : propertiesAsString.split("(?<!\\\\),")) {
String[] property = StringUtils.split(propertyAsString, EQUAL, 2);
String propertyName = property[0];
Expand Down Expand Up @@ -477,7 +478,7 @@ private String decoratePropertyValue(String value, Decorator decorator) {
}

public String asString() {
return String.format("{%s}", propertiesAsString);
return PROPERTIES_OPEN_CHAR + propertiesAsString + PROPERTIES_CLOSE_CHAR;
}

@SuppressWarnings("unchecked")
Expand Down Expand Up @@ -555,8 +556,10 @@ public Properties getProperties() {
return properties;
}

public String getPropertiesAsString() {
return propertiesAsString;
public boolean isEmpty() {
String value = StringUtils.removeStart(propertiesAsString, PROPERTIES_OPEN_CHAR);
value = StringUtils.removeEnd(value, PROPERTIES_CLOSE_CHAR);
return value.trim().isEmpty();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import java.util.Deque;
import java.util.stream.Collectors;

import org.apache.commons.lang3.StringUtils;
import org.jbehave.core.configuration.Configuration;
import org.jbehave.core.configuration.Keywords;
import org.jbehave.core.i18n.LocalizedKeywords;
Expand Down Expand Up @@ -108,7 +107,8 @@ public ExamplesTable createExamplesTable(String input) {

String table = tablePropertiesQueue.getTable();
if (isExternal(tablePropertiesQueue.getTable(), headProperties)) {
table = target.stream().map(TableProperties::asString)
table = target.stream().filter(t -> !t.isEmpty())
.map(TableProperties::asString)
.collect(Collectors.joining(
System.lineSeparator(),
"",
Expand All @@ -122,7 +122,7 @@ public ExamplesTable createExamplesTable(String input) {
tablePropertiesQueue.getTable(), tableParsers, target, tableTransformerMonitor);
tablePropertiesQueue = tableParsers.parseProperties(loadedTable);
TableProperties parsedProperties = tablePropertiesQueue.getProperties().getFirst();
if (StringUtils.isEmpty(parsedProperties.getPropertiesAsString())) {
if (parsedProperties.isEmpty()) {
TableProperties newProperties = new TableProperties("", keywords, parameterConverters);
newProperties.overrideSeparatorsFrom(target.peekLast());
target.clear();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,20 @@ void shouldLoadTableFromAnotherTableContainingTableReference() {
ensureRowContentIs(rows, 2, asList("STK2", "17.0", "16.0", "ON"));
}

@Test
void shouldLoadTableFromAnotherTableContainingTableReferenceAndNoTableProperties() {
// Given
ExamplesTableFactory factory = new ExamplesTableFactory(new LoadFromClasspath(), new TableTransformers());
// When
ExamplesTable table = factory.createExamplesTable("data-simple-reference.table");

// Then
assertThat(table.getHeaders(), equalTo(asList("Language")));
List<Map<String, String>> rows = table.getRows();
assertThat(rows.size(), equalTo(1));
ensureRowContentIs(rows, 0, asList("Java"));
}

private void ensureRowContentIs(List<Map<String, String>> rows, int row, List<String> expected) {
assertThat(new ArrayList<>(rows.get(row).values()), equalTo(expected));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package org.jbehave.core.model;

import static org.jbehave.core.model.ExamplesTable.TableProperties;
import static org.jbehave.core.model.ExamplesTable.TablePropertiesQueue;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.ArgumentMatchers.argThat;
import static org.mockito.ArgumentMatchers.eq;
Expand All @@ -12,6 +10,8 @@

import org.jbehave.core.i18n.LocalizedKeywords;
import org.jbehave.core.io.LoadFromClasspath;
import org.jbehave.core.model.ExamplesTable.TableProperties;
import org.jbehave.core.model.ExamplesTable.TablePropertiesQueue;
import org.jbehave.core.steps.ParameterControls;
import org.jbehave.core.steps.ParameterConverters;
import org.junit.jupiter.api.Test;
Expand All @@ -30,14 +30,14 @@ void shouldApplyTransformers() {
String lineFromSecondTransformer = "\n!value2!";
String tableTransformed = table + lineFromFirstTransformer + lineFromSecondTransformer;
String firstTransformerName = "FIRST_TRANSFORMER";
String firstPropertiesAsString = String.format("transformer=%s, property=any_property1, valueSeparator=!",
String firstPropertiesAsString = String.format("{transformer=%s, property=any_property1, valueSeparator=!}",
firstTransformerName);
String secondTransformerName = "SECOND_TRANSFORMER";
String secondPropertiesAsString = String.format(
"transformer=%s, property=any_property2", secondTransformerName);
"{transformer=%s, property=any_property2}", secondTransformerName);
TablePropertiesQueue tablePropertiesQueue = TABLE_PARSERS.parseProperties(
"{" + firstPropertiesAsString + "}"
+ "{" + secondPropertiesAsString + "}"
firstPropertiesAsString
+ secondPropertiesAsString
+ "\n"
+ table);
Deque<TableProperties> tableProperties = tablePropertiesQueue.getProperties();
Expand All @@ -53,13 +53,13 @@ void shouldApplyTransformers() {

assertEquals(tableTransformed, output);
verify(tableTransformerMonitor).beforeTransformerApplying(eq(firstTransformerName), argThat(
p -> p.getPropertiesAsString().equals(firstPropertiesAsString)), eq(table));
p -> p.asString().equals(firstPropertiesAsString)), eq(table));
verify(tableTransformerMonitor).afterTransformerApplying(eq(firstTransformerName), argThat(p ->
p.getPropertiesAsString().equals(firstPropertiesAsString)), eq(table + lineFromFirstTransformer));
p.asString().equals(firstPropertiesAsString)), eq(table + lineFromFirstTransformer));
verify(tableTransformerMonitor).beforeTransformerApplying(eq(secondTransformerName), argThat(p ->
p.getPropertiesAsString().equals(secondPropertiesAsString)), eq(table + lineFromFirstTransformer));
p.asString().equals(secondPropertiesAsString)), eq(table + lineFromFirstTransformer));
verify(tableTransformerMonitor).afterTransformerApplying(eq(secondTransformerName), argThat(
p -> p.getPropertiesAsString().equals(secondPropertiesAsString)),
p -> p.asString().equals(secondPropertiesAsString)),
eq(table + lineFromFirstTransformer + lineFromSecondTransformer));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
data-simple.table
2 changes: 2 additions & 0 deletions jbehave-core/src/test/resources/data-simple.table
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
|Language|
|Java |

0 comments on commit 7c4f44f

Please sign in to comment.