Skip to content

Commit ac958c4

Browse files
committed
updated junit test and added java test
1 parent c283f9a commit ac958c4

File tree

2 files changed

+224
-4
lines changed

2 files changed

+224
-4
lines changed

tooling/jbang/Example.java

Lines changed: 210 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,210 @@
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.hibernate.validator:hibernate-validator:6.1.5.Final
13+
//DEPS org.assertj:assertj-core:3.13.2
14+
//DEPS io.vertx:vertx-pg-client:4.0.3
15+
//DEPS io.vertx:vertx-db2-client:4.0.3
16+
//DEPS io.vertx:vertx-mysql-client:4.0.3
17+
//DEPS io.vertx:vertx-sql-client:4.0.3
18+
//DEPS io.vertx:vertx-unit:4.0.3
19+
//
20+
//
21+
22+
import java.time.LocalDate;
23+
import java.util.ArrayList;
24+
import java.util.List;
25+
import javax.persistence.Basic;
26+
import javax.persistence.Entity;
27+
import javax.persistence.GeneratedValue;
28+
import javax.persistence.Id;
29+
import javax.persistence.ManyToOne;
30+
import javax.persistence.OneToMany;
31+
import javax.persistence.Table;
32+
import javax.validation.constraints.NotNull;
33+
import javax.validation.constraints.Past;
34+
import javax.validation.constraints.Size;
35+
36+
import org.hibernate.boot.registry.StandardServiceRegistry;
37+
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
38+
import org.hibernate.cfg.Configuration;
39+
import org.hibernate.reactive.mutiny.Mutiny;
40+
import org.hibernate.reactive.provider.ReactiveServiceRegistryBuilder;
41+
import org.hibernate.reactive.provider.Settings;
42+
43+
import static java.lang.System.out;
44+
import static java.time.Month.JANUARY;
45+
import static java.time.Month.JUNE;
46+
import static java.time.Month.MAY;
47+
import static javax.persistence.CascadeType.PERSIST;
48+
import static javax.persistence.FetchType.LAZY;
49+
50+
public class Example {
51+
52+
/**
53+
* Define the configuration parameter values for your use-case
54+
*/
55+
private static Configuration createConfiguration() {
56+
Configuration configuration = new Configuration();
57+
58+
configuration.setProperty( Settings.URL, "jdbc:postgresql://localhost:5432/hreact" );
59+
60+
// ===== OTHER Test DBs =======================================================================
61+
// MYSQL: jdbc:mysql://localhost:3306/hreact?user=hreact&password=hreact&serverTimezone=UTC
62+
// DB2: jdbc:db2://localhost:50000/hreact:user=hreact;password=hreact;
63+
// CockroachDB jdbc:cockroachdb://localhost:26257/postgres?sslmode=disable&user=root
64+
// NOTE: CockroachDB does not need password and requires //DEPS io.vertx:vertx-pg-client:4.0.3
65+
// ==============================================================================================
66+
67+
configuration.setProperty( Settings.USER, "hreact" );
68+
configuration.setProperty( Settings.PASS, "hreact" );
69+
70+
configuration.setProperty( Settings.HBM2DDL_AUTO, "create" );
71+
72+
configuration.addAnnotatedClass( Author.class );
73+
configuration.addAnnotatedClass( Book.class );
74+
75+
configuration.setProperty( Settings.SHOW_SQL, "true" );
76+
configuration.setProperty( Settings.HIGHLIGHT_SQL, "true" );
77+
configuration.setProperty( Settings.FORMAT_SQL, "true" );
78+
return configuration;
79+
}
80+
81+
public static Mutiny.SessionFactory createSessionFactory() {
82+
Configuration configuration = createConfiguration();
83+
StandardServiceRegistryBuilder builder = new ReactiveServiceRegistryBuilder()
84+
.applySettings( configuration.getProperties() );
85+
StandardServiceRegistry registry = builder.build();
86+
87+
return configuration.buildSessionFactory( registry )
88+
.unwrap( Mutiny.SessionFactory.class );
89+
}
90+
91+
public static void main(String[] args) {
92+
out.println( "== Mutiny API Example ==" );
93+
94+
// define some test data
95+
Author author1 = new Author( "Iain M. Banks" );
96+
Author author2 = new Author( "Neal Stephenson" );
97+
Book book1 = new Book( "1-85723-235-6", "Feersum Endjinn", author1, LocalDate.of( 1994, JANUARY, 1 ) );
98+
Book book2 = new Book( "0-380-97346-4", "Cryptonomicon", author2, LocalDate.of( 1999, MAY, 1 ) );
99+
Book book3 = new Book( "0-553-08853-X", "Snow Crash", author2, LocalDate.of( 1992, JUNE, 1 ) );
100+
author1.getBooks().add( book1 );
101+
author2.getBooks().add( book2 );
102+
author2.getBooks().add( book3 );
103+
104+
try (Mutiny.SessionFactory factory = createSessionFactory()) {
105+
// obtain a reactive session
106+
factory.withTransaction(
107+
// persist the Authors with their Books in a transaction
108+
(session, tx) -> session.persistAll( author1, author2 )
109+
)
110+
// wait for it to finish
111+
.await().indefinitely();
112+
113+
factory.withSession(
114+
// retrieve a Book
115+
session -> session.find( Book.class, book1.getId() )
116+
// print its title
117+
.invoke( book -> out.println( book.getTitle() + " is a great book!" ) )
118+
)
119+
.await().indefinitely();
120+
}
121+
}
122+
123+
@Entity
124+
@Table(name = "authors")
125+
static class Author {
126+
@Id
127+
@GeneratedValue
128+
private Integer id;
129+
130+
@NotNull
131+
@Size(max = 100)
132+
private String name;
133+
134+
@OneToMany(mappedBy = "author", cascade = PERSIST)
135+
private List<Book> books = new ArrayList<>();
136+
137+
Author(String name) {
138+
this.name = name;
139+
}
140+
141+
Author() {
142+
}
143+
144+
Integer getId() {
145+
return id;
146+
}
147+
148+
String getName() {
149+
return name;
150+
}
151+
152+
List<Book> getBooks() {
153+
return books;
154+
}
155+
}
156+
157+
@Entity
158+
@Table(name = "books")
159+
static class Book {
160+
@Id
161+
@GeneratedValue
162+
private Integer id;
163+
164+
@Size(min = 13, max = 13)
165+
private String isbn;
166+
167+
@NotNull
168+
@Size(max = 100)
169+
private String title;
170+
171+
@Basic(fetch = LAZY)
172+
@NotNull
173+
@Past
174+
private LocalDate published;
175+
176+
@NotNull
177+
@ManyToOne(fetch = LAZY)
178+
private Author author;
179+
180+
Book(String isbn, String title, Author author, LocalDate published) {
181+
this.title = title;
182+
this.isbn = isbn;
183+
this.author = author;
184+
this.published = published;
185+
}
186+
187+
Book() {
188+
}
189+
190+
Integer getId() {
191+
return id;
192+
}
193+
194+
String getIsbn() {
195+
return isbn;
196+
}
197+
198+
String getTitle() {
199+
return title;
200+
}
201+
202+
Author getAuthor() {
203+
return author;
204+
}
205+
206+
LocalDate getPublished() {
207+
return published;
208+
}
209+
}
210+
}

tooling/jbang/SampleIssueTest.java

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,13 @@ private Configuration createConfiguration() {
7878
// Use the correct JDBC url
7979
configuration.setProperty( Settings.URL, "jdbc:postgresql://localhost:5432/hreact" );
8080

81+
// ===== OTHER Test DBs =======================================================================
82+
// MYSQL: jdbc:mysql://localhost:3306/hreact?user=hreact&password=hreact&serverTimezone=UTC
83+
// DB2: jdbc:db2://localhost:50000/hreact:user=hreact;password=hreact;
84+
// CockroachDB jdbc:cockroachdb://localhost:26257/postgres?sslmode=disable&user=root
85+
// NOTE: CockroachDB does not need password and requires //DEPS io.vertx:vertx-pg-client:4.0.3
86+
// ==============================================================================================
87+
8188
// Credentials
8289
configuration.setProperty( Settings.USER, "hreact");
8390
configuration.setProperty( Settings.PASS, "hreact");
@@ -91,6 +98,7 @@ private Configuration createConfiguration() {
9198
// Query logging
9299
configuration.setProperty( Settings.SHOW_SQL, "true" );
93100
configuration.setProperty( Settings.HIGHLIGHT_SQL, "true" );
101+
configuration.setProperty( Settings.FORMAT_SQL, "true" );
94102
return configuration;
95103
}
96104

@@ -168,10 +176,12 @@ public String toString() {
168176
public static void main(String[] args) {
169177
Result result = JUnitCore.runClasses( SampleIssueTest.class);
170178

171-
for ( Failure failure : result.getFailures()) {
172-
System.out.println(failure.toString());
179+
if( result.wasSuccessful() ) {
180+
System.out.println( "All unit tests passed");
181+
} else {
182+
for ( Failure failure : result.getFailures() ) {
183+
System.out.println( "TEST FAILURE: " + failure.toString() );
184+
}
173185
}
174-
175-
System.out.println(" All unit tests succeeded: " + result.wasSuccessful());
176186
}
177187
}

0 commit comments

Comments
 (0)