-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
JBEHAVE-1603 Apply transformers from table file at its loading (#80)
- Loading branch information
Showing
7 changed files
with
149 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
jbehave-core/src/main/java/org/jbehave/core/model/TableTransformersExecutor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package org.jbehave.core.model; | ||
|
||
import static org.jbehave.core.model.ExamplesTable.TableProperties; | ||
|
||
import java.util.Deque; | ||
|
||
public final class TableTransformersExecutor { | ||
|
||
private TableTransformersExecutor() { | ||
} | ||
|
||
public static String applyTransformers(TableTransformers tableTransformers, String tableAsString, | ||
TableParsers tableParsers, Deque<TableProperties> tablePropertiesQueue, | ||
TableTransformerMonitor tableTransformerMonitor) { | ||
String transformedTable = tableAsString; | ||
TableProperties previousProperties = null; | ||
for (TableProperties properties : tablePropertiesQueue) { | ||
String transformer = properties.getTransformer(); | ||
if (transformer != null) { | ||
if (previousProperties != null) { | ||
properties.overrideSeparatorsFrom(previousProperties); | ||
} | ||
tableTransformerMonitor.beforeTransformerApplying(transformer, properties, transformedTable); | ||
transformedTable = tableTransformers.transform(transformer, transformedTable, tableParsers, properties); | ||
tableTransformerMonitor.afterTransformerApplying(transformer, properties, transformedTable); | ||
} | ||
previousProperties = properties; | ||
} | ||
return transformedTable; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
jbehave-core/src/test/java/org/jbehave/core/model/TableTransformersExecutorBehaviour.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
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; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.verify; | ||
|
||
import java.util.Deque; | ||
|
||
import org.jbehave.core.i18n.LocalizedKeywords; | ||
import org.jbehave.core.io.LoadFromClasspath; | ||
import org.jbehave.core.steps.ParameterControls; | ||
import org.jbehave.core.steps.ParameterConverters; | ||
import org.junit.jupiter.api.Test; | ||
|
||
class TableTransformersExecutorBehaviour { | ||
|
||
private static final TableTransformers TRANSFORMERS = new TableTransformers(); | ||
private static final ParameterConverters PARAMETER_CONVERTERS = new ParameterConverters( | ||
new LoadFromClasspath(), new ParameterControls(), TRANSFORMERS, true); | ||
private static final TableParsers TABLE_PARSERS = new TableParsers(new LocalizedKeywords(), PARAMETER_CONVERTERS); | ||
|
||
@Test | ||
void shouldApplyTransformers() { | ||
String table = "|key|\n!value0!"; | ||
String lineFromFirstTransformer = "\n!value1!"; | ||
String lineFromSecondTransformer = "\n!value2!"; | ||
String tableTransformed = table + lineFromFirstTransformer + lineFromSecondTransformer; | ||
String firstTransformerName = "FIRST_TRANSFORMER"; | ||
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); | ||
TablePropertiesQueue tablePropertiesQueue = TABLE_PARSERS.parseProperties( | ||
"{" + firstPropertiesAsString + "}" | ||
+ "{" + secondPropertiesAsString + "}" | ||
+ "\n" | ||
+ table); | ||
Deque<TableProperties> tableProperties = tablePropertiesQueue.getProperties(); | ||
|
||
|
||
TRANSFORMERS.useTransformer(firstTransformerName, (tableAsString, tableParsers, properties) -> | ||
tableAsString + lineFromFirstTransformer); | ||
TRANSFORMERS.useTransformer(secondTransformerName, (tableAsString, tableParsers, properties) -> | ||
tableAsString + lineFromSecondTransformer); | ||
TableTransformerMonitor tableTransformerMonitor = mock(TableTransformerMonitor.class); | ||
String output = TableTransformersExecutor.applyTransformers( | ||
TRANSFORMERS, table, TABLE_PARSERS, tableProperties, tableTransformerMonitor); | ||
|
||
assertEquals(tableTransformed, output); | ||
verify(tableTransformerMonitor).beforeTransformerApplying(eq(firstTransformerName), argThat( | ||
p -> p.getPropertiesAsString().equals(firstPropertiesAsString)), eq(table)); | ||
verify(tableTransformerMonitor).afterTransformerApplying(eq(firstTransformerName), argThat(p -> | ||
p.getPropertiesAsString().equals(firstPropertiesAsString)), eq(table + lineFromFirstTransformer)); | ||
verify(tableTransformerMonitor).beforeTransformerApplying(eq(secondTransformerName), argThat(p -> | ||
p.getPropertiesAsString().equals(secondPropertiesAsString)), eq(table + lineFromFirstTransformer)); | ||
verify(tableTransformerMonitor).afterTransformerApplying(eq(secondTransformerName), argThat( | ||
p -> p.getPropertiesAsString().equals(secondPropertiesAsString)), | ||
eq(table + lineFromFirstTransformer + lineFromSecondTransformer)); | ||
} | ||
} |