Coverage Report - org.jbehave.core.embedder.StoryRunner
 
Classes in this File Line Coverage Branch Coverage Complexity
StoryRunner
92%
179/194
84%
79/94
2.308
StoryRunner$1
N/A
N/A
2.308
StoryRunner$FineSoFar
100%
14/14
70%
7/10
2.308
StoryRunner$RunContext
100%
35/35
75%
3/4
2.308
StoryRunner$SomethingHappened
100%
6/6
N/A
2.308
StoryRunner$State
N/A
N/A
2.308
 
 1  
 package org.jbehave.core.embedder;
 2  
 
 3  
 import java.util.ArrayList;
 4  
 import java.util.HashMap;
 5  
 import java.util.List;
 6  
 import java.util.Map;
 7  
 
 8  
 import org.jbehave.core.annotations.ScenarioType;
 9  
 import org.jbehave.core.configuration.Configuration;
 10  
 import org.jbehave.core.failures.FailureStrategy;
 11  
 import org.jbehave.core.failures.PendingStepFound;
 12  
 import org.jbehave.core.failures.PendingStepStrategy;
 13  
 import org.jbehave.core.failures.RestartingScenarioFailure;
 14  
 import org.jbehave.core.failures.UUIDExceptionWrapper;
 15  
 import org.jbehave.core.model.ExamplesTable;
 16  
 import org.jbehave.core.model.GivenStories;
 17  
 import org.jbehave.core.model.GivenStory;
 18  
 import org.jbehave.core.model.Meta;
 19  
 import org.jbehave.core.model.Scenario;
 20  
 import org.jbehave.core.model.Story;
 21  
 import org.jbehave.core.model.StoryDuration;
 22  
 import org.jbehave.core.reporters.ConcurrentStoryReporter;
 23  
 import org.jbehave.core.reporters.StoryReporter;
 24  
 import org.jbehave.core.steps.CandidateSteps;
 25  
 import org.jbehave.core.steps.InjectableStepsFactory;
 26  
 import org.jbehave.core.steps.PendingStepMethodGenerator;
 27  
 import org.jbehave.core.steps.ProvidedStepsFactory;
 28  
 import org.jbehave.core.steps.Step;
 29  
 import org.jbehave.core.steps.StepCollector.Stage;
 30  
 import org.jbehave.core.steps.StepCreator.PendingStep;
 31  
 import org.jbehave.core.steps.StepResult;
 32  
 
 33  
 import static org.codehaus.plexus.util.StringUtils.capitalizeFirstLetter;
 34  
 
 35  
 /**
 36  
  * Runs a {@link Story}, given a {@link Configuration} and a list of
 37  
  * {@link CandidateSteps}, describing the results to the {@link StoryReporter}.
 38  
  * 
 39  
  * @author Elizabeth Keogh
 40  
  * @author Mauro Talevi
 41  
  * @author Paul Hammant
 42  
  */
 43  253
 public class StoryRunner {
 44  
 
 45  65
     private ThreadLocal<FailureStrategy> currentStrategy = new ThreadLocal<FailureStrategy>();
 46  65
     private ThreadLocal<FailureStrategy> failureStrategy = new ThreadLocal<FailureStrategy>();
 47  65
     private ThreadLocal<PendingStepStrategy> pendingStepStrategy = new ThreadLocal<PendingStepStrategy>();
 48  65
     private ThreadLocal<UUIDExceptionWrapper> storyFailure = new ThreadLocal<UUIDExceptionWrapper>();
 49  65
     private ThreadLocal<StoryReporter> reporter = new ThreadLocal<StoryReporter>();
 50  65
     private ThreadLocal<String> reporterStoryPath = new ThreadLocal<String>();
 51  65
     private ThreadLocal<State> storiesState = new ThreadLocal<State>();
 52  
     // should this be volatile?
 53  65
     private Map<Story, StoryDuration> cancelledStories = new HashMap<Story, StoryDuration>();
 54  
 
 55  
     /**
 56  
      * Run steps before or after a collection of stories. Steps are execute only
 57  
      * <b>once</b> per collection of stories.
 58  
      * 
 59  
      * @param configuration the Configuration used to find the steps to run
 60  
      * @param candidateSteps the List of CandidateSteps containing the candidate
 61  
      *            steps methods
 62  
      * @param stage the Stage
 63  
      * @return The State after running the steps
 64  
      */
 65  
     public State runBeforeOrAfterStories(Configuration configuration, List<CandidateSteps> candidateSteps, Stage stage) {
 66  7
         String storyPath = capitalizeFirstLetter(stage.name().toLowerCase()) + "Stories";
 67  7
         reporter.set(configuration.storyReporter(storyPath));
 68  7
         reporter.get().beforeStory(new Story(storyPath), false);
 69  7
         RunContext context = new RunContext(configuration, candidateSteps, storyPath, MetaFilter.EMPTY);
 70  7
         if (stage == Stage.BEFORE) {
 71  4
             resetStoryFailure(context);
 72  
         }
 73  7
         if (stage == Stage.AFTER && storiesState.get() != null) {
 74  3
             context.stateIs(storiesState.get());
 75  
         }
 76  
         try {
 77  7
             runStepsWhileKeepingState(context,
 78  
                     configuration.stepCollector().collectBeforeOrAfterStoriesSteps(context.candidateSteps(), stage));
 79  0
         } catch (InterruptedException e) {
 80  0
             throw new UUIDExceptionWrapper(e);
 81  7
         }
 82  7
         reporter.get().afterStory(false);
 83  7
         storiesState.set(context.state());
 84  
         // handle any after stories failure according to strategy
 85  7
         if (stage == Stage.AFTER) {
 86  
             try {
 87  3
                 handleStoryFailureByStrategy();
 88  1
             } catch (Throwable e) {
 89  1
                 return new SomethingHappened(storyFailure.get());
 90  
             } finally {
 91  3
                 if (reporter.get() instanceof ConcurrentStoryReporter) {
 92  1
                     ((ConcurrentStoryReporter) reporter.get()).invokeDelayed();
 93  
                 }
 94  
             }
 95  
         }
 96  6
         return context.state();
 97  
     }
 98  
 
 99  
     /**
 100  
      * Runs a Story with the given configuration and steps.
 101  
      * 
 102  
      * @param configuration the Configuration used to run story
 103  
      * @param candidateSteps the List of CandidateSteps containing the candidate
 104  
      *            steps methods
 105  
      * @param story the Story to run
 106  
      * @throws Throwable if failures occurred and FailureStrategy dictates it to
 107  
      *             be re-thrown.
 108  
      */
 109  
     public void run(Configuration configuration, List<CandidateSteps> candidateSteps, Story story) throws Throwable {
 110  15
         run(configuration, candidateSteps, story, MetaFilter.EMPTY);
 111  14
     }
 112  
 
 113  
     /**
 114  
      * Runs a Story with the given configuration and steps, applying the given
 115  
      * meta filter.
 116  
      * 
 117  
      * @param configuration the Configuration used to run story
 118  
      * @param candidateSteps the List of CandidateSteps containing the candidate
 119  
      *            steps methods
 120  
      * @param story the Story to run
 121  
      * @param filter the Filter to apply to the story Meta
 122  
      * @throws Throwable if failures occurred and FailureStrategy dictates it to
 123  
      *             be re-thrown.
 124  
      */
 125  
     public void run(Configuration configuration, List<CandidateSteps> candidateSteps, Story story, MetaFilter filter)
 126  
             throws Throwable {
 127  19
         run(configuration, candidateSteps, story, filter, null);
 128  18
     }
 129  
 
 130  
     /**
 131  
      * Runs a Story with the given configuration and steps, applying the given
 132  
      * meta filter, and staring from given state.
 133  
      * 
 134  
      * @param configuration the Configuration used to run story
 135  
      * @param candidateSteps the List of CandidateSteps containing the candidate
 136  
      *            steps methods
 137  
      * @param story the Story to run
 138  
      * @param filter the Filter to apply to the story Meta
 139  
      * @param beforeStories the State before running any of the stories, if not
 140  
      *            <code>null</code>
 141  
      * @throws Throwable if failures occurred and FailureStrategy dictates it to
 142  
      *             be re-thrown.
 143  
      */
 144  
     public void run(Configuration configuration, List<CandidateSteps> candidateSteps, Story story, MetaFilter filter,
 145  
             State beforeStories) throws Throwable {
 146  19
         run(configuration, new ProvidedStepsFactory(candidateSteps), story, filter, beforeStories);
 147  18
     }
 148  
 
 149  
     /**
 150  
      * Runs a Story with the given steps factory, applying the given meta
 151  
      * filter, and staring from given state.
 152  
      * 
 153  
      * @param configuration the Configuration used to run story
 154  
      * @param stepsFactory the InjectableStepsFactory used to created the
 155  
      *            candidate steps methods
 156  
      * @param story the Story to run
 157  
      * @param filter the Filter to apply to the story Meta
 158  
      * @param beforeStories the State before running any of the stories, if not
 159  
      *            <code>null</code>
 160  
      * 
 161  
      * @throws Throwable if failures occurred and FailureStrategy dictates it to
 162  
      *             be re-thrown.
 163  
      */
 164  
     public void run(Configuration configuration, InjectableStepsFactory stepsFactory, Story story, MetaFilter filter,
 165  
             State beforeStories) throws Throwable {
 166  21
         RunContext context = new RunContext(configuration, stepsFactory, story.getPath(), filter);
 167  21
         if (beforeStories != null) {
 168  2
             context.stateIs(beforeStories);
 169  
         }
 170  21
         Map<String, String> storyParameters = new HashMap<String, String>();
 171  21
         run(context, story, storyParameters);
 172  18
     }
 173  
 
 174  
     /**
 175  
      * Returns the parsed story from the given path
 176  
      * 
 177  
      * @param configuration the Configuration used to run story
 178  
      * @param storyPath the story path
 179  
      * @return The parsed Story
 180  
      */
 181  
     public Story storyOfPath(Configuration configuration, String storyPath) {
 182  3
         String storyAsText = configuration.storyLoader().loadStoryAsText(storyPath);
 183  3
         return configuration.storyParser().parseStory(storyAsText, storyPath);
 184  
     }
 185  
 
 186  
     /**
 187  
      * Returns the parsed story from the given text
 188  
      * 
 189  
      * @param configuration the Configuration used to run story
 190  
      * @param storyAsText the story text
 191  
      * @param storyId the story Id, which will be returned as story path
 192  
      * @return The parsed Story
 193  
      */
 194  
     public Story storyOfText(Configuration configuration, String storyAsText, String storyId) {
 195  0
         return configuration.storyParser().parseStory(storyAsText, storyId);
 196  
     }
 197  
 
 198  
     /**
 199  
      * Cancels story execution following a timeout
 200  
      * 
 201  
      * @param story the Story that was timed out
 202  
      * @param storyDuration the StoryDuration
 203  
      */
 204  
     public void cancelStory(Story story, StoryDuration storyDuration) {
 205  2
         cancelledStories.put(story, storyDuration);
 206  2
     }
 207  
 
 208  
     private void run(RunContext context, Story story, Map<String, String> storyParameters) throws Throwable {
 209  
         try {
 210  23
             runCancellable(context, story, storyParameters);
 211  3
         } catch (Throwable e) {
 212  3
             if (cancelledStories.containsKey(story)) {
 213  2
                 reporter.get().storyCancelled(story, cancelledStories.get(story));
 214  2
                 reporter.get().afterStory(context.givenStory);
 215  
             }
 216  3
             throw e;
 217  
         } finally {
 218  23
             if (!context.givenStory() && reporter.get() instanceof ConcurrentStoryReporter) {
 219  21
                 ((ConcurrentStoryReporter) reporter.get()).invokeDelayed();
 220  
             }
 221  
         }
 222  20
     }
 223  
 
 224  
     private void runCancellable(RunContext context, Story story, Map<String, String> storyParameters) throws Throwable {
 225  23
         if (!context.givenStory) {
 226  21
             reporter.set(reporterFor(context, story));
 227  
         }
 228  23
         pendingStepStrategy.set(context.configuration().pendingStepStrategy());
 229  23
         failureStrategy.set(context.configuration().failureStrategy());
 230  
 
 231  23
         resetStoryFailure(context);
 232  
 
 233  23
         if (context.dryRun()) {
 234  2
             reporter.get().dryRun();
 235  
         }
 236  
 
 237  23
         if (context.configuration().storyControls().resetStateBeforeStory()) {
 238  21
             context.resetState();
 239  
         }
 240  
 
 241  
         // run before story steps, if any
 242  23
         reporter.get().beforeStory(story, context.givenStory());
 243  
 
 244  23
         boolean storyAllowed = true;
 245  
 
 246  23
         FilteredStory filterContext = context.filter(story);
 247  22
         Meta storyMeta = story.getMeta();
 248  22
         if (!filterContext.allowed()) {
 249  2
             reporter.get().storyNotAllowed(story, context.metaFilterAsString());
 250  2
             storyAllowed = false;
 251  
         }
 252  
 
 253  22
         if (storyAllowed) {
 254  
 
 255  20
             reporter.get().narrative(story.getNarrative());
 256  
 
 257  20
             runBeforeOrAfterStorySteps(context, story, Stage.BEFORE);
 258  
 
 259  
             // determine if before and after scenario steps should be run
 260  20
             boolean runBeforeAndAfterScenarioSteps = shouldRunBeforeOrAfterScenarioSteps(context);
 261  
 
 262  20
             for (Scenario scenario : story.getScenarios()) {
 263  
                 // scenario also inherits meta from story
 264  26
                 boolean scenarioAllowed = true;
 265  26
                 if (failureOccurred(context) && context.configuration().storyControls().skipScenariosAfterFailure()) {
 266  1
                     continue;
 267  
                 }
 268  25
                 reporter.get().beforeScenario(scenario.getTitle());
 269  25
                 reporter.get().scenarioMeta(scenario.getMeta());
 270  
 
 271  25
                 if (!filterContext.allowed(scenario)) {
 272  2
                     reporter.get().scenarioNotAllowed(scenario, context.metaFilterAsString());
 273  2
                     scenarioAllowed = false;
 274  
                 }
 275  
 
 276  25
                 if (scenarioAllowed) {
 277  23
                     if (context.configuration().storyControls().resetStateBeforeScenario()) {
 278  20
                         context.resetState();
 279  
                     }
 280  23
                     Meta storyAndScenarioMeta = scenario.getMeta().inheritFrom(storyMeta);
 281  
                     // run before scenario steps, if allowed
 282  23
                     if (runBeforeAndAfterScenarioSteps) {
 283  22
                         runBeforeOrAfterScenarioSteps(context, scenario, storyAndScenarioMeta, Stage.BEFORE,
 284  
                                 ScenarioType.NORMAL);
 285  
                     }
 286  
 
 287  
                     // run given stories, if any
 288  23
                     runGivenStories(scenario, context);
 289  23
                     if (isParameterisedByExamples(scenario)) {
 290  
                         // run parametrised scenarios by examples
 291  1
                         runParametrisedScenariosByExamples(context, scenario, storyAndScenarioMeta);
 292  
                     } else { // run as plain old scenario
 293  22
                         addMetaParameters(storyParameters, storyAndScenarioMeta);
 294  22
                         runScenarioSteps(context, scenario, storyParameters);
 295  
                     }
 296  
 
 297  
                     // run after scenario steps, if allowed
 298  22
                     if (runBeforeAndAfterScenarioSteps) {
 299  21
                         runBeforeOrAfterScenarioSteps(context, scenario, storyAndScenarioMeta, Stage.AFTER,
 300  
                                 ScenarioType.NORMAL);
 301  
                     }
 302  
 
 303  
                 }
 304  
 
 305  24
                 reporter.get().afterScenario();
 306  24
             }
 307  
 
 308  
             // run after story steps, if any
 309  19
             runBeforeOrAfterStorySteps(context, story, Stage.AFTER);
 310  
 
 311  
         }
 312  
 
 313  21
         reporter.get().afterStory(context.givenStory());
 314  
 
 315  
         // handle any failure according to strategy
 316  21
         if (!context.givenStory()) {
 317  19
             handleStoryFailureByStrategy();
 318  
         }
 319  20
     }
 320  
 
 321  
     private void addMetaParameters(Map<String, String> storyParameters, Meta meta) {
 322  22
         for (String name : meta.getPropertyNames()) {
 323  0
             storyParameters.put(name, meta.getProperty(name));
 324  
         }
 325  22
     }
 326  
 
 327  
     private boolean shouldRunBeforeOrAfterScenarioSteps(RunContext context) {
 328  20
         Configuration configuration = context.configuration();
 329  20
         if (!configuration.storyControls().skipBeforeAndAfterScenarioStepsIfGivenStory()) {
 330  18
             return true;
 331  
         }
 332  
 
 333  2
         return !context.givenStory();
 334  
     }
 335  
 
 336  
     private boolean failureOccurred(RunContext context) {
 337  26
         return context.failureOccurred();
 338  
     }
 339  
 
 340  
     private StoryReporter reporterFor(RunContext context, Story story) {
 341  21
         Configuration configuration = context.configuration();
 342  21
         if (context.givenStory()) {
 343  0
             return configuration.storyReporter(reporterStoryPath.get());
 344  
         } else {
 345  
             // store parent story path for reporting
 346  21
             reporterStoryPath.set(story.getPath());
 347  21
             return configuration.storyReporter(reporterStoryPath.get());
 348  
         }
 349  
     }
 350  
 
 351  
     private void handleStoryFailureByStrategy() throws Throwable {
 352  22
         Throwable throwable = storyFailure.get();
 353  22
         if (throwable != null) {
 354  11
             currentStrategy.get().handleFailure(throwable);
 355  
         }
 356  20
     }
 357  
 
 358  
     private void resetStoryFailure(RunContext context) {
 359  27
         if (context.givenStory()) {
 360  
             // do not reset failure for given stories
 361  2
             return;
 362  
         }
 363  25
         currentStrategy.set(context.configuration().failureStrategy());
 364  25
         storyFailure.set(null);
 365  25
     }
 366  
 
 367  
     private void runGivenStories(Scenario scenario, RunContext context) throws Throwable {
 368  23
         GivenStories givenStories = scenario.getGivenStories();
 369  23
         if (givenStories.getPaths().size() > 0) {
 370  2
             reporter.get().givenStories(givenStories);
 371  2
             for (GivenStory givenStory : givenStories.getStories()) {
 372  2
                 RunContext childContext = context.childContextFor(givenStory);
 373  
                 // run given story, using any parameters if provided
 374  2
                 Story story = storyOfPath(context.configuration(), childContext.path());
 375  2
                 run(childContext, story, givenStory.getParameters());
 376  2
             }
 377  
         }
 378  23
     }
 379  
 
 380  
     private boolean isParameterisedByExamples(Scenario scenario) {
 381  23
         return scenario.getExamplesTable().getRowCount() > 0 && !scenario.getGivenStories().requireParameters();
 382  
     }
 383  
 
 384  
     private void runParametrisedScenariosByExamples(RunContext context, Scenario scenario, Meta storyAndScenarioMeta)
 385  
             throws InterruptedException {
 386  1
         ExamplesTable table = scenario.getExamplesTable();
 387  1
         reporter.get().beforeExamples(scenario.getSteps(), table);
 388  1
         for (Map<String, String> scenarioParameters : table.getRows()) {
 389  1
             reporter.get().example(scenarioParameters);
 390  1
             if (context.configuration().storyControls().resetStateBeforeScenario()) {
 391  1
                 context.resetState();
 392  
             }
 393  1
             runBeforeOrAfterScenarioSteps(context, scenario, storyAndScenarioMeta, Stage.BEFORE, ScenarioType.EXAMPLE);
 394  1
             runScenarioSteps(context, scenario, scenarioParameters);
 395  1
             runBeforeOrAfterScenarioSteps(context, scenario, storyAndScenarioMeta, Stage.AFTER, ScenarioType.EXAMPLE);
 396  
         }
 397  1
         reporter.get().afterExamples();
 398  1
     }
 399  
 
 400  
     private void runBeforeOrAfterStorySteps(RunContext context, Story story, Stage stage) throws InterruptedException {
 401  39
         runStepsWhileKeepingState(context, context.collectBeforeOrAfterStorySteps(story, stage));
 402  39
     }
 403  
 
 404  
     private void runBeforeOrAfterScenarioSteps(RunContext context, Scenario scenario, Meta storyAndScenarioMeta,
 405  
             Stage stage, ScenarioType type) throws InterruptedException {
 406  45
         runStepsWhileKeepingState(context, context.collectBeforeOrAfterScenarioSteps(storyAndScenarioMeta, stage, type));
 407  45
     }
 408  
 
 409  
     private void runScenarioSteps(RunContext context, Scenario scenario, Map<String, String> scenarioParameters)
 410  
             throws InterruptedException {
 411  23
         boolean restart = true;
 412  46
         while (restart) {
 413  24
             restart = false;
 414  24
             List<Step> steps = context.collectScenarioSteps(scenario, scenarioParameters);
 415  
             try {
 416  24
                 runStepsWhileKeepingState(context, steps);
 417  1
             } catch (RestartingScenarioFailure e) {
 418  1
                 restart = true;
 419  1
                 continue;
 420  22
             }
 421  22
             generatePendingStepMethods(context, steps);
 422  22
         }
 423  22
     }
 424  
 
 425  
     private void generatePendingStepMethods(RunContext context, List<Step> steps) {
 426  22
         List<PendingStep> pendingSteps = new ArrayList<PendingStep>();
 427  22
         for (Step step : steps) {
 428  33
             if (step instanceof PendingStep) {
 429  0
                 pendingSteps.add((PendingStep) step);
 430  
             }
 431  
         }
 432  22
         if (!pendingSteps.isEmpty()) {
 433  0
             PendingStepMethodGenerator generator = new PendingStepMethodGenerator(context.configuration().keywords());
 434  0
             List<String> methods = new ArrayList<String>();
 435  0
             for (PendingStep pendingStep : pendingSteps) {
 436  0
                 if (!pendingStep.annotated()) {
 437  0
                     methods.add(generator.generateMethod(pendingStep));
 438  
                 }
 439  
             }
 440  0
             reporter.get().pendingMethods(methods);
 441  
         }
 442  22
     }
 443  
 
 444  
     private void runStepsWhileKeepingState(RunContext context, List<Step> steps) throws InterruptedException {
 445  115
         if (steps == null || steps.size() == 0) {
 446  82
             return;
 447  
         }
 448  33
         State state = context.state();
 449  33
         for (Step step : steps) {
 450  
             try {
 451  46
                 context.interruptIfCancelled();
 452  45
                 state = state.run(step);
 453  1
             } catch (RestartingScenarioFailure e) {
 454  1
                 reporter.get().restarted(step.toString(), e);
 455  1
                 throw e;
 456  44
             }
 457  
         }
 458  31
         context.stateIs(state);
 459  31
     }
 460  
 
 461  
     public interface State {
 462  
         State run(Step step);
 463  
     }
 464  
 
 465  144
     private final class FineSoFar implements State {
 466  
 
 467  
         public State run(Step step) {
 468  38
             UUIDExceptionWrapper storyFailureIfItHappened = storyFailure.get();
 469  38
             StepResult result = step.perform(storyFailureIfItHappened);
 470  37
             result.describeTo(reporter.get());
 471  37
             UUIDExceptionWrapper stepFailure = result.getFailure();
 472  37
             if (stepFailure == null) {
 473  22
                 return this;
 474  
             }
 475  
 
 476  15
             storyFailure.set(mostImportantOf(storyFailureIfItHappened, stepFailure));
 477  15
             currentStrategy.set(strategyFor(storyFailure.get()));
 478  15
             return new SomethingHappened(stepFailure);
 479  
         }
 480  
 
 481  
         private UUIDExceptionWrapper mostImportantOf(UUIDExceptionWrapper failure1, UUIDExceptionWrapper failure2) {
 482  15
             return failure1 == null ? failure2
 483  
                     : failure1.getCause() instanceof PendingStepFound ? (failure2 == null ? failure1 : failure2)
 484  
                             : failure1;
 485  
         }
 486  
 
 487  
         private FailureStrategy strategyFor(Throwable failure) {
 488  15
             if (failure instanceof PendingStepFound) {
 489  6
                 return pendingStepStrategy.get();
 490  
             } else {
 491  9
                 return failureStrategy.get();
 492  
             }
 493  
         }
 494  
     }
 495  
 
 496  
     private final class SomethingHappened implements State {
 497  
         UUIDExceptionWrapper scenarioFailure;
 498  
 
 499  16
         public SomethingHappened(UUIDExceptionWrapper scenarioFailure) {
 500  16
             this.scenarioFailure = scenarioFailure;
 501  16
         }
 502  
 
 503  
         public State run(Step step) {
 504  7
             StepResult result = step.doNotPerform(scenarioFailure);
 505  7
             result.describeTo(reporter.get());
 506  7
             return this;
 507  
         }
 508  
     }
 509  
 
 510  
     @Override
 511  
     public String toString() {
 512  1
         return this.getClass().getSimpleName();
 513  
     }
 514  
 
 515  
     /**
 516  
      * The context for running a story.
 517  
      */
 518  65
     private class RunContext {
 519  
         private final Configuration configuration;
 520  
         private final List<CandidateSteps> candidateSteps;
 521  
         private final String path;
 522  
         private final MetaFilter filter;
 523  
         private final boolean givenStory;
 524  
         private State state;
 525  
 
 526  
         public RunContext(Configuration configuration, InjectableStepsFactory stepsFactory, String path,
 527  
                 MetaFilter filter) {
 528  21
             this(configuration, stepsFactory.createCandidateSteps(), path, filter);
 529  21
         }
 530  
 
 531  
         public RunContext(Configuration configuration, List<CandidateSteps> steps, String path, MetaFilter filter) {
 532  28
             this(configuration, steps, path, filter, false);
 533  28
         }
 534  
 
 535  
         private RunContext(Configuration configuration, List<CandidateSteps> steps, String path, MetaFilter filter,
 536  30
                 boolean givenStory) {
 537  30
             this.configuration = configuration;
 538  30
             this.candidateSteps = steps;
 539  30
             this.path = path;
 540  30
             this.filter = filter;
 541  30
             this.givenStory = givenStory;
 542  30
             resetState();
 543  30
         }
 544  
 
 545  
         public void interruptIfCancelled() throws InterruptedException {
 546  46
             for (Story story : cancelledStories.keySet()) {
 547  1
                 if (path.equals(story.getPath())) {
 548  1
                     throw new InterruptedException(path);
 549  
                 }
 550  
             }
 551  45
         }
 552  
 
 553  
         public boolean dryRun() {
 554  23
             return configuration.storyControls().dryRun();
 555  
         }
 556  
 
 557  
         public Configuration configuration() {
 558  166
             return configuration;
 559  
         }
 560  
 
 561  
         public List<CandidateSteps> candidateSteps() {
 562  7
             return candidateSteps;
 563  
         }
 564  
 
 565  
         public boolean givenStory() {
 566  138
             return givenStory;
 567  
         }
 568  
 
 569  
         public String path() {
 570  2
             return path;
 571  
         }
 572  
 
 573  
         public FilteredStory filter(Story story) {
 574  23
             return new FilteredStory(filter, story, configuration.storyControls());
 575  
         }
 576  
 
 577  
         public String metaFilterAsString() {
 578  4
             return filter.asString();
 579  
         }
 580  
 
 581  
         public List<Step> collectBeforeOrAfterStorySteps(Story story, Stage stage) {
 582  39
             return configuration.stepCollector().collectBeforeOrAfterStorySteps(candidateSteps, story, stage,
 583  
                     givenStory);
 584  
         }
 585  
 
 586  
         public List<Step> collectBeforeOrAfterScenarioSteps(Meta storyAndScenarioMeta, Stage stage, ScenarioType type) {
 587  45
             return configuration.stepCollector().collectBeforeOrAfterScenarioSteps(candidateSteps,
 588  
                     storyAndScenarioMeta, stage, type);
 589  
         }
 590  
 
 591  
         public List<Step> collectScenarioSteps(Scenario scenario, Map<String, String> parameters) {
 592  24
             return configuration.stepCollector().collectScenarioSteps(candidateSteps, scenario, parameters);
 593  
         }
 594  
 
 595  
         public RunContext childContextFor(GivenStory givenStory) {
 596  2
             String actualPath = configuration.pathCalculator().calculate(path, givenStory.getPath());
 597  2
             return new RunContext(configuration, candidateSteps, actualPath, filter, true);
 598  
         }
 599  
 
 600  
         public State state() {
 601  46
             return state;
 602  
         }
 603  
 
 604  
         public void stateIs(State state) {
 605  36
             this.state = state;
 606  36
         }
 607  
 
 608  
         public boolean failureOccurred() {
 609  26
             return failed(state);
 610  
         }
 611  
 
 612  
         public void resetState() {
 613  72
             this.state = new FineSoFar();
 614  72
         }
 615  
 
 616  
     }
 617  
 
 618  
     public boolean failed(State state) {
 619  28
         return !state.getClass().equals(FineSoFar.class);
 620  
     }
 621  
 
 622  
     public Throwable failure(State state) {
 623  0
         if (failed(state)) {
 624  0
             return ((SomethingHappened) state).scenarioFailure.getCause();
 625  
         }
 626  0
         return null;
 627  
     }
 628  
 }