| 1 | |
package org.jbehave.core.steps; |
| 2 | |
|
| 3 | |
import java.lang.reflect.Method; |
| 4 | |
import java.util.List; |
| 5 | |
import java.util.Map; |
| 6 | |
|
| 7 | |
import org.apache.commons.lang.StringUtils; |
| 8 | |
import org.jbehave.core.annotations.Given; |
| 9 | |
import org.jbehave.core.annotations.Pending; |
| 10 | |
import org.jbehave.core.annotations.Then; |
| 11 | |
import org.jbehave.core.annotations.When; |
| 12 | |
import org.jbehave.core.configuration.Keywords; |
| 13 | |
import org.jbehave.core.configuration.Keywords.StartingWordNotFound; |
| 14 | |
import org.jbehave.core.parsers.StepMatcher; |
| 15 | |
import org.jbehave.core.parsers.StepPatternParser; |
| 16 | |
|
| 17 | |
import com.thoughtworks.paranamer.Paranamer; |
| 18 | |
|
| 19 | |
|
| 20 | |
|
| 21 | |
|
| 22 | |
|
| 23 | |
|
| 24 | |
|
| 25 | |
|
| 26 | |
public class StepCandidate { |
| 27 | |
|
| 28 | |
private final String patternAsString; |
| 29 | |
private final Integer priority; |
| 30 | |
private final StepType stepType; |
| 31 | |
private final Method method; |
| 32 | |
private final Class<?> stepsType; |
| 33 | |
private final InjectableStepsFactory stepsFactory; |
| 34 | |
private final Keywords keywords; |
| 35 | |
private final StepMatcher stepMatcher; |
| 36 | |
private final StepCreator stepCreator; |
| 37 | |
private String[] composedSteps; |
| 38 | 98 | private StepMonitor stepMonitor = new SilentStepMonitor(); |
| 39 | |
|
| 40 | |
public StepCandidate(String patternAsString, int priority, StepType stepType, Method method, Class<?> stepsType, |
| 41 | |
InjectableStepsFactory stepsFactory, Keywords keywords, StepPatternParser stepPatternParser, |
| 42 | 98 | ParameterConverters parameterConverters, ParameterControls parameterControls) { |
| 43 | 98 | this.patternAsString = patternAsString; |
| 44 | 98 | this.priority = priority; |
| 45 | 98 | this.stepType = stepType; |
| 46 | 98 | this.method = method; |
| 47 | 98 | this.stepsType = stepsType; |
| 48 | 98 | this.stepsFactory = stepsFactory; |
| 49 | 98 | this.keywords = keywords; |
| 50 | 98 | this.stepMatcher = stepPatternParser.parseStep(stepType, patternAsString); |
| 51 | 98 | this.stepCreator = new StepCreator(stepsType, stepsFactory, parameterConverters, parameterControls, |
| 52 | |
stepMatcher, stepMonitor); |
| 53 | 98 | } |
| 54 | |
|
| 55 | |
public Method getMethod() { |
| 56 | 7 | return method; |
| 57 | |
} |
| 58 | |
|
| 59 | |
public Integer getPriority() { |
| 60 | 1 | return priority; |
| 61 | |
} |
| 62 | |
|
| 63 | |
public String getPatternAsString() { |
| 64 | 84 | return patternAsString; |
| 65 | |
} |
| 66 | |
|
| 67 | |
public Object getStepsInstance() { |
| 68 | 7 | return stepsFactory.createInstanceOfType(stepsType); |
| 69 | |
} |
| 70 | |
|
| 71 | |
public StepType getStepType() { |
| 72 | 124 | return stepType; |
| 73 | |
} |
| 74 | |
|
| 75 | |
public String getStartingWord() { |
| 76 | 46 | return keywords.startingWordFor(stepType); |
| 77 | |
} |
| 78 | |
|
| 79 | |
public void useStepMonitor(StepMonitor stepMonitor) { |
| 80 | 67 | this.stepMonitor = stepMonitor; |
| 81 | 67 | this.stepCreator.useStepMonitor(stepMonitor); |
| 82 | 67 | } |
| 83 | |
|
| 84 | |
public void doDryRun(boolean dryRun) { |
| 85 | 67 | this.stepCreator.doDryRun(dryRun); |
| 86 | 67 | } |
| 87 | |
|
| 88 | |
public void useParanamer(Paranamer paranamer) { |
| 89 | 70 | this.stepCreator.useParanamer(paranamer); |
| 90 | 70 | } |
| 91 | |
|
| 92 | |
public void composedOf(String[] steps) { |
| 93 | 6 | this.composedSteps = steps; |
| 94 | 6 | } |
| 95 | |
|
| 96 | |
public boolean isComposite() { |
| 97 | 17 | return composedSteps != null && composedSteps.length > 0; |
| 98 | |
} |
| 99 | |
|
| 100 | |
public String[] composedSteps() { |
| 101 | 2 | return composedSteps; |
| 102 | |
} |
| 103 | |
|
| 104 | |
public boolean ignore(String stepAsString) { |
| 105 | |
try { |
| 106 | 4 | String ignoreWord = keywords.startingWordFor(StepType.IGNORABLE); |
| 107 | 3 | return keywords.stepStartsWithWord(stepAsString, ignoreWord); |
| 108 | 1 | } catch (StartingWordNotFound e) { |
| 109 | 1 | return false; |
| 110 | |
} |
| 111 | |
} |
| 112 | |
|
| 113 | |
public boolean isPending() { |
| 114 | 4 | return method.isAnnotationPresent(Pending.class); |
| 115 | |
} |
| 116 | |
|
| 117 | |
public boolean matches(String stepAsString) { |
| 118 | 41 | return matches(stepAsString, null); |
| 119 | |
} |
| 120 | |
|
| 121 | |
public boolean matches(String step, String previousNonAndStep) { |
| 122 | |
try { |
| 123 | 43 | boolean matchesType = true; |
| 124 | 43 | if (isAndStep(step)) { |
| 125 | 2 | if (previousNonAndStep == null) { |
| 126 | |
|
| 127 | 1 | matchesType = false; |
| 128 | |
} else { |
| 129 | |
|
| 130 | 1 | matchesType = keywords.startingWordFor(stepType).equals(findStartingWord(previousNonAndStep)); |
| 131 | |
} |
| 132 | |
} |
| 133 | 42 | stepMonitor.stepMatchesType(step, previousNonAndStep, matchesType, stepType, method, stepsType); |
| 134 | 42 | boolean matchesPattern = stepMatcher.matches(stripStartingWord(step)); |
| 135 | 38 | stepMonitor.stepMatchesPattern(step, matchesPattern, stepMatcher.pattern(), method, stepsType); |
| 136 | |
|
| 137 | 38 | return matchesType && matchesPattern; |
| 138 | 5 | } catch (StartingWordNotFound e) { |
| 139 | 5 | return false; |
| 140 | |
} |
| 141 | |
} |
| 142 | |
|
| 143 | |
public Step createMatchedStep(String stepAsString, Map<String, String> namedParameters) { |
| 144 | 74 | return stepCreator.createParametrisedStep(method, stepAsString, stripStartingWord(stepAsString), |
| 145 | |
namedParameters); |
| 146 | |
} |
| 147 | |
|
| 148 | |
public void addComposedSteps(List<Step> steps, String stepAsString, Map<String, String> namedParameters, |
| 149 | |
List<StepCandidate> allCandidates) { |
| 150 | 4 | addComposedStepsRecursively(steps, stepAsString, namedParameters, allCandidates, composedSteps); |
| 151 | 4 | } |
| 152 | |
|
| 153 | |
private void addComposedStepsRecursively(List<Step> steps, String stepAsString, |
| 154 | |
Map<String, String> namedParameters, List<StepCandidate> allCandidates, String[] composedSteps) { |
| 155 | 6 | Map<String, String> matchedParameters = stepCreator.matchedParameters(method, stepAsString, |
| 156 | |
stripStartingWord(stepAsString), namedParameters); |
| 157 | 6 | matchedParameters.putAll(namedParameters); |
| 158 | 17 | for (String composedStep : composedSteps) { |
| 159 | 11 | addComposedStep(steps, composedStep, matchedParameters, allCandidates); |
| 160 | |
} |
| 161 | 6 | } |
| 162 | |
|
| 163 | |
private void addComposedStep(List<Step> steps, String composedStep, Map<String, String> matchedParameters, |
| 164 | |
List<StepCandidate> allCandidates) { |
| 165 | 11 | StepCandidate candidate = findComposedCandidate(composedStep, allCandidates); |
| 166 | 11 | if (candidate != null) { |
| 167 | 11 | steps.add(candidate.createMatchedStep(composedStep, matchedParameters)); |
| 168 | 11 | if (candidate.isComposite()) { |
| 169 | |
|
| 170 | 2 | addComposedStepsRecursively(steps, composedStep, matchedParameters, allCandidates, |
| 171 | |
candidate.composedSteps()); |
| 172 | |
} |
| 173 | |
} else { |
| 174 | 0 | steps.add(StepCreator.createPendingStep(composedStep, null)); |
| 175 | |
} |
| 176 | 11 | } |
| 177 | |
|
| 178 | |
private StepCandidate findComposedCandidate(String composedStep, List<StepCandidate> allCandidates) { |
| 179 | 11 | for (StepCandidate candidate : allCandidates) { |
| 180 | 39 | if (StringUtils.startsWith(composedStep, candidate.getStartingWord()) |
| 181 | |
&& (StringUtils.endsWith(composedStep, candidate.getPatternAsString()) || candidate |
| 182 | |
.matches(composedStep))) { |
| 183 | 11 | return candidate; |
| 184 | |
} |
| 185 | |
} |
| 186 | 0 | return null; |
| 187 | |
} |
| 188 | |
|
| 189 | |
public boolean isAndStep(String stepAsString) { |
| 190 | 45 | return keywords.isAndStep(stepAsString); |
| 191 | |
} |
| 192 | |
|
| 193 | |
public boolean isIgnorableStep(String stepAsString) { |
| 194 | 2 | return keywords.isIgnorableStep(stepAsString); |
| 195 | |
} |
| 196 | |
|
| 197 | |
private String findStartingWord(String stepAsString) { |
| 198 | 1 | return keywords.startingWord(stepAsString, stepType); |
| 199 | |
} |
| 200 | |
|
| 201 | |
private String stripStartingWord(String stepAsString) { |
| 202 | 122 | return keywords.stepWithoutStartingWord(stepAsString, stepType); |
| 203 | |
} |
| 204 | |
|
| 205 | |
@Override |
| 206 | |
public String toString() { |
| 207 | 93 | return stepType + " " + patternAsString; |
| 208 | |
} |
| 209 | |
|
| 210 | |
} |