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