cucumber.runtime.RuntimeGlue Maven / Gradle / Ivy
package cucumber.runtime;
import cucumber.api.StepDefinitionReporter;
import cucumber.runtime.xstream.LocalizedXStreams;
import gherkin.pickles.PickleStep;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
public class RuntimeGlue implements Glue {
final Map stepDefinitionsByPattern = new TreeMap();
final List beforeHooks = new ArrayList();
final List afterHooks = new ArrayList();
final Map matchedStepDefinitionsCache = new HashMap();
private final LocalizedXStreams localizedXStreams;
public RuntimeGlue(LocalizedXStreams localizedXStreams) {
this(null, localizedXStreams);
}
@Deprecated
public RuntimeGlue(UndefinedStepsTracker tracker, LocalizedXStreams localizedXStreams) {
this.localizedXStreams = localizedXStreams;
}
@Override
public void addStepDefinition(StepDefinition stepDefinition) {
StepDefinition previous = stepDefinitionsByPattern.get(stepDefinition.getPattern());
if (previous != null) {
throw new DuplicateStepDefinitionException(previous, stepDefinition);
}
stepDefinitionsByPattern.put(stepDefinition.getPattern(), stepDefinition);
}
@Override
public void addBeforeHook(HookDefinition hookDefinition) {
beforeHooks.add(hookDefinition);
Collections.sort(beforeHooks, new HookComparator(true));
}
@Override
public void addAfterHook(HookDefinition hookDefinition) {
afterHooks.add(hookDefinition);
Collections.sort(afterHooks, new HookComparator(false));
}
@Override
public List getBeforeHooks() {
return beforeHooks;
}
@Override
public List getAfterHooks() {
return afterHooks;
}
@Override
public StepDefinitionMatch stepDefinitionMatch(String featurePath, PickleStep step) {
String stepText = step.getText();
CacheEntry cacheEntry = matchedStepDefinitionsCache.get(stepText);
if (cacheEntry != null) {
return new StepDefinitionMatch(cacheEntry.arguments, cacheEntry.stepDefinition, featurePath, step, localizedXStreams);
}
List matches = stepDefinitionMatches(featurePath, step);
if (matches.isEmpty()) {
return null;
}
if (matches.size() == 1) {
StepDefinitionMatch match = matches.get(0);
matchedStepDefinitionsCache.put(stepText, new CacheEntry(match.getStepDefinition(), match.getArguments()));
return match;
}
throw new AmbiguousStepDefinitionsException(step, matches);
}
private List stepDefinitionMatches(String featurePath, PickleStep step) {
List result = new ArrayList();
for (StepDefinition stepDefinition : stepDefinitionsByPattern.values()) {
List arguments = stepDefinition.matchedArguments(step);
if (arguments != null) {
result.add(new StepDefinitionMatch(arguments, stepDefinition, featurePath, step, localizedXStreams));
}
}
return result;
}
@Override
public void reportStepDefinitions(StepDefinitionReporter stepDefinitionReporter) {
for (StepDefinition stepDefinition : stepDefinitionsByPattern.values()) {
stepDefinitionReporter.stepDefinition(stepDefinition);
}
}
@Override
public void removeScenarioScopedGlue() {
removeScenarioScopedHooks(beforeHooks);
removeScenarioScopedHooks(afterHooks);
removeScenarioScopedStepdefs();
}
private void removeScenarioScopedHooks(List beforeHooks1) {
Iterator hookIterator = beforeHooks1.iterator();
while (hookIterator.hasNext()) {
HookDefinition hook = hookIterator.next();
if (hook.isScenarioScoped()) {
hookIterator.remove();
}
}
}
private void removeScenarioScopedStepdefs() {
Iterator> stepdefs = stepDefinitionsByPattern.entrySet().iterator();
while (stepdefs.hasNext()) {
StepDefinition stepDefinition = stepdefs.next().getValue();
if (stepDefinition.isScenarioScoped()) {
stepdefs.remove();
}
}
}
static final class CacheEntry {
StepDefinition stepDefinition;
List arguments;
private CacheEntry(StepDefinition stepDefinition, List arguments) {
this.stepDefinition = stepDefinition;
this.arguments = arguments;
}
}
}