Skip to content

Commit 13dcc3b

Browse files
committed
[Core] Pass class loader to ServiceLoader.load invocations
Fixes: #2217, #2219
1 parent 31c1361 commit 13dcc3b

File tree

6 files changed

+24
-20
lines changed

6 files changed

+24
-20
lines changed

core/src/main/java/io/cucumber/core/runtime/BackendServiceLoader.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ public BackendServiceLoader(
2929

3030
@Override
3131
public Collection<? extends Backend> get() {
32-
return get(ServiceLoader.load(BackendProviderService.class));
32+
ClassLoader classLoader = classLoaderSupplier.get();
33+
return get(ServiceLoader.load(BackendProviderService.class, classLoader));
3334
}
3435

3536
Collection<? extends Backend> get(Iterable<BackendProviderService> serviceLoader) {

core/src/main/java/io/cucumber/core/runtime/ObjectFactoryServiceLoader.java

+10-7
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,19 @@
99
import java.util.Iterator;
1010
import java.util.Map;
1111
import java.util.ServiceLoader;
12+
import java.util.function.Supplier;
1213
import java.util.stream.Collectors;
1314
import java.util.stream.Stream;
1415

1516
import static java.util.Objects.requireNonNull;
1617

1718
public final class ObjectFactoryServiceLoader {
1819

20+
private final Supplier<ClassLoader> classLoaderSupplier;
1921
private final Options options;
2022

21-
public ObjectFactoryServiceLoader(Options options) {
23+
public ObjectFactoryServiceLoader(Supplier<ClassLoader> classLoaderSupplier, Options options) {
24+
this.classLoaderSupplier = requireNonNull(classLoaderSupplier);
2225
this.options = requireNonNull(options);
2326
}
2427

@@ -38,9 +41,9 @@ public ObjectFactoryServiceLoader(Options options) {
3841
* @return an instance of {@link ObjectFactory}
3942
*/
4043
ObjectFactory loadObjectFactory() {
41-
Class<? extends ObjectFactory> objectFactoryClass = this.options.getObjectFactoryClass();
42-
43-
final ServiceLoader<ObjectFactory> loader = ServiceLoader.load(ObjectFactory.class);
44+
Class<? extends ObjectFactory> objectFactoryClass = options.getObjectFactoryClass();
45+
ClassLoader classLoader = classLoaderSupplier.get();
46+
ServiceLoader<ObjectFactory> loader = ServiceLoader.load(ObjectFactory.class, classLoader);
4447
if (objectFactoryClass == null) {
4548
return loadSingleObjectFactoryOrDefault(loader);
4649

@@ -50,7 +53,7 @@ ObjectFactory loadObjectFactory() {
5053
}
5154

5255
private static ObjectFactory loadSingleObjectFactoryOrDefault(ServiceLoader<ObjectFactory> loader) {
53-
final Iterator<ObjectFactory> objectFactories = loader.iterator();
56+
Iterator<ObjectFactory> objectFactories = loader.iterator();
5457

5558
ObjectFactory objectFactory;
5659
if (objectFactories.hasNext()) {
@@ -134,8 +137,8 @@ private <T> T cacheNewInstance(Class<T> type) {
134137
return instance;
135138
} catch (NoSuchMethodException e) {
136139
throw new CucumberException(String.format(
137-
"%s doesn't have an empty constructor. If you need dependency injection, put cucumber-picocontainer on the classpath",
138-
type), e);
140+
"%s doesn't have an empty constructor. If you need dependency injection, put cucumber-picocontainer on the classpath",
141+
type), e);
139142
} catch (Exception e) {
140143
throw new CucumberException(String.format("Failed to instantiate %s", type), e);
141144
}

core/src/main/java/io/cucumber/core/runtime/Runtime.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -157,8 +157,7 @@ public Builder withEventBus(final EventBus eventBus) {
157157
}
158158

159159
public Runtime build() {
160-
final ObjectFactoryServiceLoader objectFactoryServiceLoader = new ObjectFactoryServiceLoader(
161-
runtimeOptions);
160+
final ObjectFactoryServiceLoader objectFactoryServiceLoader = new ObjectFactoryServiceLoader(classLoader, runtimeOptions);
162161

163162
final ObjectFactorySupplier objectFactorySupplier = runtimeOptions.isMultiThreaded()
164163
? new ThreadLocalObjectFactorySupplier(objectFactoryServiceLoader)

core/src/test/java/io/cucumber/core/runtime/BackendServiceLoaderTest.java

+9-8
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
import org.junit.jupiter.api.Test;
66
import org.junit.jupiter.api.function.Executable;
77

8+
import java.util.function.Supplier;
9+
810
import static java.util.Collections.emptyList;
911
import static org.hamcrest.CoreMatchers.notNullValue;
1012
import static org.hamcrest.MatcherAssert.assertThat;
@@ -14,21 +16,20 @@
1416

1517
class BackendServiceLoaderTest {
1618

19+
final RuntimeOptions runtimeOptions = RuntimeOptions.defaultOptions();
20+
final Supplier<ClassLoader> classLoaderSupplier = this.getClass()::getClassLoader;
21+
final ObjectFactoryServiceLoader objectFactoryServiceLoader = new ObjectFactoryServiceLoader(classLoaderSupplier, runtimeOptions);
22+
final ObjectFactorySupplier objectFactory = new SingletonObjectFactorySupplier(objectFactoryServiceLoader);
23+
1724
@Test
1825
void should_create_a_backend() {
19-
RuntimeOptions runtimeOptions = RuntimeOptions.defaultOptions();
20-
ObjectFactoryServiceLoader objectFactoryServiceLoader = new ObjectFactoryServiceLoader(runtimeOptions);
21-
ObjectFactorySupplier objectFactory = new SingletonObjectFactorySupplier(objectFactoryServiceLoader);
22-
BackendSupplier backendSupplier = new BackendServiceLoader(getClass()::getClassLoader, objectFactory);
26+
BackendSupplier backendSupplier = new BackendServiceLoader(classLoaderSupplier, objectFactory);
2327
assertThat(backendSupplier.get().iterator().next(), is(notNullValue()));
2428
}
2529

2630
@Test
2731
void should_throw_an_exception_when_no_backend_could_be_found() {
28-
RuntimeOptions runtimeOptions = RuntimeOptions.defaultOptions();
29-
ObjectFactoryServiceLoader objectFactoryServiceLoader = new ObjectFactoryServiceLoader(runtimeOptions);
30-
ObjectFactorySupplier objectFactory = new SingletonObjectFactorySupplier(objectFactoryServiceLoader);
31-
BackendServiceLoader backendSupplier = new BackendServiceLoader(getClass()::getClassLoader, objectFactory);
32+
BackendServiceLoader backendSupplier = new BackendServiceLoader(classLoaderSupplier, objectFactory);
3233

3334
Executable testMethod = () -> backendSupplier.get(emptyList()).iterator().next();
3435
CucumberException actualThrown = assertThrows(CucumberException.class, testMethod);

core/src/test/java/io/cucumber/core/runtime/SingletonRunnerSupplierTest.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class SingletonRunnerSupplierTest {
2222
void before() {
2323
Supplier<ClassLoader> classLoader = SingletonRunnerSupplier.class::getClassLoader;
2424
RuntimeOptions runtimeOptions = RuntimeOptions.defaultOptions();
25-
ObjectFactoryServiceLoader objectFactoryServiceLoader = new ObjectFactoryServiceLoader(runtimeOptions);
25+
ObjectFactoryServiceLoader objectFactoryServiceLoader = new ObjectFactoryServiceLoader(classLoader, runtimeOptions);
2626
ObjectFactorySupplier objectFactory = new SingletonObjectFactorySupplier(objectFactoryServiceLoader);
2727
BackendServiceLoader backendSupplier = new BackendServiceLoader(getClass()::getClassLoader, objectFactory);
2828
EventBus eventBus = new TimeServiceEventBus(Clock.systemUTC(), UUID::randomUUID);

core/src/test/java/io/cucumber/core/runtime/ThreadLocalRunnerSupplierTest.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ class ThreadLocalRunnerSupplierTest {
3131
void before() {
3232
Supplier<ClassLoader> classLoader = ThreadLocalRunnerSupplierTest.class::getClassLoader;
3333
RuntimeOptions runtimeOptions = RuntimeOptions.defaultOptions();
34-
ObjectFactoryServiceLoader objectFactoryServiceLoader = new ObjectFactoryServiceLoader(runtimeOptions);
34+
ObjectFactoryServiceLoader objectFactoryServiceLoader = new ObjectFactoryServiceLoader(classLoader, runtimeOptions);
3535
ObjectFactorySupplier objectFactory = new SingletonObjectFactorySupplier(objectFactoryServiceLoader);
3636
BackendServiceLoader backendSupplier = new BackendServiceLoader(classLoader, objectFactory);
3737
eventBus = new TimeServiceEventBus(Clock.systemUTC(), UUID::randomUUID);

0 commit comments

Comments
 (0)