Skip to content

Commit a92d0f1

Browse files
committed
Add tests for Runner and Backend suppliers
1 parent 59eef86 commit a92d0f1

File tree

5 files changed

+151
-4
lines changed

5 files changed

+151
-4
lines changed

core/src/main/java/cucumber/api/cli/Main.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@
1818
import cucumber.runtime.io.ResourceLoaderClassFinder;
1919
import cucumber.runtime.model.FeatureLoader;
2020

21-
import java.util.ArrayList;
22-
2321
import static java.util.Arrays.asList;
2422

2523
public class Main {
@@ -37,7 +35,7 @@ public static void main(String[] argv) {
3735
* @return 0 if execution was successful, 1 if it was not (test failures)
3836
*/
3937
public static byte run(String[] argv, ClassLoader classLoader) {
40-
RuntimeOptions runtimeOptions = new RuntimeOptions(new ArrayList<String>(asList(argv)));
38+
RuntimeOptions runtimeOptions = new RuntimeOptions(asList(argv));
4139

4240
ResourceLoader resourceLoader = new MultiLoader(classLoader);
4341
ClassFinder classFinder = new ResourceLoaderClassFinder(resourceLoader, classLoader);

core/src/main/java/cucumber/runtime/RunnerSupplier.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,12 @@ public class RunnerSupplier implements Supplier<Runner> {
1414

1515
private final ThreadLocal<Runner> runners = new ThreadLocal<Runner>();
1616

17-
public RunnerSupplier(RuntimeOptions runtimeOptions, EventBus eventBus, Supplier<Collection<? extends Backend>> backendSupplier, Supplier<Glue> glueSupplier) {
17+
public RunnerSupplier(
18+
RuntimeOptions runtimeOptions,
19+
EventBus eventBus,
20+
Supplier<Collection<? extends Backend>> backendSupplier,
21+
Supplier<Glue> glueSupplier
22+
) {
1823
this.backendSupplier = backendSupplier;
1924
this.runtimeOptions = runtimeOptions;
2025
this.glueSupplier = glueSupplier;
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package cucumber.runtime;
2+
3+
import cucumber.runtime.io.MultiLoader;
4+
import cucumber.runtime.io.ResourceLoader;
5+
import cucumber.runtime.io.ResourceLoaderClassFinder;
6+
import org.junit.Before;
7+
import org.junit.Test;
8+
9+
import java.util.Collections;
10+
11+
import static org.hamcrest.CoreMatchers.is;
12+
import static org.hamcrest.CoreMatchers.notNullValue;
13+
import static org.junit.Assert.assertThat;
14+
15+
public class BackendSupplierTest {
16+
17+
private BackendSupplier backendSupplier;
18+
19+
@Before
20+
public void before() {
21+
ClassLoader classLoader = getClass().getClassLoader();
22+
RuntimeOptions runtimeOptions = new RuntimeOptions(Collections.<String>emptyList());
23+
ResourceLoader resourceLoader = new MultiLoader(classLoader);
24+
ClassFinder classFinder = new ResourceLoaderClassFinder(resourceLoader, classLoader);
25+
this.backendSupplier = new BackendSupplier(resourceLoader, classFinder, runtimeOptions);
26+
}
27+
28+
@Test
29+
public void should_create_a_backend() {
30+
assertThat(backendSupplier.get().iterator().next(), is(notNullValue()));
31+
}
32+
33+
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package cucumber.runtime;
2+
3+
4+
import cucumber.runner.EventBus;
5+
import cucumber.runner.Runner;
6+
import cucumber.runner.TimeService;
7+
import cucumber.runtime.io.MultiLoader;
8+
import cucumber.runtime.io.ResourceLoader;
9+
import cucumber.runtime.io.ResourceLoaderClassFinder;
10+
import org.junit.Before;
11+
import org.junit.Test;
12+
13+
import java.util.Collections;
14+
15+
import static org.hamcrest.CoreMatchers.is;
16+
import static org.hamcrest.CoreMatchers.notNullValue;
17+
import static org.junit.Assert.assertNotSame;
18+
import static org.junit.Assert.assertSame;
19+
import static org.junit.Assert.assertThat;
20+
21+
22+
public class RunnerSupplierTest {
23+
24+
private RunnerSupplier runnerSupplier;
25+
26+
@Before
27+
public void before() {
28+
ClassLoader classLoader = getClass().getClassLoader();
29+
RuntimeOptions runtimeOptions = new RuntimeOptions(Collections.<String>emptyList());
30+
ResourceLoader resourceLoader = new MultiLoader(classLoader);
31+
ClassFinder classFinder = new ResourceLoaderClassFinder(resourceLoader, classLoader);
32+
BackendSupplier backendSupplier = new BackendSupplier(resourceLoader, classFinder, runtimeOptions);
33+
EventBus eventBus = new EventBus(TimeService.SYSTEM);
34+
RuntimeGlueSupplier glueSupplier = new RuntimeGlueSupplier();
35+
runnerSupplier = new RunnerSupplier(runtimeOptions, eventBus, backendSupplier, glueSupplier);
36+
}
37+
38+
39+
@Test
40+
public void should_create_a_runner() {
41+
assertThat(runnerSupplier.get(), is(notNullValue()));
42+
}
43+
44+
@Test
45+
public void should_create_a_runner_per_thread() throws InterruptedException {
46+
final Runner[] runners = new Runner[2];
47+
Thread thread0 = new Thread(new Runnable() {
48+
@Override
49+
public void run() {
50+
runners[0] = runnerSupplier.get();
51+
}
52+
});
53+
54+
Thread thread1 = new Thread(new Runnable() {
55+
@Override
56+
public void run() {
57+
runners[1] = runnerSupplier.get();
58+
}
59+
});
60+
61+
thread0.start();
62+
thread1.start();
63+
64+
thread0.join();
65+
thread1.join();
66+
67+
assertNotSame(runners[0], runners[1]);
68+
}
69+
70+
@Test
71+
public void should_return_the_same_runner_on_subsequent_calls() {
72+
assertSame(runnerSupplier.get(), runnerSupplier.get());
73+
}
74+
75+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package cucumber.runtime;
2+
3+
import cucumber.runtime.io.ResourceLoader;
4+
import cucumber.runtime.snippets.FunctionNameGenerator;
5+
import gherkin.pickles.PickleStep;
6+
import io.cucumber.stepexpression.TypeRegistry;
7+
8+
import java.util.List;
9+
10+
public class StubBackend implements Backend {
11+
12+
@SuppressWarnings("unused") // reflection to create backend
13+
public StubBackend(ResourceLoader resourceLoader, TypeRegistry typeRegistry) {
14+
15+
}
16+
17+
@Override
18+
public void loadGlue(Glue glue, List<String> gluePaths) {
19+
20+
}
21+
22+
@Override
23+
public void buildWorld() {
24+
25+
}
26+
27+
@Override
28+
public void disposeWorld() {
29+
30+
}
31+
32+
@Override
33+
public String getSnippet(PickleStep step, String keyword, FunctionNameGenerator functionNameGenerator) {
34+
return null;
35+
}
36+
}

0 commit comments

Comments
 (0)