Skip to content
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

[Jakarta Cdi] Correctly cast the UnmanagedInstance values #2250

Merged
merged 1 commit into from
Feb 28, 2021
Merged
Show file tree
Hide file tree
Changes from all 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
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)
* [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)

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

Expand Down
6 changes: 3 additions & 3 deletions jakarta-cdi/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ And for Weld it is:
<dependency>
<groupId>org.jboss.weld.se</groupId>
<artifactId>weld-se-core</artifactId>
<version>4.0.0.Alpha2</version>
<version>4.0.0</version>
<scope>test</scope>
</dependency>
```
Expand All @@ -85,13 +85,13 @@ If you don't know which one to use, you can import the following one but if you
<dependency>
<groupId>jakarta.enterprise</groupId>
<artifactId>jakarta.enterprise.cdi-api</artifactId>
<version>3.0.0-M3</version>
<version>3.0.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>jakarta.activation</groupId>
<artifactId>jakarta.activation-api</artifactId>
<version>2.0.0-RC3</version>
<version>2.0.0</version>
<scope>provided</scope>
</dependency>
```
145 changes: 88 additions & 57 deletions jakarta-cdi/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
<properties>
<project.Automatic-Module-Name>io.cucumber.cdi.jakarta</project.Automatic-Module-Name>
<openwebbeans.version>2.0.21</openwebbeans.version>
<jakarta.enterprise.cdi-api.version>3.0.0-M3</jakarta.enterprise.cdi-api.version>
<jakarta.enterprise.cdi-api.version>3.0.0</jakarta.enterprise.cdi-api.version>
<jakarta.activation-api.version>2.0.0</jakarta.activation-api.version>
<xbean.version>4.18</xbean.version>
<weld.version>4.0.0.Final</weld.version>
</properties>

<parent>
Expand Down Expand Up @@ -41,66 +41,14 @@
<version>${jakarta.activation-api.version}</version>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>org.apache.xbean</groupId>
<artifactId>xbean-finder-shaded</artifactId>
<version>${xbean.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.xbean</groupId>
<artifactId>xbean-asm9-shaded</artifactId>
<version>${xbean.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.openwebbeans</groupId>
<artifactId>openwebbeans-impl</artifactId>
<version>${openwebbeans.version}</version>
<scope>test</scope>
<classifier>jakarta</classifier>
<exclusions>
<exclusion>
<groupId>*</groupId>
<artifactId>*</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.openwebbeans</groupId>
<artifactId>openwebbeans-spi</artifactId>
<version>${openwebbeans.version}</version>
<scope>test</scope>
<classifier>jakarta</classifier>
<exclusions>
<exclusion>
<groupId>*</groupId>
<artifactId>*</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.openwebbeans</groupId>
<artifactId>openwebbeans-se</artifactId>
<version>${openwebbeans.version}</version>
<classifier>jakarta</classifier>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>*</groupId>
<artifactId>*</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-java</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-junit</artifactId>
<artifactId>cucumber-junit-platform-engine</artifactId>
<scope>test</scope>
</dependency>
<dependency>
Expand All @@ -109,9 +57,92 @@
<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>

<profiles>
<profile>
<id>jakarta-cdi-openwebbeans</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<xbean.version>4.18</xbean.version>
</properties>
<dependencies>
<dependency>
<groupId>org.apache.openwebbeans</groupId>
<artifactId>openwebbeans-se</artifactId>
<version>${openwebbeans.version}</version>
<classifier>jakarta</classifier>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>*</groupId>
<artifactId>*</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.openwebbeans</groupId>
<artifactId>openwebbeans-impl</artifactId>
<version>${openwebbeans.version}</version>
<scope>test</scope>
<classifier>jakarta</classifier>
<exclusions>
<exclusion>
<groupId>*</groupId>
<artifactId>*</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.openwebbeans</groupId>
<artifactId>openwebbeans-spi</artifactId>
<version>${openwebbeans.version}</version>
<scope>test</scope>
<classifier>jakarta</classifier>
<exclusions>
<exclusion>
<groupId>*</groupId>
<artifactId>*</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.xbean</groupId>
<artifactId>xbean-finder-shaded</artifactId>
<version>${xbean.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.xbean</groupId>
<artifactId>xbean-asm9-shaded</artifactId>
<version>${xbean.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
</profile>

<profile>
<id>jakarta-cdi-weld</id>
<dependencies>
<dependency>
<groupId>org.jboss.weld.se</groupId>
<artifactId>weld-se-core</artifactId>
<version>${weld.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jboss.weld</groupId>
<artifactId>weld-core-impl</artifactId>
<version>${weld.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
</profile>
</profiles>
</project>
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.

This file was deleted.

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
Loading