| 1 | |
package org.jbehave.core.steps; |
| 2 | |
|
| 3 | |
import java.lang.annotation.Annotation; |
| 4 | |
import java.lang.reflect.Method; |
| 5 | |
import java.util.ArrayList; |
| 6 | |
import java.util.List; |
| 7 | |
|
| 8 | |
import org.apache.commons.lang.builder.ToStringBuilder; |
| 9 | |
import org.apache.commons.lang.builder.ToStringStyle; |
| 10 | |
import org.jbehave.core.annotations.AfterScenario; |
| 11 | |
import org.jbehave.core.annotations.AfterScenario.Outcome; |
| 12 | |
import org.jbehave.core.annotations.AfterStories; |
| 13 | |
import org.jbehave.core.annotations.AfterStory; |
| 14 | |
import org.jbehave.core.annotations.Alias; |
| 15 | |
import org.jbehave.core.annotations.Aliases; |
| 16 | |
import org.jbehave.core.annotations.BeforeScenario; |
| 17 | |
import org.jbehave.core.annotations.BeforeStories; |
| 18 | |
import org.jbehave.core.annotations.BeforeStory; |
| 19 | |
import org.jbehave.core.annotations.Composite; |
| 20 | |
import org.jbehave.core.annotations.Given; |
| 21 | |
import org.jbehave.core.annotations.ScenarioType; |
| 22 | |
import org.jbehave.core.annotations.Then; |
| 23 | |
import org.jbehave.core.annotations.When; |
| 24 | |
import org.jbehave.core.configuration.Configuration; |
| 25 | |
import org.jbehave.core.configuration.MostUsefulConfiguration; |
| 26 | |
import org.jbehave.core.parsers.RegexPrefixCapturingPatternParser; |
| 27 | |
import org.jbehave.core.parsers.StepPatternParser; |
| 28 | |
import org.jbehave.core.steps.StepCollector.Stage; |
| 29 | |
|
| 30 | |
import static java.util.Arrays.asList; |
| 31 | |
import static org.jbehave.core.annotations.AfterScenario.Outcome.ANY; |
| 32 | |
import static org.jbehave.core.annotations.AfterScenario.Outcome.FAILURE; |
| 33 | |
import static org.jbehave.core.annotations.AfterScenario.Outcome.SUCCESS; |
| 34 | |
import static org.jbehave.core.steps.StepType.GIVEN; |
| 35 | |
import static org.jbehave.core.steps.StepType.THEN; |
| 36 | |
import static org.jbehave.core.steps.StepType.WHEN; |
| 37 | |
|
| 38 | |
|
| 39 | |
|
| 40 | |
|
| 41 | |
|
| 42 | |
|
| 43 | |
|
| 44 | |
|
| 45 | |
|
| 46 | |
|
| 47 | |
|
| 48 | |
|
| 49 | |
|
| 50 | |
|
| 51 | |
|
| 52 | |
|
| 53 | |
|
| 54 | |
|
| 55 | |
|
| 56 | |
|
| 57 | |
|
| 58 | |
|
| 59 | |
|
| 60 | |
|
| 61 | |
|
| 62 | |
|
| 63 | |
|
| 64 | |
|
| 65 | |
|
| 66 | |
|
| 67 | |
|
| 68 | |
|
| 69 | |
|
| 70 | |
|
| 71 | |
|
| 72 | |
|
| 73 | |
|
| 74 | |
|
| 75 | |
|
| 76 | |
|
| 77 | |
|
| 78 | |
|
| 79 | |
|
| 80 | |
|
| 81 | |
|
| 82 | |
|
| 83 | |
|
| 84 | |
|
| 85 | |
|
| 86 | |
|
| 87 | |
|
| 88 | |
|
| 89 | |
|
| 90 | |
|
| 91 | |
|
| 92 | |
|
| 93 | |
|
| 94 | |
public class Steps implements CandidateSteps { |
| 95 | |
|
| 96 | |
private final Configuration configuration; |
| 97 | |
private Class<?> type; |
| 98 | |
private InjectableStepsFactory stepsFactory; |
| 99 | |
|
| 100 | |
|
| 101 | |
|
| 102 | |
|
| 103 | |
|
| 104 | |
public Steps() { |
| 105 | 105 | this(new MostUsefulConfiguration()); |
| 106 | 105 | } |
| 107 | |
|
| 108 | |
|
| 109 | |
|
| 110 | |
|
| 111 | |
|
| 112 | |
|
| 113 | |
|
| 114 | 112 | public Steps(Configuration configuration) { |
| 115 | 112 | this.configuration = configuration; |
| 116 | 112 | this.type = this.getClass(); |
| 117 | 112 | this.stepsFactory = new InstanceStepsFactory(configuration, this); |
| 118 | 112 | } |
| 119 | |
|
| 120 | |
|
| 121 | |
|
| 122 | |
|
| 123 | |
|
| 124 | |
|
| 125 | |
|
| 126 | |
|
| 127 | 0 | public Steps(Configuration configuration, Object instance) { |
| 128 | 0 | this.configuration = configuration; |
| 129 | 0 | this.type = instance.getClass(); |
| 130 | 0 | this.stepsFactory = new InstanceStepsFactory(configuration, instance); |
| 131 | 0 | } |
| 132 | |
|
| 133 | |
|
| 134 | |
|
| 135 | |
|
| 136 | |
|
| 137 | |
|
| 138 | |
|
| 139 | |
|
| 140 | |
|
| 141 | |
|
| 142 | 22 | public Steps(Configuration configuration, Class<?> type, InjectableStepsFactory stepsFactory) { |
| 143 | 22 | this.configuration = configuration; |
| 144 | 22 | this.type = type; |
| 145 | 22 | this.stepsFactory = stepsFactory; |
| 146 | 22 | } |
| 147 | |
|
| 148 | |
public Class<?> type() { |
| 149 | 0 | return type; |
| 150 | |
} |
| 151 | |
|
| 152 | |
public Object instance() { |
| 153 | 25 | return stepsFactory.createInstanceOfType(type); |
| 154 | |
} |
| 155 | |
|
| 156 | |
public Configuration configuration() { |
| 157 | 1 | return configuration; |
| 158 | |
} |
| 159 | |
|
| 160 | |
public List<StepCandidate> listCandidates() { |
| 161 | 29 | List<StepCandidate> candidates = new ArrayList<StepCandidate>(); |
| 162 | 29 | for (Method method : allMethods()) { |
| 163 | 539 | if (method.isAnnotationPresent(Given.class)) { |
| 164 | 30 | Given annotation = method.getAnnotation(Given.class); |
| 165 | 30 | String value = annotation.value(); |
| 166 | 30 | int priority = annotation.priority(); |
| 167 | 30 | addCandidatesFromVariants(candidates, method, GIVEN, value, priority); |
| 168 | 29 | addCandidatesFromAliases(candidates, method, GIVEN, priority); |
| 169 | |
} |
| 170 | 538 | if (method.isAnnotationPresent(When.class)) { |
| 171 | 23 | When annotation = method.getAnnotation(When.class); |
| 172 | 23 | String value = annotation.value(); |
| 173 | 23 | int priority = annotation.priority(); |
| 174 | 23 | addCandidatesFromVariants(candidates, method, WHEN, value, priority); |
| 175 | 23 | addCandidatesFromAliases(candidates, method, WHEN, priority); |
| 176 | |
} |
| 177 | 538 | if (method.isAnnotationPresent(Then.class)) { |
| 178 | 16 | Then annotation = method.getAnnotation(Then.class); |
| 179 | 16 | String value = annotation.value(); |
| 180 | 16 | int priority = annotation.priority(); |
| 181 | 16 | addCandidatesFromVariants(candidates, method, THEN, value, priority); |
| 182 | 16 | addCandidatesFromAliases(candidates, method, THEN, priority); |
| 183 | |
} |
| 184 | 538 | } |
| 185 | 28 | return candidates; |
| 186 | |
} |
| 187 | |
|
| 188 | |
private void addCandidatesFromVariants(List<StepCandidate> candidates, Method method, StepType stepType, String value, int priority) { |
| 189 | 81 | PatternVariantBuilder b = new PatternVariantBuilder(value); |
| 190 | 81 | for (String variant : b.allVariants()) { |
| 191 | 81 | addCandidate(candidates, method, stepType, variant, priority); |
| 192 | 80 | } |
| 193 | 80 | } |
| 194 | |
|
| 195 | |
private void addCandidatesFromAliases(List<StepCandidate> candidates, Method method, StepType stepType, int priority) { |
| 196 | 68 | if (method.isAnnotationPresent(Aliases.class)) { |
| 197 | 3 | String[] aliases = method.getAnnotation(Aliases.class).values(); |
| 198 | 9 | for (String alias : aliases) { |
| 199 | 6 | addCandidatesFromVariants(candidates, method, stepType, alias, priority); |
| 200 | |
} |
| 201 | |
} |
| 202 | 68 | if (method.isAnnotationPresent(Alias.class)) { |
| 203 | 6 | String alias = method.getAnnotation(Alias.class).value(); |
| 204 | 6 | addCandidatesFromVariants(candidates, method, stepType, alias, priority); |
| 205 | |
} |
| 206 | 68 | } |
| 207 | |
|
| 208 | |
private void addCandidate(List<StepCandidate> candidates, Method method, StepType stepType, |
| 209 | |
String stepPatternAsString, int priority) { |
| 210 | 81 | checkForDuplicateCandidates(candidates, stepType, stepPatternAsString); |
| 211 | 80 | StepCandidate candidate = createCandidate(method, stepType, stepPatternAsString, priority, configuration); |
| 212 | 80 | candidate.useStepMonitor(configuration.stepMonitor()); |
| 213 | 80 | candidate.useParanamer(configuration.paranamer()); |
| 214 | 80 | candidate.doDryRun(configuration.storyControls().dryRun()); |
| 215 | 80 | if (method.isAnnotationPresent(Composite.class)) { |
| 216 | 8 | candidate.composedOf(method.getAnnotation(Composite.class).steps()); |
| 217 | |
} |
| 218 | 80 | candidates.add(candidate); |
| 219 | 80 | } |
| 220 | |
|
| 221 | |
private void checkForDuplicateCandidates(List<StepCandidate> candidates, StepType stepType, String patternAsString) { |
| 222 | 81 | for (StepCandidate candidate : candidates) { |
| 223 | 138 | if (candidate.getStepType() == stepType && candidate.getPatternAsString().equals(patternAsString)) { |
| 224 | 1 | throw new DuplicateCandidateFound(stepType, patternAsString); |
| 225 | |
} |
| 226 | 137 | } |
| 227 | 80 | } |
| 228 | |
|
| 229 | |
private StepCandidate createCandidate(Method method, StepType stepType, String stepPatternAsString, int priority, |
| 230 | |
Configuration configuration) { |
| 231 | 80 | return new StepCandidate(stepPatternAsString, priority, stepType, method, type, stepsFactory, |
| 232 | |
configuration.keywords(), configuration.stepPatternParser(), configuration.parameterConverters(), configuration.parameterControls()); |
| 233 | |
} |
| 234 | |
|
| 235 | |
public List<BeforeOrAfterStep> listBeforeOrAfterStories() { |
| 236 | 5 | List<BeforeOrAfterStep> steps = new ArrayList<BeforeOrAfterStep>(); |
| 237 | 5 | steps.addAll(stepsHaving(Stage.BEFORE, BeforeStories.class)); |
| 238 | 5 | steps.addAll(stepsHaving(Stage.AFTER, AfterStories.class)); |
| 239 | 5 | return steps; |
| 240 | |
} |
| 241 | |
|
| 242 | |
public List<BeforeOrAfterStep> listBeforeOrAfterStory(boolean givenStory) { |
| 243 | 12 | List<BeforeOrAfterStep> steps = new ArrayList<BeforeOrAfterStep>(); |
| 244 | 12 | steps.addAll(stepsHaving(Stage.BEFORE, BeforeStory.class, givenStory)); |
| 245 | 12 | steps.addAll(stepsHaving(Stage.AFTER, AfterStory.class, givenStory)); |
| 246 | 12 | return steps; |
| 247 | |
} |
| 248 | |
|
| 249 | |
public List<BeforeOrAfterStep> listBeforeOrAfterScenario(ScenarioType type) { |
| 250 | 19 | List<BeforeOrAfterStep> steps = new ArrayList<BeforeOrAfterStep>(); |
| 251 | 19 | steps.addAll(scenarioStepsHaving(type, Stage.BEFORE, BeforeScenario.class)); |
| 252 | 19 | steps.addAll(scenarioStepsHaving(type, Stage.AFTER, AfterScenario.class, ANY, SUCCESS, FAILURE)); |
| 253 | 19 | return steps; |
| 254 | |
} |
| 255 | |
|
| 256 | |
private boolean runnableStoryStep(Annotation annotation, boolean givenStory) { |
| 257 | 20 | boolean uponGivenStory = uponGivenStory(annotation); |
| 258 | 20 | return uponGivenStory == givenStory; |
| 259 | |
} |
| 260 | |
|
| 261 | |
private boolean uponGivenStory(Annotation annotation) { |
| 262 | 20 | if (annotation instanceof BeforeStory) { |
| 263 | 10 | return ((BeforeStory) annotation).uponGivenStory(); |
| 264 | 10 | } else if (annotation instanceof AfterStory) { |
| 265 | 10 | return ((AfterStory) annotation).uponGivenStory(); |
| 266 | |
} |
| 267 | 0 | return false; |
| 268 | |
} |
| 269 | |
|
| 270 | |
private List<BeforeOrAfterStep> stepsHaving(Stage stage, Class<? extends Annotation> annotationClass) { |
| 271 | 10 | List<BeforeOrAfterStep> steps = new ArrayList<BeforeOrAfterStep>(); |
| 272 | 10 | for (Method method : methodsAnnotatedWith(annotationClass)) { |
| 273 | 2 | steps.add(createBeforeOrAfterStep(stage, method)); |
| 274 | 2 | } |
| 275 | 10 | return steps; |
| 276 | |
} |
| 277 | |
|
| 278 | |
private List<BeforeOrAfterStep> stepsHaving(Stage stage, Class<? extends Annotation> annotationClass, |
| 279 | |
boolean givenStory) { |
| 280 | 24 | List<BeforeOrAfterStep> steps = new ArrayList<BeforeOrAfterStep>(); |
| 281 | 24 | for (final Method method : methodsAnnotatedWith(annotationClass)) { |
| 282 | 20 | if (runnableStoryStep(method.getAnnotation(annotationClass), givenStory)) { |
| 283 | 14 | steps.add(createBeforeOrAfterStep(stage, method)); |
| 284 | |
} |
| 285 | 20 | } |
| 286 | 24 | return steps; |
| 287 | |
} |
| 288 | |
|
| 289 | |
private List<BeforeOrAfterStep> scenarioStepsHaving(ScenarioType type, Stage stage, |
| 290 | |
Class<? extends Annotation> annotationClass, Outcome... outcomes) { |
| 291 | 38 | List<BeforeOrAfterStep> steps = new ArrayList<BeforeOrAfterStep>(); |
| 292 | 38 | for (Method method : methodsAnnotatedWith(annotationClass)) { |
| 293 | 42 | ScenarioType scenarioType = scenarioType(method, annotationClass); |
| 294 | 42 | if (type == scenarioType) { |
| 295 | 32 | if (stage == Stage.BEFORE) { |
| 296 | 13 | steps.add(createBeforeOrAfterStep(stage, method)); |
| 297 | |
} |
| 298 | 32 | if (stage == Stage.AFTER) { |
| 299 | 19 | Outcome scenarioOutcome = scenarioOutcome(method, annotationClass); |
| 300 | 76 | for (Outcome outcome : outcomes) { |
| 301 | 57 | if (outcome.equals(scenarioOutcome)) { |
| 302 | 19 | steps.add(createBeforeOrAfterStep(stage, method, outcome)); |
| 303 | |
} |
| 304 | |
} |
| 305 | |
} |
| 306 | |
} |
| 307 | 42 | } |
| 308 | 38 | return steps; |
| 309 | |
} |
| 310 | |
|
| 311 | |
private ScenarioType scenarioType(Method method, Class<? extends Annotation> annotationClass) { |
| 312 | 42 | if (annotationClass.isAssignableFrom(BeforeScenario.class)) { |
| 313 | 17 | return ((BeforeScenario) method.getAnnotation(annotationClass)).uponType(); |
| 314 | |
} |
| 315 | 25 | if (annotationClass.isAssignableFrom(AfterScenario.class)) { |
| 316 | 25 | return ((AfterScenario) method.getAnnotation(annotationClass)).uponType(); |
| 317 | |
} |
| 318 | 0 | return ScenarioType.NORMAL; |
| 319 | |
} |
| 320 | |
|
| 321 | |
private Outcome scenarioOutcome(Method method, Class<? extends Annotation> annotationClass) { |
| 322 | 19 | if (annotationClass.isAssignableFrom(AfterScenario.class)) { |
| 323 | 19 | return ((AfterScenario) method.getAnnotation(annotationClass)).uponOutcome(); |
| 324 | |
} |
| 325 | 0 | return Outcome.ANY; |
| 326 | |
} |
| 327 | |
|
| 328 | |
private BeforeOrAfterStep createBeforeOrAfterStep(Stage stage, Method method) { |
| 329 | 29 | return createBeforeOrAfterStep(stage, method, Outcome.ANY); |
| 330 | |
} |
| 331 | |
|
| 332 | |
private BeforeOrAfterStep createBeforeOrAfterStep(Stage stage, Method method, Outcome outcome) { |
| 333 | 48 | return new BeforeOrAfterStep(stage, method, outcome, new StepCreator(type, stepsFactory, |
| 334 | |
configuration.parameterConverters(), configuration.parameterControls(), null, configuration.stepMonitor())); |
| 335 | |
} |
| 336 | |
|
| 337 | |
private List<Method> allMethods() { |
| 338 | 101 | return asList(type.getMethods()); |
| 339 | |
} |
| 340 | |
|
| 341 | |
private List<Method> methodsAnnotatedWith(Class<? extends Annotation> annotationClass) { |
| 342 | 72 | List<Method> annotated = new ArrayList<Method>(); |
| 343 | 72 | for (Method method : allMethods()) { |
| 344 | 1556 | if (method.isAnnotationPresent(annotationClass)) { |
| 345 | 64 | annotated.add(method); |
| 346 | |
} |
| 347 | 1556 | } |
| 348 | 72 | return annotated; |
| 349 | |
} |
| 350 | |
|
| 351 | |
@SuppressWarnings("serial") |
| 352 | |
public static class DuplicateCandidateFound extends RuntimeException { |
| 353 | |
|
| 354 | |
public DuplicateCandidateFound(StepType stepType, String patternAsString) { |
| 355 | 1 | super(stepType + " " + patternAsString); |
| 356 | 1 | } |
| 357 | |
|
| 358 | |
} |
| 359 | |
|
| 360 | |
@Override |
| 361 | |
public String toString() { |
| 362 | 4 | return new ToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE).append(instance()).toString(); |
| 363 | |
} |
| 364 | |
|
| 365 | |
} |