FileDocCategorySizeDatePackage
MutableNaturalIdTest.javaAPI DocHibernate 3.2.56699Wed Jun 06 16:33:32 BST 2007org.hibernate.test.naturalid.mutable

MutableNaturalIdTest

public class MutableNaturalIdTest extends org.hibernate.junit.functional.FunctionalTestCase
author
Gavin King

Fields Summary
Constructors Summary
public MutableNaturalIdTest(String str)

		super(str);
	
Methods Summary
public voidconfigure(org.hibernate.cfg.Configuration cfg)

		cfg.setProperty(Environment.USE_SECOND_LEVEL_CACHE, "true");
		cfg.setProperty(Environment.USE_QUERY_CACHE, "true");
		cfg.setProperty(Environment.GENERATE_STATISTICS, "true");
	
public java.lang.String[]getMappings()

		return new String[] { "naturalid/mutable/User.hbm.xml" };
	
public static junit.framework.Testsuite()

		return new FunctionalTestClassTestSuite( MutableNaturalIdTest.class );
	
public voidtestNaturalIdCache()

		Session s = openSession();
		Transaction t = s.beginTransaction();
		
		User u = new User("gavin", "hb", "secret");
		s.persist(u);
		
		t.commit();
		s.close();
		
		getSessions().getStatistics().clear();

		s = openSession();
		t = s.beginTransaction();
		
		u = (User) s.createCriteria(User.class)
			.add( Restrictions.naturalId()
				.set("name", "gavin")
				.set("org", "hb") 
			)
			.setCacheable(true)
			.uniqueResult();
		
		assertNotNull(u);
		
		t.commit();
		s.close();

		assertEquals( getSessions().getStatistics().getQueryExecutionCount(), 1 );
		assertEquals( getSessions().getStatistics().getQueryCacheHitCount(), 0 );
		assertEquals( getSessions().getStatistics().getQueryCachePutCount(), 1 );
		
		s = openSession();
		t = s.beginTransaction();
		
		User v = new User("xam", "hb", "foobar");
		s.persist(v);
		
		t.commit();
		s.close();
		
		getSessions().getStatistics().clear();

		s = openSession();
		t = s.beginTransaction();
		
		u = (User) s.createCriteria( User.class)
			.add( Restrictions.naturalId()
				.set("name", "gavin")
				.set("org", "hb") 
			).setCacheable(true)
			.uniqueResult();
		
		assertNotNull(u);
		
		t.commit();
		s.close();
		
		assertEquals( getSessions().getStatistics().getQueryExecutionCount(), 0 );
		assertEquals( getSessions().getStatistics().getQueryCacheHitCount(), 1 );

		s = openSession();
		t = s.beginTransaction();
		s.createQuery("delete User").executeUpdate();
		t.commit();
		s.close();
	
public voidtestNaturalIdCheck()

		Session s = openSession();
		Transaction t = s.beginTransaction();
		
		User u = new User("gavin", "hb", "secret");
		s.persist(u);
		Field name = u.getClass().getDeclaredField("name");
		name.setAccessible(true);
		name.set(u, "Gavin");
		try {
			s.flush();
			fail();
		}
		catch (HibernateException he) {}
		name.set(u, "gavin");
		s.delete(u);
		t.commit();
		s.close();
	
public voidtestNonexistentNaturalIdCache()

		getSessions().getStatistics().clear();

		Session s = openSession();
		Transaction t = s.beginTransaction();
		
		Object nullUser = s.createCriteria(User.class)
			.add( Restrictions.naturalId()
				.set("name", "gavin")
				.set("org", "hb") 
			)
			.setCacheable(true)
			.uniqueResult();
		
		assertNull(nullUser);
	
		t.commit();
		s.close();
	
		assertEquals( getSessions().getStatistics().getQueryExecutionCount(), 1 );
		assertEquals( getSessions().getStatistics().getQueryCacheHitCount(), 0 );
		assertEquals( getSessions().getStatistics().getQueryCachePutCount(), 0 );
		
		s = openSession();
		t = s.beginTransaction();
		
		User u = new User("gavin", "hb", "secret");
		s.persist(u);
		
		t.commit();
		s.close();
		
		getSessions().getStatistics().clear();

		s = openSession();
		t = s.beginTransaction();
		
		u = (User) s.createCriteria(User.class)
			.add( Restrictions.naturalId()
				.set("name", "gavin")
				.set("org", "hb") 
			)
			.setCacheable(true)
			.uniqueResult();
		
		assertNotNull(u);
		
		t.commit();
		s.close();

		assertEquals( getSessions().getStatistics().getQueryExecutionCount(), 1 );
		assertEquals( getSessions().getStatistics().getQueryCacheHitCount(), 0 );
		assertEquals( getSessions().getStatistics().getQueryCachePutCount(), 1 );
		
		getSessions().getStatistics().clear();

		s = openSession();
		t = s.beginTransaction();
		
		u = (User) s.createCriteria(User.class)
			.add( Restrictions.naturalId()
				.set("name", "gavin")
				.set("org", "hb") 
			).setCacheable(true)
			.uniqueResult();
		
		s.delete(u);
		
		t.commit();
		s.close();
		
		assertEquals( getSessions().getStatistics().getQueryExecutionCount(), 0 );
		assertEquals( getSessions().getStatistics().getQueryCacheHitCount(), 1 );

		getSessions().getStatistics().clear();

		s = openSession();
		t = s.beginTransaction();
		
		nullUser = s.createCriteria(User.class)
			.add( Restrictions.naturalId()
				.set("name", "gavin")
				.set("org", "hb") 
			)
			.setCacheable(true)
			.uniqueResult();
		
		assertNull(nullUser);
	
		t.commit();
		s.close();
	
		assertEquals( getSessions().getStatistics().getQueryExecutionCount(), 1 );
		assertEquals( getSessions().getStatistics().getQueryCacheHitCount(), 0 );
		assertEquals( getSessions().getStatistics().getQueryCachePutCount(), 0 );
		
	
public voidtestQuerying()

		Session s = openSession();
		Transaction t = s.beginTransaction();

		User u = new User("emmanuel", "hb", "bh");
		s.persist(u);

		t.commit();
		s.close();

		s = openSession();
		t = s.beginTransaction();

		u = (User) s.createQuery( "from User u where u.name = :name" )
			.setParameter( "name", "emmanuel" ).uniqueResult();
		assertEquals( "emmanuel", u.getName() );
		s.delete( u );

		t.commit();
		s.close();
	
public voidtestReattachmentNaturalIdCheck()

		Session s = openSession();
		s.beginTransaction();
		User u = new User( "gavin", "hb", "secret" );
		s.persist( u );
		s.getTransaction().commit();
		s.close();

		Field name = u.getClass().getDeclaredField("name");
		name.setAccessible(true);
		name.set(u, "Gavin");
		s = openSession();
		s.beginTransaction();
		try {
			s.update( u );
			s.getTransaction().commit();
		}
		catch( HibernateException expected ) {
			s.getTransaction().rollback();
		}
		catch( Throwable t ) {
			try {
				s.getTransaction().rollback();
			}
			catch ( Throwable ignore ) {
			}
			throw t;
		}
		finally {
			s.close();
		}

		s = openSession();
		s.beginTransaction();
		s.delete( u );
		s.getTransaction().commit();
		s.close();