Skip to content

Commit 61ae4c1

Browse files
committed
single executable jbang-based junit test class
1 parent 134fca9 commit 61ae4c1

File tree

1 file changed

+179
-0
lines changed

1 file changed

+179
-0
lines changed

tooling/jbang/SampleIssueTest.java

Lines changed: 179 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,179 @@
1+
/* Hibernate, Relational Persistence for Idiomatic Java
2+
*
3+
* SPDX-License-Identifier: LGPL-2.1-or-later
4+
* Copyright: Red Hat Inc. and Hibernate Authors
5+
*/
6+
7+
///usr/bin/env jbang "$0" "$@" ; exit $?
8+
//DEPS org.hibernate:hibernate-core:5.4.31.Final
9+
//DEPS junit:junit:4.12
10+
//DEPS javax.persistence:javax.persistence-api:2.2
11+
//DEPS org.hibernate.reactive:hibernate-reactive-core:1.0.0.CR4
12+
//DEPS org.assertj:assertj-core:3.13.2
13+
//DEPS io.vertx:vertx-pg-client:4.0.3
14+
//DEPS io.vertx:vertx-db2-client:4.0.3
15+
//DEPS io.vertx:vertx-mysql-client:4.0.3
16+
//DEPS io.vertx:vertx-sql-client:4.0.3
17+
//DEPS io.vertx:vertx-unit:4.0.3
18+
//
19+
//
20+
21+
package org.hibernate.reactive;
22+
23+
import javax.persistence.Entity;
24+
import javax.persistence.Id;
25+
26+
import org.hibernate.boot.registry.StandardServiceRegistry;
27+
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
28+
import org.hibernate.cfg.Configuration;
29+
import org.hibernate.reactive.mutiny.Mutiny;
30+
import org.hibernate.reactive.provider.ReactiveServiceRegistryBuilder;
31+
import org.hibernate.reactive.provider.Settings;
32+
33+
import org.junit.After;
34+
import org.junit.Before;
35+
import org.junit.Test;
36+
import org.junit.runner.JUnitCore;
37+
import org.junit.runner.Result;
38+
import org.junit.runner.RunWith;
39+
import org.junit.runner.notification.Failure;
40+
41+
import io.vertx.ext.unit.Async;
42+
import io.vertx.ext.unit.TestContext;
43+
import io.vertx.ext.unit.junit.VertxUnitRunner;
44+
45+
import static org.assertj.core.api.Assertions.assertThat;
46+
47+
/**
48+
* This test class provides a working example of setting up a simple hibernate-reactive test compatible with
49+
* hibernate-reactive junit tests.
50+
* See <a href="github.com/hibernate/hibernate-reactive/tree/main/hibernate-reactive-core/src/test/java/org/hibernate/reactive">hibernate reactive junit tests</a>
51+
*
52+
* Developers can copy/paste/edit this test to capture test failures or use-cases and attach an executable class to
53+
* issues or other correspondence.
54+
*
55+
* This test can be executed using the jbang CLI with the command `jbang SampleIssueTest.java`
56+
* See <a href="https://github.com/jbangdev/jbang">jbang</a>"
57+
* jbang will compile and execute a jar generated based on dependencies defined above in the
58+
* file header (i.e. //DEPS org.hibernate:hibernate-core:5.4.31.Final )
59+
*
60+
* > Customize your JDBC connection properties
61+
* > Define your specific entity classes and relationships
62+
* > Replicate your issue and or exception
63+
* > attach the resulting file or a URL link to it
64+
*
65+
* Note this test utilizes a VertxUnitRunner class which provides the hooks to the reactive
66+
* framework for transaction control.
67+
*
68+
*/
69+
@RunWith(VertxUnitRunner.class)
70+
public class SampleIssueTest {
71+
72+
private Mutiny.SessionFactory sessionFactory;
73+
74+
/**
75+
* Define the configuration parameter values for your use-case
76+
*/
77+
private Configuration createConfiguration() {
78+
Configuration configuration = new Configuration();
79+
80+
// Use the correct JDBC url
81+
configuration.setProperty( Settings.URL, "jdbc:postgresql://localhost:5432/hreact" );
82+
83+
// Credentials
84+
configuration.setProperty( Settings.USER, "hreact");
85+
configuration.setProperty( Settings.PASS, "hreact");
86+
87+
// Schema generation
88+
configuration.setProperty( Settings.HBM2DDL_AUTO, "create" );
89+
90+
// Add additional entities here
91+
configuration.addAnnotatedClass(MyEntity.class);
92+
93+
// Query logging
94+
configuration.setProperty( Settings.SHOW_SQL, "true" );
95+
configuration.setProperty( Settings.HIGHLIGHT_SQL, "true" );
96+
return configuration;
97+
}
98+
99+
@Before
100+
public void createSessionFactory() {
101+
Configuration configuration = createConfiguration();
102+
StandardServiceRegistryBuilder builder = new ReactiveServiceRegistryBuilder()
103+
.applySettings( configuration.getProperties() );
104+
StandardServiceRegistry registry = builder.build();
105+
106+
sessionFactory = configuration.buildSessionFactory( registry )
107+
.unwrap( Mutiny.SessionFactory.class );
108+
}
109+
110+
@Test
111+
public void firstTest(TestContext context) {
112+
Async async = context.async();
113+
114+
MyEntity entity = new MyEntity( "first entity", 1 );
115+
sessionFactory
116+
.withTransaction( (session, tx) -> session.persist( entity ) )
117+
.chain( () -> sessionFactory
118+
.withSession( session -> session
119+
.find( MyEntity.class, entity.getId() )
120+
.invoke( foundEntity -> assertThat( foundEntity.getName() ).isEqualTo( entity.getName() ) ) ) )
121+
.subscribe().with( res -> async.complete(), throwable -> context.fail( throwable )
122+
);
123+
}
124+
125+
@After
126+
public void closeFactory() {
127+
if ( sessionFactory != null ) {
128+
sessionFactory.close();
129+
}
130+
}
131+
132+
/*
133+
* Define your hibernate entity classes and relationships.
134+
*
135+
* This initial test includes a single entity but you can create joined and/or embedded entities too
136+
*
137+
* Be sure to add all annotated entity classes in the createConfiguration() method above
138+
* example: configuration.addAnnotatedClass(MyOtherEntity.class);
139+
*/
140+
@Entity(name = "MyEntity")
141+
public static class MyEntity {
142+
@Id
143+
public Integer id;
144+
145+
public String name;
146+
147+
public MyEntity() {}
148+
149+
public MyEntity(String name, Integer id) {
150+
this.name = name;
151+
this.id = id;
152+
}
153+
154+
public Integer getId() {
155+
return id;
156+
}
157+
158+
public String getName() {
159+
return name;
160+
}
161+
162+
@Override
163+
public String toString() {
164+
return "MyEntity"
165+
+ "\n\t id = " + id
166+
+ "\n\t name = " + name;
167+
}
168+
}
169+
170+
public static void main(String[] args) {
171+
Result result = JUnitCore.runClasses( SampleIssueTest.class);
172+
173+
for ( Failure failure : result.getFailures()) {
174+
System.out.println(failure.toString());
175+
}
176+
177+
System.out.println(" All unit tests succeeded: " + result.wasSuccessful());
178+
}
179+
}

0 commit comments

Comments
 (0)