From aee2312475c421a1f5f1c91e2dc5333db4cb46c6 Mon Sep 17 00:00:00 2001 From: Gail Badner Date: Fri, 19 Aug 2011 14:20:42 -0700 Subject: [PATCH] HHH-1645 : Refresh with LockMode on an unitialized proxy does not work --- .../org/hibernate/test/proxy/ProxyTest.java | 84 ++++++++++++++++++- 1 file changed, 83 insertions(+), 1 deletion(-) diff --git a/hibernate-core/src/test/java/org/hibernate/test/proxy/ProxyTest.java b/hibernate-core/src/test/java/org/hibernate/test/proxy/ProxyTest.java index ef7041e14c..abb7c764f0 100755 --- a/hibernate-core/src/test/java/org/hibernate/test/proxy/ProxyTest.java +++ b/hibernate-core/src/test/java/org/hibernate/test/proxy/ProxyTest.java @@ -30,6 +30,7 @@ import org.hibernate.FlushMode; import org.hibernate.Hibernate; import org.hibernate.LazyInitializationException; import org.hibernate.LockMode; +import org.hibernate.LockOptions; import org.hibernate.ObjectNotFoundException; import org.hibernate.Session; import org.hibernate.Transaction; @@ -41,6 +42,7 @@ import org.hibernate.proxy.HibernateProxy; import org.junit.Test; +import org.hibernate.testing.FailureExpected; import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; import static org.junit.Assert.assertEquals; @@ -430,5 +432,85 @@ public class ProxyTest extends BaseCoreFunctionalTestCase { t.commit(); s.close(); } -} + @Test + public void testRefreshLockInitializedProxy() { + Session s = openSession(); + Transaction t = s.beginTransaction(); + DataPoint dp = newPersistentDataPoint( s ); + + dp = ( DataPoint ) s.load( DataPoint.class, new Long( dp.getId() ) ); + dp.getX(); + assertTrue( Hibernate.isInitialized( dp ) ); + + s.refresh( dp, LockOptions.UPGRADE ); + assertSame( LockOptions.UPGRADE.getLockMode(), s.getCurrentLockMode( dp ) ); + + s.delete( dp ); + t.commit(); + s.close(); + } + + @Test + @FailureExpected( jiraKey = "HHH-1645", message = "Session.refresh with LockOptions does not work on uninitialized proxies" ) + public void testRefreshLockUninitializedProxy() { + Session s = openSession(); + Transaction t = s.beginTransaction(); + DataPoint dp = newPersistentDataPoint( s ); + + dp = ( DataPoint ) s.load( DataPoint.class, new Long( dp.getId() ) ); + assertFalse( Hibernate.isInitialized( dp ) ); + + s.refresh( dp, LockOptions.UPGRADE ); + assertSame( LockOptions.UPGRADE.getLockMode(), s.getCurrentLockMode( dp ) ); + + s.delete( dp ); + t.commit(); + s.close(); + } + + private static DataPoint newPersistentDataPoint(Session s) { + DataPoint dp = new DataPoint(); + dp.setDescription( "a data point" ); + dp.setX( new BigDecimal( 1.0 ) ); + dp.setY( new BigDecimal( 2.0 ) ); + s.persist( dp ); + s.flush(); + s.clear(); + return dp; + } + + @Test + @FailureExpected( jiraKey = "HHH-1645", message = "Session.refresh with LockOptions does not work on uninitialized proxies" ) + public void testRefreshLockUninitializedProxyThenRead() { + Session s = openSession(); + Transaction t = s.beginTransaction(); + DataPoint dp = newPersistentDataPoint( s ); + + dp = ( DataPoint ) s.load( DataPoint.class, new Long( dp.getId() ) ); + assertFalse( Hibernate.isInitialized( dp ) ); + s.refresh( dp, LockOptions.UPGRADE ); + dp.getX(); + assertSame( LockOptions.UPGRADE.getLockMode(), s.getCurrentLockMode( dp ) ); + + s.delete( dp ); + t.commit(); + s.close(); + } + + @Test + public void testLockUninitializedProxy() { + Session s = openSession(); + Transaction t = s.beginTransaction(); + DataPoint dp = newPersistentDataPoint( s ); + + dp = ( DataPoint) s.load( DataPoint.class, new Long( dp.getId() ) ); + assertFalse( Hibernate.isInitialized( dp ) ); + s.buildLockRequest( LockOptions.UPGRADE ).lock( dp ); + assertSame( LockOptions.UPGRADE.getLockMode(), s.getCurrentLockMode( dp ) ); + + s.delete( dp ); + t.commit(); + s.close(); + } +}