Skip to content

Commit 95dddd5

Browse files
[Cdi2] Correctly cast the UnmanagedInstance values (#2244)
Co-authored-by: Daniel Beland <[email protected]> Co-authored-by: M.P. Korstanje <[email protected]>
1 parent 873cd45 commit 95dddd5

File tree

21 files changed

+101
-263
lines changed

21 files changed

+101
-263
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
1616
### Removed
1717

1818
### Fixed
19+
* [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)
1920

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

cdi2/src/main/java/io/cucumber/cdi2/Cdi2Factory.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,9 @@ public boolean addClass(final Class<?> clazz) {
5353

5454
@Override
5555
public <T> T getInstance(final Class<T> type) {
56-
final Object instance = standaloneInstances.get(type);
56+
final Unmanaged.UnmanagedInstance<?> instance = standaloneInstances.get(type);
5757
if (instance != null) {
58-
return type.cast(instance);
58+
return type.cast(instance.get());
5959
}
6060
final Instance<T> selected = container.select(type);
6161
if (selected.isUnsatisfied()) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package io.cucumber.cdi2;
2+
3+
import javax.enterprise.context.ApplicationScoped;
4+
5+
@ApplicationScoped
6+
public class ApplicationScopedBean {
7+
8+
}

cdi2/src/test/java/io/cucumber/cdi2/BellyStepDefinitions.java

-19
This file was deleted.

cdi2/src/test/java/io/cucumber/cdi2/Cdi2FactoryTest.java

+34-15
Original file line numberDiff line numberDiff line change
@@ -9,35 +9,54 @@
99
import static org.hamcrest.core.IsNot.not;
1010
import static org.hamcrest.core.IsNull.notNullValue;
1111
import static org.junit.jupiter.api.Assertions.assertAll;
12+
import static org.junit.jupiter.api.Assertions.assertNotNull;
1213

1314
class Cdi2FactoryTest {
1415

15-
@Test
16-
void shouldGiveUsNewInstancesForEachScenario() {
16+
final ObjectFactory factory = new Cdi2Factory();
1717

18-
final ObjectFactory factory = new Cdi2Factory();
19-
factory.addClass(BellyStepDefinitions.class);
20-
factory.addClass(CdiBellyStepDefinitions.class);
18+
@Test
19+
void shouldCreateNewInstancesForEachScenario() {
20+
factory.addClass(VetoedBean.class);
2121

2222
// Scenario 1
2323
factory.start();
24-
final BellyStepDefinitions o1 = factory.getInstance(BellyStepDefinitions.class);
25-
final CdiBellyStepDefinitions cdiStep = factory.getInstance(CdiBellyStepDefinitions.class);
26-
assertAll(
27-
// assert that it is is a CDI proxy
28-
() -> assertThat(cdiStep.getClass(), not(is(CdiBellyStepDefinitions.class))),
29-
() -> assertThat(cdiStep.getClass().getSuperclass(), is(CdiBellyStepDefinitions.class)));
24+
VetoedBean a1 = factory.getInstance(VetoedBean.class);
25+
VetoedBean a2 = factory.getInstance(VetoedBean.class);
26+
assertThat(a1, is(equalTo(a2)));
3027
factory.stop();
3128

3229
// Scenario 2
3330
factory.start();
34-
final BellyStepDefinitions o2 = factory.getInstance(BellyStepDefinitions.class);
31+
VetoedBean b1 = factory.getInstance(VetoedBean.class);
3532
factory.stop();
3633

34+
// VetoedBean makes it possible to compare the object outside the
35+
// scenario/application scope
36+
assertAll(
37+
() -> assertThat(a1, is(notNullValue())),
38+
() -> assertThat(a1, is(not(equalTo(b1)))),
39+
() -> assertThat(b1, is(not(equalTo(a1)))));
40+
}
41+
42+
@Test
43+
void shouldCreateApplicationScopedInstance() {
44+
factory.addClass(ApplicationScopedBean.class);
45+
factory.start();
46+
ApplicationScopedBean cdiStep = factory.getInstance(ApplicationScopedBean.class);
3747
assertAll(
38-
() -> assertThat(o1, is(notNullValue())),
39-
() -> assertThat(o1, is(not(equalTo(o2)))),
40-
() -> assertThat(o2, is(not(equalTo(o1)))));
48+
// assert that it is is a CDI proxy
49+
() -> assertThat(cdiStep.getClass(), not(is(ApplicationScopedBean.class))),
50+
() -> assertThat(cdiStep.getClass().getSuperclass(), is(ApplicationScopedBean.class)));
51+
factory.stop();
52+
}
53+
54+
@Test
55+
void shouldCreateUnmanagedInstance() {
56+
factory.addClass(UnmanagedBean.class);
57+
factory.start();
58+
assertNotNull(factory.getInstance(UnmanagedBean.class));
59+
factory.stop();
4160
}
4261

4362
}

cdi2/src/test/java/io/cucumber/cdi2/CdiBellyStepDefinitions.java

-21
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package io.cucumber.cdi2;
2+
3+
public class UnmanagedBean {
4+
5+
}

cdi2/src/test/java/io/cucumber/cdi2/UnusedGlue.java

-22
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package io.cucumber.cdi2;
2+
3+
import javax.enterprise.inject.Vetoed;
4+
5+
@Vetoed
6+
public class VetoedBean {
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package io.cucumber.cdi2.example;
2+
3+
import io.cucumber.cdi2.Belly;
4+
import io.cucumber.java.en.Given;
5+
import io.cucumber.java.en.Then;
6+
7+
import javax.inject.Inject;
8+
9+
import static org.junit.jupiter.api.Assertions.assertEquals;
10+
11+
public class BellyStepDefinitions {
12+
13+
@Inject
14+
private Belly belly;
15+
16+
@Given("I have {int} cukes in my belly")
17+
public void haveCukes(int n) {
18+
belly.setCukes(n);
19+
}
20+
21+
@Given("I eat {int} more cukes")
22+
public void addCukes(int n) {
23+
belly.setCukes(belly.getCukes() + n);
24+
}
25+
26+
@Then("there are {int} cukes in my belly")
27+
public void checkCukes(int n) {
28+
assertEquals(n, belly.getCukes());
29+
}
30+
31+
32+
}

cdi2/src/test/java/io/cucumber/cdi2/RunCucumberTest.java renamed to cdi2/src/test/java/io/cucumber/cdi2/example/RunCucumberTest.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package io.cucumber.cdi2;
1+
package io.cucumber.cdi2.example;
22

33
import io.cucumber.junit.Cucumber;
44
import org.junit.runner.RunWith;
+9-6
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
1-
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2-
xmlns="http://java.sun.com/xml/ns/javaee"
3-
xsi:schemaLocation="
4-
http://java.sun.com/xml/ns/javaee
5-
http://java.sun.com/xml/ns/javaee/beans_1_0.xsd">
6-
1+
<beans xmlns="http://xmlns.jcp.org/xml/ns/javaee"
2+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/beans_2_0.xsd"
4+
bean-discovery-mode="all" version="2.0">
5+
<!-- Exclude the class to force it to be loaded as an unmanaged dependency -->
6+
<scan>
7+
<exclude
8+
name="io.cucumber.cdi2.UnmanagedBean" />
9+
</scan>
710
</beans>

cdi2/src/test/resources/io/cucumber/cdi2/cukes.feature renamed to cdi2/src/test/resources/io/cucumber/cdi2/example/cukes.feature

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,5 @@ Feature: Cukes
66

77
Scenario: Eat some more cukes
88
Given I have 6 cukes in my belly
9-
Then there are 6 cukes in my belly
9+
And I eat 2 more cukes
10+
Then there are 8 cukes in my belly

deltaspike/src/test/java/io/cucumber/deltaspike/UnusedGlue.java

-22
This file was deleted.

guice/src/test/java/io/cucumber/guice/integration/UnusedGlue.java

-22
This file was deleted.

jakarta-cdi/src/test/java/io/cucumber/jakarta/cdi/UnusedGlue.java

-22
This file was deleted.

java/src/test/java/io/cucumber/java/annotation/UnusedGlue.java

-22
This file was deleted.

openejb/src/test/java/io/cucumber/openejb/UnusedGlue.java

-22
This file was deleted.

picocontainer/src/test/java/io/cucumber/picocontainer/UnusedGlue.java

-22
This file was deleted.

0 commit comments

Comments
 (0)