HHH-7085 Add delete to immutable test

This commit is contained in:
Eric Dalquist 2012-02-20 09:37:42 -06:00 committed by Steve Ebersole
parent 571266aa3d
commit b5ba7bef42
1 changed files with 92 additions and 47 deletions

View File

@ -24,13 +24,17 @@
package org.hibernate.test.annotations.naturalid; package org.hibernate.test.annotations.naturalid;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
import org.hibernate.NaturalIdLoadAccess;
import org.hibernate.Session; import org.hibernate.Session;
import org.hibernate.Transaction; import org.hibernate.Transaction;
import org.hibernate.annotations.Immutable; import org.hibernate.annotations.Immutable;
import org.hibernate.cfg.Configuration; import org.hibernate.cfg.Configuration;
import org.hibernate.metadata.ClassMetadata; import org.hibernate.metadata.ClassMetadata;
import org.hibernate.stat.Statistics;
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
import org.junit.Test; import org.junit.Test;
@ -57,6 +61,15 @@ public class ImmutableEntityNaturalIdTest extends BaseCoreFunctionalTestCase {
@Test @Test
public void testImmutableNaturalIdLifecycle() { public void testImmutableNaturalIdLifecycle() {
Statistics stats = sessionFactory().getStatistics();
stats.setStatisticsEnabled( true );
stats.clear();
assertEquals( "Cache hits should be empty", 0, stats.getNaturalIdCacheHitCount() );
assertEquals( "Cache misses should be empty", 0, stats.getNaturalIdCacheMissCount() );
assertEquals( "Cache put should be empty", 0, stats.getNaturalIdCachePutCount() );
assertEquals( "Query count should be empty", 0, stats.getNaturalIdQueryExecutionCount() );
Building b1 = new Building(); Building b1 = new Building();
b1.setName( "Computer Science" ); b1.setName( "Computer Science" );
b1.setAddress( "1210 W. Dayton St." ); b1.setAddress( "1210 W. Dayton St." );
@ -69,53 +82,85 @@ public class ImmutableEntityNaturalIdTest extends BaseCoreFunctionalTestCase {
tx.commit(); tx.commit();
s.close(); s.close();
// Statistics stats = sessionFactory().getStatistics(); assertEquals( "Cache hits should be empty", 0, stats.getNaturalIdCacheHitCount() );
// assertEquals( assertEquals( "Cache misses should be empty", 0, stats.getNaturalIdCacheMissCount() );
// "Cache hits should be empty", 0, stats assertEquals( "Cache put should be one after insert", 1, stats.getNaturalIdCachePutCount() );
// .getNaturalIdCacheHitCount() assertEquals( "Query count should be empty", 0, stats.getNaturalIdQueryExecutionCount() );
// );
// assertEquals( s = openSession();
// "First load should be a miss", 1, stats tx = s.beginTransaction();
// .getNaturalIdCacheMissCount()
// ); //Clear caches and reset cache stats
// assertEquals( s.getSessionFactory().getCache().evictNaturalIdRegions();
// "Query result should be added to cache", 3, stats stats.clear();
// .getNaturalIdCachePutCount()
// ); NaturalIdLoadAccess naturalIdLoader = s.byNaturalId( Building.class );
// naturalIdLoader.using( "address", "1210 W. Dayton St." ).using( "city", "Madison" ).using( "state", "WI" );
// Session s = openSession();
// Transaction tx = s.beginTransaction(); // first query
// State france = ( State ) s.load( State.class, 2 ); Building building = (Building) naturalIdLoader.load();
// final NaturalIdLoadAccess naturalIdLoader = s.byNaturalId( Citizen.class ); assertNotNull( building );
// naturalIdLoader.using( "ssn", "1234" ).using( "state", france ); assertEquals( "Cache hits should be empty", 0, stats.getNaturalIdCacheHitCount() );
// assertEquals( "Cache misses should be one after first query", 1, stats.getNaturalIdCacheMissCount() );
// //Not clearing naturalId caches, should be warm from entity loading assertEquals( "Cache put should be one after first query", 1, stats.getNaturalIdCachePutCount() );
// stats.setStatisticsEnabled( true ); assertEquals( "Query count should be one after first query", 1, stats.getNaturalIdQueryExecutionCount() );
// stats.clear();
// assertEquals( // cleanup
// "Cache hits should be empty", 0, stats tx.rollback();
// .getNaturalIdCacheHitCount() s.close();
// );
// //Try two, should be a cache hit
// // first query
// Citizen citizen = (Citizen)naturalIdLoader.load(); s = openSession();
// assertNotNull( citizen ); tx = s.beginTransaction();
// assertEquals( naturalIdLoader = s.byNaturalId( Building.class );
// "Cache hits should be empty", 1, stats naturalIdLoader.using( "address", "1210 W. Dayton St." ).using( "city", "Madison" ).using( "state", "WI" );
// .getNaturalIdCacheHitCount()
// ); // second query
// assertEquals( building = (Building) naturalIdLoader.load();
// "First load should be a miss", 0, stats assertNotNull( building );
// .getNaturalIdCacheMissCount() assertEquals( "Cache hits should be one after second query", 1, stats.getNaturalIdCacheHitCount() );
// ); assertEquals( "Cache misses should be one after second query", 1, stats.getNaturalIdCacheMissCount() );
// assertEquals( assertEquals( "Cache put should be one after second query", 1, stats.getNaturalIdCachePutCount() );
// "Query result should be added to cache", 0, stats assertEquals( "Query count should be one after second query", 1, stats.getNaturalIdQueryExecutionCount() );
// .getNaturalIdCachePutCount()
// ); // Try Deleting
// s.delete( building );
// // cleanup
// tx.rollback(); // third query
// s.close(); building = (Building) naturalIdLoader.load();
assertNull( building );
assertEquals( "Cache hits should be one after second query", 1, stats.getNaturalIdCacheHitCount() );
assertEquals( "Cache misses should be one after second query", 1, stats.getNaturalIdCacheMissCount() );
assertEquals( "Cache put should be one after second query", 1, stats.getNaturalIdCachePutCount() );
assertEquals( "Query count should be one after second query", 1, stats.getNaturalIdQueryExecutionCount() );
// cleanup
tx.commit();
s.close();
//Try three, should be db lookup and miss
s = openSession();
tx = s.beginTransaction();
naturalIdLoader = s.byNaturalId( Building.class );
naturalIdLoader.using( "address", "1210 W. Dayton St." ).using( "city", "Madison" ).using( "state", "WI" );
// second query
building = (Building) naturalIdLoader.load();
assertNull( building );
assertEquals( "Cache hits should be one after third query", 1, stats.getNaturalIdCacheHitCount() );
assertEquals( "Cache misses should be one after third query", 2, stats.getNaturalIdCacheMissCount() );
assertEquals( "Cache put should be one after third query", 1, stats.getNaturalIdCachePutCount() );
assertEquals( "Query count should be one after third query", 2, stats.getNaturalIdQueryExecutionCount() );
// cleanup
tx.rollback();
s.close();
} }
@Override @Override