Skip to content

Commit

Permalink
Java Calculator TestNG example project.
Browse files Browse the repository at this point in the history
  • Loading branch information
Dmytro Chyzhykov committed Sep 1, 2013
1 parent 37d3185 commit b5a3bc5
Show file tree
Hide file tree
Showing 13 changed files with 324 additions and 0 deletions.
3 changes: 3 additions & 0 deletions examples/java-calculator-testng/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/.settings
/.classpath
/.project
7 changes: 7 additions & 0 deletions examples/java-calculator-testng/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
This is a TestNG copy-paste version of the [JUnit Calculator example](https://github.com/cucumber/cucumber-jvm/tree/master/examples/java-calculator) project.

If you find its TestNG report is not idiomatic, consider making a contribution to improve Cucumber JVM TestNG Support.

**Note**
Please keep code base of this project up-to-date

32 changes: 32 additions & 0 deletions examples/java-calculator-testng/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>info.cukes</groupId>
<artifactId>cucumber-jvm</artifactId>
<relativePath>../../pom.xml</relativePath>
<version>1.1.5-SNAPSHOT</version>
</parent>

<artifactId>java-calculator-testng</artifactId>
<packaging>jar</packaging>
<name>Examples: Java Calculator TestNG</name>

<dependencies>
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-jvm-deps</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-java</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-testng</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package cucumber.examples.java.calculator;

import java.util.Date;

public class DateCalculator {
private Date now;

public DateCalculator(Date now) {
this.now = now;
}

public String isDateInThePast(Date date) {
return (date.before(now)) ? "yes" : "no";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package cucumber.examples.java.calculator;

import java.util.Deque;
import java.util.LinkedList;
import java.util.List;

import static java.util.Arrays.asList;

public class RpnCalculator {
private final Deque<Number> stack = new LinkedList<Number>();
private static final List<String> OPS = asList("-", "+", "*", "/");

public void push(Object arg) {
if (OPS.contains(arg)) {
Number y = stack.removeLast();
Number x = stack.isEmpty() ? 0 : stack.removeLast();
Double val = null;
if (arg.equals("-")) {
val = x.doubleValue() - y.doubleValue();
} else if (arg.equals("+")) {
val = x.doubleValue() + y.doubleValue();
} else if (arg.equals("*")) {
val = x.doubleValue() * y.doubleValue();
} else if (arg.equals("/")) {
val = x.doubleValue() / y.doubleValue();
}
push(val);
} else {
stack.add((Number) arg);
}
}

public void PI() {
push(Math.PI);
}

public Number value() {
return stack.getLast();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package cucumber.examples.java.calculator;

import cucumber.api.Format;
import cucumber.api.java.en.Given;
import cucumber.api.java.en.Then;
import cucumber.api.java.en.When;

import java.util.Date;

import static org.junit.Assert.assertEquals;

public class DateStepdefs {
private String result;
private DateCalculator calculator;

@Given("^today is (.+)$")
public void today_is(@Format("yyyy-MM-dd") Date date) {
calculator = new DateCalculator(date);
}

/**
* We don't need to use @Format here, since the date string in the step
* conforms to <code>SimpleDateFormat.SHORT</code>. Cucumber has built-in support for
* <code>SimpleDateFormat.SHORT</code>, <code>SimpleDateFormat.MEDIUM</code>,
* <code>SimpleDateFormat.LONG</code> and <code>SimpleDateFormat.FULL</code>.
*
* @see <a href="http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html">SimpleDateFormat</a>
*/
@When("^I ask if (.+) is in the past$")
public void I_ask_if_date_is_in_the_past(Date date) {
result = calculator.isDateInThePast(date);
}

@Then("^the result should be (.+)$")
public void the_result_should_be(String expectedResult) {
assertEquals(expectedResult, result);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package cucumber.examples.java.calculator;

import cucumber.api.Scenario;
import cucumber.api.java.After;
import cucumber.api.java.Before;
import cucumber.api.java.en.Given;
import cucumber.api.java.en.Then;
import cucumber.api.java.en.When;

import java.util.List;

import static org.junit.Assert.assertEquals;

public class RpnCalculatorStepdefs {
private RpnCalculator calc;

@Given("^a calculator I just turned on$")
public void a_calculator_I_just_turned_on() {
calc = new RpnCalculator();
}

@When("^I add (\\d+) and (\\d+)$")
public void adding(int arg1, int arg2) {
calc.push(arg1);
calc.push(arg2);
calc.push("+");
}

@Given("^I press (.+)$")
public void I_press(String what) {
calc.push(what);
}

@Then("^the result is (\\d+)$")
public void the_result_is(double expected) {
assertEquals(expected, calc.value());
}

@Before({"~@foo"})
public void before() {
System.out.println("Runs before scenarios *not* tagged with @foo");
}

@After
public void after(Scenario scenario) {
// result.write("HELLLLOO");
}

@Given("^the previous entries:$")
public void thePreviousEntries(List<Entry> entries) {
for (Entry entry : entries) {
calc.push(entry.first);
calc.push(entry.second);
calc.push(entry.operation);
}
}

public class Entry {
Integer first;
Integer second;
String operation;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package cucumber.examples.java.calculator;

import cucumber.api.CucumberOptions;
import cucumber.api.testng.AbstractTestNGCucumberTests;

@CucumberOptions(format = "json:target/cucumber-report.json")
public class RunCukesTest extends AbstractTestNGCucumberTests {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package cucumber.examples.java.calculator;

import cucumber.api.Transformer;
import cucumber.api.java.en.Given;
import cucumber.api.java.en.Then;
import cucumber.api.java.en.When;
import cucumber.deps.com.thoughtworks.xstream.annotations.XStreamConverter;

import java.util.List;

import static org.junit.Assert.assertEquals;

public class ShoppingStepdefs {
private RpnCalculator calc = new RpnCalculator();

@Given("^the following groceries:$")
public void the_following_groceries(List<Grocery> groceries) {
for (Grocery grocery : groceries) {
calc.push(grocery.price.value);
calc.push("+");
}
}

@When("^I pay (\\d+)$")
public void i_pay(int amount) {
calc.push(amount);
calc.push("-");
}

@Then("^my change should be (\\d+)$")
public void my_change_should_be_(int change) {
assertEquals(-calc.value().intValue(), change);
}

public static class Grocery {
public String name;
@XStreamConverter(Price.Converter.class)
public Price price;

public Grocery() {
super();
}
}

public static class Price {
public int value;

public Price(int value) {
this.value = value;
}

public static class Converter extends Transformer<Price> {
@Override
public Price transform(String value) {
return new Price(Integer.parseInt(value));
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
@foo
Feature: Basic Arithmetic

Background: A Calculator
Given a calculator I just turned on

Scenario: Addition
# Try to change one of the values below to provoke a failure
When I add 4 and 5
Then the result is 9

Scenario: Another Addition
# Try to change one of the values below to provoke a failure
When I add 4 and 7
Then the result is 11

Scenario Outline: Many additions
Given the previous entries:
| first | second | operation |
| 1 | 1 | + |
| 2 | 1 | + |
When I press +
And I add <a> and <b>
And I press +
Then the result is <c>

Examples: Single digits
| a | b | c |
| 1 | 2 | 8 |
| 2 | 3 | 10 |

Examples: Double digits
| a | b | c |
| 10 | 20 | 35 |
| 20 | 30 | 55 |
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
Feature: Dates with different date formats
This feature shows you can have different date formats, as long as you annotate the
corresponding step definition method accordingly.

Scenario: Determine past date
Given today is 2011-01-20
When I ask if Jan 19, 2011 is in the past
Then the result should be yes
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
Feature: Shopping

Scenario: Give correct change
Given the following groceries:
| name | price |
| milk | 9 |
| bread | 7 |
| soap | 5 |
When I pay 25
Then my change should be 4
6 changes: 6 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,11 @@
<artifactId>cucumber-junit</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-testng</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-picocontainer</artifactId>
Expand Down Expand Up @@ -484,6 +489,7 @@
<module>examples/java-wicket</module>
<module>examples/spring-txn</module>
<module>examples/java-calculator</module>
<module>examples/java-calculator-testng</module>
<module>examples/groovy-calculator</module>
<module>examples/scala-calculator</module>
<module>examples/java-webbit-websockets-selenium</module>
Expand Down

0 comments on commit b5a3bc5

Please sign in to comment.