-
-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Replace generic supplier interface with specific purpose interfaces
- Loading branch information
1 parent
a92d0f1
commit 199f9aa
Showing
30 changed files
with
772 additions
and
736 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
core/src/main/java/cucumber/runtime/BackendModuleBackendSupplier.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package cucumber.runtime; | ||
|
||
import cucumber.api.TypeRegistryConfigurer; | ||
import cucumber.runtime.io.MultiLoader; | ||
import cucumber.runtime.io.ResourceLoader; | ||
import io.cucumber.stepexpression.TypeRegistry; | ||
|
||
import java.util.Collection; | ||
import java.util.List; | ||
|
||
import static java.util.Collections.singletonList; | ||
|
||
|
||
/** | ||
* Supplies instances of {@link Backend} found by scanning {@code cucumber.runtime} for implementations. | ||
*/ | ||
public final class BackendModuleBackendSupplier implements BackendSupplier { | ||
|
||
private final ResourceLoader resourceLoader; | ||
private final ClassFinder classFinder; | ||
private final RuntimeOptions runtimeOptions; | ||
private final List<String> packages; | ||
|
||
public BackendModuleBackendSupplier(ResourceLoader resourceLoader, ClassFinder classFinder, RuntimeOptions runtimeOptions) { | ||
this(resourceLoader, classFinder, runtimeOptions, singletonList("cucumber.runtime")); | ||
} | ||
|
||
BackendModuleBackendSupplier(ResourceLoader resourceLoader, ClassFinder classFinder, RuntimeOptions runtimeOptions, List<String> packages) { | ||
this.resourceLoader = resourceLoader; | ||
this.classFinder = classFinder; | ||
this.runtimeOptions = runtimeOptions; | ||
this.packages = packages; | ||
} | ||
|
||
@Override | ||
public Collection<? extends Backend> get() { | ||
Collection<? extends Backend> backends = loadBackends(); | ||
if (backends.isEmpty()) { | ||
throw new CucumberException("No backends were found. Please make sure you have a backend module on your CLASSPATH."); | ||
} | ||
return backends; | ||
} | ||
|
||
private Collection<? extends Backend> loadBackends() { | ||
Reflections reflections = new Reflections(classFinder); | ||
TypeRegistryConfigurer typeRegistryConfigurer = reflections.instantiateExactlyOneSubclass(TypeRegistryConfigurer.class, MultiLoader.packageName(runtimeOptions.getGlue()), new Class[0], new Object[0], new DefaultTypeRegistryConfiguration()); | ||
TypeRegistry typeRegistry = new TypeRegistry(typeRegistryConfigurer.locale()); | ||
typeRegistryConfigurer.configureTypeRegistry(typeRegistry); | ||
|
||
return reflections.instantiateSubclasses(Backend.class, packages, new Class[]{ResourceLoader.class, TypeRegistry.class}, new Object[]{resourceLoader, typeRegistry}); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,40 +1,7 @@ | ||
package cucumber.runtime; | ||
|
||
import cucumber.api.TypeRegistryConfigurer; | ||
import cucumber.runtime.io.MultiLoader; | ||
import cucumber.runtime.io.ResourceLoader; | ||
import io.cucumber.stepexpression.TypeRegistry; | ||
|
||
import java.util.Collection; | ||
|
||
import static java.util.Collections.singletonList; | ||
|
||
public class BackendSupplier implements Supplier<Collection<? extends Backend>> { | ||
|
||
private final ResourceLoader resourceLoader; | ||
private final ClassFinder classFinder; | ||
private final RuntimeOptions runtimeOptions; | ||
|
||
public BackendSupplier(ResourceLoader resourceLoader, ClassFinder classFinder, RuntimeOptions runtimeOptions) { | ||
this.resourceLoader = resourceLoader; | ||
this.classFinder = classFinder; | ||
this.runtimeOptions = runtimeOptions; | ||
} | ||
|
||
|
||
@Override | ||
public Collection<? extends Backend> get() { | ||
Collection<? extends Backend> backends = loadBackends(resourceLoader, classFinder, runtimeOptions); | ||
|
||
return backends; | ||
} | ||
|
||
private static Collection<? extends Backend> loadBackends(ResourceLoader resourceLoader, ClassFinder classFinder, RuntimeOptions runtimeOptions) { | ||
Reflections reflections = new Reflections(classFinder); | ||
TypeRegistryConfigurer typeRegistryConfigurer = reflections.instantiateExactlyOneSubclass(TypeRegistryConfigurer.class, MultiLoader.packageName(runtimeOptions.getGlue()), new Class[0], new Object[0], new DefaultTypeRegistryConfiguration()); | ||
TypeRegistry typeRegistry = new TypeRegistry(typeRegistryConfigurer.locale()); | ||
typeRegistryConfigurer.configureTypeRegistry(typeRegistry); | ||
return reflections.instantiateSubclasses(Backend.class, singletonList("cucumber.runtime"), new Class[]{ResourceLoader.class, TypeRegistry.class}, new Object[]{resourceLoader, typeRegistry}); | ||
} | ||
|
||
public interface BackendSupplier { | ||
Collection<? extends Backend> get(); | ||
} |
24 changes: 24 additions & 0 deletions
24
core/src/main/java/cucumber/runtime/FeaturePathFeatureSupplier.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package cucumber.runtime; | ||
|
||
import cucumber.runtime.model.CucumberFeature; | ||
import cucumber.runtime.model.FeatureLoader; | ||
|
||
import java.util.List; | ||
|
||
/** | ||
* Supplies a list of features found on the the feature path provided to RuntimeOptions. | ||
*/ | ||
public class FeaturePathFeatureSupplier implements FeatureSupplier { | ||
private final FeatureLoader featureLoader; | ||
private final RuntimeOptions runtimeOptions; | ||
|
||
public FeaturePathFeatureSupplier(FeatureLoader featureLoader, RuntimeOptions runtimeOptions) { | ||
this.featureLoader = featureLoader; | ||
this.runtimeOptions = runtimeOptions; | ||
} | ||
|
||
@Override | ||
public List<CucumberFeature> get() { | ||
return featureLoader.load(runtimeOptions.getFeaturePaths(), System.out); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,9 @@ | ||
package cucumber.runtime; | ||
|
||
import cucumber.runtime.model.CucumberFeature; | ||
import cucumber.runtime.model.FeatureLoader; | ||
|
||
import java.util.List; | ||
|
||
public class FeatureSupplier implements Supplier<List<CucumberFeature>> { | ||
private final FeatureLoader featureLoader; | ||
private final RuntimeOptions runtimeOptions; | ||
|
||
public FeatureSupplier(FeatureLoader featureLoader, RuntimeOptions runtimeOptions) { | ||
this.featureLoader = featureLoader; | ||
this.runtimeOptions = runtimeOptions; | ||
} | ||
|
||
@Override | ||
public List<CucumberFeature> get() { | ||
return featureLoader.load(runtimeOptions.getFeaturePaths(), System.out); | ||
} | ||
public interface FeatureSupplier { | ||
List<CucumberFeature> get(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package cucumber.runtime; | ||
|
||
public interface GlueSupplier { | ||
Glue get(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,47 +1,7 @@ | ||
package cucumber.runtime; | ||
|
||
import cucumber.runner.EventBus; | ||
import cucumber.runner.Runner; | ||
|
||
import java.util.Collection; | ||
|
||
public class RunnerSupplier implements Supplier<Runner> { | ||
|
||
private final Supplier<Collection<? extends Backend>> backendSupplier; | ||
private final RuntimeOptions runtimeOptions; | ||
private final Supplier<Glue> glueSupplier; | ||
private final EventBus eventBus; | ||
|
||
private final ThreadLocal<Runner> runners = new ThreadLocal<Runner>(); | ||
|
||
public RunnerSupplier( | ||
RuntimeOptions runtimeOptions, | ||
EventBus eventBus, | ||
Supplier<Collection<? extends Backend>> backendSupplier, | ||
Supplier<Glue> glueSupplier | ||
) { | ||
this.backendSupplier = backendSupplier; | ||
this.runtimeOptions = runtimeOptions; | ||
this.glueSupplier = glueSupplier; | ||
this.eventBus = eventBus; | ||
} | ||
|
||
@Override | ||
public Runner get() { | ||
Runner runner = runners.get(); | ||
if (runner == null) { | ||
runner = createRunner(); | ||
runners.set(runner); | ||
} | ||
return runner; | ||
} | ||
|
||
private Runner createRunner() { | ||
Collection<? extends Backend> backends = backendSupplier.get(); | ||
if (backends.isEmpty()) { | ||
throw new CucumberException("No backends were found. Please make sure you have a backend module on your CLASSPATH."); | ||
} | ||
return new Runner(glueSupplier.get(), eventBus.createBatchedEventBus(), backends, runtimeOptions); | ||
} | ||
|
||
public interface RunnerSupplier { | ||
Runner get(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
core/src/main/java/cucumber/runtime/ThreadLocalRunnerSupplier.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package cucumber.runtime; | ||
|
||
import cucumber.runner.EventBus; | ||
import cucumber.runner.Runner; | ||
|
||
/** | ||
* Returns a distinct runner for each calling thread. | ||
*/ | ||
public class ThreadLocalRunnerSupplier implements RunnerSupplier { | ||
|
||
private final BackendSupplier backendSupplier; | ||
private final RuntimeOptions runtimeOptions; | ||
private final GlueSupplier glueSupplier; | ||
private final EventBus eventBus; | ||
|
||
private final ThreadLocal<Runner> runners = new ThreadLocal<Runner>(); | ||
|
||
public ThreadLocalRunnerSupplier( | ||
RuntimeOptions runtimeOptions, | ||
EventBus eventBus, | ||
BackendSupplier backendSupplier, | ||
GlueSupplier glueSupplier | ||
) { | ||
this.backendSupplier = backendSupplier; | ||
this.runtimeOptions = runtimeOptions; | ||
this.glueSupplier = glueSupplier; | ||
this.eventBus = eventBus; | ||
} | ||
|
||
@Override | ||
public Runner get() { | ||
Runner runner = runners.get(); | ||
if (runner == null) { | ||
runner = createRunner(); | ||
runners.set(runner); | ||
} | ||
return runner; | ||
} | ||
|
||
private Runner createRunner() { | ||
return new Runner(glueSupplier.get(), eventBus.createBatchedEventBus(), backendSupplier.get(), runtimeOptions); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.