Skip to content

Commit b2719bd

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

File tree

1 file changed

+177
-0
lines changed

1 file changed

+177
-0
lines changed

tooling/jbang/SampleIssueTest.java

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

0 commit comments

Comments
 (0)