Skip to content

Commit f4cfb7d

Browse files
committed
Template for Hibernate Reactive
Included as part of the fix for hibernate/hibernate-reactive#2064
1 parent e833f43 commit f4cfb7d

File tree

8 files changed

+519
-0
lines changed

8 files changed

+519
-0
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Hibernate Test Case Templates: Reactive
2+
3+
This repo contains test case templates, useful for reporting bugs
4+
against [Hibernate Reactive](https://hibernate.org/reactive/).
5+
6+
The template contains examples of tests written using both the Mutiny and Stage API.
7+
The bug report can be written either with one or the other.
8+
9+
The database is started via [Testcontainers](https://java.testcontainers.org/).
10+
It's possible to select a different database by changing the value of the constant `SELECT_DB`.
11+
12+
The package [org.hibernate.reactive.util](src/test/java/org/hibernate/reactive/util) contains:
13+
14+
* `ConnectionUrl`: The JDBC urls to connect to databases started following the instructions in the
15+
[PODMAN.md](https://github.com/hibernate/hibernate-reactive/blob/main/podman.md) file
16+
in the [Hibernate Reactive GitHub repository](https://github.com/hibernate/hibernate-reactive)
17+
* `Database`: Containers configuration via [Testcontainers](https://java.testcontainers.org/)
18+
* `DockerImage`: Utility class for the creation of a `DockerImageName` to use with Testcontainers

reactive/hibernate-reactive-2/pom.xml

Lines changed: 189 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,189 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
5+
<groupId>org.hibernate.testcasetemplate</groupId>
6+
<artifactId>test-case-template-hibernate-reactive-7</artifactId>
7+
<version>1.0.0.Final</version>
8+
<name>Hibernate Reactive 2 Test Case Template</name>
9+
10+
<properties>
11+
<version.junit-jupiter>5.11.4</version.junit-jupiter>
12+
<version.org.hibernate.reactive>2.4.5.Final</version.org.hibernate.reactive>
13+
<version.org.assertj.assertj-core>3.27.3</version.org.assertj.assertj-core>
14+
<version.io.vertx.vertx-sql-client>4.5.13</version.io.vertx.vertx-sql-client>
15+
<version.org.testcontainers>1.20.6</version.org.testcontainers>
16+
</properties>
17+
18+
<dependencyManagement>
19+
<dependencies>
20+
<dependency>
21+
<groupId>org.junit</groupId>
22+
<artifactId>junit-bom</artifactId>
23+
<version>${version.junit-jupiter}</version>
24+
<type>pom</type>
25+
<scope>import</scope>
26+
</dependency>
27+
</dependencies>
28+
</dependencyManagement>
29+
30+
<dependencies>
31+
<dependency>
32+
<groupId>org.hibernate.reactive</groupId>
33+
<artifactId>hibernate-reactive-core</artifactId>
34+
<version>${version.org.hibernate.reactive}</version>
35+
</dependency>
36+
37+
<!-- PostgreSQL -->
38+
<dependency>
39+
<groupId>io.vertx</groupId>
40+
<artifactId>vertx-pg-client</artifactId>
41+
<version>${version.io.vertx.vertx-sql-client}</version>
42+
</dependency>
43+
<!-- Allow authentication to PostgreSQL using SCRAM -->
44+
<dependency>
45+
<groupId>com.ongres.scram</groupId>
46+
<artifactId>client</artifactId>
47+
<version>2.1</version>
48+
</dependency>
49+
50+
<!-- MySQL -->
51+
<dependency>
52+
<groupId>io.vertx</groupId>
53+
<artifactId>vertx-mysql-client</artifactId>
54+
<version>${version.io.vertx.vertx-sql-client}</version>
55+
</dependency>
56+
57+
<!-- DB2 -->
58+
<dependency>
59+
<groupId>io.vertx</groupId>
60+
<artifactId>vertx-db2-client</artifactId>
61+
<version>${version.io.vertx.vertx-sql-client}</version>
62+
</dependency>
63+
64+
<!-- MSSQL -->
65+
<dependency>
66+
<groupId>io.vertx</groupId>
67+
<artifactId>vertx-mssql-client</artifactId>
68+
<version>${version.io.vertx.vertx-sql-client}</version>
69+
</dependency>
70+
71+
<!-- Oracle -->
72+
<dependency>
73+
<groupId>io.vertx</groupId>
74+
<artifactId>vertx-oracle-client</artifactId>
75+
<version>${version.io.vertx.vertx-sql-client}</version>
76+
</dependency>
77+
78+
<dependency>
79+
<groupId>org.junit.jupiter</groupId>
80+
<artifactId>junit-jupiter</artifactId>
81+
<scope>test</scope>
82+
</dependency>
83+
<dependency>
84+
<groupId>org.junit.platform</groupId>
85+
<artifactId>junit-platform-launcher</artifactId>
86+
<scope>test</scope>
87+
</dependency>
88+
<dependency>
89+
<groupId>org.junit.jupiter</groupId>
90+
<artifactId>junit-jupiter-engine</artifactId>
91+
<scope>test</scope>
92+
</dependency>
93+
<dependency>
94+
<groupId>org.assertj</groupId>
95+
<artifactId>assertj-core</artifactId>
96+
<version>${version.org.assertj.assertj-core}</version>
97+
<scope>test</scope>
98+
</dependency>
99+
<dependency>
100+
<groupId>io.vertx</groupId>
101+
<artifactId>vertx-junit5</artifactId>
102+
<version>${version.io.vertx.vertx-sql-client}</version>
103+
<scope>test</scope>
104+
</dependency>
105+
<!-- Testcontainers -->
106+
<dependency>
107+
<groupId>org.testcontainers</groupId>
108+
<artifactId>postgresql</artifactId>
109+
<version>${version.org.testcontainers}</version>
110+
<scope>test</scope>
111+
</dependency>
112+
<dependency>
113+
<groupId>org.testcontainers</groupId>
114+
<artifactId>mysql</artifactId>
115+
<version>${version.org.testcontainers}</version>
116+
<scope>test</scope>
117+
</dependency>
118+
<!-- JDBC driver for Testcontainers with MYSQL -->
119+
<dependency>
120+
<groupId>com.mysql</groupId>
121+
<artifactId>mysql-connector-j</artifactId>
122+
<version>9.1.0</version>
123+
<scope>test</scope>
124+
</dependency>
125+
<dependency>
126+
<groupId>org.testcontainers</groupId>
127+
<artifactId>mariadb</artifactId>
128+
<version>${version.org.testcontainers}</version>
129+
<scope>test</scope>
130+
</dependency>
131+
<!-- JDBC driver for Testcontainers with MariaDB -->
132+
<dependency>
133+
<groupId>org.mariadb.jdbc</groupId>
134+
<artifactId>mariadb-java-client</artifactId>
135+
<version>3.5.1</version>
136+
<scope>test</scope>
137+
</dependency>
138+
<dependency>
139+
<groupId>org.testcontainers</groupId>
140+
<artifactId>db2</artifactId>
141+
<version>${version.org.testcontainers}</version>
142+
<scope>test</scope>
143+
</dependency>
144+
<!-- JDBC driver for Testcontainers with Db2 -->
145+
<dependency>
146+
<groupId>com.ibm.db2</groupId>
147+
<artifactId>jcc</artifactId>
148+
<version>12.1.0.0</version>
149+
<scope>test</scope>
150+
</dependency>
151+
<dependency>
152+
<groupId>org.testcontainers</groupId>
153+
<artifactId>cockroachdb</artifactId>
154+
<version>${version.org.testcontainers}</version>
155+
<scope>test</scope>
156+
</dependency>
157+
<dependency>
158+
<groupId>org.testcontainers</groupId>
159+
<artifactId>oracle-xe</artifactId>
160+
<version>${version.org.testcontainers}</version>
161+
<scope>test</scope>
162+
</dependency>
163+
<dependency>
164+
<groupId>org.testcontainers</groupId>
165+
<artifactId>mssqlserver</artifactId>
166+
<version>${version.org.testcontainers}</version>
167+
<scope>test</scope>
168+
</dependency>
169+
<dependency>
170+
<groupId>com.microsoft.sqlserver</groupId>
171+
<artifactId>mssql-jdbc</artifactId>
172+
<version>12.8.1.jre11</version>
173+
<scope>test</scope>
174+
</dependency>
175+
</dependencies>
176+
177+
<build>
178+
<plugins>
179+
<plugin>
180+
<groupId>org.apache.maven.plugins</groupId>
181+
<artifactId>maven-compiler-plugin</artifactId>
182+
<version>3.14.0</version>
183+
<configuration>
184+
<release>11</release>
185+
</configuration>
186+
</plugin>
187+
</plugins>
188+
</build>
189+
</project>
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
package org.hibernate.reactive.bugs;
2+
3+
import org.hibernate.SessionFactory;
4+
import org.hibernate.boot.Metadata;
5+
import org.hibernate.boot.MetadataSources;
6+
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
7+
import org.hibernate.reactive.bugs.model.MyEntity;
8+
import org.hibernate.reactive.mutiny.Mutiny;
9+
import org.hibernate.reactive.provider.ReactiveServiceRegistryBuilder;
10+
import org.hibernate.reactive.stage.Stage;
11+
import org.hibernate.reactive.util.Database;
12+
13+
import org.junit.jupiter.api.AfterAll;
14+
import org.junit.jupiter.api.BeforeAll;
15+
import org.junit.jupiter.api.Test;
16+
import org.junit.jupiter.api.extension.ExtendWith;
17+
18+
import io.smallrye.mutiny.Uni;
19+
import io.vertx.junit5.VertxExtension;
20+
import io.vertx.junit5.VertxTestContext;
21+
import java.util.concurrent.CompletableFuture;
22+
import org.testcontainers.containers.JdbcDatabaseContainer;
23+
24+
/**
25+
* Demonstrates how to develop a standalone test case for Hibernate Reactive using
26+
* the {@link org.hibernate.reactive.mutiny.Mutiny.SessionFactory}
27+
* or {@link org.hibernate.reactive.stage.Stage.SessionFactory}.
28+
* <p>
29+
* It uses the {@link VertxExtension} to ease the testing of async code using JUnit.
30+
* </p>
31+
* <p>
32+
* There are two test methods (feel free to pick your favorite):
33+
* <ui>
34+
* <li>{@link #testWithMutiny(VertxTestContext)} for using the Mutiny API</li>
35+
* <li>{@link #testWithStage(VertxTestContext)} for using the Stage API</li>
36+
* </ui>
37+
* </p>
38+
* <p>
39+
* Databases for the tests start via <a href="https://java.testcontainers.org/">Testcontainers</a>.
40+
* The configuration of the containers is in {@link Database} and the test
41+
* can run on different databases by changing the value of {@link #SELECTED_DB}.
42+
* </p>
43+
*/
44+
@ExtendWith(VertxExtension.class)
45+
public class ReactiveStandaloneTestCase {
46+
47+
/**
48+
* The selected database to run the test against.
49+
* If the value is set to null, no container will start.
50+
*
51+
* @see Database
52+
*/
53+
private static final Database SELECTED_DB = Database.POSTGRESQL;
54+
55+
private static JdbcDatabaseContainer<?> container;
56+
private static SessionFactory sf;
57+
58+
@BeforeAll
59+
public static void startContainer() {
60+
if ( SELECTED_DB != null ) {
61+
container = SELECTED_DB.startContainer();
62+
}
63+
}
64+
65+
@BeforeAll
66+
public static void setup() {
67+
StandardServiceRegistryBuilder srb = new ReactiveServiceRegistryBuilder()
68+
.applySetting( "hibernate.connection.url", container.getJdbcUrl() )
69+
.applySetting( "hibernate.connection.username", container.getUsername() )
70+
.applySetting( "hibernate.connection.password", container.getPassword() )
71+
.applySetting( "hibernate.hbm2ddl.auto", "update" )
72+
.applySetting( "hibernate.show_sql", "true" )
73+
.applySetting( "hibernate.highlight_sql", "true" )
74+
.applySetting( "hibernate.format_sql", "true" );
75+
76+
Metadata metadata = new MetadataSources( srb.build() )
77+
// Add your entities here.
78+
.addAnnotatedClass( MyEntity.class )
79+
.buildMetadata();
80+
81+
sf = metadata.buildSessionFactory();
82+
}
83+
84+
@Test
85+
public void testWithMutiny(VertxTestContext context) {
86+
Mutiny.SessionFactory mutinySf = sf.unwrap( Mutiny.SessionFactory.class );
87+
mutinySf
88+
// Example of transactional block
89+
.withTransaction( session -> {
90+
return Uni.createFrom().voidItem();
91+
} )
92+
// Subscribe the uni and wait until it completes
93+
.subscribe().with( res -> context.completeNow(), context::failNow );
94+
}
95+
96+
@Test
97+
public void testWithStage(VertxTestContext context) {
98+
Stage.SessionFactory stageSf = sf.unwrap( Stage.SessionFactory.class );
99+
stageSf
100+
// Example of transactional block
101+
.withTransaction( session -> {
102+
return CompletableFuture.completedFuture( null );
103+
} )
104+
// Stop the test when the CompletionStage completes
105+
.whenComplete( (res, err) -> {
106+
if ( err != null ) {
107+
context.failNow( err );
108+
}
109+
else {
110+
context.completeNow();
111+
}
112+
} );
113+
}
114+
115+
// It's important to always close the factory at the end of the tests.
116+
@AfterAll
117+
public static void closeSessionFactory() {
118+
if ( sf != null ) {
119+
sf.close();
120+
}
121+
}
122+
123+
@AfterAll
124+
public static void stopContainer() {
125+
if ( container != null ) {
126+
container.stop();
127+
}
128+
}
129+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package org.hibernate.reactive.bugs.model;
2+
3+
import jakarta.persistence.Entity;
4+
import jakarta.persistence.Id;
5+
6+
/**
7+
* An entity to starts with.
8+
*/
9+
@Entity
10+
public class MyEntity {
11+
12+
@Id
13+
private Long id;
14+
private String name;
15+
16+
public Long getId() {
17+
return id;
18+
}
19+
20+
public void setId(Long id) {
21+
this.id = id;
22+
}
23+
24+
public String getName() {
25+
return name;
26+
}
27+
28+
public void setName(String name) {
29+
this.name = name;
30+
}
31+
32+
@Override
33+
public String toString() {
34+
return "MyEntity(" + id + ":" + name + ")";
35+
}
36+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package org.hibernate.reactive.util;
2+
3+
/**
4+
* JDBC URL connection strings for the property {@link org.hibernate.reactive.provider.Settings#URL} when the database
5+
* is started the instructions from the
6+
* <a href="https://github.com/hibernate/hibernate-reactive/blob/main/podman.md#how-to-start-the-test-databases-using-podman">Hibernate Reactive GitHub repository</a>.
7+
*/
8+
public final class ConnectionURL {
9+
public static final String POSTGRES = "jdbc:postgresql://localhost:5432/hreact?user=hreact&password=hreact";
10+
public static final String COCKROACH = "jdbc:cockroachdb://localhost:26257/postgres?sslmode=disable&user=root";
11+
public static final String MYSQL = "jdbc:mysql://localhost/hreact?user=hreact&password=hreact";
12+
public static final String MARIA = "jdbc:mariadb://localhost:3306/hreact?user=hreact&password=hreact";
13+
public static final String MSSQL = "jdbc:sqlserver://localhost:1433;Encrypt=false;user=sa;password=~!HReact!~";
14+
public static final String DB2 = "jdbc:db2://localhost:50000/hreact:user=hreact;password=hreact;";
15+
public static final String ORACLE = "jdbc:oracle:thin:hreact/hreact@localhost:1521/hreact?user=hreact&password=hreact";
16+
}

0 commit comments

Comments
 (0)