Skip to content

Commit 0dd4592

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

File tree

7 files changed

+323
-0
lines changed

7 files changed

+323
-0
lines changed
+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
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+
By default, the test will try to connect to a running PostgreSQL instance started
7+
following the instructions in the
8+
[PODMAN.md](https://github.com/hibernate/hibernate-reactive/blob/main/podman.md) file
9+
in the [Hibernate Reactive GitHub repository](https://github.com/hibernate/hibernate-reactive).
10+
11+
The template contains examples of tests written using both the Mutiny and Stage API.
12+
The bug report can be written either with one or the other.

reactive/hibernate-reactive-2/pom.xml

+112
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
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+
</properties>
16+
17+
<dependencyManagement>
18+
<dependencies>
19+
<dependency>
20+
<groupId>org.junit</groupId>
21+
<artifactId>junit-bom</artifactId>
22+
<version>${version.junit-jupiter}</version>
23+
<type>pom</type>
24+
<scope>import</scope>
25+
</dependency>
26+
</dependencies>
27+
</dependencyManagement>
28+
29+
<dependencies>
30+
<dependency>
31+
<groupId>org.hibernate.reactive</groupId>
32+
<artifactId>hibernate-reactive-core</artifactId>
33+
<version>${version.org.hibernate.reactive}</version>
34+
</dependency>
35+
<!-- PostgreSQL -->
36+
<dependency>
37+
<groupId>io.vertx</groupId>
38+
<artifactId>vertx-pg-client</artifactId>
39+
<version>${version.io.vertx.vertx-sql-client}</version>
40+
</dependency>
41+
<!-- Allow authentication to PostgreSQL using SCRAM -->
42+
<dependency>
43+
<groupId>com.ongres.scram</groupId>
44+
<artifactId>client</artifactId>
45+
<version>2.1</version>
46+
</dependency>
47+
<!-- MySQL -->
48+
<dependency>
49+
<groupId>io.vertx</groupId>
50+
<artifactId>vertx-mysql-client</artifactId>
51+
<version>${version.io.vertx.vertx-sql-client}</version>
52+
</dependency>
53+
<!-- DB2 -->
54+
<dependency>
55+
<groupId>io.vertx</groupId>
56+
<artifactId>vertx-db2-client</artifactId>
57+
<version>${version.io.vertx.vertx-sql-client}</version>
58+
</dependency>
59+
<!-- MSSQL -->
60+
<dependency>
61+
<groupId>io.vertx</groupId>
62+
<artifactId>vertx-mssql-client</artifactId>
63+
<version>${version.io.vertx.vertx-sql-client}</version>
64+
</dependency>
65+
<!-- Oracle -->
66+
<dependency>
67+
<groupId>io.vertx</groupId>
68+
<artifactId>vertx-oracle-client</artifactId>
69+
<version>${version.io.vertx.vertx-sql-client}</version>
70+
</dependency>
71+
<dependency>
72+
<groupId>org.junit.jupiter</groupId>
73+
<artifactId>junit-jupiter</artifactId>
74+
<scope>test</scope>
75+
</dependency>
76+
<dependency>
77+
<groupId>org.junit.platform</groupId>
78+
<artifactId>junit-platform-launcher</artifactId>
79+
<scope>test</scope>
80+
</dependency>
81+
<dependency>
82+
<groupId>org.junit.jupiter</groupId>
83+
<artifactId>junit-jupiter-engine</artifactId>
84+
<scope>test</scope>
85+
</dependency>
86+
<dependency>
87+
<groupId>org.assertj</groupId>
88+
<artifactId>assertj-core</artifactId>
89+
<version>${version.org.assertj.assertj-core}</version>
90+
<scope>test</scope>
91+
</dependency>
92+
<dependency>
93+
<groupId>io.vertx</groupId>
94+
<artifactId>vertx-junit5</artifactId>
95+
<version>${version.io.vertx.vertx-sql-client}</version>
96+
<scope>test</scope>
97+
</dependency>
98+
</dependencies>
99+
100+
<build>
101+
<plugins>
102+
<plugin>
103+
<groupId>org.apache.maven.plugins</groupId>
104+
<artifactId>maven-compiler-plugin</artifactId>
105+
<version>3.14.0</version>
106+
<configuration>
107+
<release>11</release>
108+
</configuration>
109+
</plugin>
110+
</plugins>
111+
</build>
112+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package org.hibernate.bugs;
2+
3+
/**
4+
* JDBC URL connection strings for the property {@link org.hibernate.reactive.provider.Settings#URL} when the database
5+
* is started following the instructions on 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+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
package org.hibernate.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.bugs.model.Foo;
8+
import org.hibernate.reactive.mutiny.Mutiny;
9+
import org.hibernate.reactive.provider.ReactiveServiceRegistryBuilder;
10+
import org.hibernate.reactive.stage.Stage;
11+
12+
import org.junit.jupiter.api.AfterAll;
13+
import org.junit.jupiter.api.BeforeAll;
14+
import org.junit.jupiter.api.Test;
15+
import org.junit.jupiter.api.extension.ExtendWith;
16+
17+
import io.smallrye.mutiny.Uni;
18+
import io.vertx.junit5.VertxExtension;
19+
import io.vertx.junit5.VertxTestContext;
20+
import java.util.concurrent.CompletableFuture;
21+
22+
/**
23+
* Demonstrates how to develop a standalone test case for Hibernate Reactive using
24+
* the {@link org.hibernate.reactive.mutiny.Mutiny.SessionFactory}
25+
* or {@link org.hibernate.reactive.stage.Stage.SessionFactory}.
26+
* <p>
27+
* It uses the {@link VertxExtension} to ease the testing of async code using JUnit.
28+
* </p>
29+
* <p>
30+
* There are two test methods (feel free to pick your favorite):
31+
* <ui>
32+
* <li>{@link #testWithMutiny(VertxTestContext)} for using the Mutiny API</li>
33+
* <li>{@link #testWithStage(VertxTestContext)} for using the Stage API</li>
34+
* </ui>
35+
* </p>
36+
* <p>
37+
* By default, the tests expect a running PostgreSQL with {@code hreact}
38+
* as value for username, password, and database name.
39+
* </p>
40+
*/
41+
@ExtendWith(VertxExtension.class)
42+
public class ReactiveStandaloneTestCase {
43+
44+
private static SessionFactory sf;
45+
46+
@BeforeAll
47+
public static void setup() {
48+
StandardServiceRegistryBuilder srb = new ReactiveServiceRegistryBuilder()
49+
// Add in any settings that are specific to your test. See resources/hibernate.properties for the defaults.
50+
.applySetting( "hibernate.connection.url", ConnectionURL.POSTGRES )
51+
.applySetting( "hibernate.show_sql", "true" )
52+
.applySetting( "hibernate.format_sql", "true" );
53+
54+
Metadata metadata = new MetadataSources( srb.build() )
55+
// Add your entities here.
56+
.addAnnotatedClass( Foo.class )
57+
.buildMetadata();
58+
59+
sf = metadata.buildSessionFactory();
60+
}
61+
62+
@Test
63+
public void testWithMutiny(VertxTestContext context) {
64+
Mutiny.SessionFactory mutinySf = sf.unwrap( Mutiny.SessionFactory.class );
65+
mutinySf
66+
// Example of transactional block
67+
.withTransaction( session -> {
68+
return Uni.createFrom().voidItem();
69+
} )
70+
// Subscribe the uni and wait until it completes
71+
.subscribe().with( res -> context.completeNow(), context::failNow );
72+
}
73+
74+
@Test
75+
public void testWithStage(VertxTestContext context) {
76+
Stage.SessionFactory stageSf = sf.unwrap( Stage.SessionFactory.class );
77+
stageSf
78+
// Example of transactional block
79+
.withTransaction( session -> {
80+
return CompletableFuture.completedFuture( null );
81+
} )
82+
// Stop the test when the CompletionStage completes
83+
.whenComplete( (res, err) -> {
84+
if ( err != null ) {
85+
context.failNow( err );
86+
}
87+
else {
88+
context.completeNow();
89+
}
90+
} );
91+
}
92+
93+
// It's important to always close the factory at the end of the tests.
94+
@AfterAll
95+
public static void closeSessionFactory() {
96+
if ( sf != null ) {
97+
sf.close();
98+
}
99+
}
100+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package org.hibernate.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 Foo {
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 id + ":" + name;
35+
}
36+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#
2+
# Hibernate, Relational Persistence for Idiomatic Java
3+
#
4+
# License: GNU Lesser General Public License (LGPL), version 2.1 or later.
5+
# See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
6+
#
7+
8+
# The connection URL is set in the test class during setup and will override this values
9+
#hibernate.connection.url = jdbc:postgresql://localhost:5432/hreact?user=hreact&password=hreact
10+
#hibernate.connection.username = hreact
11+
#hibernate.connection.password = hreact
12+
13+
hibernate.hbm2ddl.auto = update
14+
hibernate.connection.pool_size = 5
15+
16+
hibernate.show_sql = false
17+
hibernate.format_sql = true
18+
hibernate.highlight_sql=true
19+
20+
hibernate.max_fetch_depth = 5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Root logger level
2+
rootLogger.level = info
3+
rootLogger.appenderRefs = console
4+
rootLogger.appenderRef.console.ref = console
5+
6+
# Print the selected dialect information
7+
logger.hibernate-dialect.name = org.hibernate.orm.dialect
8+
logger.hibernate-dialect.level = debug
9+
10+
# SQL logging disabled by default to speed test suite execution
11+
# It can also be enabled by setting the environment variable:
12+
# JAVA_TOOL_OPTIONS='-Dhibernate.show_sql=true'
13+
logger.hibernate.name = org.hibernate.SQL
14+
logger.hibernate.level = info
15+
16+
# Setting level TRACE will show when a connection is opened/closed
17+
logger.sql-connection.name = org.hibernate.reactive.pool.impl
18+
logger.sql-connection.level = info
19+
20+
# Setting level to TRACE will show parameters values for SQL queries
21+
logger.sql-parameters-values.name = org.hibernate.type
22+
logger.sql-parameters-values.level = info
23+
24+
appender.console.name = console
25+
appender.console.type = Console
26+
appender.console.layout.type = PatternLayout
27+
appender.console.layout.pattern = %highlight{[%p]} %m%n

0 commit comments

Comments
 (0)