HHH-16049 Restructure lazy-basic tests for easier re-execution and better test reports

This commit is contained in:
Yoann Rodière 2023-01-16 13:54:19 +01:00 committed by Andrea Boriero
parent 12c69c8528
commit acbfa0a060
4 changed files with 210 additions and 349 deletions

View File

@ -12,11 +12,11 @@ import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import org.hibernate.testing.TestForIssue;
import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner;
import org.hibernate.testing.bytecode.enhancement.CustomEnhancementContext;
import org.hibernate.testing.bytecode.enhancement.EnhancerTestContext;
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
@ -29,7 +29,8 @@ import jakarta.persistence.Table;
@RunWith(BytecodeEnhancerRunner.class)
@CustomEnhancementContext( {EnhancerTestContext.class, NoDirtyCheckingContext.class} )
public class MultiLazyBasicUpdateTest extends BaseCoreFunctionalTestCase {
@TestForIssue(jiraKey = "HHH-15634")
public class EagerAndLazyBasicUpdateTest extends BaseCoreFunctionalTestCase {
private Long entityId;
@ -38,8 +39,7 @@ public class MultiLazyBasicUpdateTest extends BaseCoreFunctionalTestCase {
return new Class<?>[] { LazyEntity.class };
}
@Before
public void prepare() {
private void initNull() {
doInHibernate( this::sessionFactory, s -> {
LazyEntity entity = new LazyEntity();
s.persist( entity );
@ -47,88 +47,164 @@ public class MultiLazyBasicUpdateTest extends BaseCoreFunctionalTestCase {
} );
}
@Test
public void updateOneLazyProperty() {
// null -> non-null
private void initNonNull() {
doInHibernate( this::sessionFactory, s -> {
LazyEntity entity = s.get( LazyEntity.class, entityId );
entity.setLazyProperty1( "update1" );
} );
doInHibernate( this::sessionFactory, s -> {
LazyEntity entity = s.get( LazyEntity.class, entityId );
assertEquals( "update1", entity.getLazyProperty1() );
assertNull( entity.getEagerProperty() );
assertNull( entity.getLazyProperty2() );
} );
// non-null -> non-null
doInHibernate( this::sessionFactory, s -> {
LazyEntity entity = s.get( LazyEntity.class, entityId );
entity.setLazyProperty1( "update2" );
} );
doInHibernate( this::sessionFactory, s -> {
LazyEntity entity = s.get( LazyEntity.class, entityId );
assertEquals( "update2", entity.getLazyProperty1() );
assertNull( entity.getLazyProperty2() );
assertNull( entity.getEagerProperty() );
LazyEntity entity = new LazyEntity();
entity.setEagerProperty( "eager_initial" );
entity.setLazyProperty1( "lazy1_initial" );
entity.setLazyProperty2( "lazy2_initial" );
s.persist( entity );
entityId = entity.getId();
} );
}
@Test
public void updateOneEagerPropertyAndOneLazyProperty() {
// null -> non-null
public void updateOneLazyProperty_nullToNonNull() {
initNull();
doInHibernate( this::sessionFactory, s -> {
LazyEntity entity = s.get( LazyEntity.class, entityId );
entity.setEagerProperty( "eager_update1" );
entity.setLazyProperty1( "update1" );
entity.setLazyProperty1( "lazy1_update" );
} );
doInHibernate( this::sessionFactory, s -> {
LazyEntity entity = s.get( LazyEntity.class, entityId );
assertEquals( "eager_update1", entity.getEagerProperty() );
assertEquals( "update1", entity.getLazyProperty1() );
assertNull( entity.getLazyProperty2() );
} );
assertEquals( "lazy1_update", entity.getLazyProperty1() );
// non-null -> non-null
doInHibernate( this::sessionFactory, s -> {
LazyEntity entity = s.get( LazyEntity.class, entityId );
entity.setEagerProperty( "eager_update2" );
entity.setLazyProperty1( "update2" );
} );
doInHibernate( this::sessionFactory, s -> {
LazyEntity entity = s.get( LazyEntity.class, entityId );
assertEquals( "eager_update2", entity.getEagerProperty() );
assertEquals( "update2", entity.getLazyProperty1() );
assertNull( entity.getEagerProperty() );
assertNull( entity.getLazyProperty2() );
} );
}
@Test
public void updateAllLazyProperties() {
// null -> non-null
public void updateOneLazyProperty_nonNullToNonNull() {
initNonNull();
doInHibernate( this::sessionFactory, s -> {
LazyEntity entity = s.get( LazyEntity.class, entityId );
entity.setLazyProperty1( "update1" );
entity.setLazyProperty2( "update2_1" );
entity.setLazyProperty1( "lazy1_update" );
} );
doInHibernate( this::sessionFactory, s -> {
LazyEntity entity = s.get( LazyEntity.class, entityId );
assertEquals( "update1", entity.getLazyProperty1() );
assertEquals( "update2_1", entity.getLazyProperty2() );
assertNull( entity.getEagerProperty() );
} );
assertEquals( "lazy1_update", entity.getLazyProperty1() );
// non-null -> non-null
assertEquals( "eager_initial", entity.getEagerProperty() );
assertEquals( "lazy2_initial", entity.getLazyProperty2() );
} );
}
@Test
public void updateOneLazyProperty_nonNullToNull() {
initNonNull();
doInHibernate( this::sessionFactory, s -> {
LazyEntity entity = s.get( LazyEntity.class, entityId );
entity.setLazyProperty1( "update2" );
entity.setLazyProperty2( "update2_2" );
entity.setLazyProperty1( null );
} );
doInHibernate( this::sessionFactory, s -> {
LazyEntity entity = s.get( LazyEntity.class, entityId );
assertNull( entity.getLazyProperty1() );
assertEquals( "eager_initial", entity.getEagerProperty() );
assertEquals( "lazy2_initial", entity.getLazyProperty2() );
} );
}
@Test
public void updateOneEagerPropertyAndOneLazyProperty_nullToNonNull() {
initNull();
doInHibernate( this::sessionFactory, s -> {
LazyEntity entity = s.get( LazyEntity.class, entityId );
entity.setEagerProperty( "eager_update" );
entity.setLazyProperty1( "lazy1_update" );
} );
doInHibernate( this::sessionFactory, s -> {
LazyEntity entity = s.get( LazyEntity.class, entityId );
assertEquals( "eager_update", entity.getEagerProperty() );
assertEquals( "lazy1_update", entity.getLazyProperty1() );
assertNull( entity.getLazyProperty2() );
} );
}
@Test
public void updateOneEagerPropertyAndOneLazyProperty_nonNullToNonNull() {
initNonNull();
doInHibernate( this::sessionFactory, s -> {
LazyEntity entity = s.get( LazyEntity.class, entityId );
entity.setEagerProperty( "eager_update" );
entity.setLazyProperty1( "lazy1_update" );
} );
doInHibernate( this::sessionFactory, s -> {
LazyEntity entity = s.get( LazyEntity.class, entityId );
assertEquals( "eager_update", entity.getEagerProperty() );
assertEquals( "lazy1_update", entity.getLazyProperty1() );
assertEquals( "lazy2_initial", entity.getLazyProperty2() );
} );
}
@Test
public void updateOneEagerPropertyAndOneLazyProperty_nonNullToNull() {
initNonNull();
doInHibernate( this::sessionFactory, s -> {
LazyEntity entity = s.get( LazyEntity.class, entityId );
entity.setEagerProperty( null );
entity.setLazyProperty1( null );
} );
doInHibernate( this::sessionFactory, s -> {
LazyEntity entity = s.get( LazyEntity.class, entityId );
assertEquals( "update2", entity.getLazyProperty1() );
assertEquals( "update2_2", entity.getLazyProperty2() );
assertNull( entity.getEagerProperty() );
assertNull( entity.getLazyProperty1() );
assertEquals( "lazy2_initial", entity.getLazyProperty2() );
} );
}
@Test
public void updateAllLazyProperties_nullToNonNull() {
initNull();
doInHibernate( this::sessionFactory, s -> {
LazyEntity entity = s.get( LazyEntity.class, entityId );
entity.setLazyProperty1( "lazy1_update" );
entity.setLazyProperty2( "lazy2_update" );
} );
doInHibernate( this::sessionFactory, s -> {
LazyEntity entity = s.get( LazyEntity.class, entityId );
assertEquals( "lazy1_update", entity.getLazyProperty1() );
assertEquals( "lazy2_update", entity.getLazyProperty2() );
assertNull( entity.getEagerProperty() );
} );
}
@Test
public void updateAllLazyProperties_nonNullToNonNull() {
initNonNull();
doInHibernate( this::sessionFactory, s -> {
LazyEntity entity = s.get( LazyEntity.class, entityId );
entity.setLazyProperty1( "lazy1_update" );
entity.setLazyProperty2( "lazy2_update" );
} );
doInHibernate( this::sessionFactory, s -> {
LazyEntity entity = s.get( LazyEntity.class, entityId );
assertEquals( "lazy1_update", entity.getLazyProperty1() );
assertEquals( "lazy2_update", entity.getLazyProperty2() );
assertEquals( "eager_initial", entity.getEagerProperty() );
} );
}
@Test
public void updateAllLazyProperties_nonNullToNull() {
initNonNull();
doInHibernate( this::sessionFactory, s -> {
LazyEntity entity = s.get( LazyEntity.class, entityId );
entity.setLazyProperty1( null );
entity.setLazyProperty2( null );
} );
doInHibernate( this::sessionFactory, s -> {
LazyEntity entity = s.get( LazyEntity.class, entityId );
assertNull( entity.getLazyProperty1() );
assertNull( entity.getLazyProperty2() );
assertEquals( "eager_initial", entity.getEagerProperty() );
} );
}

View File

@ -1,146 +0,0 @@
/*
* 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.lazy.basic;
import org.hibernate.orm.test.bytecode.enhancement.lazy.NoDirtyCheckingContext;
import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner;
import org.hibernate.testing.bytecode.enhancement.CustomEnhancementContext;
import org.hibernate.testing.bytecode.enhancement.EnhancerTestContext;
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import jakarta.persistence.Basic;
import jakarta.persistence.Entity;
import jakarta.persistence.FetchType;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.Id;
import jakarta.persistence.Table;
import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate;
import static org.junit.Assert.assertNull;
import static org.junit.jupiter.api.Assertions.assertNotNull;
@RunWith(BytecodeEnhancerRunner.class)
@CustomEnhancementContext({ EnhancerTestContext.class, NoDirtyCheckingContext.class })
public class MultiLazyBasicUpdateToNullTest extends BaseCoreFunctionalTestCase {
private Long entityId;
@Override
public Class<?>[] getAnnotatedClasses() {
return new Class<?>[] { LazyEntity.class };
}
@Before
public void prepare() {
doInHibernate( this::sessionFactory, s -> {
LazyEntity entity = new LazyEntity();
entity.setEagerProperty( "eager" );
entity.setLazyProperty1( "update1" );
entity.setLazyProperty2( "update2" );
s.persist( entity );
entityId = entity.getId();
} );
}
@Test
public void updateOneLazyProperty() {
// non-null -> null
doInHibernate( this::sessionFactory, s -> {
LazyEntity entity = s.get( LazyEntity.class, entityId );
entity.setLazyProperty1( null );
} );
doInHibernate( this::sessionFactory, s -> {
LazyEntity entity = s.get( LazyEntity.class, entityId );
assertNull( entity.getLazyProperty1() );
assertNotNull( entity.getLazyProperty2() );
} );
}
@Test
public void updateOneEagerPropertyAndOneLazyProperty() {
// non-null -> null
doInHibernate( this::sessionFactory, s -> {
LazyEntity entity = s.get( LazyEntity.class, entityId );
entity.setEagerProperty( null );
entity.setLazyProperty1( null );
} );
doInHibernate( this::sessionFactory, s -> {
LazyEntity entity = s.get( LazyEntity.class, entityId );
assertNull( entity.getEagerProperty() );
assertNull( entity.getLazyProperty1() );
assertNotNull( entity.getLazyProperty2() );
} );
}
@Test
public void updateAllLazyProperties() {
// non-null -> null
doInHibernate( this::sessionFactory, s -> {
LazyEntity entity = s.get( LazyEntity.class, entityId );
entity.setLazyProperty1( null );
entity.setLazyProperty2( null );
} );
doInHibernate( this::sessionFactory, s -> {
LazyEntity entity = s.get( LazyEntity.class, entityId );
assertNotNull( entity.getEagerProperty() );
assertNull( entity.getLazyProperty1() );
assertNull( entity.getLazyProperty2() );
} );
}
@Entity
@Table(name = "LAZY_ENTITY")
private static class LazyEntity {
@Id
@GeneratedValue
Long id;
// We need at least one eager property to avoid a different problem.
@Basic
String eagerProperty;
@Basic(fetch = FetchType.LAZY)
String lazyProperty1;
// We need multiple lazy properties to reproduce the problem.
@Basic(fetch = FetchType.LAZY)
String lazyProperty2;
Long getId() {
return id;
}
void setId(Long id) {
this.id = id;
}
public String getEagerProperty() {
return eagerProperty;
}
public void setEagerProperty(String eagerProperty) {
this.eagerProperty = eagerProperty;
}
public String getLazyProperty1() {
return lazyProperty1;
}
public void setLazyProperty1(String lazyProperty1) {
this.lazyProperty1 = lazyProperty1;
}
public String getLazyProperty2() {
return lazyProperty2;
}
public void setLazyProperty2(String lazyProperty2) {
this.lazyProperty2 = lazyProperty2;
}
}
}

View File

@ -8,11 +8,11 @@ package org.hibernate.orm.test.bytecode.enhancement.lazy.basic;
import org.hibernate.orm.test.bytecode.enhancement.lazy.NoDirtyCheckingContext;
import org.hibernate.testing.TestForIssue;
import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner;
import org.hibernate.testing.bytecode.enhancement.CustomEnhancementContext;
import org.hibernate.testing.bytecode.enhancement.EnhancerTestContext;
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
@ -29,6 +29,7 @@ import static org.junit.Assert.assertNull;
@RunWith(BytecodeEnhancerRunner.class)
@CustomEnhancementContext({ EnhancerTestContext.class, NoDirtyCheckingContext.class })
@TestForIssue(jiraKey = "HHH-15634")
public class OnlyLazyBasicUpdateTest extends BaseCoreFunctionalTestCase {
private Long entityId;
@ -38,8 +39,7 @@ public class OnlyLazyBasicUpdateTest extends BaseCoreFunctionalTestCase {
return new Class<?>[] { LazyEntity.class };
}
@Before
public void prepare() {
private void initNull() {
doInHibernate( this::sessionFactory, s -> {
LazyEntity entity = new LazyEntity();
s.persist( entity );
@ -47,55 +47,103 @@ public class OnlyLazyBasicUpdateTest extends BaseCoreFunctionalTestCase {
} );
}
@Test
public void updateSomeLazyProperty() {
// null -> non-null
private void initNonNull() {
doInHibernate( this::sessionFactory, s -> {
LazyEntity entity = s.get( LazyEntity.class, entityId );
entity.setLazyProperty1( "update1" );
} );
doInHibernate( this::sessionFactory, s -> {
LazyEntity entity = s.get( LazyEntity.class, entityId );
assertEquals( "update1", entity.getLazyProperty1() );
assertNull( entity.getLazyProperty2() );
LazyEntity entity = new LazyEntity();
entity.setLazyProperty1( "lazy1_initial" );
entity.setLazyProperty2( "lazy2_initial" );
s.persist( entity );
entityId = entity.getId();
} );
}
// non-null -> non-null
@Test
public void updateSomeLazyProperty_nullToNonNull() {
initNull();
doInHibernate( this::sessionFactory, s -> {
LazyEntity entity = s.get( LazyEntity.class, entityId );
entity.setLazyProperty1( "update2" );
entity.setLazyProperty1( "lazy1_update" );
} );
doInHibernate( this::sessionFactory, s -> {
LazyEntity entity = s.get( LazyEntity.class, entityId );
assertEquals( "update2", entity.getLazyProperty1() );
assertEquals( "lazy1_update", entity.getLazyProperty1() );
assertNull( entity.getLazyProperty2() );
} );
}
@Test
public void updateAllLazyProperties() {
// null -> non-null
public void updateSomeLazyProperty_nonNullToNonNull() {
initNonNull();
doInHibernate( this::sessionFactory, s -> {
LazyEntity entity = s.get( LazyEntity.class, entityId );
entity.setLazyProperty1( "update1" );
entity.setLazyProperty2( "update2_1" );
entity.setLazyProperty1( "lazy1_update" );
} );
doInHibernate( this::sessionFactory, s -> {
LazyEntity entity = s.get( LazyEntity.class, entityId );
assertEquals( "update1", entity.getLazyProperty1() );
assertEquals( "update2_1", entity.getLazyProperty2() );
} );
assertEquals( "lazy1_update", entity.getLazyProperty1() );
// non-null -> non-null
assertEquals( "lazy2_initial", entity.getLazyProperty2() );
} );
}
@Test
public void updateSomeLazyProperty_nonNullToNull() {
initNonNull();
doInHibernate( this::sessionFactory, s -> {
LazyEntity entity = s.get( LazyEntity.class, entityId );
entity.setLazyProperty1( "update2" );
entity.setLazyProperty2( "update2_2" );
entity.setLazyProperty1( null );
} );
doInHibernate( this::sessionFactory, s -> {
LazyEntity entity = s.get( LazyEntity.class, entityId );
assertEquals( "update2", entity.getLazyProperty1() );
assertEquals( "update2_2", entity.getLazyProperty2() );
assertNull( entity.getLazyProperty1() );
assertEquals( "lazy2_initial", entity.getLazyProperty2() );
} );
}
@Test
public void updateAllLazyProperties_nullToNonNull() {
initNull();
doInHibernate( this::sessionFactory, s -> {
LazyEntity entity = s.get( LazyEntity.class, entityId );
entity.setLazyProperty1( "lazy1_update" );
entity.setLazyProperty2( "lazy2_update" );
} );
doInHibernate( this::sessionFactory, s -> {
LazyEntity entity = s.get( LazyEntity.class, entityId );
assertEquals( "lazy1_update", entity.getLazyProperty1() );
assertEquals( "lazy2_update", entity.getLazyProperty2() );
} );
}
@Test
public void updateAllLazyProperties_nonNullToNonNull() {
initNonNull();
doInHibernate( this::sessionFactory, s -> {
LazyEntity entity = s.get( LazyEntity.class, entityId );
entity.setLazyProperty1( "lazy1_update" );
entity.setLazyProperty2( "lazy2_update" );
} );
doInHibernate( this::sessionFactory, s -> {
LazyEntity entity = s.get( LazyEntity.class, entityId );
assertEquals( "lazy1_update", entity.getLazyProperty1() );
assertEquals( "lazy2_update", entity.getLazyProperty2() );
} );
}
@Test
public void updateAllLazyProperties_nonNullToNull() {
initNonNull();
doInHibernate( this::sessionFactory, s -> {
LazyEntity entity = s.get( LazyEntity.class, entityId );
entity.setLazyProperty1( null );
entity.setLazyProperty2( null );
} );
doInHibernate( this::sessionFactory, s -> {
LazyEntity entity = s.get( LazyEntity.class, entityId );
assertNull( entity.getLazyProperty1() );
assertNull( entity.getLazyProperty2() );
} );
}

View File

@ -1,117 +0,0 @@
/*
* 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.lazy.basic;
import org.hibernate.orm.test.bytecode.enhancement.lazy.NoDirtyCheckingContext;
import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner;
import org.hibernate.testing.bytecode.enhancement.CustomEnhancementContext;
import org.hibernate.testing.bytecode.enhancement.EnhancerTestContext;
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import jakarta.persistence.Basic;
import jakarta.persistence.Entity;
import jakarta.persistence.FetchType;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.Id;
import jakarta.persistence.Table;
import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate;
import static org.junit.Assert.assertNull;
import static org.junit.jupiter.api.Assertions.assertNotNull;
@RunWith(BytecodeEnhancerRunner.class)
@CustomEnhancementContext( { EnhancerTestContext.class, NoDirtyCheckingContext.class} )
public class OnlyLazyBasicUpdateToNullTest extends BaseCoreFunctionalTestCase {
private Long entityId;
@Override
public Class<?>[] getAnnotatedClasses() {
return new Class<?>[] { LazyEntity.class };
}
@Before
public void prepare() {
doInHibernate( this::sessionFactory, s -> {
LazyEntity entity = new LazyEntity();
entity.setLazyProperty1( "update1" );
entity.setLazyProperty2( "update2" );
s.persist( entity );
entityId = entity.getId();
} );
}
@Test
public void updateSomeLazyProperty() {
// non-null -> null
doInHibernate( this::sessionFactory, s -> {
LazyEntity entity = s.get( LazyEntity.class, entityId );
entity.setLazyProperty1( null );
} );
doInHibernate( this::sessionFactory, s -> {
LazyEntity entity = s.get( LazyEntity.class, entityId );
assertNull( entity.getLazyProperty1() );
assertNotNull( entity.getLazyProperty2() );
} );
}
@Test
public void updateAllLazyProperties() {
// non-null -> null
doInHibernate( this::sessionFactory, s -> {
LazyEntity entity = s.get( LazyEntity.class, entityId );
entity.setLazyProperty1( null );
entity.setLazyProperty2( null );
} );
doInHibernate( this::sessionFactory, s -> {
LazyEntity entity = s.get( LazyEntity.class, entityId );
assertNull( entity.getLazyProperty1() );
assertNull( entity.getLazyProperty2() );
} );
}
@Entity
@Table(name = "LAZY_ENTITY")
private static class LazyEntity {
@Id
@GeneratedValue
Long id;
// ALL properties must be lazy in order to reproduce the problem.
@Basic(fetch = FetchType.LAZY)
String lazyProperty1;
@Basic(fetch = FetchType.LAZY)
String lazyProperty2;
Long getId() {
return id;
}
void setId(Long id) {
this.id = id;
}
public String getLazyProperty1() {
return lazyProperty1;
}
public void setLazyProperty1(String lazyProperty1) {
this.lazyProperty1 = lazyProperty1;
}
public String getLazyProperty2() {
return lazyProperty2;
}
public void setLazyProperty2(String lazyProperty2) {
this.lazyProperty2 = lazyProperty2;
}
}
}