Skip to content

Commit

Permalink
[Jakarta Cdi] Correctly cast the UnmanagedInstance values
Browse files Browse the repository at this point in the history
Fixes: #2242
  • Loading branch information
mpkorstanje committed Feb 27, 2021
1 parent 609f284 commit a514cf8
Show file tree
Hide file tree
Showing 12 changed files with 105 additions and 51 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

### Fixed
* [Cdi2] Correctly cast the UnmanagedInstance values ([#2242](https://github.com/cucumber/cucumber-jvm/pull/2242), [#2244](https://github.com/cucumber/cucumber-jvm/pull/2244) Daniel Beland)
* [Cdi2] Correctly cast the UnmanagedInstance values ([#2242](https://github.com/cucumber/cucumber-jvm/pull/2242), [#2248](https://github.com/cucumber/cucumber-jvm/pull/2248) Daniel Beland)

## [6.10.0] (2021-02-14)

Expand Down
6 changes: 3 additions & 3 deletions jakarta-cdi/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@
</dependency>
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-junit</artifactId>
<artifactId>cucumber-junit-platform-engine</artifactId>
<scope>test</scope>
</dependency>
<dependency>
Expand All @@ -109,8 +109,8 @@
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-core</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,9 @@ public boolean addClass(final Class<?> clazz) {

@Override
public <T> T getInstance(final Class<T> type) {
final Object instance = standaloneInstances.get(type);
final Unmanaged.UnmanagedInstance<?> instance = standaloneInstances.get(type);
if (instance != null) {
return type.cast(instance);
return type.cast(instance.get());
}
final Instance<T> selected = container.select(type);
if (selected.isUnsatisfied()) {
Expand All @@ -69,5 +69,4 @@ public <T> T getInstance(final Class<T> type) {
}
return selected.get();
}

}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.cucumber.jakarta.cdi;

import io.cucumber.jakarta.cdi.example.Belly;
import io.cucumber.java.en.Then;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package io.cucumber.jakarta.cdi;

import io.cucumber.core.backend.ObjectFactory;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.enterprise.inject.Vetoed;
import org.junit.jupiter.api.Test;

import static org.hamcrest.MatcherAssert.assertThat;
Expand All @@ -9,35 +11,69 @@
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;

class CdiJakartaFactoryTest {

@Test
void shouldGiveUsNewInstancesForEachScenario() {
final ObjectFactory factory = new CdiJakartaFactory();

@Vetoed
static class VetoedBean {

}

final ObjectFactory factory = new CdiJakartaFactory();
factory.addClass(BellyStepDefinitions.class);
factory.addClass(CdiBellyStepDefinitions.class);
@Test
void shouldCreateNewInstancesForEachScenario() {
factory.addClass(VetoedBean.class);

// Scenario 1
factory.start();
final BellyStepDefinitions o1 = factory.getInstance(BellyStepDefinitions.class);
final CdiBellyStepDefinitions cdiStep = factory.getInstance(CdiBellyStepDefinitions.class);
assertAll(
// assert that it is is a CDI proxy
() -> assertThat(cdiStep.getClass(), not(is(CdiBellyStepDefinitions.class))),
() -> assertThat(cdiStep.getClass().getSuperclass(), is(CdiBellyStepDefinitions.class)));
VetoedBean a1 = factory.getInstance(VetoedBean.class);
VetoedBean a2 = factory.getInstance(VetoedBean.class);
assertThat(a1, is(equalTo(a2)));
factory.stop();

// Scenario 2
factory.start();
final BellyStepDefinitions o2 = factory.getInstance(BellyStepDefinitions.class);
VetoedBean b1 = factory.getInstance(VetoedBean.class);
factory.stop();

// VetoedBean makes it possible to compare the object outside the
// scenario/application scope
assertAll(
() -> assertThat(a1, is(notNullValue())),
() -> assertThat(a1, is(not(equalTo(b1)))),
() -> assertThat(b1, is(not(equalTo(a1)))));
}

@ApplicationScoped
static class ApplicationScopedBean {

}

@Test
void shouldCreateApplicationScopedInstance() {
factory.addClass(ApplicationScopedBean.class);
factory.start();
ApplicationScopedBean cdiStep = factory.getInstance(ApplicationScopedBean.class);
assertAll(
() -> assertThat(o1, is(notNullValue())),
() -> assertThat(o1, is(not(equalTo(o2)))),
() -> assertThat(o2, is(not(equalTo(o1)))));
// assert that it is is a CDI proxy
() -> assertThat(cdiStep.getClass(), not(is(ApplicationScopedBean.class))),
() -> assertThat(cdiStep.getClass().getSuperclass(), is(ApplicationScopedBean.class)));
factory.stop();
}

@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));
factory.stop();
}

static class UnmanagedBean {

}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.cucumber.jakarta.cdi;
package io.cucumber.jakarta.cdi.example;

import jakarta.enterprise.context.ApplicationScoped;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package io.cucumber.jakarta.cdi.example;

import io.cucumber.java.en.Given;
import io.cucumber.java.en.Then;
import jakarta.enterprise.inject.Vetoed;
import jakarta.inject.Inject;

import static org.junit.jupiter.api.Assertions.assertEquals;

@Vetoed
public class BellyStepDefinitions {

@Inject
private Belly belly;

@Given("I have {int} cukes in my belly")
public void haveCukes(int n) {
belly.setCukes(n);
}

@Given("I eat {int} more cukes")
public void addCukes(int n) {
belly.setCukes(belly.getCukes() + n);
}

@Then("there are {int} cukes in my belly")
public void checkCukes(int n) {
assertEquals(n, belly.getCukes());
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package io.cucumber.jakarta.cdi.example;

import io.cucumber.junit.platform.engine.Cucumber;

@Cucumber
public class RunCucumberTest {

}
6 changes: 5 additions & 1 deletion jakarta-cdi/src/test/resources/META-INF/beans.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,9 @@
xsi:schemaLocation="
http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/beans_1_0.xsd">

<!-- Exclude the class to force it to be loaded as an unmanaged dependency -->
<scan>
<exclude
name="io.cucumber.jakarta.cdi.CdiJakartaFactoryTest$UnmanagedBean" />
</scan>
</beans>
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ Feature: Cukes

Scenario: Eat some more cukes
Given I have 6 cukes in my belly
Then there are 6 cukes in my belly
And I eat 2 more cukes
Then there are 8 cukes in my belly

0 comments on commit a514cf8

Please sign in to comment.