-
Notifications
You must be signed in to change notification settings - Fork 149
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for validation plugins from the project
Currently it is only possible to validate the glue on execution but it some cases it would be useful to have some prevalidation. This adds support for specify validation plugins in the glue code that are executed as part of the parsing of document and show up as errors immediately on save.
- Loading branch information
Showing
6 changed files
with
185 additions
and
0 deletions.
There are no files selected for viewing
78 changes: 78 additions & 0 deletions
78
examples/java-datatable/src/main/java/cucumber/examples/datatable/AnimalValidator.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,78 @@ | ||
package cucumber.examples.datatable; | ||
|
||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
|
||
import io.cucumber.core.gherkin.DataTableArgument; | ||
import io.cucumber.plugin.ConcurrentEventListener; | ||
import io.cucumber.plugin.Plugin; | ||
import io.cucumber.plugin.event.EventPublisher; | ||
import io.cucumber.plugin.event.PickleStepTestStep; | ||
import io.cucumber.plugin.event.Step; | ||
import io.cucumber.plugin.event.StepArgument; | ||
import io.cucumber.plugin.event.TestStep; | ||
import io.cucumber.plugin.event.TestStepFinished; | ||
|
||
/** | ||
* This validator is enabled in the feature by using | ||
* <code>#validation-plugin: cucumber.examples.datatable.AnimalValidator</code> | ||
*/ | ||
public class AnimalValidator implements Plugin, ConcurrentEventListener { | ||
|
||
private ConcurrentHashMap<Integer, String> errors = new ConcurrentHashMap<>(); | ||
|
||
@Override | ||
public void setEventPublisher(EventPublisher publisher) { | ||
publisher.registerHandlerFor(TestStepFinished.class, this::handleTestStepFinished); | ||
} | ||
|
||
private void handleTestStepFinished(TestStepFinished event) { | ||
TestStep testStep = event.getTestStep(); | ||
if (testStep instanceof PickleStepTestStep) { | ||
PickleStepTestStep pickleStepTestStep = (PickleStepTestStep) testStep; | ||
Step step = pickleStepTestStep.getStep(); | ||
if ("the animal {string}".equals(pickleStepTestStep.getPattern())) { | ||
StepArgument argument = step.getArgument(); | ||
Animals animal = loadAnimal(pickleStepTestStep.getDefinitionArgument().get(0).getValue()); | ||
if (animal == null) { | ||
// Invalid animal! | ||
return; | ||
} | ||
if (argument instanceof DataTableArgument dataTable) { | ||
List<String> availableData = animal.getAvailableData(); | ||
List<List<String>> cells = dataTable.cells(); | ||
for (int i = 1; i < cells.size(); i++) { | ||
int line = dataTable.getLine() + i; | ||
List<String> list = cells.get(i); | ||
String vv = list.get(0); | ||
if (!animal.getAvailableDataForAnimals().contains(vv)) { | ||
errors.put(line, vv + " is not valid for any animal"); | ||
} else if (!availableData.contains(vv)) { | ||
errors.put(line, vv + " is not valid for animal " + animal.getClass().getSimpleName()); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
//This is a magic method called by cucumber-eclipse to fetch the final errors and display them in the document | ||
public Map<Integer, String> getValidationErrors() { | ||
return errors; | ||
} | ||
|
||
private Animals loadAnimal(String value) { | ||
try { | ||
Class<?> clz = getClass().getClassLoader() | ||
.loadClass("cucumber.examples.datatable." + value.replace("\"", "")); | ||
Object instance = clz.getConstructor().newInstance(); | ||
if (instance instanceof Animals anml) { | ||
return anml; | ||
} | ||
} catch (Exception e) { | ||
e.printStackTrace(); | ||
} | ||
return null; | ||
} | ||
|
||
} |
2 changes: 2 additions & 0 deletions
2
examples/java-datatable/src/test/resources/cucumber/examples/datatable.feature
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
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