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