HHH-15072 Add test for out of the box support for records as embeddables

This commit is contained in:
Christian Beikov 2022-08-17 15:43:44 +02:00
parent 75240b0cd3
commit 9f5f31ec97
3 changed files with 131 additions and 1 deletions

View File

@ -212,3 +212,50 @@ tasks.withType( Test.class ).each { test ->
}
}
// Tests with records
if ( gradle.ext.javaVersions.test.release.asInt() >= 17 ) {
// Add a new source set, which contains tests that can run on JDK17+
sourceSets {
testJava17 {
java {
srcDirs = ['src/test/java17']
}
// Refer to the main test resources to avoid processing variables twice
resources {
srcDir 'target/resources/test'
}
}
}
// For the new source set, we need to configure the source and target version to 17
compileTestJava17Java {
sourceCompatibility = 17
targetCompatibility = 17
// We also depend on the main test resources
dependsOn( tasks.processTestResources )
}
// The source set gets a custom configuration which extends the normal test implementation config
configurations {
testJava17Implementation.extendsFrom(testImplementation, testRuntimeOnly)
}
// Add the output from src/main/java as dependency
dependencies {
testJava17Implementation files(sourceSets.main.output.classesDirs) {
builtBy compileJava
}
}
// We execute the Java 17 tests in a custom test task
task java17Test(type: Test) {
useJUnitPlatform()
testClassesDirs = sourceSets.testJava17.output.classesDirs
classpath = sourceSets.testJava17.runtimeClasspath
}
testClasses.dependsOn compileTestJava17Java
// And run this as part of the check task by default
check.dependsOn java17Test
}

View File

@ -0,0 +1,83 @@
/*
* 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.test.records;
import org.hibernate.testing.orm.junit.DomainModel;
import org.hibernate.testing.orm.junit.JiraKey;
import org.hibernate.testing.orm.junit.SessionFactory;
import org.hibernate.testing.orm.junit.SessionFactoryScope;
import org.junit.jupiter.api.Test;
import jakarta.persistence.Embeddable;
import jakarta.persistence.Embedded;
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
@JiraKey( "HHH-15072" )
@DomainModel(annotatedClasses = { OutOfTheBoxRecordAsEmbeddableTest.MyEntity.class})
@SessionFactory
public class OutOfTheBoxRecordAsEmbeddableTest {
@Test
public void testRecordPersistLoadAndMerge(SessionFactoryScope scope) {
scope.inTransaction(
session -> {
session.persist( new MyEntity( 1L, new MyRecord( "test", "abc" ) ) );
}
);
scope.inTransaction(
session -> {
MyEntity myEntity = session.get( MyEntity.class, 1L );
assertNotNull( myEntity );
assertEquals( "test", myEntity.getRecord().name() );
assertEquals( "abc", myEntity.getRecord().description() );
myEntity.setRecord( new MyRecord( "test2", "def" ) );
}
);
}
@Entity(name = "MyEntity")
public static class MyEntity {
@Id
Long id;
@Embedded
MyRecord record;
public MyEntity() {
}
public MyEntity(Long id, MyRecord record) {
this.id = id;
this.record = record;
}
public Long getId() {
return id;
}
public MyRecord getRecord() {
return record;
}
public void setId(Long id) {
this.id = id;
}
public void setRecord(MyRecord record) {
this.record = record;
}
}
@Embeddable
public static record MyRecord(String name, String description) {}
}

View File

@ -220,7 +220,7 @@ gradle.ext.baselineJavaVersion = JavaLanguageVersion.of( 11 )
def GRADLE_MAX_SUPPORTED_BYTECODE_VERSION = 17
// If either 'main.jdk.version' or 'test.jdk.version' is set, enable the toolchain and use the selected jdk.
// If only one property is set, the other defaults to the baseline Java version (8).
// If only one property is set, the other defaults to the baseline Java version (11).
// Note that when toolchain is enabled, you also need to specify
// the location of the selected jdks
// (auto-download and auto-detect are disabled in gradle.properties).