| 1 | |
package org.jbehave.core.steps; |
| 2 | |
|
| 3 | |
import java.util.ArrayList; |
| 4 | |
import java.util.List; |
| 5 | |
import java.util.Map; |
| 6 | |
|
| 7 | |
import org.jbehave.core.configuration.Keywords; |
| 8 | |
import org.jbehave.core.i18n.LocalizedKeywords; |
| 9 | |
import org.jbehave.core.model.Scenario; |
| 10 | |
import org.jbehave.core.model.Story; |
| 11 | |
import org.jbehave.core.steps.AbstractStepResult.Pending; |
| 12 | |
import org.jbehave.core.steps.StepCreator.PendingStep; |
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
public class MarkUnmatchedStepsAsPending implements StepCollector { |
| 19 | |
|
| 20 | |
private final StepFinder stepFinder; |
| 21 | |
private final Keywords keywords; |
| 22 | |
|
| 23 | |
public MarkUnmatchedStepsAsPending() { |
| 24 | 461 | this(new StepFinder()); |
| 25 | 461 | } |
| 26 | |
|
| 27 | |
public MarkUnmatchedStepsAsPending(StepFinder stepFinder) { |
| 28 | 462 | this(stepFinder, new LocalizedKeywords()); |
| 29 | 462 | } |
| 30 | |
|
| 31 | 462 | public MarkUnmatchedStepsAsPending(StepFinder stepFinder, Keywords keywords) { |
| 32 | 462 | this.stepFinder = stepFinder; |
| 33 | 462 | this.keywords = keywords; |
| 34 | 462 | } |
| 35 | |
|
| 36 | |
public List<Step> collectBeforeOrAfterStoriesSteps(List<CandidateSteps> candidateSteps, Stage stage) { |
| 37 | 2 | List<Step> steps = new ArrayList<Step>(); |
| 38 | 2 | for (CandidateSteps candidates : candidateSteps) { |
| 39 | 4 | steps.addAll(createSteps(candidates.listBeforeOrAfterStories(), stage)); |
| 40 | |
} |
| 41 | 2 | return steps; |
| 42 | |
} |
| 43 | |
|
| 44 | |
public List<Step> collectBeforeOrAfterStorySteps(List<CandidateSteps> candidateSteps, Story story, Stage stage, |
| 45 | |
boolean givenStory) { |
| 46 | 2 | List<Step> steps = new ArrayList<Step>(); |
| 47 | 2 | for (CandidateSteps candidates : candidateSteps) { |
| 48 | 4 | steps.addAll(createSteps(candidates.listBeforeOrAfterStory(givenStory), stage)); |
| 49 | |
} |
| 50 | 2 | return steps; |
| 51 | |
} |
| 52 | |
|
| 53 | |
public List<Step> collectBeforeOrAfterScenarioSteps(List<CandidateSteps> candidateSteps, Stage stage, |
| 54 | |
boolean failureOccured) { |
| 55 | 4 | List<Step> steps = new ArrayList<Step>(); |
| 56 | 4 | for (CandidateSteps candidates : candidateSteps) { |
| 57 | 8 | List<BeforeOrAfterStep> beforeOrAfterScenarioSteps = candidates.listBeforeOrAfterScenario(); |
| 58 | 8 | if (stage == Stage.BEFORE) { |
| 59 | 4 | steps.addAll(createSteps(beforeOrAfterScenarioSteps, stage)); |
| 60 | |
} else { |
| 61 | 4 | steps.addAll(0, createStepsUponOutcome(beforeOrAfterScenarioSteps, stage, failureOccured)); |
| 62 | |
} |
| 63 | 8 | } |
| 64 | 4 | return steps; |
| 65 | |
} |
| 66 | |
|
| 67 | |
private List<Step> createSteps(List<BeforeOrAfterStep> beforeOrAfter, Stage stage) { |
| 68 | 12 | List<Step> steps = new ArrayList<Step>(); |
| 69 | 12 | for (BeforeOrAfterStep step : beforeOrAfter) { |
| 70 | 24 | if (stage == step.getStage()) { |
| 71 | 12 | steps.add(step.createStep()); |
| 72 | |
} |
| 73 | |
} |
| 74 | 12 | return steps; |
| 75 | |
} |
| 76 | |
|
| 77 | |
public List<Step> collectScenarioSteps(List<CandidateSteps> candidateSteps, Scenario scenario, |
| 78 | |
Map<String, String> parameters) { |
| 79 | 8 | List<Step> steps = new ArrayList<Step>(); |
| 80 | 8 | addMatchedScenarioSteps(scenario, steps, parameters, candidateSteps); |
| 81 | 8 | return steps; |
| 82 | |
} |
| 83 | |
|
| 84 | |
private List<Step> createStepsUponOutcome(List<BeforeOrAfterStep> beforeOrAfter, Stage stage, boolean failureOccured) { |
| 85 | 4 | List<Step> steps = new ArrayList<Step>(); |
| 86 | 4 | for (BeforeOrAfterStep step : beforeOrAfter) { |
| 87 | 8 | if (stage == step.getStage()) { |
| 88 | 4 | steps.add(step.createStepUponOutcome(failureOccured)); |
| 89 | |
} |
| 90 | |
} |
| 91 | 4 | return steps; |
| 92 | |
} |
| 93 | |
|
| 94 | |
private void addMatchedScenarioSteps(Scenario scenario, List<Step> steps, Map<String, String> namedParameters, |
| 95 | |
List<CandidateSteps> candidateSteps) { |
| 96 | 8 | List<StepCandidate> allCandidates = stepFinder.collectCandidates(candidateSteps); |
| 97 | 8 | String previousNonAndStep = null; |
| 98 | 8 | for (String stepAsString : scenario.getSteps()) { |
| 99 | |
|
| 100 | 15 | Step step = StepCreator.createPendingStep(stepAsString, previousNonAndStep); |
| 101 | 15 | List<Step> composedSteps = new ArrayList<Step>(); |
| 102 | 15 | List<StepCandidate> prioritisedCandidates = stepFinder.prioritise(stepAsString, allCandidates); |
| 103 | 15 | for (StepCandidate candidate : prioritisedCandidates) { |
| 104 | 24 | if (candidate.ignore(stepAsString)) { |
| 105 | |
|
| 106 | |
|
| 107 | 1 | step = StepCreator.createIgnorableStep(stepAsString); |
| 108 | 1 | break; |
| 109 | |
} |
| 110 | 23 | if (matchesCandidate(stepAsString, previousNonAndStep, candidate)) { |
| 111 | |
|
| 112 | 5 | if (candidate.isPending()) { |
| 113 | 0 | ((PendingStep) step).annotatedOn(candidate.getMethod()); |
| 114 | |
} else { |
| 115 | 5 | step = candidate.createMatchedStep(stepAsString, namedParameters); |
| 116 | 5 | if (candidate.isComposite()) { |
| 117 | 1 | composedSteps = candidate.createComposedSteps(stepAsString, namedParameters, allCandidates); |
| 118 | |
} |
| 119 | |
} |
| 120 | 5 | if (!candidate.isAndStep(stepAsString)) { |
| 121 | |
|
| 122 | 5 | previousNonAndStep = stepAsString; |
| 123 | |
} |
| 124 | |
break; |
| 125 | |
} |
| 126 | |
} |
| 127 | 15 | if ( !keywords.isAndStep(stepAsString)){ |
| 128 | 11 | previousNonAndStep = stepAsString; |
| 129 | |
} |
| 130 | 15 | steps.add(step); |
| 131 | 15 | steps.addAll(composedSteps); |
| 132 | 15 | } |
| 133 | 8 | } |
| 134 | |
|
| 135 | |
private boolean matchesCandidate(String step, String previousNonAndStep, StepCandidate candidate) { |
| 136 | 23 | if (previousNonAndStep != null) { |
| 137 | 14 | return candidate.matches(step, previousNonAndStep); |
| 138 | |
} |
| 139 | 9 | return candidate.matches(step); |
| 140 | |
} |
| 141 | |
|
| 142 | |
} |