Skip to content

Commit

Permalink
JBEHAVE-1604 Add ability to load chains of external resources
Browse files Browse the repository at this point in the history
  • Loading branch information
uarlouski committed Nov 6, 2024
1 parent d8a9fd2 commit e35f6b1
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,7 @@ public String asString() {
for (TableProperties properties : tablePropertiesQueue) {
String propertiesAsString = properties.getPropertiesAsString();
if (!propertiesAsString.isEmpty()) {
sb.append("{").append(propertiesAsString).append("}").append(lastTableProperties().getRowSeparator());
sb.append("{").append(propertiesAsString).append("}").append(TableProperties.getRowSeparator());
}
}
sb.append(ExamplesTableStringBuilder.buildExamplesTableString(lastTableProperties(), getHeaders(),
Expand Down Expand Up @@ -484,10 +484,14 @@ public <T> T getMandatoryNonBlankProperty(String propertyName, Type type) {
return (T) parameterConverters.convert(propertyValue, type);
}

public String getRowSeparator() {
public static String getRowSeparator() {
return ROW_SEPARATOR;
}

public String formatted() {
return String.format("{%s}", getPropertiesAsString());
}

public String getHeaderSeparator() {
return properties.getProperty(HEADER_SEPARATOR_KEY);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import static org.apache.commons.lang3.Validate.isTrue;

import java.util.Deque;
import java.util.stream.Collectors;

import org.apache.commons.lang3.StringUtils;
import org.jbehave.core.configuration.Configuration;
Expand Down Expand Up @@ -94,7 +95,7 @@ public ExamplesTable createExamplesTable(String input) {
String tableAsString = tablePropertiesQueue.getTable().trim();
Deque<TableProperties> properties = tablePropertiesQueue.getProperties();

if (!isTable(tableAsString, properties.peekFirst()) && !tableAsString.isEmpty()) {
if (isExternal(tableAsString, properties.peekFirst())) {
checkResourceProperties(input, properties.peekFirst());
String loadedTable = resourceLoader.loadResourceAsText(tableAsString.trim());
tablePropertiesQueue = tableParsers.parseProperties(loadedTable);
Expand All @@ -105,6 +106,16 @@ public ExamplesTable createExamplesTable(String input) {
target.addLast(outerProperties);
}

String table = tablePropertiesQueue.getTable();
if (isExternal(tablePropertiesQueue.getTable(), headProperties)) {
table = target.stream().map(TableProperties::formatted)
.collect(Collectors.joining(
TableProperties.getRowSeparator(),
"",
TableProperties.getRowSeparator() + table));
return createExamplesTable(table);
}

boolean hasTransformers = target.getFirst().getTransformer() != null;
if (hasTransformers) {
loadedTable = TableTransformersExecutor.applyTransformers(tableTransformers,
Expand All @@ -128,6 +139,10 @@ public ExamplesTable createExamplesTable(String input) {
tableTransformers, tableTransformerMonitor);
}

private boolean isExternal(String tableAsString, TableProperties properties) {
return !isTable(tableAsString, properties) && !tableAsString.isEmpty();
}

private void checkResourceProperties(String table, TableProperties properties) {
isTrue(keywords.examplesTableHeaderSeparator().equals(properties.getHeaderSeparator())
&& keywords.examplesTableValueSeparator().equals(properties.getValueSeparator())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,25 @@ void shouldLoadTableFromPathAndPreserveSeparators() {
ensureRowContentIs(rows, 2, asList("STK1", "15.0", "16.0", "ON"));
}

@Test
void shouldLoadTableFromAnotherTableContainingTableReference() {
// Given
ExamplesTableFactory factory = new ExamplesTableFactory(new LoadFromClasspath(), new TableTransformers());

// When
ExamplesTable table = factory.createExamplesTable("{transformer=REPLACING, replacing=STK1, replacement=STK2}\n"
+ "{transformer=REPLACING, replacing=price, replacement=cost}\n"
+ "data-reference.table");

// Then
assertThat(table.getHeaders(), equalTo(asList("symbol", "threshold", "cost", "status")));
List<Map<String, String>> rows = table.getRows();
assertThat(rows.size(), equalTo(3));
ensureRowContentIs(rows, 0, asList("STK2", "17.0", "5.0", "OFF"));
ensureRowContentIs(rows, 1, asList("STK2", "17.0", "10.0", "OFF"));
ensureRowContentIs(rows, 2, asList("STK2", "17.0", "16.0", "ON"));
}

private void ensureRowContentIs(List<Map<String, String>> rows, int row, List<String> expected) {
assertThat(new ArrayList<>(rows.get(row).values()), equalTo(expected));
}
Expand Down
2 changes: 2 additions & 0 deletions jbehave-core/src/test/resources/data-reference.table
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
{transformer=REPLACING, replacing=15.0, replacement=17.0}
data.table

0 comments on commit e35f6b1

Please sign in to comment.