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  580
         this(new StepFinder());
 29  580
     }
 30  
 
 31  
     public MarkUnmatchedStepsAsPending(StepFinder stepFinder) {
 32  581
         this(stepFinder, new LocalizedKeywords());
 33  581
     }
 34  
 
 35  
     public MarkUnmatchedStepsAsPending(Keywords keywords) {
 36  0
         this(new StepFinder(), keywords);
 37  0
     }
 38  
 
 39  581
    public MarkUnmatchedStepsAsPending(StepFinder stepFinder, Keywords keywords) {
 40  581
         this.stepFinder = stepFinder;
 41  581
         this.keywords = keywords;
 42  581
     }
 43  
 
 44  
      public List<Step> collectBeforeOrAfterStoriesSteps(List<CandidateSteps> candidateSteps, Stage stage) {
 45  6
         List<Step> steps = new ArrayList<Step>();
 46  6
         for (CandidateSteps candidates : candidateSteps) {
 47  8
             steps.addAll(createSteps(candidates.listBeforeOrAfterStories(), stage));
 48  8
         }
 49  6
         return steps;
 50  
     }
 51  
 
 52  
     public List<Step> collectBeforeOrAfterStorySteps(List<CandidateSteps> candidateSteps, Story story, Stage stage,
 53  
             boolean givenStory) {
 54  11
         List<Step> steps = new ArrayList<Step>();
 55  11
         for (CandidateSteps candidates : candidateSteps) {
 56  13
             steps.addAll(createSteps(candidates.listBeforeOrAfterStory(givenStory), story.getMeta(), stage));
 57  13
         }
 58  11
         return steps;
 59  
     }
 60  
 
 61  
     public List<Step> collectBeforeOrAfterScenarioSteps(List<CandidateSteps> candidateSteps, Meta storyAndScenarioMeta, Stage stage, ScenarioType type) {
 62  15
         List<Step> steps = new ArrayList<Step>();
 63  15
         for (CandidateSteps candidates : candidateSteps) {
 64  21
             List<BeforeOrAfterStep> beforeOrAfterScenarioSteps = candidates.listBeforeOrAfterScenario(type);
 65  21
             if (stage == Stage.BEFORE) {
 66  11
                 steps.addAll(createSteps(beforeOrAfterScenarioSteps, storyAndScenarioMeta, stage));
 67  
             } else {
 68  10
                 steps.addAll(0, createStepsUponOutcome(beforeOrAfterScenarioSteps, storyAndScenarioMeta, stage));
 69  
             }
 70  21
         }
 71  15
         return steps;
 72  
     }
 73  
 
 74  
     public List<Step> collectLifecycleSteps(List<CandidateSteps> candidateSteps, Lifecycle lifecycle, Meta storyAndScenarioMeta, Stage stage) {
 75  5
         List<Step> steps = new ArrayList<Step>();
 76  5
         Map<String, String> namedParameters = new HashMap<String, String>();
 77  5
         if (stage == Stage.BEFORE) {
 78  3
             addMatchedSteps(lifecycle.getBeforeSteps(), steps, namedParameters, candidateSteps);
 79  
         } else {
 80  2
             addMatchedSteps(lifecycle.getAfterSteps(), steps, namedParameters, candidateSteps);
 81  
         }
 82  5
         return steps;
 83  
     }
 84  
 
 85  
     public List<Step> collectScenarioSteps(List<CandidateSteps> candidateSteps, Scenario scenario,
 86  
             Map<String, String> parameters) {
 87  11
         List<Step> steps = new ArrayList<Step>();
 88  11
         addMatchedSteps(scenario.getSteps(), steps, parameters, candidateSteps);
 89  11
         return steps;
 90  
     }
 91  
 
 92  
     private List<Step> createSteps(List<BeforeOrAfterStep> beforeOrAfter, Stage stage) {
 93  8
         return createSteps(beforeOrAfter, null, stage);
 94  
     }
 95  
 
 96  
     private List<Step> createSteps(List<BeforeOrAfterStep> beforeOrAfter, Meta meta, Stage stage) {
 97  32
         List<Step> steps = new ArrayList<Step>();
 98  32
         for (BeforeOrAfterStep step : beforeOrAfter) {
 99  40
             if (stage == step.getStage()) {
 100  20
                 steps.add(meta == null ? step.createStep() : step.createStepWith(meta));
 101  
             }
 102  40
         }
 103  32
         return steps;
 104  
     }
 105  
 
 106  
     private List<Step> createStepsUponOutcome(List<BeforeOrAfterStep> beforeOrAfter, Meta storyAndScenarioMeta, Stage stage) {
 107  10
         List<Step> steps = new ArrayList<Step>();
 108  10
         for (BeforeOrAfterStep step : beforeOrAfter) {
 109  16
             if (stage == step.getStage()) {
 110  8
                 steps.add(step.createStepUponOutcome(storyAndScenarioMeta));
 111  
             }
 112  16
         }
 113  10
         return steps;
 114  
     }
 115  
 
 116  
     private void addMatchedSteps(List<String> stepsAsString, List<Step> steps, Map<String, String> namedParameters,
 117  
             List<CandidateSteps> candidateSteps) {
 118  16
         List<StepCandidate> allCandidates = stepFinder.collectCandidates(candidateSteps);
 119  16
         String previousNonAndStep = null;
 120  16
         for (String stepAsString : stepsAsString) {
 121  
             // pending is default step, overridden below
 122  18
             Step step = StepCreator.createPendingStep(stepAsString, previousNonAndStep);
 123  18
             List<Step> composedSteps = new ArrayList<Step>();
 124  18
             List<StepCandidate> prioritisedCandidates = stepFinder.prioritise(stepAsString, allCandidates);
 125  18
             for (StepCandidate candidate : prioritisedCandidates) {
 126  27
                 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  26
                 if (matchesCandidate(stepAsString, previousNonAndStep, candidate)) {
 133  
                     // step matches candidate
 134  8
                     if (candidate.isPending()) {
 135  0
                         ((PendingStep) step).annotatedOn(candidate.getMethod());
 136  
                     } else {
 137  8
                         step = candidate.createMatchedStep(stepAsString, namedParameters);
 138  8
                         if ( candidate.isComposite() ){
 139  1
                             candidate.addComposedSteps(composedSteps, stepAsString, namedParameters, allCandidates);
 140  
                         }
 141  
                     }
 142  8
                     if (!(candidate.isAndStep(stepAsString) || candidate.isIgnorableStep(stepAsString))) {
 143  
                         // only update previous step if not AND or IGNORABLE step
 144  8
                         previousNonAndStep = stepAsString;
 145  
                     }
 146  
                     break;
 147  
                 }
 148  18
             }
 149  18
             if ( !(keywords.isAndStep(stepAsString) || keywords.isIgnorableStep(stepAsString)) ){
 150  14
                 previousNonAndStep = stepAsString;
 151  
             }
 152  18
             steps.add(step);
 153  18
             steps.addAll(composedSteps);
 154  18
         }
 155  16
     }
 156  
 
 157  
     private boolean matchesCandidate(String step, String previousNonAndStep, StepCandidate candidate) {
 158  26
         if (previousNonAndStep != null) {
 159  15
             return candidate.matches(step, previousNonAndStep);
 160  
         }
 161  11
         return candidate.matches(step);
 162  
     }
 163  
 
 164  
 }