diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 97c330a625..62d2814639 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -30,6 +30,8 @@ jobs: run: mvn install -DskipTests=true -DskipITs=true -Darchetype.test.skip=true -Dmaven.javadoc.skip=true -B -V --toolchains .github/workflows/.toolchains.xml - name: test run: mvn verify -P-spotless-apply --toolchains .github/workflows/.toolchains.xml + - name: test cdi2-weld + run: cd cdi2 && mvn verify -Pcdi2-weld --toolchains ../.github/workflows/.toolchains.xml javadoc: name: 'Javadoc' diff --git a/cdi2/README.md b/cdi2/README.md index b8d2ac9d12..1f8ab9772e 100644 --- a/cdi2/README.md +++ b/cdi2/README.md @@ -27,7 +27,7 @@ And for Weld it is: org.jboss.weld.se weld-se-core - 3.1.1.Final + 4.0.0.Final test ``` diff --git a/cdi2/src/main/java/io/cucumber/cdi2/Cdi2Factory.java b/cdi2/src/main/java/io/cucumber/cdi2/Cdi2Factory.java index ec15e0bf22..527cd77776 100644 --- a/cdi2/src/main/java/io/cucumber/cdi2/Cdi2Factory.java +++ b/cdi2/src/main/java/io/cucumber/cdi2/Cdi2Factory.java @@ -16,12 +16,14 @@ public final class Cdi2Factory implements ObjectFactory { private final Map, Unmanaged.UnmanagedInstance> standaloneInstances = new HashMap<>(); - private SeContainerInitializer initializer; private SeContainer container; @Override public void start() { - container = getInitializer().initialize(); + if (container == null) { + SeContainerInitializer initializer = SeContainerInitializer.newInstance(); + container = initializer.initialize(); + } } @Override @@ -29,35 +31,26 @@ public void stop() { if (container != null) { container.close(); container = null; - initializer = null; } - for (final Unmanaged.UnmanagedInstance unmanaged : standaloneInstances.values()) { + for (Unmanaged.UnmanagedInstance unmanaged : standaloneInstances.values()) { unmanaged.preDestroy(); unmanaged.dispose(); } standaloneInstances.clear(); } - private SeContainerInitializer getInitializer() { - if (initializer == null) { - initializer = SeContainerInitializer.newInstance(); - } - return initializer; - } - @Override public boolean addClass(final Class clazz) { - getInitializer().addBeanClasses(clazz); return true; } @Override public T getInstance(final Class type) { - final Unmanaged.UnmanagedInstance instance = standaloneInstances.get(type); + Unmanaged.UnmanagedInstance instance = standaloneInstances.get(type); if (instance != null) { return type.cast(instance.get()); } - final Instance selected = container.select(type); + Instance selected = container.select(type); if (selected.isUnsatisfied()) { BeanManager beanManager = container.getBeanManager(); Unmanaged unmanaged = new Unmanaged<>(beanManager, type); diff --git a/cdi2/src/test/java/io/cucumber/cdi2/Cdi2FactoryTest.java b/cdi2/src/test/java/io/cucumber/cdi2/Cdi2FactoryTest.java index c3aea7135d..d9ef740135 100644 --- a/cdi2/src/test/java/io/cucumber/cdi2/Cdi2FactoryTest.java +++ b/cdi2/src/test/java/io/cucumber/cdi2/Cdi2FactoryTest.java @@ -1,10 +1,12 @@ package io.cucumber.cdi2; import io.cucumber.core.backend.ObjectFactory; +import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.Test; import javax.enterprise.context.ApplicationScoped; import javax.enterprise.inject.Vetoed; +import javax.inject.Inject; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.core.Is.is; @@ -12,12 +14,26 @@ import static org.hamcrest.core.IsNot.not; import static org.hamcrest.core.IsNull.notNullValue; import static org.junit.jupiter.api.Assertions.assertAll; -import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; class Cdi2FactoryTest { final ObjectFactory factory = new Cdi2Factory(); + @AfterEach + void stop(){ + factory.stop(); + } + + @Test + void lifecycleIsIdempotent(){ + assertDoesNotThrow(factory::stop); + factory.start(); + assertDoesNotThrow(factory::start); + factory.stop(); + assertDoesNotThrow(factory::stop); + } + @Vetoed static class VetoedBean { @@ -25,8 +41,6 @@ static class VetoedBean { @Test void shouldCreateNewInstancesForEachScenario() { - factory.addClass(VetoedBean.class); - // Scenario 1 factory.start(); VetoedBean a1 = factory.getInstance(VetoedBean.class); @@ -54,28 +68,46 @@ static class ApplicationScopedBean { @Test void shouldCreateApplicationScopedInstance() { - factory.addClass(ApplicationScopedBean.class); factory.start(); - ApplicationScopedBean cdiStep = factory.getInstance(ApplicationScopedBean.class); + ApplicationScopedBean bean = factory.getInstance(ApplicationScopedBean.class); assertAll( // assert that it is is a CDI proxy - () -> assertThat(cdiStep.getClass(), not(is(ApplicationScopedBean.class))), - () -> assertThat(cdiStep.getClass().getSuperclass(), is(ApplicationScopedBean.class))); + () -> assertThat(bean.getClass(), not(is(ApplicationScopedBean.class))), + () -> assertThat(bean.getClass().getSuperclass(), is(ApplicationScopedBean.class))); factory.stop(); } + static class UnmanagedBean { + + } + @Test void shouldCreateUnmanagedInstance() { - factory.addClass(UnmanagedBean.class); factory.start(); - assertNotNull(factory.getInstance(UnmanagedBean.class)); - UnmanagedBean cdiStep = factory.getInstance(UnmanagedBean.class); - assertThat(cdiStep.getClass(), is(UnmanagedBean.class)); + UnmanagedBean bean = factory.getInstance(UnmanagedBean.class); + assertThat(bean.getClass(), is(UnmanagedBean.class)); factory.stop(); } - static class UnmanagedBean { + static class OtherStepDefinitions { + + } + + static class StepDefinitions { + + @Inject + OtherStepDefinitions injected; } + @Test + void shouldInjectStepDefinitions() { + factory.addClass(OtherStepDefinitions.class); + factory.addClass(StepDefinitions.class); + factory.start(); + StepDefinitions stepDefinitions = factory.getInstance(StepDefinitions.class); + assertThat(stepDefinitions.injected, is(notNullValue())); + factory.stop(); + } + }