Skip to content

[Spring] Share application context #1848

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 12 commits into from
Dec 21, 2019
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
127 changes: 0 additions & 127 deletions spring/src/main/java/io/cucumber/spring/FixBootstrapUtils.java

This file was deleted.

41 changes: 21 additions & 20 deletions spring/src/main/java/io/cucumber/spring/SpringFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@
import java.util.Set;

import static io.cucumber.spring.CucumberTestContext.SCOPE_CUCUMBER_GLUE;
import static io.cucumber.spring.FixBootstrapUtils.createBootstrapContext;
import static io.cucumber.spring.FixBootstrapUtils.resolveTestContextBootstrapper;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed the FixBootstrapUtils. Wasn't needed any more.

import static java.util.Arrays.asList;

/**
Expand Down Expand Up @@ -65,6 +63,8 @@
@API(status = API.Status.STABLE)
public final class SpringFactory implements ObjectFactory {

private static final Object monitor = new Object();

private ConfigurableListableBeanFactory beanFactory;
private CucumberTestContextManager testContextManager;

Expand Down Expand Up @@ -129,21 +129,24 @@ private static boolean hasAnnotation(Annotation annotation, Collection<Class<? e

@Override
public void start() {
if (stepClassWithSpringContext != null) {
testContextManager = new CucumberTestContextManager(stepClassWithSpringContext);
} else {
if (beanFactory == null) {
beanFactory = createFallbackContext();
synchronized (monitor) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Replaced the SpringFactory.class with monitor. The class is accessible by any one. And while I don't expect any one to use it to synchronize on it, there also no need to do so.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also explained the why of it all.

if (stepClassWithSpringContext != null) {
testContextManager = new CucumberTestContextManager(stepClassWithSpringContext);
} else {
if (beanFactory == null) {
beanFactory = createFallbackContext();
}
}
}
notifyContextManagerAboutTestClassStarted();
if (beanFactory == null || isNewContextCreated()) {
beanFactory = testContextManager.getBeanFactory();
for (Class<?> stepClass : stepClasses) {
registerStepClassBeanDefinition(beanFactory, stepClass);
notifyContextManagerAboutTestClassStarted();
if (beanFactory == null || isNewContextCreated()) {
beanFactory = testContextManager.getBeanFactory();
for (Class<?> stepClass : stepClasses) {
registerStepClassBeanDefinition(beanFactory, stepClass);
}
}
GlueCodeContext.getInstance().start();
}
GlueCodeContext.getInstance().start();

}

@SuppressWarnings("resource")
Expand Down Expand Up @@ -183,9 +186,9 @@ private boolean isNewContextCreated() {
private void registerStepClassBeanDefinition(ConfigurableListableBeanFactory beanFactory, Class<?> stepClass) {
BeanDefinitionRegistry registry = (BeanDefinitionRegistry) beanFactory;
BeanDefinition beanDefinition = BeanDefinitionBuilder
.genericBeanDefinition(stepClass)
.setScope(SCOPE_CUCUMBER_GLUE)
.getBeanDefinition();
.genericBeanDefinition(stepClass)
.setScope(SCOPE_CUCUMBER_GLUE)
.getBeanDefinition();
registry.registerBeanDefinition(stepClass.getName(), beanDefinition);
}

Expand Down Expand Up @@ -233,9 +236,7 @@ private static boolean annotatedWithSupportedSpringRootTestAnnotations(Annotatio
static class CucumberTestContextManager extends TestContextManager {

CucumberTestContextManager(Class<?> testClass) {
// Does the same as TestContextManager(Class<?>) but creates a
// DefaultCacheAwareContextLoaderDelegate that uses a thread local contextCache.
super(resolveTestContextBootstrapper(createBootstrapContext(testClass)));
super(testClass);
registerGlueCodeScope(getContext());
}

Expand Down