Coverage Report - org.jbehave.core.embedder.StoryRunner
 
Classes in this File Line Coverage Branch Coverage Complexity
StoryRunner
85%
202/235
75%
87/116
2.474
StoryRunner$1
N/A
N/A
2.474
StoryRunner$FineSoFar
100%
16/16
75%
9/12
2.474
StoryRunner$RunContext
97%
39/40
83%
5/6
2.474
StoryRunner$SomethingHappened
100%
6/6
N/A
2.474
StoryRunner$State
N/A
N/A
2.474
 
 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.configuration.Keywords;
 11  
 import org.jbehave.core.failures.FailureStrategy;
 12  
 import org.jbehave.core.failures.PendingStepFound;
 13  
 import org.jbehave.core.failures.PendingStepStrategy;
 14  
 import org.jbehave.core.failures.RestartingScenarioFailure;
 15  
 import org.jbehave.core.failures.UUIDExceptionWrapper;
 16  
 import org.jbehave.core.model.ExamplesTable;
 17  
 import org.jbehave.core.model.GivenStories;
 18  
 import org.jbehave.core.model.GivenStory;
 19  
 import org.jbehave.core.model.Lifecycle;
 20  
 import org.jbehave.core.model.Meta;
 21  
 import org.jbehave.core.model.Scenario;
 22  
 import org.jbehave.core.model.Story;
 23  
 import org.jbehave.core.model.StoryDuration;
 24  
 import org.jbehave.core.reporters.ConcurrentStoryReporter;
 25  
 import org.jbehave.core.reporters.StoryReporter;
 26  
 import org.jbehave.core.steps.CandidateSteps;
 27  
 import org.jbehave.core.steps.InjectableStepsFactory;
 28  
 import org.jbehave.core.steps.PendingStepMethodGenerator;
 29  
 import org.jbehave.core.steps.ProvidedStepsFactory;
 30  
 import org.jbehave.core.steps.Step;
 31  
 import org.jbehave.core.steps.StepCollector.Stage;
 32  
 import org.jbehave.core.steps.StepCreator.ParameterisedStep;
 33  
 import org.jbehave.core.steps.StepCreator.PendingStep;
 34  
 import org.jbehave.core.steps.StepResult;
 35  
 
 36  
 import static org.codehaus.plexus.util.StringUtils.capitalizeFirstLetter;
 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  267
 public class StoryRunner {
 47  
 
 48  67
     private ThreadLocal<FailureStrategy> currentStrategy = new ThreadLocal<FailureStrategy>();
 49  67
     private ThreadLocal<FailureStrategy> failureStrategy = new ThreadLocal<FailureStrategy>();
 50  67
     private ThreadLocal<PendingStepStrategy> pendingStepStrategy = new ThreadLocal<PendingStepStrategy>();
 51  67
     private ThreadLocal<UUIDExceptionWrapper> storyFailure = new ThreadLocal<UUIDExceptionWrapper>();
 52  67
     private ThreadLocal<StoryReporter> reporter = new ThreadLocal<StoryReporter>();
 53  67
     private ThreadLocal<String> reporterStoryPath = new ThreadLocal<String>();
 54  67
     private ThreadLocal<State> storiesState = new ThreadLocal<State>();
 55  
     // should this be volatile?
 56  67
     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  9
         String storyPath = capitalizeFirstLetter(stage.name().toLowerCase()) + "Stories";
 70  9
         reporter.set(configuration.storyReporter(storyPath));
 71  9
         reporter.get().beforeStory(new Story(storyPath), false);
 72  9
         RunContext context = new RunContext(configuration, candidateSteps, storyPath, MetaFilter.EMPTY);
 73  9
         if (stage == Stage.BEFORE) {
 74  5
             resetStoryFailure(context);
 75  
         }
 76  9
         if (stage == Stage.AFTER && storiesState.get() != null) {
 77  4
             context.stateIs(storiesState.get());
 78  
         }
 79  
         try {
 80  9
             runStepsWhileKeepingState(context,
 81  
                     configuration.stepCollector().collectBeforeOrAfterStoriesSteps(context.candidateSteps(), stage));
 82  0
         } catch (InterruptedException e) {
 83  0
             throw new UUIDExceptionWrapper(e);
 84  9
         }
 85  9
         reporter.get().afterStory(false);
 86  9
         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  9
         if (stage == Stage.BEFORE) {
 90  5
             if (reporter.get() instanceof ConcurrentStoryReporter) {
 91  2
                 ((ConcurrentStoryReporter) reporter.get()).invokeDelayed();
 92  
             }
 93  
         }
 94  
         // handle any after stories failure according to strategy
 95  9
         if (stage == Stage.AFTER) {
 96  
             try {
 97  4
                 handleStoryFailureByStrategy();
 98  1
             } catch (Throwable e) {
 99  1
                 return new SomethingHappened(storyFailure.get());
 100  
             } finally {
 101  4
                 if (reporter.get() instanceof ConcurrentStoryReporter) {
 102  1
                     ((ConcurrentStoryReporter) reporter.get()).invokeDelayed();
 103  
                 }
 104  
             }
 105  
         }
 106  8
         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  23
         RunContext context = new RunContext(configuration, stepsFactory, story.getPath(), filter);
 177  23
         if (beforeStories != null) {
 178  4
             context.stateIs(beforeStories);
 179  
         }
 180  23
         Map<String, String> storyParameters = new HashMap<String, String>();
 181  23
         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  6
         String storyAsText = configuration.storyLoader().loadStoryAsText(storyPath);
 193  6
         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  3
         cancelledStories.put(story, storyDuration);
 216  3
     }
 217  
 
 218  
     private void run(RunContext context, Story story, Map<String, String> storyParameters) throws Throwable {
 219  
         try {
 220  26
             runCancellable(context, story, storyParameters);
 221  4
         } catch (Throwable e) {
 222  4
             if (cancelledStories.containsKey(story)) {
 223  3
                 reporter.get().storyCancelled(story, cancelledStories.get(story));
 224  3
                 reporter.get().afterScenario();
 225  3
                 reporter.get().afterStory(context.givenStory);
 226  
             }
 227  4
             throw e;
 228  
         } finally {
 229  26
             if (!context.givenStory() && reporter.get() instanceof ConcurrentStoryReporter) {
 230  22
                 ((ConcurrentStoryReporter) reporter.get()).invokeDelayed();
 231  
             }
 232  
         }
 233  22
     }
 234  
 
 235  
     private void runCancellable(RunContext context, Story story, Map<String, String> storyParameters) throws Throwable {
 236  26
         if (!context.givenStory()) {
 237  23
             reporter.set(reporterFor(context, story));
 238  
         }
 239  26
         pendingStepStrategy.set(context.configuration().pendingStepStrategy());
 240  26
         failureStrategy.set(context.configuration().failureStrategy());
 241  
 
 242  26
         resetStoryFailure(context);
 243  
 
 244  26
         if (context.dryRun()) {
 245  2
             reporter.get().dryRun();
 246  
         }
 247  
 
 248  26
         if (context.configuration().storyControls().resetStateBeforeStory()) {
 249  24
             context.resetState();
 250  
         }
 251  
 
 252  
         // run before story steps, if any
 253  26
         reporter.get().beforeStory(story, context.givenStory());
 254  
 
 255  26
         boolean storyAllowed = true;
 256  
 
 257  26
         FilteredStory filterContext = context.filter(story);
 258  25
         Meta storyMeta = story.getMeta();
 259  25
         if (!filterContext.allowed()) {
 260  2
             reporter.get().storyNotAllowed(story, context.metaFilterAsString());
 261  2
             storyAllowed = false;
 262  
         }
 263  
 
 264  25
         if (storyAllowed) {
 265  
 
 266  23
             reporter.get().narrative(story.getNarrative());
 267  
 
 268  23
             runBeforeOrAfterStorySteps(context, story, Stage.BEFORE);
 269  
 
 270  23
             addMetaParameters(storyParameters, storyMeta);
 271  
 
 272  23
             runGivenStories(story.getGivenStories(), storyParameters, context);
 273  
             
 274  
             // determine if before and after scenario steps should be run
 275  23
             boolean runBeforeAndAfterScenarioSteps = shouldRunBeforeOrAfterScenarioSteps(context);
 276  
             
 277  23
             reporter.get().lifecyle(story.getLifecycle());
 278  23
             for (Scenario scenario : story.getScenarios()) {
 279  
                 // scenario also inherits meta from story
 280  29
                 boolean scenarioAllowed = true;
 281  29
                 if (failureOccurred(context) && context.configuration().storyControls().skipScenariosAfterFailure()) {
 282  1
                     continue;
 283  
                 }
 284  28
                 reporter.get().beforeScenario(scenario.getTitle());
 285  28
                 reporter.get().scenarioMeta(scenario.getMeta());
 286  
 
 287  28
                 if (!filterContext.allowed(scenario)) {
 288  2
                     reporter.get().scenarioNotAllowed(scenario, context.metaFilterAsString());
 289  2
                     scenarioAllowed = false;
 290  
                 }
 291  
 
 292  28
                 if (scenarioAllowed) {
 293  26
                     if (context.configuration().storyControls().resetStateBeforeScenario()) {
 294  23
                         context.resetState();
 295  
                     }
 296  26
                     Meta storyAndScenarioMeta = scenario.getMeta().inheritFrom(storyMeta);
 297  
                     // run before scenario steps, if allowed
 298  26
                     if (runBeforeAndAfterScenarioSteps) {
 299  25
                         runBeforeOrAfterScenarioSteps(context, scenario, storyAndScenarioMeta, Stage.BEFORE,
 300  
                                 ScenarioType.NORMAL);
 301  
                     }
 302  26
                     runLifecycleSteps(context, story.getLifecycle(), Stage.BEFORE, storyAndScenarioMeta);
 303  26
                     if (isParameterisedByExamples(scenario)) { // run parametrised scenarios by examples
 304  1
                         runScenariosParametrisedByExamples(context, scenario, storyAndScenarioMeta);
 305  
                     } else { // run as plain old scenario
 306  25
                         addMetaParameters(storyParameters, storyAndScenarioMeta);
 307  25
                         runGivenStories(scenario.getGivenStories(), storyParameters, context);
 308  25
                         runScenarioSteps(context, scenario, storyParameters);
 309  
                     }
 310  25
                     runLifecycleSteps(context, story.getLifecycle(), Stage.AFTER, storyAndScenarioMeta);
 311  
 
 312  
                     // run after scenario steps, if allowed
 313  25
                     if (runBeforeAndAfterScenarioSteps) {
 314  24
                         runBeforeOrAfterScenarioSteps(context, scenario, storyAndScenarioMeta, Stage.AFTER,
 315  
                                 ScenarioType.NORMAL);
 316  
                     }
 317  
 
 318  
                 }
 319  
 
 320  27
                 reporter.get().afterScenario();
 321  27
             }
 322  
 
 323  
             // run after story steps, if any
 324  22
             runBeforeOrAfterStorySteps(context, story, Stage.AFTER);
 325  
 
 326  
         }
 327  
 
 328  24
         reporter.get().afterStory(context.givenStory());
 329  
 
 330  
         // handle any failure according to strategy
 331  24
         if (!context.givenStory()) {
 332  21
             handleStoryFailureByStrategy();
 333  
         }
 334  22
     }
 335  
 
 336  
     private void addMetaParameters(Map<String, String> storyParameters, Meta meta) {
 337  49
         for (String name : meta.getPropertyNames()) {
 338  0
             storyParameters.put(name, meta.getProperty(name));
 339  0
         }
 340  49
     }
 341  
 
 342  
     private boolean shouldRunBeforeOrAfterScenarioSteps(RunContext context) {
 343  23
         Configuration configuration = context.configuration();
 344  23
         if (!configuration.storyControls().skipBeforeAndAfterScenarioStepsIfGivenStory()) {
 345  21
             return true;
 346  
         }
 347  
 
 348  2
         return !context.givenStory();
 349  
     }
 350  
 
 351  
     private boolean failureOccurred(RunContext context) {
 352  29
         return context.failureOccurred();
 353  
     }
 354  
 
 355  
     private StoryReporter reporterFor(RunContext context, Story story) {
 356  23
         Configuration configuration = context.configuration();
 357  23
         if (context.givenStory()) {
 358  0
             return configuration.storyReporter(reporterStoryPath.get());
 359  
         } else {
 360  
             // store parent story path for reporting
 361  23
             reporterStoryPath.set(story.getPath());
 362  23
             return configuration.storyReporter(reporterStoryPath.get());
 363  
         }
 364  
     }
 365  
 
 366  
     private void handleStoryFailureByStrategy() throws Throwable {
 367  25
         Throwable throwable = storyFailure.get();
 368  25
         if (throwable != null) {
 369  12
             currentStrategy.get().handleFailure(throwable);
 370  
         }
 371  22
     }
 372  
 
 373  
     private void resetStoryFailure(RunContext context) {
 374  31
         if (context.givenStory()) {
 375  
             // do not reset failure for given stories
 376  3
             return;
 377  
         }
 378  28
         currentStrategy.set(context.configuration().failureStrategy());
 379  28
         storyFailure.set(null);
 380  28
     }
 381  
 
 382  
     private void runGivenStories(GivenStories givenStories, Map<String, String> parameters, RunContext context) throws Throwable {
 383  49
         if (givenStories.getPaths().size() > 0) {
 384  3
             reporter.get().givenStories(givenStories);
 385  3
             for (GivenStory givenStory : givenStories.getStories()) {
 386  3
                 RunContext childContext = context.childContextFor(givenStory);
 387  
                 // run given story, using any parameters provided
 388  3
                 Story story = storyOfPath(context.configuration(), childContext.path());
 389  3
                 if ( givenStory.hasAnchorParameters() ){
 390  0
                     story = storyWithMatchingScenarios(story, givenStory.getAnchorParameters());
 391  
                 }
 392  3
                 parameters.putAll(givenStory.getParameters());
 393  3
                 run(childContext, story, parameters);
 394  3
             }
 395  
         }
 396  49
     }
 397  
 
 398  
     private Story storyWithMatchingScenarios(Story story, Map<String,String> parameters) {
 399  0
         if ( parameters.isEmpty() ) return story;
 400  0
         List<Scenario> scenarios = new ArrayList<Scenario>();
 401  0
         for ( Scenario scenario : story.getScenarios() ){
 402  0
             if ( matchesParameters(scenario, parameters) ){
 403  0
                 scenarios.add(scenario);
 404  
             }
 405  0
         }
 406  0
         return new Story(story.getPath(), story.getDescription(), story.getMeta(), story.getNarrative(), scenarios); 
 407  
     }
 408  
 
 409  
     private boolean matchesParameters(Scenario scenario, Map<String, String> parameters) {
 410  0
         Meta meta = scenario.getMeta();
 411  0
         for ( String name : parameters.keySet() ){
 412  0
             if ( meta.hasProperty(name) ){
 413  0
                 return meta.getProperty(name).equals(parameters.get(name));
 414  
             }
 415  0
         }
 416  0
         return false;
 417  
     }
 418  
 
 419  
     private boolean isParameterisedByExamples(Scenario scenario) {
 420  26
         return scenario.getExamplesTable().getRowCount() > 0 && !scenario.getGivenStories().requireParameters();
 421  
     }
 422  
 
 423  
     private void runScenariosParametrisedByExamples(RunContext context, Scenario scenario, Meta storyAndScenarioMeta)
 424  
             throws Throwable {
 425  1
         ExamplesTable table = scenario.getExamplesTable();
 426  1
         reporter.get().beforeExamples(scenario.getSteps(), table);
 427  1
             Keywords keywords = context.configuration().keywords();
 428  1
         for (Map<String, String> scenarioParameters : table.getRows()) {
 429  1
                         Meta parameterMeta = parameterMeta(keywords, scenarioParameters);
 430  1
                         if ( !parameterMeta.isEmpty() && !context.filter.allow(parameterMeta) ){
 431  0
                                 continue;
 432  
                         }
 433  1
             reporter.get().example(scenarioParameters);
 434  1
             if (context.configuration().storyControls().resetStateBeforeScenario()) {
 435  1
                 context.resetState();
 436  
             }
 437  1
             runBeforeOrAfterScenarioSteps(context, scenario, storyAndScenarioMeta, Stage.BEFORE, ScenarioType.EXAMPLE);
 438  1
             addMetaParameters(scenarioParameters, storyAndScenarioMeta);
 439  1
             runGivenStories(scenario.getGivenStories(), scenarioParameters, context);
 440  1
             runScenarioSteps(context, scenario, scenarioParameters);
 441  1
             runBeforeOrAfterScenarioSteps(context, scenario, storyAndScenarioMeta, Stage.AFTER, ScenarioType.EXAMPLE);
 442  1
         }
 443  1
         reporter.get().afterExamples();
 444  1
     }
 445  
 
 446  
         private Meta parameterMeta(Keywords keywords,
 447  
                         Map<String, String> scenarioParameters) {
 448  1
                 String meta = keywords.meta();
 449  1
                 if (scenarioParameters.containsKey(meta)) {
 450  0
                         return Meta.createMeta(scenarioParameters.get(meta), keywords);
 451  
                 }
 452  1
                 return Meta.EMPTY;
 453  
         }
 454  
 
 455  
         private void runBeforeOrAfterStorySteps(RunContext context, Story story, Stage stage) throws InterruptedException {
 456  45
         runStepsWhileKeepingState(context, context.collectBeforeOrAfterStorySteps(story, stage));
 457  45
     }
 458  
 
 459  
     private void runBeforeOrAfterScenarioSteps(RunContext context, Scenario scenario, Meta storyAndScenarioMeta,
 460  
             Stage stage, ScenarioType type) throws InterruptedException {
 461  51
         runStepsWhileKeepingState(context, context.collectBeforeOrAfterScenarioSteps(storyAndScenarioMeta, stage, type));
 462  51
     }
 463  
 
 464  
     private void runLifecycleSteps(RunContext context, Lifecycle lifecycle, Stage stage, Meta storyAndScenarioMeta) throws InterruptedException {
 465  51
         runStepsWhileKeepingState(context, context.collectLifecycleSteps(lifecycle, storyAndScenarioMeta, stage));        
 466  51
     }
 467  
 
 468  
     private void runScenarioSteps(RunContext context, Scenario scenario, Map<String, String> scenarioParameters)
 469  
             throws InterruptedException {
 470  26
         boolean restart = true;
 471  52
         while (restart) {
 472  27
             restart = false;
 473  27
             List<Step> steps = context.collectScenarioSteps(scenario, scenarioParameters);
 474  
             try {
 475  27
                 runStepsWhileKeepingState(context, steps);
 476  1
             } catch (RestartingScenarioFailure e) {
 477  1
                 restart = true;
 478  1
                 continue;
 479  25
             }
 480  25
             generatePendingStepMethods(context, steps);
 481  25
         }
 482  25
     }
 483  
 
 484  
     private void generatePendingStepMethods(RunContext context, List<Step> steps) {
 485  25
         List<PendingStep> pendingSteps = new ArrayList<PendingStep>();
 486  25
         for (Step step : steps) {
 487  35
             if (step instanceof PendingStep) {
 488  0
                 pendingSteps.add((PendingStep) step);
 489  
             }
 490  35
         }
 491  25
         if (!pendingSteps.isEmpty()) {
 492  0
             PendingStepMethodGenerator generator = new PendingStepMethodGenerator(context.configuration().keywords());
 493  0
             List<String> methods = new ArrayList<String>();
 494  0
             for (PendingStep pendingStep : pendingSteps) {
 495  0
                 if (!pendingStep.annotated()) {
 496  0
                     methods.add(generator.generateMethod(pendingStep));
 497  
                 }
 498  0
             }
 499  0
             reporter.get().pendingMethods(methods);
 500  
         }
 501  25
     }
 502  
 
 503  
     private void runStepsWhileKeepingState(RunContext context, List<Step> steps) throws InterruptedException {
 504  183
         if (steps == null || steps.size() == 0) {
 505  148
             return;
 506  
         }
 507  35
         State state = context.state();
 508  35
         for (Step step : steps) {
 509  
             try {
 510  48
                 context.interruptIfCancelled();
 511  47
                 state = state.run(step);
 512  1
             } catch (RestartingScenarioFailure e) {
 513  1
                 reporter.get().restarted(step.toString(), e);
 514  1
                 throw e;
 515  46
             }
 516  46
         }
 517  33
         context.stateIs(state);
 518  33
     }
 519  
 
 520  
     public interface State {
 521  
         State run(Step step);
 522  
     }
 523  
 
 524  166
     private final class FineSoFar implements State {
 525  
 
 526  
         public State run(Step step) {
 527  40
             if ( step instanceof ParameterisedStep ){
 528  2
                 ((ParameterisedStep)step).describeTo(reporter.get());
 529  
             }
 530  40
             UUIDExceptionWrapper storyFailureIfItHappened = storyFailure.get(); 
 531  40
             StepResult result = step.perform(storyFailureIfItHappened);
 532  39
             result.describeTo(reporter.get());
 533  39
             UUIDExceptionWrapper stepFailure = result.getFailure();
 534  39
             if (stepFailure == null) {
 535  23
                 return this;
 536  
             }
 537  
 
 538  16
             storyFailure.set(mostImportantOf(storyFailureIfItHappened, stepFailure));
 539  16
             currentStrategy.set(strategyFor(storyFailure.get()));
 540  16
             return new SomethingHappened(stepFailure);
 541  
         }
 542  
 
 543  
         private UUIDExceptionWrapper mostImportantOf(UUIDExceptionWrapper failure1, UUIDExceptionWrapper failure2) {
 544  16
             return failure1 == null ? failure2
 545  
                     : failure1.getCause() instanceof PendingStepFound ? (failure2 == null ? failure1 : failure2)
 546  
                             : failure1;
 547  
         }
 548  
 
 549  
         private FailureStrategy strategyFor(Throwable failure) {
 550  16
             if (failure instanceof PendingStepFound) {
 551  6
                 return pendingStepStrategy.get();
 552  
             } else {
 553  10
                 return failureStrategy.get();
 554  
             }
 555  
         }
 556  
     }
 557  
 
 558  
     private final class SomethingHappened implements State {
 559  
         UUIDExceptionWrapper scenarioFailure;
 560  
 
 561  17
         public SomethingHappened(UUIDExceptionWrapper scenarioFailure) {
 562  17
             this.scenarioFailure = scenarioFailure;
 563  17
         }
 564  
 
 565  
         public State run(Step step) {
 566  7
             StepResult result = step.doNotPerform(scenarioFailure);
 567  7
             result.describeTo(reporter.get());
 568  7
             return this;
 569  
         }
 570  
     }
 571  
 
 572  
     @Override
 573  
     public String toString() {
 574  1
         return this.getClass().getSimpleName();
 575  
     }
 576  
 
 577  
     /**
 578  
      * The context for running a story.
 579  
      */
 580  67
     private class RunContext {
 581  
         private final Configuration configuration;
 582  
         private final List<CandidateSteps> candidateSteps;
 583  
         private final String path;
 584  
         private final MetaFilter filter;
 585  
         private final boolean givenStory;
 586  
                 private State state;
 587  
                 private RunContext parentContext;
 588  
 
 589  
         public RunContext(Configuration configuration, InjectableStepsFactory stepsFactory, String path,
 590  
                 MetaFilter filter) {
 591  23
             this(configuration, stepsFactory.createCandidateSteps(), path, filter);
 592  23
         }
 593  
 
 594  
         public RunContext(Configuration configuration, List<CandidateSteps> steps, String path, MetaFilter filter) {
 595  32
             this(configuration, steps, path, filter, false, null);
 596  32
         }
 597  
 
 598  
         private RunContext(Configuration configuration, List<CandidateSteps> steps, String path, MetaFilter filter,
 599  35
                 boolean givenStory, RunContext parentContext) {
 600  35
             this.configuration = configuration;
 601  35
             this.candidateSteps = steps;
 602  35
             this.path = path;
 603  35
             this.filter = filter;
 604  35
             this.givenStory = givenStory;
 605  35
                         this.parentContext = parentContext;
 606  35
             resetState();
 607  35
         }
 608  
 
 609  
         public void interruptIfCancelled() throws InterruptedException {
 610  48
             for (Story story : cancelledStories.keySet()) {
 611  1
                 if (path.equals(story.getPath())) {
 612  1
                     throw new InterruptedException(path);
 613  
                 }
 614  0
             }
 615  47
         }
 616  
 
 617  
         public boolean dryRun() {
 618  26
             return configuration.storyControls().dryRun();
 619  
         }
 620  
 
 621  
         public Configuration configuration() {
 622  188
             return configuration;
 623  
         }
 624  
 
 625  
         public List<CandidateSteps> candidateSteps() {
 626  9
             return candidateSteps;
 627  
         }
 628  
 
 629  
         public boolean givenStory() {
 630  182
             return givenStory;
 631  
         }
 632  
 
 633  
         public String path() {
 634  3
             return path;
 635  
         }
 636  
 
 637  
         public FilteredStory filter(Story story) {
 638  26
             return new FilteredStory(filter, story, configuration.storyControls());
 639  
         }
 640  
 
 641  
         public String metaFilterAsString() {
 642  4
             return filter.asString();
 643  
         }
 644  
         
 645  
         public List<Step> collectBeforeOrAfterStorySteps(Story story, Stage stage) {
 646  45
             return configuration.stepCollector().collectBeforeOrAfterStorySteps(candidateSteps, story, stage,
 647  
                     givenStory);
 648  
         }
 649  
 
 650  
         public List<Step> collectBeforeOrAfterScenarioSteps(Meta storyAndScenarioMeta, Stage stage, ScenarioType type) {
 651  51
             return configuration.stepCollector().collectBeforeOrAfterScenarioSteps(candidateSteps,
 652  
                     storyAndScenarioMeta, stage, type);
 653  
         }
 654  
 
 655  
         public List<Step> collectLifecycleSteps(Lifecycle lifecycle, Meta storyAndScenarioMeta, Stage stage) {
 656  51
             return configuration.stepCollector().collectLifecycleSteps(candidateSteps, lifecycle, storyAndScenarioMeta, stage);
 657  
         }
 658  
 
 659  
         public List<Step> collectScenarioSteps(Scenario scenario, Map<String, String> parameters) {
 660  27
             return configuration.stepCollector().collectScenarioSteps(candidateSteps, scenario, parameters);
 661  
         }
 662  
 
 663  
         public RunContext childContextFor(GivenStory givenStory) {
 664  3
             String actualPath = configuration.pathCalculator().calculate(path, givenStory.getPath());
 665  3
             return new RunContext(configuration, candidateSteps, actualPath, filter, true, this);
 666  
         }
 667  
 
 668  
         public State state() {
 669  52
             return state;
 670  
         }
 671  
 
 672  
         public void stateIs(State state) {
 673  44
             this.state = state;
 674  44
             if ( parentContext != null ){
 675  3
                     parentContext.stateIs(state);
 676  
             }
 677  44
         }
 678  
 
 679  
         public boolean failureOccurred() {
 680  29
             return failed(state);
 681  
         }
 682  
 
 683  
         public void resetState() {
 684  83
             this.state = new FineSoFar();
 685  83
         }
 686  
 
 687  
     }
 688  
 
 689  
     public boolean failed(State state) {
 690  33
         return !state.getClass().equals(FineSoFar.class);
 691  
     }
 692  
 
 693  
     public Throwable failure(State state) {
 694  0
         if (failed(state)) {
 695  0
             return ((SomethingHappened) state).scenarioFailure.getCause();
 696  
         }
 697  0
         return null;
 698  
     }
 699  
 }