@@ -1,9 +1,14 @@
package net.thucydides.jbehave;
package net.thucydides.jbehave.internals;

import net.thucydides.core.guice.Injectors;
import org.jbehave.core.configuration.Configuration;
import org.jbehave.core.configuration.ParanamerConfiguration;
import org.jbehave.core.io.StoryFinder;
import org.jbehave.core.reporters.StoryReporterBuilder;

import java.util.List;

import static org.jbehave.core.io.CodeLocations.codeLocationFromClass;
import static org.jbehave.core.reporters.Format.CONSOLE;
import static org.jbehave.core.reporters.Format.HTML;

@@ -17,10 +22,14 @@ public class ThucydidesJBehave {
*
* @return
*/
public static Configuration defaultConfiguration() {
public static Configuration defaultConfiguration(net.thucydides.core.webdriver.Configuration systemConfiguration) {
return new ParanamerConfiguration()
.useStoryReporterBuilder(new StoryReporterBuilder().withDefaultFormats()
.withFormats(CONSOLE, HTML)
.withReporters(new ThucydidesReporter()));
.withReporters(new ThucydidesReporter(systemConfiguration)));
}

public static Configuration defaultConfiguration() {
return defaultConfiguration(Injectors.getInjector().getInstance(net.thucydides.core.webdriver.Configuration.class));
}
}
@@ -1,11 +1,14 @@
package net.thucydides.jbehave;
package net.thucydides.jbehave.internals;

import net.thucydides.core.ThucydidesListeners;
import net.thucydides.core.ThucydidesReports;
import net.thucydides.core.model.TestOutcome;
import net.thucydides.core.reports.ReportService;
import net.thucydides.core.steps.ExecutedStepDescription;
import net.thucydides.core.steps.StepEventBus;
import net.thucydides.core.steps.StepFailure;
import net.thucydides.core.util.NameConverter;
import net.thucydides.core.webdriver.Configuration;
import org.jbehave.core.model.ExamplesTable;
import org.jbehave.core.model.GivenStories;
import org.jbehave.core.model.Meta;
@@ -29,6 +32,11 @@ public class ThucydidesReporter implements StoryReporter {

private ThucydidesListeners thucydidesListeners;
private ReportService reportService;
private final Configuration systemConfiguration;

public ThucydidesReporter(Configuration systemConfiguration) {
this.systemConfiguration = systemConfiguration;
}

public void storyNotAllowed(Story story, String s) {
}
@@ -39,9 +47,10 @@ public void storyCancelled(Story story, StoryDuration storyDuration) {
public void beforeStory(Story story, boolean b) {
System.out.println("Before story: " + story.getName());
String storyName = removeSuffixFrom(story.getName());
thucydidesListeners = ThucydidesReports.setupListeners();
reportService = ThucydidesReports.getReportService();
StepEventBus.getEventBus().testSuiteStarted(net.thucydides.core.model.Story.withId(storyName, storyName));
String storyTitle = NameConverter.humanize(storyName);
reportService = ThucydidesReports.getReportService(systemConfiguration);
thucydidesListeners = ThucydidesReports.setupListeners(systemConfiguration);
StepEventBus.getEventBus().testSuiteStarted(net.thucydides.core.model.Story.withId(storyName, storyTitle));
}

private String removeSuffixFrom(String name) {
@@ -1,9 +1,9 @@
package net.thucydides.jbehave;
package net.thucydides.jbehave.internals;

import com.thoughtworks.paranamer.Paranamer;
import com.wakaleo.jbehave.introspection.Extract;
import net.thucydides.core.steps.StepAnnotations;
import net.thucydides.core.steps.StepFactory;
import net.thucydides.jbehave.reflection.Extract;
import org.jbehave.core.configuration.Keywords;
import org.jbehave.core.parsers.RegexPrefixCapturingPatternParser;
import org.jbehave.core.steps.InjectableStepsFactory;
@@ -0,0 +1,19 @@
package net.thucydides.jbehave.internals;

/**
* Keeps track of instantiated JBehave step libraries used in Thucydides tests.
*/
public class ThucydidesStepContext {

public ThucydidesStepContext() {
}

public Object newInstanceOf(Class<?> type) {
try {
return type.newInstance();
} catch (Exception e) {
throw new ThucydidesStepInitializationError(e);
}
}
}

@@ -1,4 +1,4 @@
package net.thucydides.jbehave;
package net.thucydides.jbehave.internals;

import ch.lambdaj.function.convert.Converter;
import com.google.common.collect.Lists;
@@ -9,10 +9,8 @@
import org.jbehave.core.configuration.Configuration;
import org.jbehave.core.steps.AbstractStepsFactory;
import org.jbehave.core.steps.CandidateSteps;
import org.jbehave.core.steps.InjectableStepsFactory;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Set;

@@ -91,7 +89,7 @@ public Object createInstanceOfType(Class<?> type) {
}

public static ThucydidesStepFactory withStoriesFromPackage(String rootPackage) {
return new ThucydidesStepFactory(ThucydidesJBehavePlugin.defaultConfiguration(), rootPackage);
return new ThucydidesStepFactory(ThucydidesJBehave.defaultConfiguration(), rootPackage);
}

public ThucydidesStepFactory andConfiguration(Configuration configuration) {
@@ -0,0 +1,7 @@
package net.thucydides.jbehave.internals;

public class ThucydidesStepInitializationError extends RuntimeException {
public ThucydidesStepInitializationError(Exception cause) {
super(cause);
}
}
@@ -1,4 +1,4 @@
package net.thucydides.jbehave;
package net.thucydides.jbehave.reflection;

import java.lang.reflect.Field;

@@ -0,0 +1,59 @@
package net.thucydides.jbehave;

import net.thucydides.core.model.TestOutcome;
import net.thucydides.core.reports.xml.XMLTestOutcomeReporter;
import net.thucydides.core.util.MockEnvironmentVariables;
import net.thucydides.core.webdriver.Configuration;
import net.thucydides.core.webdriver.SystemPropertiesConfiguration;
import org.jbehave.core.reporters.StoryReporter;
import org.jbehave.core.reporters.TxtOutput;
import org.junit.Before;
import org.junit.Rule;
import org.junit.rules.TemporaryFolder;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintStream;
import java.util.List;

public class AbstractJBehaveStory {
protected OutputStream output;
protected StoryReporter printOutput;

protected MockEnvironmentVariables environmentVariables;
protected Configuration systemConfiguration;

@Rule
public TemporaryFolder temporaryFolder = new TemporaryFolder();

protected File outputDirectory;

@Before
public void prepareReporter() {

environmentVariables = new MockEnvironmentVariables();

outputDirectory = temporaryFolder.newFolder("output");
environmentVariables.setProperty("thucydides.outputDirectory", outputDirectory.getAbsolutePath());

output = new ByteArrayOutputStream();
printOutput = new TxtOutput(new PrintStream(output));
systemConfiguration = new SystemPropertiesConfiguration(environmentVariables);
}


protected void run(JUnitThucydidesStories stories) {
try {
stories.run();
} catch(Throwable e) {
// Ignore
}
}

protected List<TestOutcome> loadTestOutcomes() throws IOException {
XMLTestOutcomeReporter outcomeReporter = new XMLTestOutcomeReporter();
return outcomeReporter.loadReportsFrom(outputDirectory);
}
}

This file was deleted.

@@ -0,0 +1,92 @@
package net.thucydides.jbehave;

import net.thucydides.core.model.TestOutcome;
import net.thucydides.core.model.TestResult;
import net.thucydides.core.reports.xml.XMLTestOutcomeReporter;
import net.thucydides.core.util.MockEnvironmentVariables;
import net.thucydides.core.webdriver.Configuration;
import net.thucydides.core.webdriver.SystemPropertiesConfiguration;
import org.jbehave.core.reporters.StoryReporter;
import org.jbehave.core.reporters.TxtOutput;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintStream;
import java.util.List;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;

public class WhenRunningJBehaveStories extends AbstractJBehaveStory {

private static final int TOTAL_NUMBER_OF_JBEHAVE_SCENARIOS = 8;

final class AllStories extends JUnitThucydidesStories {}

@Test
public void all_stories_on_the_classpath_should_be_run_by_default() throws Throwable {

// Given
JUnitThucydidesStories stories = new AllStories();
stories.setSystemConfiguration(systemConfiguration);
stories.configuredEmbedder().configuration().storyReporterBuilder().withReporters(printOutput);

// When
run(stories);

// Then
List<TestOutcome> outcomes = loadTestOutcomes();
assertThat(outcomes.size(), is(TOTAL_NUMBER_OF_JBEHAVE_SCENARIOS));
}

final class StoriesInTheSubsetFolder extends JUnitThucydidesStories {
public void configure() {
findStoriesIn("stories/subset");
}
}

@Test
public void a_subset_of_the_stories_can_be_run_individually() throws Throwable {

// Given
JUnitThucydidesStories stories = new StoriesInTheSubsetFolder();
stories.setSystemConfiguration(systemConfiguration);
stories.configuredEmbedder().configuration().storyReporterBuilder().withReporters(printOutput);

// When
run(stories);

// Then

List<TestOutcome> outcomes = loadTestOutcomes();
assertThat(outcomes.size(), is(2));
}

final class SomePassingStories extends JUnitThucydidesStories {
public void configure() {
findStoriesCalled("*PassingStory.story");
}
}
@Test
public void stories_with_a_matching_name_can_be_run() throws Throwable {

// Given
JUnitThucydidesStories stories = new SomePassingStories();
stories.setSystemConfiguration(systemConfiguration);
stories.configuredEmbedder().configuration().storyReporterBuilder().withReporters(printOutput);

// When
run(stories);

// Then
List<TestOutcome> outcomes = loadTestOutcomes();
assertThat(outcomes.size(), is(3));
}

}
@@ -1,10 +1,4 @@
package net.thucydides.jbehave.samples;

/**
* A description goes here.
* User: johnsmart
* Date: 19/05/12
* Time: 2:36 PM
*/
public class SampleJBehaveSteps {
}
@@ -0,0 +1,15 @@
package net.thucydides.jbehave.steps;

import net.thucydides.core.annotations.Step;

public class SomeThucydidesSteps {

@Step
public void step1() {}

@Step
public void step2() {}

@Step
public void step3() {}
}
@@ -0,0 +1,42 @@
package net.thucydides.jbehave.steps;

import org.jbehave.core.annotations.Given;
import org.jbehave.core.annotations.Pending;
import org.jbehave.core.annotations.Then;
import org.jbehave.core.annotations.When;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;

public class StorySteps {
@Given("I have an implemented JBehave scenario")
public void givenIHaveAnImplementedJBehaveScenario() {
}

@Given("the scenario works")
public void givenTheScenarioWorks() {
}

@When("I run the scenario")
public void whenIRunTheScenario() {
}

@Then("I should get a successful result")
public void thenIShouldGetASuccessfulResult() {
}

@Given("the scenario fails")
public void givenTheScenarioFails() {
}

@Then("I should get a failed result")
public void thenIShouldGetAFailedResult() {
assertThat(true,is(false));
}

@Given("a JBehave story with a pending implementation")
@Pending
public void aJBehaveStoryWithAPendingImplementation() {}


}
@@ -0,0 +1,28 @@
package net.thucydides.jbehave.steps;

import net.thucydides.core.annotations.Steps;
import org.jbehave.core.annotations.Given;
import org.jbehave.core.annotations.Pending;
import org.jbehave.core.annotations.Then;
import org.jbehave.core.annotations.When;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;

public class StoryStepsWithThucydidesSteps {

@Steps
SomeThucydidesSteps steps;

@Given("the scenario has steps")
public void givenTheScenarioHasSteps() {
steps.step1();
steps.step2();
steps.step3();
}

@Then("the steps should appear in the outcome")
public void thenTheStepsShouldAppearInTheOutcome() {
}

}
@@ -0,0 +1,6 @@
Scenario: A scenario that works

Given I have an implemented JBehave scenario
And the scenario fails
When I run the scenario
Then I should get a failed result
@@ -1,15 +1,6 @@
Scenario: A scenario with groovy steps

Given a date of 10/16/2010
When 4 days pass
Then the date is 10/18/2010
And some otherwise ambiguous two string step, with one and two as strings


Scenario: Another scenario with groovy steps

Given a date of 10/16/2010
When 4 days pass
Then the date is 10/18/2010
And some otherwise ambiguous two string step, with one and two as strings
Scenario: A scenario that works

Given I have an implemented JBehave scenario
And the scenario works
When I run the scenario
Then I should get a successful result
@@ -0,0 +1,6 @@
Scenario: A scenario using steps

Given I have an implemented JBehave scenario
And the scenario has steps
When I run the scenario
Then the steps should appear in the outcome
@@ -0,0 +1,5 @@
Scenario: A scenario with implemented pending steps

Given a JBehave story with a pending implementation
When the story is executed
Then the steps should be marked as pending
@@ -0,0 +1,8 @@
import org.jbehave.core.annotations.Then

Scenario: A scenario with pending steps

Given JBehave story with no implementation
When the story is executed
Then the steps should be marked as pending
And sample implementations should be proposed
@@ -0,0 +1,19 @@
package stories.subset

import org.jbehave.core.annotations.Given

Scenario: A scenario with groovy steps

Given a date of 10/16/2010
When 4 days pass
Then the date is 10/18/2010
And some otherwise ambiguous two string step, with one and two as strings


Scenario: Another scenario with groovy steps

Given a date of 10/16/2010
When 4 days pass
Then the date is 10/18/2010
And some otherwise ambiguous two string step, with one and two as strings