Coverage Report - org.jbehave.core.steps.MarkUnmatchedStepsAsPending
 
Classes in this File Line Coverage Branch Coverage Complexity
MarkUnmatchedStepsAsPending
96%
87/90
90%
40/44
2.278
 
 1  
 package org.jbehave.core.steps;
 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.AfterScenario.Outcome;
 9  
 import org.jbehave.core.annotations.ScenarioType;
 10  
 import org.jbehave.core.configuration.Keywords;
 11  
 import org.jbehave.core.i18n.LocalizedKeywords;
 12  
 import org.jbehave.core.model.Lifecycle;
 13  
 import org.jbehave.core.model.Meta;
 14  
 import org.jbehave.core.model.Scenario;
 15  
 import org.jbehave.core.model.Story;
 16  
 import org.jbehave.core.steps.AbstractStepResult.Pending;
 17  
 import org.jbehave.core.steps.StepCreator.PendingStep;
 18  
 
 19  
 /**
 20  
  * StepCollector that marks unmatched steps as {@link Pending}. It uses a
 21  
  * {@link StepFinder} to collect and prioritise {@link StepCandidate}s.
 22  
  */
 23  
 public class MarkUnmatchedStepsAsPending implements StepCollector {
 24  
 
 25  
     private final StepFinder stepFinder;
 26  
     private final Keywords keywords;
 27  
 
 28  
     public MarkUnmatchedStepsAsPending() {
 29  322
         this(new StepFinder());
 30  322
     }
 31  
 
 32  
     public MarkUnmatchedStepsAsPending(StepFinder stepFinder) {
 33  323
         this(stepFinder, new LocalizedKeywords());
 34  323
     }
 35  
 
 36  
     public MarkUnmatchedStepsAsPending(Keywords keywords) {
 37  0
         this(new StepFinder(), keywords);
 38  0
     }
 39  
 
 40  323
    public MarkUnmatchedStepsAsPending(StepFinder stepFinder, Keywords keywords) {
 41  323
         this.stepFinder = stepFinder;
 42  323
         this.keywords = keywords;
 43  323
     }
 44  
 
 45  
      public List<Step> collectBeforeOrAfterStoriesSteps(List<CandidateSteps> candidateSteps, Stage stage) {
 46  34
         List<Step> steps = new ArrayList<Step>();
 47  34
         for (CandidateSteps candidates : candidateSteps) {
 48  36
             steps.addAll(createSteps(candidates.listBeforeOrAfterStories(), stage));
 49  36
         }
 50  34
         return steps;
 51  
     }
 52  
 
 53  
     public List<Step> collectBeforeOrAfterStorySteps(List<CandidateSteps> candidateSteps, Story story, Stage stage,
 54  
             boolean givenStory) {
 55  98
         List<Step> steps = new ArrayList<Step>();
 56  98
         for (CandidateSteps candidates : candidateSteps) {
 57  100
             steps.addAll(createSteps(candidates.listBeforeOrAfterStory(givenStory), story.getMeta(), stage));
 58  100
         }
 59  98
         return steps;
 60  
     }
 61  
 
 62  
     public List<Step> collectBeforeOrAfterScenarioSteps(List<CandidateSteps> candidateSteps, Meta storyAndScenarioMeta, Stage stage, ScenarioType type) {
 63  196
         List<Step> steps = new ArrayList<Step>();
 64  196
         for (CandidateSteps candidates : candidateSteps) {
 65  204
             List<BeforeOrAfterStep> beforeOrAfterScenarioSteps = candidates.listBeforeOrAfterScenario(type);
 66  204
             if (stage == Stage.BEFORE) {
 67  102
                 steps.addAll(createSteps(beforeOrAfterScenarioSteps, storyAndScenarioMeta, stage));
 68  
             } else {
 69  102
                 steps.addAll(0, createStepsUponOutcome(beforeOrAfterScenarioSteps, storyAndScenarioMeta, stage));
 70  
             }
 71  204
         }
 72  196
         return steps;
 73  
     }
 74  
 
 75  
     public List<Step> collectLifecycleSteps(List<CandidateSteps> candidateSteps, Lifecycle lifecycle, Meta storyAndScenarioMeta, Stage stage) {
 76  93
         List<Step> steps = new ArrayList<Step>();
 77  93
         Map<String, String> namedParameters = new HashMap<String, String>();
 78  93
         if (stage == Stage.BEFORE) {
 79  46
             addMatchedSteps(lifecycle.getBeforeSteps(), steps, namedParameters, candidateSteps);
 80  
         } else {
 81  47
             addMatchedSteps(lifecycle.getAfterSteps(Outcome.ANY), steps, namedParameters, candidateSteps, Outcome.ANY);
 82  47
             addMatchedSteps(lifecycle.getAfterSteps(Outcome.SUCCESS), steps, namedParameters, candidateSteps, Outcome.SUCCESS);
 83  47
             addMatchedSteps(lifecycle.getAfterSteps(Outcome.FAILURE), steps, namedParameters, candidateSteps, Outcome.FAILURE);
 84  
         }
 85  93
         return steps;
 86  
     }
 87  
 
 88  
     public List<Step> collectScenarioSteps(List<CandidateSteps> candidateSteps, Scenario scenario,
 89  
             Map<String, String> parameters) {
 90  8
         return collectScenarioSteps(candidateSteps, scenario, parameters, new NullStepMonitor());
 91  
     }
 92  
 
 93  
     public List<Step> collectScenarioSteps(List<CandidateSteps> candidateSteps, Scenario scenario,
 94  
             Map<String, String> parameters, StepMonitor stepMonitor) {
 95  54
         List<Step> steps = new ArrayList<Step>();
 96  54
         addMatchedSteps(scenario.getSteps(), steps, parameters, candidateSteps, stepMonitor);
 97  54
         return steps;
 98  
     }
 99  
 
 100  
     private List<Step> createSteps(List<BeforeOrAfterStep> beforeOrAfter, Stage stage) {
 101  36
         return createSteps(beforeOrAfter, null, stage);
 102  
     }
 103  
 
 104  
     private List<Step> createSteps(List<BeforeOrAfterStep> beforeOrAfter, Meta meta, Stage stage) {
 105  238
         List<Step> steps = new ArrayList<Step>();
 106  238
         for (BeforeOrAfterStep step : beforeOrAfter) {
 107  44
             if (stage == step.getStage()) {
 108  22
                 steps.add(meta == null ? step.createStep() : step.createStepWith(meta));
 109  
             }
 110  44
         }
 111  238
         return steps;
 112  
     }
 113  
 
 114  
     private List<Step> createStepsUponOutcome(List<BeforeOrAfterStep> beforeOrAfter, Meta storyAndScenarioMeta, Stage stage) {
 115  102
         List<Step> steps = new ArrayList<Step>();
 116  102
         for (BeforeOrAfterStep step : beforeOrAfter) {
 117  20
             if (stage == step.getStage()) {
 118  10
                 steps.add(step.createStepUponOutcome(storyAndScenarioMeta));
 119  
             }
 120  20
         }
 121  102
         return steps;
 122  
     }
 123  
 
 124  
     private void addMatchedSteps(List<String> stepsAsString, List<Step> steps, Map<String, String> namedParameters,
 125  
             List<CandidateSteps> candidateSteps) {
 126  46
         addMatchedSteps(stepsAsString, steps, namedParameters, candidateSteps, (Outcome)null);
 127  46
     }
 128  
 
 129  
     private void addMatchedSteps(List<String> stepsAsString, List<Step> steps, Map<String, String> namedParameters,
 130  
             List<CandidateSteps> candidateSteps, Outcome outcome) {
 131  187
         addMatchedSteps(stepsAsString, steps, namedParameters, candidateSteps, outcome, new NullStepMonitor());
 132  187
     }
 133  
 
 134  
     private void addMatchedSteps(List<String> stepsAsString, List<Step> steps, Map<String, String> namedParameters,
 135  
             List<CandidateSteps> candidateSteps, StepMonitor stepMonitor) {
 136  54
         addMatchedSteps(stepsAsString, steps, namedParameters, candidateSteps, null, stepMonitor);
 137  54
     }
 138  
 
 139  
     private void addMatchedSteps(List<String> stepsAsString, List<Step> steps, Map<String, String> namedParameters,
 140  
             List<CandidateSteps> candidateSteps, Outcome outcome, StepMonitor stepMonitor) {
 141  241
         List<StepCandidate> allCandidates = stepFinder.collectCandidates(candidateSteps);
 142  241
         String previousNonAndStep = null;
 143  241
         for (String stepAsString : stepsAsString) {
 144  
             // pending is default step, overridden below
 145  75
             Step step = StepCreator.createPendingStep(stepAsString, previousNonAndStep);
 146  75
             List<Step> composedSteps = new ArrayList<Step>();
 147  75
             List<StepCandidate> prioritisedCandidates = stepFinder.prioritise(stepAsString, allCandidates);
 148  75
             for (StepCandidate candidate : prioritisedCandidates) {
 149  86
                 candidate.useStepMonitor(stepMonitor);
 150  86
                 if (candidate.ignore(stepAsString)) {
 151  
                     // ignorable steps are added
 152  
                     // so they can be reported
 153  1
                     step = StepCreator.createIgnorableStep(stepAsString);
 154  1
                     break;
 155  
                 }
 156  85
                 if (matchesCandidate(stepAsString, previousNonAndStep, candidate)) {
 157  
                     // step matches candidate
 158  65
                     if (candidate.isPending()) {
 159  0
                         ((PendingStep) step).annotatedOn(candidate.getMethod());
 160  
                     } else {
 161  65
                             if ( outcome != null ){
 162  3
                                     step = candidate.createMatchedStepUponOutcome(stepAsString, namedParameters, outcome);
 163  
                             } else {
 164  62
                                     step = candidate.createMatchedStep(stepAsString, namedParameters);
 165  
                             }
 166  65
                         if ( candidate.isComposite() ){
 167  1
                             candidate.addComposedSteps(composedSteps, stepAsString, namedParameters, allCandidates);
 168  
                         }
 169  
                     }
 170  65
                     if (!(candidate.isAndStep(stepAsString) || candidate.isIgnorableStep(stepAsString))) {
 171  
                         // only update previous step if not AND or IGNORABLE step
 172  65
                         previousNonAndStep = stepAsString;
 173  
                     }
 174  
                     break;
 175  
                 }
 176  20
             }
 177  75
             if ( !(keywords.isAndStep(stepAsString) || keywords.isIgnorableStep(stepAsString)) ){
 178  71
                 previousNonAndStep = stepAsString;
 179  
             }
 180  75
             steps.add(step);
 181  75
             steps.addAll(composedSteps);
 182  75
         }
 183  241
     }
 184  
 
 185  
     private boolean matchesCandidate(String step, String previousNonAndStep, StepCandidate candidate) {
 186  85
         if (previousNonAndStep != null) {
 187  35
             return candidate.matches(step, previousNonAndStep);
 188  
         }
 189  50
         return candidate.matches(step);
 190  
     }
 191  
 
 192  
 }