package com.merchantwarehouse.zucchini;

import cucumber.api.java.en.Given;
import cucumber.api.java.en.Then;
import cucumber.api.java.en.When;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.runner.RunWith;
import static org.junit.Assert.assertEquals;

/**
 * This class tests the integration with various junit features (@BeforeClass and @AfterClass). It also has the side
 * effect of testing that the same Gherkin rules and the same Cucumber Step Definitions may appear in more than one file
 * without them conflicting.
 *
 * @author dominicl
 */
@RunWith(Cucumber.class)
@Cucumber.Options(features = "DifferentlyNamedFeature.feature")
public class JunitIntegrationTest {

    // counters of how many times junit's beforeclass/afterclass were called. these should be 1x each.
    private static int countOfTimesBeforeClassWasCalled = 0;
    private static int countOfTimesAfterClassWasCalled = 0;

    // counters of how many times given/when/then were called. given the above feature file, these should be 2x each.
    // this is important, because there are 2 Gherkin feature files with the same rules, so the stock version of cucumber
    // would run these rules 4x each. actually, it would fail to run at all because both this class and the InheritanceTest
    // and GlueClassTest define identical step definitions. we'll validate the counters in @AfterClass
    private static int countOfTimesGivenWasCalled = 0;
    private static int countOfTimesWhenWasCalled = 0;
    private static int countOfTimesThenWasCalled = 0;

    @BeforeClass
    public static void beforeClass() {
        countOfTimesBeforeClassWasCalled++;
        System.out.format("Called beforeClass() %d times", countOfTimesBeforeClassWasCalled).println();
    }

    @AfterClass
    public static void afterClass() {
        countOfTimesAfterClassWasCalled++;
        System.out.format("Called afterClass() %d times", countOfTimesAfterClassWasCalled).println();

        assertEquals("countOfTimesBeforeClassWasCalled", 1, countOfTimesBeforeClassWasCalled);
        assertEquals("countOfTimesAfterClassWasCalled", 1, countOfTimesAfterClassWasCalled);

        assertEquals("countOfTimesGivenWasCalled", 2, countOfTimesGivenWasCalled);
        assertEquals("countOfTimesWhenWasCalled", 2, countOfTimesWhenWasCalled);
        assertEquals("countOfTimesThenWasCalled", 2, countOfTimesThenWasCalled);
    }

    @Then("^the total should be (\\d+)$")
    public void then_the_total_should_be(Integer total) {
        countOfTimesThenWasCalled++;
    }

    @Given("^two integers (\\d+) and (\\d+)$")
    public final void given_two_integers(final Integer a, final Integer b) {
        countOfTimesGivenWasCalled++;
    }

    @When("^you add them together$")
    public final void when_you_add_them_together() {
        countOfTimesWhenWasCalled++;
    }
}

@@ -133,24 +133,89 @@
 va.en.When;%0A
+import cucumber.api.java.After;%0Aimport cucumber.api.java.Before;%0A
 import org.j
@@ -227,24 +227,24 @@
 AfterClass;%0A
-
 import org.j
@@ -764,16 +764,226 @@
 Test %7B%0A%0A
+    // counters of how many times cucumber's's before/after were called. these should be 2x each.%0A    private static int countOfTimesBeforeWasCalled = 0;%0A    private static int countOfTimesAfterWasCalled = 0;%0A%0A
     // c
@@ -1826,24 +1826,191 @@
 alled = 0;%0A%0A
+    @Before%0A    public void before() %7B%0A        countOfTimesBeforeWasCalled++;%0A    %7D%0A%0A    @After%0A    public void after() %7B%0A        countOfTimesAfterWasCalled++;%0A    %7D%0A%0A
     @BeforeC
@@ -2495,32 +2495,32 @@
 lassWasCalled);%0A
-
         assertEq
@@ -2593,24 +2593,193 @@
 asCalled);%0A%0A
+        assertEquals(%22countOfTimesBeforeWasCalled%22, 2, countOfTimesBeforeWasCalled);%0A        assertEquals(%22countOfTimesAfterWasCalled%22, 2, countOfTimesAfterWasCalled);%0A%0A
         asse

test that hooks work