Skip to content

Commit

Permalink
Add tests for Runner and Backend suppliers
Browse files Browse the repository at this point in the history
  • Loading branch information
mpkorstanje committed Jun 5, 2018
1 parent 59eef86 commit a92d0f1
Show file tree
Hide file tree
Showing 5 changed files with 151 additions and 4 deletions.
4 changes: 1 addition & 3 deletions core/src/main/java/cucumber/api/cli/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@
import cucumber.runtime.io.ResourceLoaderClassFinder;
import cucumber.runtime.model.FeatureLoader;

import java.util.ArrayList;

import static java.util.Arrays.asList;

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

ResourceLoader resourceLoader = new MultiLoader(classLoader);
ClassFinder classFinder = new ResourceLoaderClassFinder(resourceLoader, classLoader);
Expand Down
7 changes: 6 additions & 1 deletion core/src/main/java/cucumber/runtime/RunnerSupplier.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,12 @@ public class RunnerSupplier implements Supplier<Runner> {

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

public RunnerSupplier(RuntimeOptions runtimeOptions, EventBus eventBus, Supplier<Collection<? extends Backend>> backendSupplier, Supplier<Glue> glueSupplier) {
public RunnerSupplier(
RuntimeOptions runtimeOptions,
EventBus eventBus,
Supplier<Collection<? extends Backend>> backendSupplier,
Supplier<Glue> glueSupplier
) {
this.backendSupplier = backendSupplier;
this.runtimeOptions = runtimeOptions;
this.glueSupplier = glueSupplier;
Expand Down
33 changes: 33 additions & 0 deletions core/src/test/java/cucumber/runtime/BackendSupplierTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package cucumber.runtime;

import cucumber.runtime.io.MultiLoader;
import cucumber.runtime.io.ResourceLoader;
import cucumber.runtime.io.ResourceLoaderClassFinder;
import org.junit.Before;
import org.junit.Test;

import java.util.Collections;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.notNullValue;
import static org.junit.Assert.assertThat;

public class BackendSupplierTest {

private BackendSupplier backendSupplier;

@Before
public void before() {
ClassLoader classLoader = getClass().getClassLoader();
RuntimeOptions runtimeOptions = new RuntimeOptions(Collections.<String>emptyList());
ResourceLoader resourceLoader = new MultiLoader(classLoader);
ClassFinder classFinder = new ResourceLoaderClassFinder(resourceLoader, classLoader);
this.backendSupplier = new BackendSupplier(resourceLoader, classFinder, runtimeOptions);
}

@Test
public void should_create_a_backend() {
assertThat(backendSupplier.get().iterator().next(), is(notNullValue()));
}

}
75 changes: 75 additions & 0 deletions core/src/test/java/cucumber/runtime/RunnerSupplierTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package cucumber.runtime;


import cucumber.runner.EventBus;
import cucumber.runner.Runner;
import cucumber.runner.TimeService;
import cucumber.runtime.io.MultiLoader;
import cucumber.runtime.io.ResourceLoader;
import cucumber.runtime.io.ResourceLoaderClassFinder;
import org.junit.Before;
import org.junit.Test;

import java.util.Collections;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.notNullValue;
import static org.junit.Assert.assertNotSame;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertThat;


public class RunnerSupplierTest {

private RunnerSupplier runnerSupplier;

@Before
public void before() {
ClassLoader classLoader = getClass().getClassLoader();
RuntimeOptions runtimeOptions = new RuntimeOptions(Collections.<String>emptyList());
ResourceLoader resourceLoader = new MultiLoader(classLoader);
ClassFinder classFinder = new ResourceLoaderClassFinder(resourceLoader, classLoader);
BackendSupplier backendSupplier = new BackendSupplier(resourceLoader, classFinder, runtimeOptions);
EventBus eventBus = new EventBus(TimeService.SYSTEM);
RuntimeGlueSupplier glueSupplier = new RuntimeGlueSupplier();
runnerSupplier = new RunnerSupplier(runtimeOptions, eventBus, backendSupplier, glueSupplier);
}


@Test
public void should_create_a_runner() {
assertThat(runnerSupplier.get(), is(notNullValue()));
}

@Test
public void should_create_a_runner_per_thread() throws InterruptedException {
final Runner[] runners = new Runner[2];
Thread thread0 = new Thread(new Runnable() {
@Override
public void run() {
runners[0] = runnerSupplier.get();
}
});

Thread thread1 = new Thread(new Runnable() {
@Override
public void run() {
runners[1] = runnerSupplier.get();
}
});

thread0.start();
thread1.start();

thread0.join();
thread1.join();

assertNotSame(runners[0], runners[1]);
}

@Test
public void should_return_the_same_runner_on_subsequent_calls() {
assertSame(runnerSupplier.get(), runnerSupplier.get());
}

}
36 changes: 36 additions & 0 deletions core/src/test/java/cucumber/runtime/StubBackend.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package cucumber.runtime;

import cucumber.runtime.io.ResourceLoader;
import cucumber.runtime.snippets.FunctionNameGenerator;
import gherkin.pickles.PickleStep;
import io.cucumber.stepexpression.TypeRegistry;

import java.util.List;

public class StubBackend implements Backend {

@SuppressWarnings("unused") // reflection to create backend
public StubBackend(ResourceLoader resourceLoader, TypeRegistry typeRegistry) {

}

@Override
public void loadGlue(Glue glue, List<String> gluePaths) {

}

@Override
public void buildWorld() {

}

@Override
public void disposeWorld() {

}

@Override
public String getSnippet(PickleStep step, String keyword, FunctionNameGenerator functionNameGenerator) {
return null;
}
}

0 comments on commit a92d0f1

Please sign in to comment.