HHH-13409 Add integration tests for running Hibernate ORM in the module path in Java 11+

This commit is contained in:
Yoann Rodière 2019-05-24 09:26:01 +02:00 committed by Sanne Grinovero
parent 6b199d2e52
commit d6cb72ee0d
9 changed files with 364 additions and 0 deletions

View File

@ -16,6 +16,7 @@ ext {
'hibernate-ehcache',
'hibernate-java8',
'hibernate-orm-modules',
'hibernate-integrationtest-java-modules',
'release'
]
}

View File

@ -0,0 +1,44 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
description = 'Integration tests for running Hibernate ORM in the Java module path'
buildscript {
repositories {
maven {
url "https://plugins.gradle.org/m2/"
}
}
dependencies {
classpath "org.javamodularity:moduleplugin:1.5.0"
}
}
apply from: rootProject.file( 'gradle/java-module.gradle' )
// No first-class, built-in support for Java modules in Gradle yet,
// so we have to use https://github.com/java9-modularity/gradle-modules-plugin
apply plugin: "org.javamodularity.moduleplugin"
// Override -source and -target
ext.baselineJavaVersion = 11
sourceCompatibility = project.baselineJavaVersion
targetCompatibility = project.baselineJavaVersion
// Checkstyle fails for module-info
checkstyleMain.exclude '**/module-info.java'
dependencies {
compile( project( ':hibernate-core' ) )
compile( project( ':hibernate-envers' ) )
compile( libraries.jpa )
}
test {
useJUnit()
}

View File

@ -0,0 +1,27 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
module org.hibernate.orm.integrationtest.java.module {
exports org.hibernate.orm.integrationtest.java.module.service;
opens org.hibernate.orm.integrationtest.java.module.entity to
org.hibernate.orm.core;
requires java.persistence;
/*
* IDEA will not find the modules below because it apparently doesn't support automatic module names
* for modules in the current project.
* Everything should work fine when building from the command line, though.
*/
requires org.hibernate.orm.core;
requires org.hibernate.orm.envers;
/*
* This is necessary in order to use SessionFactory,
* which extends "javax.naming.Referenceable".
* Without this, compilation as a Java module fails.
*/
requires java.naming;
}

View File

@ -0,0 +1,54 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
package org.hibernate.orm.integrationtest.java.module.entity;
import javax.persistence.Basic;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import org.hibernate.annotations.NaturalId;
import org.hibernate.envers.Audited;
@Entity
@Audited
public class Author {
@Id
@GeneratedValue
private Integer id;
@NaturalId
@Basic(optional = false)
private String name;
@Basic
private int favoriteNumber;
public Author() {
}
public Integer getId() {
return id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getFavoriteNumber() {
return favoriteNumber;
}
public void setFavoriteNumber(int favoriteNumber) {
this.favoriteNumber = favoriteNumber;
}
}

View File

@ -0,0 +1,107 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
package org.hibernate.orm.integrationtest.java.module.service;
import java.util.List;
import javax.persistence.Persistence;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.envers.AuditReader;
import org.hibernate.envers.AuditReaderFactory;
import org.hibernate.orm.integrationtest.java.module.entity.Author;
public class AuthorService implements AutoCloseable {
private final SessionFactory sessionFactory;
public AuthorService() {
sessionFactory = createSessionFactory();
}
private SessionFactory createSessionFactory() {
return Persistence.createEntityManagerFactory( "primaryPU" )
.unwrap( SessionFactory.class );
}
public void add(String name, Integer favoriteNumber) {
try ( Session session = sessionFactory.openSession() ) {
session.getTransaction().begin();
try {
Author entity = new Author();
entity.setName( name );
entity.setFavoriteNumber( favoriteNumber );
session.save( entity );
session.getTransaction().commit();
}
catch (Throwable e) {
try {
session.getTransaction().rollback();
}
catch (Throwable e2) {
e.addSuppressed( e2 );
}
throw e;
}
}
}
public void update(String name, Integer favoriteNumber) {
try ( Session session = sessionFactory.openSession() ) {
session.getTransaction().begin();
try {
Author entity = session.bySimpleNaturalId( Author.class ).getReference( name );
entity.setFavoriteNumber( favoriteNumber );
session.getTransaction().commit();
}
catch (Throwable e) {
try {
session.getTransaction().rollback();
}
catch (Throwable e2) {
e.addSuppressed( e2 );
}
throw e;
}
}
}
public Integer getFavoriteNumber(String name) {
try ( Session session = sessionFactory.openSession() ) {
session.getTransaction().begin();
try {
Author entity = session.bySimpleNaturalId( Author.class ).getReference( name );
Integer result = entity.getFavoriteNumber();
session.getTransaction().rollback();
return result;
}
catch (Throwable e) {
try {
session.getTransaction().rollback();
}
catch (Throwable e2) {
e.addSuppressed( e2 );
}
throw e;
}
}
}
public List<Number> getRevisions(String name) {
try ( Session session = sessionFactory.openSession() ) {
Author entity = session.bySimpleNaturalId( Author.class ).getReference( name );
AuditReader auditReader = AuditReaderFactory.get( session );
return auditReader.getRevisions( Author.class, entity.getId() );
}
}
@Override
public void close() {
sessionFactory.close();
}
}

View File

@ -0,0 +1,25 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ Hibernate, Relational Persistence for Idiomatic Java
~
~ License: GNU Lesser General Public License (LGPL), version 2.1 or later.
~ See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
-->
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"
version="1.0">
<persistence-unit name="primaryPU">
<class>org.hibernate.orm.integrationtest.java.module.entity.Author</class>
<properties>
<property name="hibernate.connection.driver_class" value="org.h2.Driver"/>
<property name="hibernate.dialect" value="org.hibernate.dialect.H2Dialect"/>
<property name="hibernate.connection.url" value="jdbc:h2:mem:db1;DB_CLOSE_DELAY=-1;MVCC=TRUE"/>
<property name="hibernate.connection.username" value="sa"/>
<property name="hibernate.connection.password" value=""/>
<property name="hibernate.hbm2ddl.auto" value="create-drop"/>
<property name="hibernate.show_sql" value="false"/>
</properties>
</persistence-unit>
</persistence>

View File

@ -0,0 +1,76 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
package org.hibernate.orm.integrationtest.java.module.test;
import java.util.Arrays;
import org.hibernate.Session;
import org.hibernate.envers.boot.internal.EnversIntegrator;
import org.hibernate.orm.integrationtest.java.module.service.AuthorService;
import org.junit.Assert;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
public class JavaModulePathIT {
/*
* Test that the service successfully uses Hibernate ORM in the module path.
* We don't really care about the features themselves,
* but the easiest way to check this is to just use Hibernate ORM features and see if it works.
*/
@Test
public void core() {
checkIsInModulePath( Object.class );
checkIsInModulePath( AuthorService.class );
checkIsInModulePath( Session.class );
AuthorService service = new AuthorService();
service.add( "foo", 7 );
service.add( "bar", 42 );
service.add( "foo bar", 777 );
service.update( "foo", 8 );
assertEquals( (Integer) 8, service.getFavoriteNumber( "foo" ) );
assertEquals( (Integer) 42, service.getFavoriteNumber( "bar" ) );
assertEquals( (Integer) 777, service.getFavoriteNumber( "foo bar" ) );
}
/*
* Test that the service successfully uses an extension of Hibernate ORM in the module path.
* We don't really care about the features themselves,
* but the easiest way to check this is to just use Envers features and see if it works.
*/
@Test
public void integrator() {
checkIsInModulePath( Object.class );
checkIsInModulePath( AuthorService.class );
checkIsInModulePath( Session.class );
checkIsInModulePath( EnversIntegrator.class );
AuthorService service = new AuthorService();
service.add( "foo", 7 );
service.add( "bar", 42 );
service.add( "foo bar", 777 );
service.update( "foo", 8 );
assertEquals( Arrays.asList( 1, 4 ), service.getRevisions( "foo" ) );
assertEquals( Arrays.asList( 2 ), service.getRevisions( "bar" ) );
assertEquals( Arrays.asList( 3 ), service.getRevisions( "foo bar" ) );
}
private void checkIsInModulePath(Class<?> clazz) {
Assert.assertTrue(
clazz + " should be part of a named module - there is a problem in test setup",
clazz.getModule().isNamed()
);
}
}

View File

@ -0,0 +1,23 @@
#
# Hibernate, Relational Persistence for Idiomatic Java
#
# License: GNU Lesser General Public License (LGPL), version 2.1 or later.
# See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
#
# Root logger option
log4j.rootLogger=DEBUG, file
# Direct log messages to a log file
log4j.appender.file=org.apache.log4j.FileAppender
log4j.appender.file.File=target/test.log
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p [%c] (%t) - %m%n
log4j.appender.file.Threshold=DEBUG
# Direct log messages to console
log4j.appender.console=org.apache.log4j.ConsoleAppender
log4j.appender.console.Target=System.out
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p [%c] (%t) - %m%n
log4j.appender.console.Threshold=WARN

View File

@ -31,6 +31,13 @@ include 'hibernate-jipijapa'
include 'hibernate-orm-modules'
if ( JavaVersion.current().isJava11Compatible() ) {
include 'hibernate-integrationtest-java-modules'
}
else {
logger.warn( '[WARN] Skipping Java module path integration tests because the JDK does not support it' )
}
include 'documentation'
include 'release'