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