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