Skip to content

Commit 6ca353d

Browse files
authored
Provide an openejb-jakarta artifact to handle TomEE 9.x (#2583)
1 parent 9bf41b8 commit 6ca353d

File tree

13 files changed

+265
-0
lines changed

13 files changed

+265
-0
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
99

1010
### Added
1111

12+
* [OpenEJB] Added new module `jakarta-openejb`, which supports the jakarta.* namespace in TomEE 9.x ([#2583](https://github.com/cucumber/cucumber-jvm/pull/2583) R. Zowalla)
13+
1214
### Changed
1315

1416
### Deprecated

jakarta-openejb/pom.xml

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
2+
<modelVersion>4.0.0</modelVersion>
3+
4+
<parent>
5+
<groupId>io.cucumber</groupId>
6+
<artifactId>cucumber-jvm</artifactId>
7+
<version>7.4.2-SNAPSHOT</version>
8+
</parent>
9+
10+
<artifactId>cucumber-jakarta-openejb</artifactId>
11+
<packaging>jar</packaging>
12+
<name>Cucumber-JVM: Jakarta OpenEJB</name>
13+
14+
<properties>
15+
<apiguardian-api.version>1.1.2</apiguardian-api.version>
16+
<hamcrest.version>2.2</hamcrest.version>
17+
<junit-jupiter.version>5.8.2</junit-jupiter.version>
18+
<openejb-core.version>9.0.0-M8</openejb-core.version>
19+
<project.Automatic-Module-Name>io.cucumber.jakarta.openejb</project.Automatic-Module-Name>
20+
</properties>
21+
22+
<dependencyManagement>
23+
<dependencies>
24+
<dependency>
25+
<groupId>io.cucumber</groupId>
26+
<artifactId>cucumber-bom</artifactId>
27+
<version>${project.version}</version>
28+
<type>pom</type>
29+
<scope>import</scope>
30+
</dependency>
31+
<dependency>
32+
<groupId>org.junit</groupId>
33+
<artifactId>junit-bom</artifactId>
34+
<version>${junit-jupiter.version}</version>
35+
<type>pom</type>
36+
<scope>import</scope>
37+
</dependency>
38+
</dependencies>
39+
</dependencyManagement>
40+
41+
<dependencies>
42+
<dependency>
43+
<groupId>io.cucumber</groupId>
44+
<artifactId>cucumber-core</artifactId>
45+
</dependency>
46+
<dependency>
47+
<groupId>org.apiguardian</groupId>
48+
<artifactId>apiguardian-api</artifactId>
49+
<version>${apiguardian-api.version}</version>
50+
</dependency>
51+
<dependency>
52+
<groupId>org.apache.tomee</groupId>
53+
<artifactId>openejb-core</artifactId>
54+
<version>${openejb-core.version}</version>
55+
<scope>provided</scope>
56+
</dependency>
57+
58+
<dependency>
59+
<groupId>io.cucumber</groupId>
60+
<artifactId>cucumber-java</artifactId>
61+
<scope>test</scope>
62+
</dependency>
63+
<dependency>
64+
<groupId>io.cucumber</groupId>
65+
<artifactId>cucumber-junit-platform-engine</artifactId>
66+
<scope>test</scope>
67+
</dependency>
68+
<dependency>
69+
<groupId>org.junit.platform</groupId>
70+
<artifactId>junit-platform-suite</artifactId>
71+
<scope>test</scope>
72+
</dependency>
73+
<dependency>
74+
<groupId>org.junit.jupiter</groupId>
75+
<artifactId>junit-jupiter</artifactId>
76+
<scope>test</scope>
77+
</dependency>
78+
79+
<dependency>
80+
<groupId>org.hamcrest</groupId>
81+
<artifactId>hamcrest-core</artifactId>
82+
<version>${hamcrest.version}</version>
83+
<scope>test</scope>
84+
</dependency>
85+
</dependencies>
86+
</project>
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package io.cucumber.jakarta.openejb;
2+
3+
import io.cucumber.core.backend.CucumberBackendException;
4+
import io.cucumber.core.backend.ObjectFactory;
5+
import jakarta.ejb.embeddable.EJBContainer;
6+
import org.apache.openejb.OpenEjbContainer;
7+
import org.apiguardian.api.API;
8+
9+
import java.util.ArrayList;
10+
import java.util.HashMap;
11+
import java.util.Iterator;
12+
import java.util.List;
13+
import java.util.Map;
14+
import java.util.Properties;
15+
16+
@API(status = API.Status.STABLE)
17+
public final class OpenEJBObjectFactory implements ObjectFactory {
18+
19+
private final List<String> classes = new ArrayList<String>();
20+
private final Map<Class<?>, Object> instances = new HashMap<Class<?>, Object>();
21+
private EJBContainer container;
22+
23+
@Override
24+
public void start() {
25+
final StringBuilder callers = new StringBuilder();
26+
for (Iterator<String> it = classes.iterator(); it.hasNext();) {
27+
callers.append(it.next());
28+
if (it.hasNext()) {
29+
callers.append(",");
30+
}
31+
}
32+
33+
Properties properties = new Properties();
34+
properties.setProperty(OpenEjbContainer.Provider.OPENEJB_ADDITIONNAL_CALLERS_KEY, callers.toString());
35+
container = EJBContainer.createEJBContainer(properties);
36+
}
37+
38+
@Override
39+
public void stop() {
40+
container.close();
41+
instances.clear();
42+
}
43+
44+
@Override
45+
public boolean addClass(Class<?> clazz) {
46+
classes.add(clazz.getName());
47+
return true;
48+
}
49+
50+
@Override
51+
public <T> T getInstance(Class<T> type) {
52+
if (instances.containsKey(type)) {
53+
return type.cast(instances.get(type));
54+
}
55+
56+
T object;
57+
try {
58+
object = type.newInstance();
59+
container.getContext().bind("inject", object);
60+
} catch (Exception e) {
61+
throw new CucumberBackendException("can't create " + type.getName(), e);
62+
}
63+
instances.put(type, object);
64+
return object;
65+
}
66+
67+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
/**
2+
* Enables dependency injection by OpenEJB
3+
* <p>
4+
* By including the <code>cucumber-jakarta-openejb</code> on your
5+
* <code>CLASSPATH</code> your step definitions will be instantiated by OpenEJB.
6+
*/
7+
package io.cucumber.jakarta.openejb;
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
io.cucumber.jakarta.openejb.OpenEJBObjectFactory
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package io.cucumber.jakarta.openejb;
2+
3+
public class Belly {
4+
5+
private int cukes;
6+
7+
public int getCukes() {
8+
return cukes;
9+
}
10+
11+
public void setCukes(int cukes) {
12+
this.cukes = cukes;
13+
}
14+
15+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package io.cucumber.jakarta.openejb;
2+
3+
import io.cucumber.java.en.Given;
4+
import io.cucumber.java.en.Then;
5+
import jakarta.inject.Inject;
6+
7+
import static org.junit.jupiter.api.Assertions.assertEquals;
8+
9+
public class BellyStepDefinitions {
10+
11+
@Inject
12+
private Belly belly;
13+
14+
@Given("I have {int} cukes in my belly")
15+
public void haveCukes(int n) {
16+
belly.setCukes(n);
17+
}
18+
19+
@Then("there are {int} cukes in my belly")
20+
public void checkCukes(int n) {
21+
assertEquals(n, belly.getCukes());
22+
}
23+
24+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package io.cucumber.jakarta.openejb;
2+
3+
import io.cucumber.core.backend.ObjectFactory;
4+
import org.junit.jupiter.api.Test;
5+
6+
import static org.hamcrest.MatcherAssert.assertThat;
7+
import static org.hamcrest.core.Is.is;
8+
import static org.hamcrest.core.IsEqual.equalTo;
9+
import static org.hamcrest.core.IsNot.not;
10+
import static org.hamcrest.core.IsNull.notNullValue;
11+
12+
class OpenEJBObjectFactoryTest {
13+
14+
@Test
15+
void shouldGiveUsNewInstancesForEachScenario() {
16+
ObjectFactory factory = new OpenEJBObjectFactory();
17+
factory.addClass(BellyStepDefinitions.class);
18+
19+
// Scenario 1
20+
factory.start();
21+
BellyStepDefinitions o1 = factory.getInstance(BellyStepDefinitions.class);
22+
factory.stop();
23+
24+
// Scenario 2
25+
factory.start();
26+
BellyStepDefinitions o2 = factory.getInstance(BellyStepDefinitions.class);
27+
factory.stop();
28+
29+
assertThat(o1, is(notNullValue()));
30+
assertThat(o1, is(not(equalTo(o2))));
31+
assertThat(o2, is(not(equalTo(o1))));
32+
}
33+
34+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package io.cucumber.jakarta.openejb;
2+
3+
import org.junit.platform.suite.api.ConfigurationParameter;
4+
import org.junit.platform.suite.api.IncludeEngines;
5+
import org.junit.platform.suite.api.SelectClasspathResource;
6+
import org.junit.platform.suite.api.Suite;
7+
8+
import static io.cucumber.junit.platform.engine.Constants.GLUE_PROPERTY_NAME;
9+
10+
@Suite
11+
@IncludeEngines("cucumber")
12+
@SelectClasspathResource("io/cucumber/jakarta/openejb")
13+
@ConfigurationParameter(key = GLUE_PROPERTY_NAME, value = "io.cucumber.jakarta.openejb")
14+
public class RunCucumberTest {
15+
16+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<beans version="3.0" bean-discovery-mode="all"
3+
xmlns="https://jakarta.ee/xml/ns/jakartaee"
4+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5+
xsi:schemaLocation="https://jakarta.ee/xml/ns/jakartaee https://jakarta.ee/xml/ns/jakartaee/beans_3_0.xsd">
6+
</beans>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Feature: Cukes
2+
3+
Scenario: Eat some cukes
4+
Given I have 4 cukes in my belly
5+
Then there are 4 cukes in my belly
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
cucumber.publish.quiet=true

pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
<module>gherkin-messages</module>
4848
<module>guice</module>
4949
<module>jakarta-cdi</module>
50+
<module>jakarta-openejb</module>
5051
<module>java8</module>
5152
<module>java</module>
5253
<module>junit</module>

0 commit comments

Comments
 (0)