Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
mpkorstanje committed Nov 25, 2023
1 parent bb93d62 commit cf5667c
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 39 deletions.
13 changes: 5 additions & 8 deletions cucumber-guice/src/main/java/io/cucumber/guice/GuiceFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
import java.util.Collection;
import java.util.HashSet;

import static io.cucumber.guice.InjectorSourceFactory.createDefaultScenarioModuleInjectorSource;
import static io.cucumber.guice.InjectorSourceFactory.instantiateUserSpecifiedInjectorSource;
import static io.cucumber.guice.InjectorSourceFactory.loadInjectorSourceFromProperties;
import static java.lang.String.format;

Expand All @@ -31,7 +33,7 @@ public GuiceFactory() {
this.injectorSourceFromProperty = loadInjectorSourceFromProperties(CucumberProperties.create());
// Eager init to allow for static binding prior to before all hooks
if (this.injectorSourceFromProperty != null) {
injector = createInjector(this.injectorSourceFromProperty);
injector = instantiateUserSpecifiedInjectorSource(this.injectorSourceFromProperty).getInjector();

Check warning on line 36 in cucumber-guice/src/main/java/io/cucumber/guice/GuiceFactory.java

View check run for this annotation

Codecov / codecov/patch

cucumber-guice/src/main/java/io/cucumber/guice/GuiceFactory.java#L36

Added line #L36 was not covered by tests
}
}

Expand All @@ -46,7 +48,7 @@ public boolean addClass(final Class<?> stepClass) {
withInjectorSource = stepClass;
// Eager init to allow for static binding prior to before all
// hooks
injector = createInjector(withInjectorSource);
injector = instantiateUserSpecifiedInjectorSource(withInjectorSource).getInjector();
}
}
stepClasses.add(stepClass);
Expand Down Expand Up @@ -79,7 +81,7 @@ public void start() {
// Last minute init. Neither properties not annotations provided an
// injector source.
if (injector == null) {
injector = createInjector(null);
injector = createDefaultScenarioModuleInjectorSource().getInjector();
}
scenarioScope = injector.getInstance(ScenarioScope.class);
scenarioScope.enterScope();
Expand All @@ -96,9 +98,4 @@ public <T> T getInstance(Class<T> clazz) {
return injector.getInstance(clazz);
}

private static Injector createInjector(Class<?> injectorSourceClass) {
InjectorSourceFactory injectorSourceFactory = new InjectorSourceFactory(injectorSourceClass);
InjectorSource injectorSource = injectorSourceFactory.create();
return injectorSource.getInjector();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,25 +12,12 @@
final class InjectorSourceFactory {
private static final Logger log = LoggerFactory.getLogger(GuiceFactory.class);
static final String GUICE_INJECTOR_SOURCE_KEY = "guice.injector-source";
private final Class<?> injectorSourceClass;

InjectorSourceFactory(Class<?> injectorSourceClass) {
this.injectorSourceClass = injectorSourceClass;
}

InjectorSource create() {
if (injectorSourceClass == null) {
return createDefaultScenarioModuleInjectorSource();
} else {
return instantiateUserSpecifiedInjectorSource(injectorSourceClass);
}
}

private InjectorSource createDefaultScenarioModuleInjectorSource() {
static InjectorSource createDefaultScenarioModuleInjectorSource() {
return () -> Guice.createInjector(Stage.PRODUCTION, CucumberModules.createScenarioModule());
}

private InjectorSource instantiateUserSpecifiedInjectorSource(Class<?> injectorSourceClass) {
static InjectorSource instantiateUserSpecifiedInjectorSource(Class<?> injectorSourceClass) {
try {
return (InjectorSource) injectorSourceClass.getConstructor().newInstance();
} catch (Exception e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import java.util.Map;

import static io.cucumber.guice.InjectorSourceFactory.GUICE_INJECTOR_SOURCE_KEY;
import static org.hamcrest.CoreMatchers.instanceOf;
import static io.cucumber.guice.InjectorSourceFactory.instantiateUserSpecifiedInjectorSource;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.core.Is.is;
import static org.hamcrest.core.Is.isA;
Expand All @@ -18,12 +18,6 @@

class InjectorSourceFactoryTest {

@Test
void createsDefaultInjectorSourceWhenGuiceModulePropertyIsNotSet() {
InjectorSourceFactory injectorSourceFactory = new InjectorSourceFactory(null);
assertThat(injectorSourceFactory.create(), is(instanceOf(InjectorSource.class)));
}

@Test
void instantiatesInjectorSourceByFullyQualifiedName() {
Map<String, String> properties = new HashMap<>();
Expand All @@ -49,9 +43,7 @@ void failsToLoadNonExistantClass() {

@Test
void failsToInstantiateClassNotImplementingInjectorSource() {
InjectorSourceFactory injectorSourceFactory = new InjectorSourceFactory(String.class);

Executable testMethod = injectorSourceFactory::create;
Executable testMethod = () -> instantiateUserSpecifiedInjectorSource(String.class);
InjectorSourceInstantiationFailed actualThrown = assertThrows(InjectorSourceInstantiationFailed.class,
testMethod);
assertAll(
Expand All @@ -63,9 +55,7 @@ void failsToInstantiateClassNotImplementingInjectorSource() {

@Test
void failsToInstantiateClassWithPrivateConstructor() {
InjectorSourceFactory injectorSourceFactory = new InjectorSourceFactory(PrivateConstructor.class);

Executable testMethod = injectorSourceFactory::create;
Executable testMethod = () -> instantiateUserSpecifiedInjectorSource(PrivateConstructor.class);
InjectorSourceInstantiationFailed actualThrown = assertThrows(InjectorSourceInstantiationFailed.class,
testMethod);
assertAll(
Expand All @@ -77,9 +67,7 @@ void failsToInstantiateClassWithPrivateConstructor() {

@Test
void failsToInstantiateClassWithNoDefaultConstructor() {
InjectorSourceFactory injectorSourceFactory = new InjectorSourceFactory(NoDefaultConstructor.class);

Executable testMethod = injectorSourceFactory::create;
Executable testMethod = () -> instantiateUserSpecifiedInjectorSource(NoDefaultConstructor.class);
InjectorSourceInstantiationFailed actualThrown = assertThrows(InjectorSourceInstantiationFailed.class,
testMethod);
assertAll(
Expand Down

0 comments on commit cf5667c

Please sign in to comment.