Skip to content

Commit cdab6a9

Browse files
authored
[Jakarta Cdi] Correctly cast the UnmanagedInstance values (#2250)
Fixes: #2242
1 parent 609f284 commit cdab6a9

File tree

13 files changed

+192
-128
lines changed

13 files changed

+192
-128
lines changed

CHANGELOG.md

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

1818
### Fixed
1919
* [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)
20+
* [Jakarta Cdi] 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)
2021

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

jakarta-cdi/README.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ And for Weld it is:
7373
<dependency>
7474
<groupId>org.jboss.weld.se</groupId>
7575
<artifactId>weld-se-core</artifactId>
76-
<version>4.0.0.Alpha2</version>
76+
<version>4.0.0</version>
7777
<scope>test</scope>
7878
</dependency>
7979
```
@@ -85,13 +85,13 @@ If you don't know which one to use, you can import the following one but if you
8585
<dependency>
8686
<groupId>jakarta.enterprise</groupId>
8787
<artifactId>jakarta.enterprise.cdi-api</artifactId>
88-
<version>3.0.0-M3</version>
88+
<version>3.0.0</version>
8989
<scope>provided</scope>
9090
</dependency>
9191
<dependency>
9292
<groupId>jakarta.activation</groupId>
9393
<artifactId>jakarta.activation-api</artifactId>
94-
<version>2.0.0-RC3</version>
94+
<version>2.0.0</version>
9595
<scope>provided</scope>
9696
</dependency>
9797
```

jakarta-cdi/pom.xml

+88-57
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44
<properties>
55
<project.Automatic-Module-Name>io.cucumber.cdi.jakarta</project.Automatic-Module-Name>
66
<openwebbeans.version>2.0.21</openwebbeans.version>
7-
<jakarta.enterprise.cdi-api.version>3.0.0-M3</jakarta.enterprise.cdi-api.version>
7+
<jakarta.enterprise.cdi-api.version>3.0.0</jakarta.enterprise.cdi-api.version>
88
<jakarta.activation-api.version>2.0.0</jakarta.activation-api.version>
9-
<xbean.version>4.18</xbean.version>
9+
<weld.version>4.0.0.Final</weld.version>
1010
</properties>
1111

1212
<parent>
@@ -41,66 +41,14 @@
4141
<version>${jakarta.activation-api.version}</version>
4242
<scope>provided</scope>
4343
</dependency>
44-
45-
<dependency>
46-
<groupId>org.apache.xbean</groupId>
47-
<artifactId>xbean-finder-shaded</artifactId>
48-
<version>${xbean.version}</version>
49-
<scope>test</scope>
50-
</dependency>
51-
<dependency>
52-
<groupId>org.apache.xbean</groupId>
53-
<artifactId>xbean-asm9-shaded</artifactId>
54-
<version>${xbean.version}</version>
55-
<scope>test</scope>
56-
</dependency>
57-
<dependency>
58-
<groupId>org.apache.openwebbeans</groupId>
59-
<artifactId>openwebbeans-impl</artifactId>
60-
<version>${openwebbeans.version}</version>
61-
<scope>test</scope>
62-
<classifier>jakarta</classifier>
63-
<exclusions>
64-
<exclusion>
65-
<groupId>*</groupId>
66-
<artifactId>*</artifactId>
67-
</exclusion>
68-
</exclusions>
69-
</dependency>
70-
<dependency>
71-
<groupId>org.apache.openwebbeans</groupId>
72-
<artifactId>openwebbeans-spi</artifactId>
73-
<version>${openwebbeans.version}</version>
74-
<scope>test</scope>
75-
<classifier>jakarta</classifier>
76-
<exclusions>
77-
<exclusion>
78-
<groupId>*</groupId>
79-
<artifactId>*</artifactId>
80-
</exclusion>
81-
</exclusions>
82-
</dependency>
83-
<dependency>
84-
<groupId>org.apache.openwebbeans</groupId>
85-
<artifactId>openwebbeans-se</artifactId>
86-
<version>${openwebbeans.version}</version>
87-
<classifier>jakarta</classifier>
88-
<scope>test</scope>
89-
<exclusions>
90-
<exclusion>
91-
<groupId>*</groupId>
92-
<artifactId>*</artifactId>
93-
</exclusion>
94-
</exclusions>
95-
</dependency>
9644
<dependency>
9745
<groupId>io.cucumber</groupId>
9846
<artifactId>cucumber-java</artifactId>
9947
<scope>test</scope>
10048
</dependency>
10149
<dependency>
10250
<groupId>io.cucumber</groupId>
103-
<artifactId>cucumber-junit</artifactId>
51+
<artifactId>cucumber-junit-platform-engine</artifactId>
10452
<scope>test</scope>
10553
</dependency>
10654
<dependency>
@@ -109,9 +57,92 @@
10957
<scope>test</scope>
11058
</dependency>
11159
<dependency>
112-
<groupId>org.junit.vintage</groupId>
113-
<artifactId>junit-vintage-engine</artifactId>
60+
<groupId>org.hamcrest</groupId>
61+
<artifactId>hamcrest-core</artifactId>
11462
<scope>test</scope>
11563
</dependency>
11664
</dependencies>
65+
66+
<profiles>
67+
<profile>
68+
<id>jakarta-cdi-openwebbeans</id>
69+
<activation>
70+
<activeByDefault>true</activeByDefault>
71+
</activation>
72+
<properties>
73+
<xbean.version>4.18</xbean.version>
74+
</properties>
75+
<dependencies>
76+
<dependency>
77+
<groupId>org.apache.openwebbeans</groupId>
78+
<artifactId>openwebbeans-se</artifactId>
79+
<version>${openwebbeans.version}</version>
80+
<classifier>jakarta</classifier>
81+
<scope>test</scope>
82+
<exclusions>
83+
<exclusion>
84+
<groupId>*</groupId>
85+
<artifactId>*</artifactId>
86+
</exclusion>
87+
</exclusions>
88+
</dependency>
89+
<dependency>
90+
<groupId>org.apache.openwebbeans</groupId>
91+
<artifactId>openwebbeans-impl</artifactId>
92+
<version>${openwebbeans.version}</version>
93+
<scope>test</scope>
94+
<classifier>jakarta</classifier>
95+
<exclusions>
96+
<exclusion>
97+
<groupId>*</groupId>
98+
<artifactId>*</artifactId>
99+
</exclusion>
100+
</exclusions>
101+
</dependency>
102+
<dependency>
103+
<groupId>org.apache.openwebbeans</groupId>
104+
<artifactId>openwebbeans-spi</artifactId>
105+
<version>${openwebbeans.version}</version>
106+
<scope>test</scope>
107+
<classifier>jakarta</classifier>
108+
<exclusions>
109+
<exclusion>
110+
<groupId>*</groupId>
111+
<artifactId>*</artifactId>
112+
</exclusion>
113+
</exclusions>
114+
</dependency>
115+
<dependency>
116+
<groupId>org.apache.xbean</groupId>
117+
<artifactId>xbean-finder-shaded</artifactId>
118+
<version>${xbean.version}</version>
119+
<scope>test</scope>
120+
</dependency>
121+
<dependency>
122+
<groupId>org.apache.xbean</groupId>
123+
<artifactId>xbean-asm9-shaded</artifactId>
124+
<version>${xbean.version}</version>
125+
<scope>test</scope>
126+
</dependency>
127+
</dependencies>
128+
</profile>
129+
130+
<profile>
131+
<id>jakarta-cdi-weld</id>
132+
<dependencies>
133+
<dependency>
134+
<groupId>org.jboss.weld.se</groupId>
135+
<artifactId>weld-se-core</artifactId>
136+
<version>${weld.version}</version>
137+
<scope>test</scope>
138+
</dependency>
139+
<dependency>
140+
<groupId>org.jboss.weld</groupId>
141+
<artifactId>weld-core-impl</artifactId>
142+
<version>${weld.version}</version>
143+
<scope>test</scope>
144+
</dependency>
145+
</dependencies>
146+
</profile>
147+
</profiles>
117148
</project>

jakarta-cdi/src/main/java/io/cucumber/jakarta/cdi/CdiJakartaFactory.java

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

5353
@Override
5454
public <T> T getInstance(final Class<T> type) {
55-
final Object instance = standaloneInstances.get(type);
55+
final Unmanaged.UnmanagedInstance<?> instance = standaloneInstances.get(type);
5656
if (instance != null) {
57-
return type.cast(instance);
57+
return type.cast(instance.get());
5858
}
5959
final Instance<T> selected = container.select(type);
6060
if (selected.isUnsatisfied()) {
@@ -69,5 +69,4 @@ public <T> T getInstance(final Class<T> type) {
6969
}
7070
return selected.get();
7171
}
72-
7372
}

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

-18
This file was deleted.

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

-20
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package io.cucumber.jakarta.cdi;
22

33
import io.cucumber.core.backend.ObjectFactory;
4+
import jakarta.enterprise.context.ApplicationScoped;
5+
import jakarta.enterprise.inject.Vetoed;
46
import org.junit.jupiter.api.Test;
57

68
import static org.hamcrest.MatcherAssert.assertThat;
@@ -9,35 +11,69 @@
911
import static org.hamcrest.core.IsNot.not;
1012
import static org.hamcrest.core.IsNull.notNullValue;
1113
import static org.junit.jupiter.api.Assertions.assertAll;
14+
import static org.junit.jupiter.api.Assertions.assertNotNull;
1215

1316
class CdiJakartaFactoryTest {
1417

15-
@Test
16-
void shouldGiveUsNewInstancesForEachScenario() {
18+
final ObjectFactory factory = new CdiJakartaFactory();
19+
20+
@Vetoed
21+
static class VetoedBean {
22+
23+
}
1724

18-
final ObjectFactory factory = new CdiJakartaFactory();
19-
factory.addClass(BellyStepDefinitions.class);
20-
factory.addClass(CdiBellyStepDefinitions.class);
25+
@Test
26+
void shouldCreateNewInstancesForEachScenario() {
27+
factory.addClass(VetoedBean.class);
2128

2229
// Scenario 1
2330
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)));
31+
VetoedBean a1 = factory.getInstance(VetoedBean.class);
32+
VetoedBean a2 = factory.getInstance(VetoedBean.class);
33+
assertThat(a1, is(equalTo(a2)));
3034
factory.stop();
3135

3236
// Scenario 2
3337
factory.start();
34-
final BellyStepDefinitions o2 = factory.getInstance(BellyStepDefinitions.class);
38+
VetoedBean b1 = factory.getInstance(VetoedBean.class);
3539
factory.stop();
3640

41+
// VetoedBean makes it possible to compare the object outside the
42+
// scenario/application scope
43+
assertAll(
44+
() -> assertThat(a1, is(notNullValue())),
45+
() -> assertThat(a1, is(not(equalTo(b1)))),
46+
() -> assertThat(b1, is(not(equalTo(a1)))));
47+
}
48+
49+
@ApplicationScoped
50+
static class ApplicationScopedBean {
51+
52+
}
53+
54+
@Test
55+
void shouldCreateApplicationScopedInstance() {
56+
factory.addClass(ApplicationScopedBean.class);
57+
factory.start();
58+
ApplicationScopedBean cdiStep = factory.getInstance(ApplicationScopedBean.class);
3759
assertAll(
38-
() -> assertThat(o1, is(notNullValue())),
39-
() -> assertThat(o1, is(not(equalTo(o2)))),
40-
() -> assertThat(o2, is(not(equalTo(o1)))));
60+
// assert that it is is a CDI proxy
61+
() -> assertThat(cdiStep.getClass(), not(is(ApplicationScopedBean.class))),
62+
() -> assertThat(cdiStep.getClass().getSuperclass(), is(ApplicationScopedBean.class)));
63+
factory.stop();
4164
}
4265

66+
@Test
67+
void shouldCreateUnmanagedInstance() {
68+
factory.addClass(UnmanagedBean.class);
69+
factory.start();
70+
assertNotNull(factory.getInstance(UnmanagedBean.class));
71+
UnmanagedBean cdiStep = factory.getInstance(UnmanagedBean.class);
72+
assertThat(cdiStep.getClass(), is(UnmanagedBean.class));
73+
factory.stop();
74+
}
75+
76+
static class UnmanagedBean {
77+
78+
}
4379
}

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

-9
This file was deleted.

jakarta-cdi/src/test/java/io/cucumber/jakarta/cdi/Belly.java renamed to jakarta-cdi/src/test/java/io/cucumber/jakarta/cdi/example/Belly.java

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

33
import jakarta.enterprise.context.ApplicationScoped;
44

0 commit comments

Comments
 (0)