HHH-10993 - Added testcase for Lazy Loaded collections outside transaction in bytecode enhanced entities

This commit is contained in:
johara 2016-07-26 19:32:36 +01:00 committed by Gail Badner
parent 72e948514e
commit 80e092a432
2 changed files with 108 additions and 0 deletions

View File

@ -33,6 +33,7 @@ import org.hibernate.test.bytecode.enhancement.lazy.HHH_10708.UnexpectedDeleteTh
import org.hibernate.test.bytecode.enhancement.lazy.HHH_10708.UnexpectedDeleteTwoTestTask;
import org.hibernate.test.bytecode.enhancement.lazy.LazyBasicFieldNotInitializedTestTask;
import org.hibernate.test.bytecode.enhancement.lazy.LazyCollectionLoadingTestTask;
import org.hibernate.test.bytecode.enhancement.lazy.LazyCollectionNoTransactionLoadingTestTask;
import org.hibernate.test.bytecode.enhancement.lazy.LazyLoadingIntegrationTestTask;
import org.hibernate.test.bytecode.enhancement.lazy.LazyLoadingTestTask;
import org.hibernate.test.bytecode.enhancement.lazy.LazyProxyOnEnhancedEntityTestTask;
@ -124,6 +125,11 @@ public class EnhancerTest extends BaseUnitTestCase {
public void testLazyGroups() {
EnhancerTestUtils.runEnhancerTestTask( LazyGroupAccessTestTask.class );
}
@Test
public void testLazyCollectionNoTransactionHandling() {
EnhancerTestUtils.runEnhancerTestTask( LazyCollectionNoTransactionLoadingTestTask.class );
}
@Test(timeout = 10000)
@TestForIssue( jiraKey = "HHH-10055" )

View File

@ -0,0 +1,102 @@
/*
* 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.test.bytecode.enhancement.lazy;
import org.hibernate.Hibernate;
import org.hibernate.Session;
import org.hibernate.cfg.Configuration;
import org.hibernate.cfg.Environment;
import org.hibernate.proxy.HibernateProxy;
import org.hibernate.test.bytecode.enhancement.AbstractEnhancerTestTask;
import org.hibernate.test.bytecode.enhancement.EnhancerTestUtils;
import java.util.ArrayList;
import java.util.List;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.instanceOf;
import static org.hamcrest.CoreMatchers.not;
import static org.hamcrest.CoreMatchers.notNullValue;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.core.IsSame.sameInstance;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
/**
* Simple test for lazy collection handling in the new bytecode support.
* Prior to HHH-10055 lazy collections were simply not handled. The tests
* initially added for HHH-10055 cover the more complicated case of handling
* lazy collection initialization outside of a transaction; that is a bigger
* fix, and I first want to get collection handling to work here in general.
*
* @author Steve Ebersole
* @author John O'Hara
*
*/
public class LazyCollectionNoTransactionLoadingTestTask extends AbstractEnhancerTestTask {
private static final int CHILDREN_SIZE = 10;
private Long parentID;
private Long lastChildID;
public Class<?>[] getAnnotatedClasses() {
return new Class<?>[] {Parent.class, Child.class};
}
public void prepare() {
Configuration cfg = new Configuration();
cfg.setProperty( Environment.ENABLE_LAZY_LOAD_NO_TRANS, "true" );
cfg.setProperty( Environment.USE_SECOND_LEVEL_CACHE, "false" );
super.prepare( cfg );
Session s = getFactory().openSession();
s.beginTransaction();
Parent parent = new Parent();
parent.setChildren( new ArrayList<Child>() );
for ( int i = 0; i < CHILDREN_SIZE; i++ ) {
final Child child = new Child();
child.setParent( parent );
s.persist( child );
lastChildID = child.getId();
}
s.persist( parent );
parentID = parent.getId();
s.getTransaction().commit();
s.close();
}
public void execute() {
Session s = getFactory().openSession();
s.beginTransaction();
Parent parent = s.load( Parent.class, parentID );
assertThat( parent, notNullValue() );
assertThat( parent, not( instanceOf( HibernateProxy.class ) ) );
assertThat( parent, not( instanceOf( HibernateProxy.class ) ) );
assertFalse( Hibernate.isPropertyInitialized( parent, "children" ) );
s.getTransaction().commit();
s.close();
s = null;
List children1 = parent.getChildren();
List children2 = parent.getChildren();
assertTrue( Hibernate.isPropertyInitialized( parent, "children" ) );
EnhancerTestUtils.checkDirtyTracking( parent );
assertThat( children1, sameInstance( children2 ) );
assertThat( children1.size(), equalTo( CHILDREN_SIZE ) );
}
protected void cleanup() {
}
}