Skip to content

[Cdi2] Correctly cast the UnmanagedInstance values #2244

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 7 commits into from
Feb 27, 2021
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
4 changes: 2 additions & 2 deletions cdi2/src/main/java/io/cucumber/cdi2/Cdi2Factory.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,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 Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package io.cucumber.cdi2;

import javax.enterprise.context.ApplicationScoped;

@ApplicationScoped
public class ApplicationScopedBean {

}
19 changes: 0 additions & 19 deletions cdi2/src/test/java/io/cucumber/cdi2/BellyStepDefinitions.java

This file was deleted.

49 changes: 34 additions & 15 deletions cdi2/src/test/java/io/cucumber/cdi2/Cdi2FactoryTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,35 +9,54 @@
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 Cdi2FactoryTest {

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

final ObjectFactory factory = new Cdi2Factory();
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)))));
}

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

}
21 changes: 0 additions & 21 deletions cdi2/src/test/java/io/cucumber/cdi2/CdiBellyStepDefinitions.java

This file was deleted.

5 changes: 5 additions & 0 deletions cdi2/src/test/java/io/cucumber/cdi2/UnmanagedBean.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package io.cucumber.cdi2;

public class UnmanagedBean {

}
22 changes: 0 additions & 22 deletions cdi2/src/test/java/io/cucumber/cdi2/UnusedGlue.java

This file was deleted.

7 changes: 7 additions & 0 deletions cdi2/src/test/java/io/cucumber/cdi2/VetoedBean.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package io.cucumber.cdi2;

import javax.enterprise.inject.Vetoed;

@Vetoed
public class VetoedBean {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package io.cucumber.cdi2.example;

import io.cucumber.cdi2.Belly;
import io.cucumber.java.en.Given;
import io.cucumber.java.en.Then;

import javax.inject.Inject;

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

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
@@ -1,4 +1,4 @@
package io.cucumber.cdi2;
package io.cucumber.cdi2.example;

import io.cucumber.junit.Cucumber;
import org.junit.runner.RunWith;
Expand Down
15 changes: 9 additions & 6 deletions cdi2/src/test/resources/META-INF/beans.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="
http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/beans_1_0.xsd">

<beans xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/beans_2_0.xsd"
bean-discovery-mode="all" version="2.0">
<!-- Exclude the class to force it to be loaded as an unmanaged dependency -->
<scan>
<exclude
name="io.cucumber.cdi2.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
22 changes: 0 additions & 22 deletions deltaspike/src/test/java/io/cucumber/deltaspike/UnusedGlue.java

This file was deleted.

22 changes: 0 additions & 22 deletions guice/src/test/java/io/cucumber/guice/integration/UnusedGlue.java

This file was deleted.

22 changes: 0 additions & 22 deletions jakarta-cdi/src/test/java/io/cucumber/jakarta/cdi/UnusedGlue.java

This file was deleted.

22 changes: 0 additions & 22 deletions java/src/test/java/io/cucumber/java/annotation/UnusedGlue.java

This file was deleted.

22 changes: 0 additions & 22 deletions openejb/src/test/java/io/cucumber/openejb/UnusedGlue.java

This file was deleted.

This file was deleted.

22 changes: 0 additions & 22 deletions spring/src/test/java/io/cucumber/spring/commonglue/UnusedGlue.java

This file was deleted.

Loading