diff --git a/examples/java-datatable/.classpath b/examples/java-datatable/.classpath new file mode 100644 index 00000000..2519c5ca --- /dev/null +++ b/examples/java-datatable/.classpath @@ -0,0 +1,36 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/examples/java-datatable/.project b/examples/java-datatable/.project new file mode 100644 index 00000000..7f2203dc --- /dev/null +++ b/examples/java-datatable/.project @@ -0,0 +1,23 @@ + + + datatable-java + + + + + + org.eclipse.jdt.core.javabuilder + + + + + org.eclipse.m2e.core.maven2Builder + + + + + + org.eclipse.jdt.core.javanature + org.eclipse.m2e.core.maven2Nature + + diff --git a/examples/java-datatable/pom.xml b/examples/java-datatable/pom.xml new file mode 100644 index 00000000..ab35d7fb --- /dev/null +++ b/examples/java-datatable/pom.xml @@ -0,0 +1,34 @@ + + 4.0.0 + io.cucumber.eclipse.examples + datatable-java + 0.0.1-SNAPSHOT + Examples: Datatable + + + + UTF-8 + 11 + 11 + 6.9.1 + 2.2 + + + + + + + io.cucumber + cucumber-java + ${cucumber-version} + + + + org.hamcrest + hamcrest-core + ${hamcrest.version} + test + + + + \ No newline at end of file diff --git a/examples/java-datatable/src/main/java/cucumber/examples/datatable/Animals.java b/examples/java-datatable/src/main/java/cucumber/examples/datatable/Animals.java new file mode 100644 index 00000000..96a663fc --- /dev/null +++ b/examples/java-datatable/src/main/java/cucumber/examples/datatable/Animals.java @@ -0,0 +1,32 @@ +package cucumber.examples.datatable; + +import java.util.Arrays; +import java.util.List; + +public class Animals { + + protected List availableData; + private List availableDataForAnimals = Arrays.asList("Color","Lifespan","Whiskers","Trunk","Tusk"); + private String food; + + public Animals() { + super(); + } + + public List getAvailableData() { + return availableData; + } + + public List getAvailableDataForAnimals() { + return availableDataForAnimals; + } + + public String getFood() { + return food; + } + + public void setFood(String food) { + this.food = food; + } + +} diff --git a/examples/java-datatable/src/main/java/cucumber/examples/datatable/Cat.java b/examples/java-datatable/src/main/java/cucumber/examples/datatable/Cat.java new file mode 100644 index 00000000..c23812cc --- /dev/null +++ b/examples/java-datatable/src/main/java/cucumber/examples/datatable/Cat.java @@ -0,0 +1,40 @@ +package cucumber.examples.datatable; + +import java.util.Arrays; + +public class Cat extends Animals{ + + private String color; + private String lifespan; + private String whiskers; + + public Cat() { + super(); + availableData = Arrays.asList("Color","Lifespan","Whiskers"); + } + + + public String getColor() { + return color; + } + public String getLifespan() { + return lifespan; + } + + public String getWhiskers() { + return whiskers; + } + + public void setColor(String color) { + this.color = color; + } + + public void setLifespan(String lifespan) { + this.lifespan = lifespan; + } + + public void setWhiskers(String whiskers) { + this.whiskers = whiskers; + } + +} diff --git a/examples/java-datatable/src/main/java/cucumber/examples/datatable/Elephant.java b/examples/java-datatable/src/main/java/cucumber/examples/datatable/Elephant.java new file mode 100644 index 00000000..8ec459b4 --- /dev/null +++ b/examples/java-datatable/src/main/java/cucumber/examples/datatable/Elephant.java @@ -0,0 +1,49 @@ +package cucumber.examples.datatable; + +import java.util.Arrays; + +public class Elephant extends Animals { + + private String color; + private String lifespan; + private String trunk; + private String tusk; + + public Elephant(){ + super(); + availableData = Arrays.asList("Color","Lifespan","Trunk","Tusk"); + } + + public String getColor() { + return color; + } + + public String getLifespan() { + return lifespan; + } + + public String getTrunk() { + return trunk; + } + + public String getTusk() { + return tusk; + } + + public void setColor(String color) { + this.color = color; + } + + public void setLifespan(String lifespan) { + this.lifespan = lifespan; + } + + public void setTrunk(String trunk) { + this.trunk = trunk; + } + + public void setTusk(String tusk) { + this.tusk = tusk; + } + +} diff --git a/examples/java-datatable/src/test/java/cucumber/examples/DatatableSteps.java b/examples/java-datatable/src/test/java/cucumber/examples/DatatableSteps.java new file mode 100644 index 00000000..17b68c56 --- /dev/null +++ b/examples/java-datatable/src/test/java/cucumber/examples/DatatableSteps.java @@ -0,0 +1,53 @@ +package cucumber.examples; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.equalTo; + +import java.util.List; +import java.util.Map; + +import cucumber.examples.datatable.Animals; +import cucumber.examples.datatable.Cat; +import cucumber.examples.datatable.Elephant; +import io.cucumber.datatable.DataTable; +import io.cucumber.java.en.Given; +import io.cucumber.java.en.Then; + +public class DatatableSteps { + + Animals animal; + + @Given("the animal {string}") + public void animalCharacteristics(final String animalToSet, final DataTable tableOfParameters){ + List> animalData = tableOfParameters.asMaps(String.class, String.class); + + switch (animalToSet) { + case "Cat" -> { + animal = new Cat(); + animal.setFood("fish"); + } + case "Elephant" -> { + animal = new Elephant(); + animal.setFood("leaves"); + } + default -> new NoClassDefFoundError("The animal '"+ animalToSet+"' doesn't exist"); + } + checkAnimalData(animalData); + } + + private void checkAnimalData(final List> animalData) { + + for (Map data : animalData) { + String key = data.get("Key"); + assertThat("Key '" + key + "' isn't contain in the available data of all animals",animal.getAvailableDataForAnimals().contains(key)); + assertThat("Key '" + key + "' isn't contain in the available data of '" + animal.getClass().getSimpleName()+"'",animal.getAvailableData().contains(key)); + } + + } + + @Then("the food is {string}") + public void theFoodIs(final String food) { + assertThat(animal.getFood(),equalTo(food)); + } + +} diff --git a/examples/java-datatable/src/test/resources/cucumber/examples/datatable.feature b/examples/java-datatable/src/test/resources/cucumber/examples/datatable.feature new file mode 100644 index 00000000..f1205aff --- /dev/null +++ b/examples/java-datatable/src/test/resources/cucumber/examples/datatable.feature @@ -0,0 +1,46 @@ +#language: en +Feature: Connection between DataTable Key and a specific Step Value + + + Scenario: All Keys are related to a Step Value. Example 1 + Given the animal "Cat" + | Key | Value | + | Color | Black | + | Lifespan | 3 | + | Whiskers | 24 | + + Then the food is "fish" + + + Scenario: All Keys are related to a Step Value. Example 2 + Given the animal "Elephant" + | Key | Value | + | Color | Grey | + | Lifespan | 70 | + | Trunk | 1.8 | + | Tusk | 1.3 | + + Then the food is "leaves" + + + Scenario: There are some unrelated Keys to a Step Value. This Keys are available for other Step Value + Given the animal "Cat" + | Key | Value | + | Color | Black | + | Lifespan | 3 | + | Whiskers | 24 | + | Trunk | 1.8 | + + Then the food is "fish" + + + Scenario: There are some unrelated Keys to a Step Value. This Keys are not available for each Step Value + Given the animal "Cat" + | Key | Value | + | Color | Black | + | Lifespan | 3 | + | Whiskers | 24 | + | Wings | 2 | + + Then the food is "fish" +