From: <hib...@li...> - 2006-03-07 14:29:21
|
Author: ste...@jb... Date: 2006-03-07 09:29:16 -0500 (Tue, 07 Mar 2006) New Revision: 9565 Modified: trunk/Hibernate3/test/org/hibernate/test/hql/ASTParserLoadingTest.java Log: tests for caching of dynamic instantiation query results Modified: trunk/Hibernate3/test/org/hibernate/test/hql/ASTParserLoadingTest.java =================================================================== --- trunk/Hibernate3/test/org/hibernate/test/hql/ASTParserLoadingTest.java 2006-03-07 14:24:58 UTC (rev 9564) +++ trunk/Hibernate3/test/org/hibernate/test/hql/ASTParserLoadingTest.java 2006-03-07 14:29:16 UTC (rev 9565) @@ -22,7 +22,9 @@ import org.hibernate.Session; import org.hibernate.Transaction; import org.hibernate.QueryException; +import org.hibernate.stat.QueryStatistics; import org.hibernate.cfg.Environment; +import org.hibernate.cfg.Configuration; import org.hibernate.dialect.DB2Dialect; import org.hibernate.dialect.HSQLDialect; import org.hibernate.dialect.MySQLDialect; @@ -54,6 +56,11 @@ private List createdAnimalIds = new ArrayList(); + public static Test suite() { + TestSuite suite = new TestSuite( ASTParserLoadingTest.class ); + return suite; + } + protected String[] getMappings() { // Make sure we are using the new AST parser translator... System.setProperty( Environment.QUERY_TRANSLATOR, "org.hibernate.hql.ast.ASTQueryTranslatorFactory" ); @@ -68,6 +75,12 @@ }; } + protected void configure(Configuration cfg) { + super.configure( cfg ); + cfg.setProperty( Environment.USE_QUERY_CACHE, "true" ); + cfg.setProperty( Environment.GENERATE_STATISTICS, "true" ); + } + public void testCollectionJoinsInSubselect() { // HHH-1248 : initially FromElementFactory treated any explicit join // as an implied join so that theta-style joins would always be used. @@ -702,7 +715,9 @@ String[] dp1 = StringHelper.split("-", dateStr1); String[] dp2 = StringHelper.split("-", dateStr2); for (int i=0; i<3; i++) { - if ( dp1[i].startsWith("0") ) dp1[i] = dp1[i].substring(1); + if ( dp1[i].startsWith( "0" ) ) { + dp1[i] = dp1[i].substring( 1 ); + } assertEquals( dp1[i], dp2[i] ); } } @@ -712,7 +727,9 @@ } public void testCast() { - if ( (getDialect() instanceof MySQLDialect) || (getDialect() instanceof DB2Dialect) ) return; + if ( ( getDialect() instanceof MySQLDialect ) || ( getDialect() instanceof DB2Dialect ) ) { + return; + } Session session = openSession(); Transaction txn = session.beginTransaction(); session.createQuery("from Human h where h.nickName like 'G%'").list(); @@ -1123,6 +1140,21 @@ assertTrue( "Incorrect return type", sr.get(0) instanceof Animal ); sr.close(); + // caching... + QueryStatistics stats = getSessions().getStatistics().getQueryStatistics( "select new Animal(an.description, an.bodyWeight) from Animal an" ); + results = session.createQuery( "select new Animal(an.description, an.bodyWeight) from Animal an" ) + .setCacheable( true ) + .list(); + assertEquals( "incorrect result size", 2, results.size() ); + assertClassAssignability( Animal.class, results.get( 0 ).getClass() ); + long initCacheHits = stats.getCacheHitCount(); + results = session.createQuery( "select new Animal(an.description, an.bodyWeight) from Animal an" ) + .setCacheable( true ) + .list(); + assertEquals( "dynamic intantiation query not served from cache", initCacheHits + 1, stats.getCacheHitCount() ); + assertEquals( "incorrect result size", 2, results.size() ); + assertClassAssignability( Animal.class, results.get( 0 ).getClass() ); + session.close(); destroyTestBaseData(); @@ -1218,14 +1250,6 @@ session.close(); } - - private void assertResultSize(String hql, int size) { - Session session = openSession(); - Transaction txn = session.beginTransaction(); - assertEquals( size, session.createQuery(hql).list().size() ); - txn.commit(); - session.close(); - } public void testSubselectBetween() { assertResultSize("from Animal x where (select max(a.bodyWeight) from Animal a) in (1,2,3)", 0); @@ -1234,10 +1258,13 @@ assertResultSize("from Animal x where (select max(a.bodyWeight) from Animal a) is not null", 0); assertResultSize("from Animal x where exists (select max(a.bodyWeight) from Animal a)", 0); } - - public static Test suite() { - TestSuite suite = new TestSuite( ASTParserLoadingTest.class ); - return suite; + + private void assertResultSize(String hql, int size) { + Session session = openSession(); + Transaction txn = session.beginTransaction(); + assertEquals( size, session.createQuery(hql).list().size() ); + txn.commit(); + session.close(); } } |