Skip to content

Commit

Permalink
[Core] Extract interfaces for options
Browse files Browse the repository at this point in the history
  • Loading branch information
mpkorstanje committed Dec 2, 2018
1 parent cf8b320 commit 170f8d2
Show file tree
Hide file tree
Showing 21 changed files with 122 additions and 83 deletions.
14 changes: 7 additions & 7 deletions core/src/main/java/cucumber/runner/Runner.java
Expand Up @@ -5,7 +5,7 @@
import cucumber.api.event.SnippetsSuggestedEvent; import cucumber.api.event.SnippetsSuggestedEvent;
import cucumber.runtime.Backend; import cucumber.runtime.Backend;
import cucumber.runtime.HookDefinition; import cucumber.runtime.HookDefinition;
import cucumber.runtime.RuntimeOptions; import io.cucumber.core.options.RunnerOptions;
import gherkin.events.PickleEvent; import gherkin.events.PickleEvent;
import gherkin.pickles.PickleStep; import gherkin.pickles.PickleStep;
import gherkin.pickles.PickleTag; import gherkin.pickles.PickleTag;
Expand All @@ -18,14 +18,14 @@ public final class Runner {
private final Glue glue = new Glue(); private final Glue glue = new Glue();
private final EventBus bus; private final EventBus bus;
private final Collection<? extends Backend> backends; private final Collection<? extends Backend> backends;
private final RuntimeOptions runtimeOptions; private final RunnerOptions runnerOptions;


public Runner(EventBus bus, Collection<? extends Backend> backends, RuntimeOptions runtimeOptions) { public Runner(EventBus bus, Collection<? extends Backend> backends, RunnerOptions runnerOptions) {
this.bus = bus; this.bus = bus;
this.runtimeOptions = runtimeOptions; this.runnerOptions = runnerOptions;
this.backends = backends; this.backends = backends;
for (Backend backend : backends) { for (Backend backend : backends) {
backend.loadGlue(glue, runtimeOptions.getGlue()); backend.loadGlue(glue, runnerOptions.getGlue());
} }


} }
Expand Down Expand Up @@ -54,7 +54,7 @@ private TestCase createTestCaseForPickle(PickleEvent pickleEvent) {
addTestStepsForPickleSteps(testSteps, pickleEvent); addTestStepsForPickleSteps(testSteps, pickleEvent);
addTestStepsForAfterHooks(afterHooks, pickleEvent.pickle.getTags()); addTestStepsForAfterHooks(afterHooks, pickleEvent.pickle.getTags());
} }
return new TestCase(testSteps, beforeHooks, afterHooks, pickleEvent, runtimeOptions.isDryRun()); return new TestCase(testSteps, beforeHooks, afterHooks, pickleEvent, runnerOptions.isDryRun());
} }


private void addTestStepsForPickleSteps(List<PickleStepTestStep> testSteps, PickleEvent pickleEvent) { private void addTestStepsForPickleSteps(List<PickleStepTestStep> testSteps, PickleEvent pickleEvent) {
Expand All @@ -65,7 +65,7 @@ private void addTestStepsForPickleSteps(List<PickleStepTestStep> testSteps, Pick
if (match == null) { if (match == null) {
List<String> snippets = new ArrayList<>(); List<String> snippets = new ArrayList<>();
for (Backend backend : backends) { for (Backend backend : backends) {
List<String> snippet = backend.getSnippet(step, "**KEYWORD**", runtimeOptions.getSnippetType().getFunctionNameGenerator()); List<String> snippet = backend.getSnippet(step, "**KEYWORD**", runnerOptions.getSnippetType().getFunctionNameGenerator());
snippets.addAll(snippet); snippets.addAll(snippet);
} }
if (!snippets.isEmpty()) { if (!snippets.isEmpty()) {
Expand Down
2 changes: 0 additions & 2 deletions core/src/main/java/cucumber/runner/RunnerSupplier.java
@@ -1,7 +1,5 @@
package cucumber.runner; package cucumber.runner;


import cucumber.runner.Runner;

public interface RunnerSupplier { public interface RunnerSupplier {
Runner get(); Runner get();
} }
10 changes: 5 additions & 5 deletions core/src/main/java/cucumber/runner/SingletonRunnerSupplier.java
@@ -1,7 +1,7 @@
package cucumber.runner; package cucumber.runner;


import cucumber.runtime.BackendSupplier; import cucumber.runtime.BackendSupplier;
import cucumber.runtime.RuntimeOptions; import io.cucumber.core.options.RunnerOptions;


/** /**
* Returns a single unique runner. * Returns a single unique runner.
Expand All @@ -11,18 +11,18 @@
public class SingletonRunnerSupplier implements RunnerSupplier { public class SingletonRunnerSupplier implements RunnerSupplier {


private final BackendSupplier backendSupplier; private final BackendSupplier backendSupplier;
private final RuntimeOptions runtimeOptions; private final RunnerOptions runnerOptions;
private final EventBus eventBus; private final EventBus eventBus;
private Runner runner; private Runner runner;




public SingletonRunnerSupplier( public SingletonRunnerSupplier(
RuntimeOptions runtimeOptions, RunnerOptions runnerOptions,
EventBus eventBus, EventBus eventBus,
BackendSupplier backendSupplier BackendSupplier backendSupplier
) { ) {
this.backendSupplier = backendSupplier; this.backendSupplier = backendSupplier;
this.runtimeOptions = runtimeOptions; this.runnerOptions = runnerOptions;
this.eventBus = eventBus; this.eventBus = eventBus;
} }


Expand All @@ -35,7 +35,7 @@ public Runner get() {
} }


private Runner createRunner() { private Runner createRunner() {
return new Runner(eventBus, backendSupplier.get(), runtimeOptions); return new Runner(eventBus, backendSupplier.get(), runnerOptions);
} }


} }
Expand Up @@ -2,10 +2,8 @@


import cucumber.api.event.Event; import cucumber.api.event.Event;
import cucumber.api.event.EventHandler; import cucumber.api.event.EventHandler;
import cucumber.runner.AbstractEventBus;
import cucumber.runner.EventBus;
import cucumber.runtime.BackendSupplier; import cucumber.runtime.BackendSupplier;
import cucumber.runtime.RuntimeOptions; import io.cucumber.core.options.RunnerOptions;


/** /**
* Creates a distinct runner for each calling thread. Each runner has its own bus, backend- and glue-suppliers. * Creates a distinct runner for each calling thread. Each runner has its own bus, backend- and glue-suppliers.
Expand All @@ -15,7 +13,7 @@
public class ThreadLocalRunnerSupplier implements RunnerSupplier { public class ThreadLocalRunnerSupplier implements RunnerSupplier {


private final BackendSupplier backendSupplier; private final BackendSupplier backendSupplier;
private final RuntimeOptions runtimeOptions; private final RunnerOptions runnerOptions;
private final SynchronizedEventBus sharedEventBus; private final SynchronizedEventBus sharedEventBus;


private final ThreadLocal<Runner> runners = new ThreadLocal<Runner>() { private final ThreadLocal<Runner> runners = new ThreadLocal<Runner>() {
Expand All @@ -26,11 +24,11 @@ protected Runner initialValue() {
}; };


public ThreadLocalRunnerSupplier( public ThreadLocalRunnerSupplier(
RuntimeOptions runtimeOptions, RunnerOptions runnerOptions,
EventBus sharedEventBus, EventBus sharedEventBus,
BackendSupplier backendSupplier BackendSupplier backendSupplier
) { ) {
this.runtimeOptions = runtimeOptions; this.runnerOptions = runnerOptions;
this.sharedEventBus = SynchronizedEventBus.synchronize(sharedEventBus); this.sharedEventBus = SynchronizedEventBus.synchronize(sharedEventBus);
this.backendSupplier = backendSupplier; this.backendSupplier = backendSupplier;
} }
Expand All @@ -41,7 +39,7 @@ public Runner get() {
} }


private Runner createRunner() { private Runner createRunner() {
return new Runner(new LocalEventBus(sharedEventBus), backendSupplier.get(), runtimeOptions); return new Runner(new LocalEventBus(sharedEventBus), backendSupplier.get(), runnerOptions);
} }


private static final class LocalEventBus extends AbstractEventBus { private static final class LocalEventBus extends AbstractEventBus {
Expand Down
Expand Up @@ -3,6 +3,7 @@
import cucumber.api.TypeRegistryConfigurer; import cucumber.api.TypeRegistryConfigurer;
import cucumber.runtime.io.MultiLoader; import cucumber.runtime.io.MultiLoader;
import cucumber.runtime.io.ResourceLoader; import cucumber.runtime.io.ResourceLoader;
import io.cucumber.core.options.RunnerOptions;
import io.cucumber.stepexpression.TypeRegistry; import io.cucumber.stepexpression.TypeRegistry;


import java.util.Collection; import java.util.Collection;
Expand All @@ -18,17 +19,17 @@ public final class BackendModuleBackendSupplier implements BackendSupplier {


private final ResourceLoader resourceLoader; private final ResourceLoader resourceLoader;
private final ClassFinder classFinder; private final ClassFinder classFinder;
private final RuntimeOptions runtimeOptions; private final RunnerOptions runnerOptions;
private final List<String> packages; private final List<String> packages;


public BackendModuleBackendSupplier(ResourceLoader resourceLoader, ClassFinder classFinder, RuntimeOptions runtimeOptions) { public BackendModuleBackendSupplier(ResourceLoader resourceLoader, ClassFinder classFinder, RunnerOptions runnerOptions) {
this(resourceLoader, classFinder, runtimeOptions, singletonList("cucumber.runtime")); this(resourceLoader, classFinder, runnerOptions, singletonList("cucumber.runtime"));
} }


BackendModuleBackendSupplier(ResourceLoader resourceLoader, ClassFinder classFinder, RuntimeOptions runtimeOptions, List<String> packages) { BackendModuleBackendSupplier(ResourceLoader resourceLoader, ClassFinder classFinder, RunnerOptions runnerOptions, List<String> packages) {
this.resourceLoader = resourceLoader; this.resourceLoader = resourceLoader;
this.classFinder = classFinder; this.classFinder = classFinder;
this.runtimeOptions = runtimeOptions; this.runnerOptions = runnerOptions;
this.packages = packages; this.packages = packages;
} }


Expand All @@ -43,7 +44,7 @@ public Collection<? extends Backend> get() {


private Collection<? extends Backend> loadBackends() { private Collection<? extends Backend> loadBackends() {
Reflections reflections = new Reflections(classFinder); Reflections reflections = new Reflections(classFinder);
TypeRegistryConfigurer typeRegistryConfigurer = reflections.instantiateExactlyOneSubclass(TypeRegistryConfigurer.class, MultiLoader.packageName(runtimeOptions.getGlue()), new Class[0], new Object[0], new DefaultTypeRegistryConfiguration()); TypeRegistryConfigurer typeRegistryConfigurer = reflections.instantiateExactlyOneSubclass(TypeRegistryConfigurer.class, MultiLoader.packageName(runnerOptions.getGlue()), new Class[0], new Object[0], new DefaultTypeRegistryConfiguration());
TypeRegistry typeRegistry = new TypeRegistry(typeRegistryConfigurer.locale()); TypeRegistry typeRegistry = new TypeRegistry(typeRegistryConfigurer.locale());
typeRegistryConfigurer.configureTypeRegistry(typeRegistry); typeRegistryConfigurer.configureTypeRegistry(typeRegistry);


Expand Down
Expand Up @@ -2,6 +2,7 @@


import cucumber.runtime.model.CucumberFeature; import cucumber.runtime.model.CucumberFeature;
import cucumber.runtime.model.FeatureLoader; import cucumber.runtime.model.FeatureLoader;
import io.cucumber.core.options.FeatureOptions;


import java.util.List; import java.util.List;


Expand All @@ -10,15 +11,15 @@
*/ */
public class FeaturePathFeatureSupplier implements FeatureSupplier { public class FeaturePathFeatureSupplier implements FeatureSupplier {
private final FeatureLoader featureLoader; private final FeatureLoader featureLoader;
private final RuntimeOptions runtimeOptions; private final FeatureOptions featureOptions;


public FeaturePathFeatureSupplier(FeatureLoader featureLoader, RuntimeOptions runtimeOptions) { public FeaturePathFeatureSupplier(FeatureLoader featureLoader, FeatureOptions featureOptions) {
this.featureLoader = featureLoader; this.featureLoader = featureLoader;
this.runtimeOptions = runtimeOptions; this.featureOptions = featureOptions;
} }


@Override @Override
public List<CucumberFeature> get() { public List<CucumberFeature> get() {
return featureLoader.load(runtimeOptions.getFeaturePaths(), System.out); return featureLoader.load(featureOptions.getFeaturePaths(), System.out);
} }
} }
37 changes: 23 additions & 14 deletions core/src/main/java/cucumber/runtime/RuntimeOptions.java
@@ -1,6 +1,10 @@
package cucumber.runtime; package cucumber.runtime;


import cucumber.api.SnippetType; import cucumber.api.SnippetType;
import io.cucumber.core.options.FeatureOptions;
import io.cucumber.core.options.FilterOptions;
import io.cucumber.core.options.PluginOptions;
import io.cucumber.core.options.RunnerOptions;
import io.cucumber.datatable.DataTable; import io.cucumber.datatable.DataTable;
import cucumber.runtime.formatter.PluginFactory; import cucumber.runtime.formatter.PluginFactory;
import cucumber.runtime.model.PathWithLines; import cucumber.runtime.model.PathWithLines;
Expand All @@ -9,7 +13,6 @@
import gherkin.GherkinDialect; import gherkin.GherkinDialect;
import gherkin.GherkinDialectProvider; import gherkin.GherkinDialectProvider;
import gherkin.IGherkinDialectProvider; import gherkin.IGherkinDialectProvider;
import io.cucumber.datatable.DataTable;


import java.io.InputStreamReader; import java.io.InputStreamReader;
import java.io.Reader; import java.io.Reader;
Expand All @@ -25,7 +28,7 @@
import static java.util.Arrays.asList; import static java.util.Arrays.asList;


// IMPORTANT! Make sure USAGE.txt is always uptodate if this class changes. // IMPORTANT! Make sure USAGE.txt is always uptodate if this class changes.
public class RuntimeOptions { public class RuntimeOptions implements FeatureOptions, FilterOptions, PluginOptions, RunnerOptions {
public static final String VERSION = ResourceBundle.getBundle("cucumber.version").getString("cucumber-jvm.version"); public static final String VERSION = ResourceBundle.getBundle("cucumber.version").getString("cucumber-jvm.version");
public static final String USAGE_RESOURCE = "/cucumber/api/cli/USAGE.txt"; public static final String USAGE_RESOURCE = "/cucumber/api/cli/USAGE.txt";


Expand Down Expand Up @@ -111,18 +114,6 @@ public RuntimeOptions noSummaryPrinter() {
return this; return this;
} }


public List<String> getPluginFormatterNames() {
return pluginFormatterNames;
}

public List<String> getPluginSummaryPrinterNames() {
return pluginSummaryPrinterNames;
}

public List<String> getPluginStepDefinitionReporterNames() {
return pluginStepDefinitionReporterNames;
}

private void parse(List<String> args) { private void parse(List<String> args) {
List<String> parsedTagFilters = new ArrayList<String>(); List<String> parsedTagFilters = new ArrayList<String>();
List<Pattern> parsedNameFilters = new ArrayList<Pattern>(); List<Pattern> parsedNameFilters = new ArrayList<Pattern>();
Expand Down Expand Up @@ -300,14 +291,26 @@ private void addKeywordRow(List<List<String>> table, String key, List<String> ke
table.add(cells); table.add(cells);
} }


@Override
public List<String> getPluginNames() {
List<String> pluginNames = new ArrayList<>();
pluginNames.addAll(pluginFormatterNames);
pluginNames.addAll(pluginStepDefinitionReporterNames);
pluginNames.addAll(pluginSummaryPrinterNames);
return pluginNames;
}

@Override
public List<String> getGlue() { public List<String> getGlue() {
return glue; return glue;
} }


@Override
public boolean isStrict() { public boolean isStrict() {
return strict; return strict;
} }


@Override
public boolean isDryRun() { public boolean isDryRun() {
return dryRun; return dryRun;
} }
Expand All @@ -316,26 +319,32 @@ public boolean isWip() {
return wip; return wip;
} }


@Override
public List<String> getFeaturePaths() { public List<String> getFeaturePaths() {
return featurePaths; return featurePaths;
} }


@Override
public List<Pattern> getNameFilters() { public List<Pattern> getNameFilters() {
return nameFilters; return nameFilters;
} }


@Override
public List<String> getTagFilters() { public List<String> getTagFilters() {
return tagFilters; return tagFilters;
} }


@Override
public Map<String, List<Long>> getLineFilters() { public Map<String, List<Long>> getLineFilters() {
return lineFilters; return lineFilters;
} }


@Override
public boolean isMonochrome() { public boolean isMonochrome() {
return monochrome; return monochrome;
} }


@Override
public SnippetType getSnippetType() { public SnippetType getSnippetType() {
return snippetType; return snippetType;
} }
Expand Down
12 changes: 6 additions & 6 deletions core/src/main/java/cucumber/runtime/filter/Filters.java
@@ -1,7 +1,7 @@
package cucumber.runtime.filter; package cucumber.runtime.filter;


import cucumber.runtime.RuntimeOptions;
import gherkin.events.PickleEvent; import gherkin.events.PickleEvent;
import io.cucumber.core.options.FilterOptions;


import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
Expand All @@ -11,23 +11,23 @@
public class Filters { public class Filters {


private final List<PicklePredicate> filters; private final List<PicklePredicate> filters;
private final RuntimeOptions runtimeOptions; private final FilterOptions runtimeOptions;
private final RerunFilters rerunFilters; private final RerunFilters rerunFilters;


public Filters(RuntimeOptions runtimeOptions, RerunFilters rerunFilters) { public Filters(FilterOptions filterOPtions, RerunFilters rerunFilters) {
this.runtimeOptions = runtimeOptions; this.runtimeOptions = filterOPtions;
this.rerunFilters = rerunFilters; this.rerunFilters = rerunFilters;


filters = new ArrayList<PicklePredicate>(); filters = new ArrayList<PicklePredicate>();
List<String> tagFilters = this.runtimeOptions.getTagFilters(); List<String> tagFilters = this.runtimeOptions.getTagFilters();
if (!tagFilters.isEmpty()) { if (!tagFilters.isEmpty()) {
this.filters.add(new TagPredicate(tagFilters)); this.filters.add(new TagPredicate(tagFilters));
} }
List<Pattern> nameFilters = runtimeOptions.getNameFilters(); List<Pattern> nameFilters = filterOPtions.getNameFilters();
if (!nameFilters.isEmpty()) { if (!nameFilters.isEmpty()) {
this.filters.add(new NamePredicate(nameFilters)); this.filters.add(new NamePredicate(nameFilters));
} }
Map<String, List<Long>> lineFilters = runtimeOptions.getLineFilters(); Map<String, List<Long>> lineFilters = filterOPtions.getLineFilters();
Map<String, List<Long>> rerunlineFilters = rerunFilters.processRerunFiles(); Map<String, List<Long>> rerunlineFilters = rerunFilters.processRerunFiles();
for (Map.Entry<String,List<Long>> line: rerunlineFilters.entrySet()) { for (Map.Entry<String,List<Long>> line: rerunlineFilters.entrySet()) {
addLineFilters(lineFilters, line.getKey(), line.getValue()); addLineFilters(lineFilters, line.getKey(), line.getValue());
Expand Down
11 changes: 5 additions & 6 deletions core/src/main/java/cucumber/runtime/filter/RerunFilters.java
@@ -1,6 +1,6 @@
package cucumber.runtime.filter; package cucumber.runtime.filter;


import cucumber.runtime.RuntimeOptions; import io.cucumber.core.options.FeatureOptions;
import cucumber.runtime.model.FeatureLoader; import cucumber.runtime.model.FeatureLoader;
import cucumber.runtime.model.PathWithLines; import cucumber.runtime.model.PathWithLines;


Expand All @@ -9,18 +9,17 @@
import java.util.Map; import java.util.Map;


public class RerunFilters { public class RerunFilters {
private final RuntimeOptions runtimeOptions; private final FeatureOptions featureOptions;
private final FeatureLoader featureLoader; private final FeatureLoader featureLoader;


public RerunFilters(RuntimeOptions runtimeOptions, FeatureLoader featureLoader) { public RerunFilters(FeatureOptions featureOptions, FeatureLoader featureLoader) {
this.runtimeOptions = runtimeOptions; this.featureOptions = featureOptions;
this.featureLoader = featureLoader; this.featureLoader = featureLoader;
} }



Map<String, List<Long>> processRerunFiles() { Map<String, List<Long>> processRerunFiles() {
final Map<String, List<Long>> lineFilters = new HashMap<String, List<Long>>(); final Map<String, List<Long>> lineFilters = new HashMap<String, List<Long>>();
for (String featurePath : runtimeOptions.getFeaturePaths()) { for (String featurePath : featureOptions.getFeaturePaths()) {
if (featurePath.startsWith("@")) { if (featurePath.startsWith("@")) {
for (PathWithLines pathWithLines : featureLoader.loadRerunFile(featurePath.substring(1))) { for (PathWithLines pathWithLines : featureLoader.loadRerunFile(featurePath.substring(1))) {
addLineFilters(lineFilters, pathWithLines.path, pathWithLines.lines); addLineFilters(lineFilters, pathWithLines.path, pathWithLines.lines);
Expand Down

0 comments on commit 170f8d2

Please sign in to comment.