HHH-16459 Test bytecode-enhanced inline dirty tracking for generic associations from mapped superclasses

This commit is contained in:
Yoann Rodière 2023-04-12 11:34:49 +02:00 committed by Andrea Boriero
parent 7d3f6188f8
commit 47bdd016a1
3 changed files with 206 additions and 10 deletions

View File

@ -0,0 +1,84 @@
/*
* 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.bytecode.enhancement.dirty;
import org.hibernate.testing.TestForIssue;
import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner;
import org.hibernate.testing.bytecode.enhancement.EnhancementOptions;
import org.hibernate.testing.bytecode.enhancement.EnhancerTestUtils;
import org.hibernate.testing.orm.junit.Jira;
import org.junit.Test;
import org.junit.runner.RunWith;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.Id;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.MappedSuperclass;
/**
* @author Yoann Rodière
* @author Marco Belladelli
*/
@RunWith( BytecodeEnhancerRunner.class )
@EnhancementOptions( inlineDirtyChecking = true )
@Jira( "https://hibernate.atlassian.net/browse/HHH-16459" )
public class DirtyTrackingInheritanceTest {
@Test
public void test() {
final ChildItem entity = new ChildItem();
EnhancerTestUtils.checkDirtyTracking( entity );
entity.setBasicValue( "basic_value" );
entity.setAssociation( new Other() );
EnhancerTestUtils.checkDirtyTracking( entity, "basicValue", "association" );
}
@Entity( name = "Other" )
public static class Other {
@Id
@GeneratedValue
private Long id;
}
@MappedSuperclass
public static abstract class Item {
private String basicValue;
@ManyToOne
private Other association;
public String getBasicValue() {
return basicValue;
}
public void setBasicValue(String basicValue) {
this.basicValue = basicValue;
}
public Other getAssociation() {
return association;
}
public void setAssociation(Other association) {
this.association = association;
}
}
@Entity( name = "ChildItem" )
public static class ChildItem extends Item {
@Id
@GeneratedValue
private Long id;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
}
}

View File

@ -0,0 +1,114 @@
/*
* 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.bytecode.enhancement.dirty;
import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner;
import org.hibernate.testing.bytecode.enhancement.EnhancementOptions;
import org.hibernate.testing.bytecode.enhancement.EnhancerTestUtils;
import org.hibernate.testing.orm.junit.Jira;
import org.junit.Test;
import org.junit.runner.RunWith;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.Id;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.MappedSuperclass;
/**
* @author Yoann Rodière
* @author Marco Belladelli
*/
@RunWith( BytecodeEnhancerRunner.class )
@EnhancementOptions( inlineDirtyChecking = true )
@Jira( "https://hibernate.atlassian.net/browse/HHH-16459" )
public class DirtyTrackingInheritanceWithGenericsTest {
@Test
public void testOne() {
final ChildItemOne entity = new ChildItemOne();
EnhancerTestUtils.checkDirtyTracking( entity );
entity.setBasicValue( "basic_value" );
entity.setAssociation( new OtherOne() );
EnhancerTestUtils.checkDirtyTracking( entity, "basicValue", "association" );
}
@Test
public void testTwo() {
final ChildItemTwo entity = new ChildItemTwo();
EnhancerTestUtils.checkDirtyTracking( entity );
entity.setBasicValue( 123 );
entity.setAssociation( new OtherTwo() );
EnhancerTestUtils.checkDirtyTracking( entity, "basicValue", "association" );
}
@Entity( name = "OtherOne" )
public static class OtherOne {
@Id
@GeneratedValue
private Long id;
}
@Entity( name = "OtherTwo" )
public static class OtherTwo {
@Id
@GeneratedValue
private Long id;
}
@MappedSuperclass
public static abstract class Item<T, S> {
private T basicValue;
@ManyToOne
private S association;
public T getBasicValue() {
return basicValue;
}
public void setBasicValue(T basicValue) {
this.basicValue = basicValue;
}
public S getAssociation() {
return association;
}
public void setAssociation(S association) {
this.association = association;
}
}
@Entity( name = "ChildItemOne" )
public static class ChildItemOne extends Item<String, OtherOne> {
@Id
@GeneratedValue
private Long id;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
}
@Entity( name = "ChildItemTwo" )
public static class ChildItemTwo extends Item<Integer, OtherTwo> {
@Id
@GeneratedValue
private Long id;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
}
}

View File

@ -6,6 +6,8 @@
*/ */
package org.hibernate.testing.bytecode.enhancement; package org.hibernate.testing.bytecode.enhancement;
import java.lang.reflect.Field;
import org.hibernate.LockMode; import org.hibernate.LockMode;
import org.hibernate.engine.internal.MutableEntityEntryFactory; import org.hibernate.engine.internal.MutableEntityEntryFactory;
import org.hibernate.engine.spi.EntityEntry; import org.hibernate.engine.spi.EntityEntry;
@ -15,11 +17,7 @@ import org.hibernate.internal.util.ReflectHelper;
import org.hibernate.testing.junit4.BaseUnitTestCase; import org.hibernate.testing.junit4.BaseUnitTestCase;
import java.lang.reflect.Field; import static org.assertj.core.api.Assertions.assertThat;
import java.util.Arrays;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail; import static org.junit.Assert.fail;
/** /**
@ -53,11 +51,11 @@ public abstract class EnhancerTestUtils extends BaseUnitTestCase {
* compares the dirty fields of an entity with a set of expected values * compares the dirty fields of an entity with a set of expected values
*/ */
public static void checkDirtyTracking(Object entityInstance, String... dirtyFields) { public static void checkDirtyTracking(Object entityInstance, String... dirtyFields) {
SelfDirtinessTracker selfDirtinessTracker = (SelfDirtinessTracker) entityInstance; final SelfDirtinessTracker selfDirtinessTracker = (SelfDirtinessTracker) entityInstance;
assertEquals( dirtyFields.length > 0, selfDirtinessTracker.$$_hibernate_hasDirtyAttributes() ); assertThat( selfDirtinessTracker.$$_hibernate_getDirtyAttributes() )
String[] tracked = selfDirtinessTracker.$$_hibernate_getDirtyAttributes(); .containsExactlyInAnyOrder( dirtyFields );
assertEquals( dirtyFields.length, tracked.length ); assertThat( selfDirtinessTracker.$$_hibernate_hasDirtyAttributes() )
assertTrue( Arrays.asList( tracked ).containsAll( Arrays.asList( dirtyFields ) ) ); .isEqualTo( dirtyFields.length > 0 );
} }
public static EntityEntry makeEntityEntry() { public static EntityEntry makeEntityEntry() {