Skip to content

Commit

Permalink
[Cdi2] Do not add step definitions to the synthetic bean archive
Browse files Browse the repository at this point in the history
Fixes: #2241
  • Loading branch information
mpkorstanje committed Feb 27, 2021
1 parent 65a6882 commit 44e7ca3
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 27 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
2 changes: 1 addition & 1 deletion cdi2/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ And for Weld it is:
<dependency>
<groupId>org.jboss.weld.se</groupId>
<artifactId>weld-se-core</artifactId>
<version>3.1.1.Final</version>
<version>4.0.0.Final</version>
<scope>test</scope>
</dependency>
```
Expand Down
21 changes: 7 additions & 14 deletions cdi2/src/main/java/io/cucumber/cdi2/Cdi2Factory.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,48 +16,41 @@
public final class Cdi2Factory implements ObjectFactory {

private final Map<Class<?>, 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
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> T getInstance(final Class<T> type) {
final Unmanaged.UnmanagedInstance<?> instance = standaloneInstances.get(type);
Unmanaged.UnmanagedInstance<?> instance = standaloneInstances.get(type);
if (instance != null) {
return type.cast(instance.get());
}
final Instance<T> selected = container.select(type);
Instance<T> selected = container.select(type);
if (selected.isUnsatisfied()) {
BeanManager beanManager = container.getBeanManager();
Unmanaged<T> unmanaged = new Unmanaged<>(beanManager, type);
Expand Down
56 changes: 44 additions & 12 deletions cdi2/src/test/java/io/cucumber/cdi2/Cdi2FactoryTest.java
Original file line number Diff line number Diff line change
@@ -1,32 +1,46 @@
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;
import static org.hamcrest.core.IsEqual.equalTo;
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 {

}

@Test
void shouldCreateNewInstancesForEachScenario() {
factory.addClass(VetoedBean.class);

// Scenario 1
factory.start();
VetoedBean a1 = factory.getInstance(VetoedBean.class);
Expand Down Expand Up @@ -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();
}

}

0 comments on commit 44e7ca3

Please sign in to comment.