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