⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 astparserloadingtest.java

📁 hibernate 开源框架的代码 jar包希望大家能喜欢
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
// $Id: ASTParserLoadingTest.java 11373 2007-03-29 19:09:07Z steve.ebersole@jboss.com $package org.hibernate.test.hql;import java.math.BigDecimal;import java.sql.Date;import java.sql.Time;import java.sql.Timestamp;import java.util.ArrayList;import java.util.Collection;import java.util.HashMap;import java.util.Iterator;import java.util.List;import java.util.Map;import junit.framework.Test;import org.hibernate.Hibernate;import org.hibernate.HibernateException;import org.hibernate.Query;import org.hibernate.QueryException;import org.hibernate.ScrollableResults;import org.hibernate.Session;import org.hibernate.Transaction;import org.hibernate.TypeMismatchException;import org.hibernate.cfg.Configuration;import org.hibernate.cfg.Environment;import org.hibernate.dialect.DB2Dialect;import org.hibernate.dialect.HSQLDialect;import org.hibernate.dialect.MySQLDialect;import org.hibernate.dialect.Oracle9Dialect;import org.hibernate.dialect.PostgreSQLDialect;import org.hibernate.dialect.SQLServerDialect;import org.hibernate.dialect.SybaseDialect;import org.hibernate.dialect.Oracle8iDialect;import org.hibernate.hql.ast.ASTQueryTranslatorFactory;import org.hibernate.junit.functional.FunctionalTestCase;import org.hibernate.junit.functional.FunctionalTestClassTestSuite;import org.hibernate.stat.QueryStatistics;import org.hibernate.test.any.IntegerPropertyValue;import org.hibernate.test.any.PropertySet;import org.hibernate.test.any.PropertyValue;import org.hibernate.test.any.StringPropertyValue;import org.hibernate.test.cid.Customer;import org.hibernate.test.cid.LineItem;import org.hibernate.test.cid.Order;import org.hibernate.test.cid.Product;import org.hibernate.transform.DistinctRootEntityResultTransformer;import org.hibernate.transform.Transformers;import org.hibernate.type.ComponentType;import org.hibernate.type.ManyToOneType;import org.hibernate.type.Type;import org.hibernate.util.StringHelper;import org.slf4j.Logger;import org.slf4j.LoggerFactory;/** * Tests the integration of the new AST parser into the loading of query results using * the Hibernate persisters and loaders. * <p/> * Also used to test the syntax of the resulting sql against the underlying * database, specifically for functionality not supported by the classic * parser. * * @author Steve */public class ASTParserLoadingTest extends FunctionalTestCase {	private static final Logger log = LoggerFactory.getLogger( ASTParserLoadingTest.class );	private List createdAnimalIds = new ArrayList();	public ASTParserLoadingTest(String name) {		super( name );	}	public String[] getMappings() {		return new String[] {				"hql/Animal.hbm.xml",				"hql/FooBarCopy.hbm.xml",				"hql/SimpleEntityWithAssociation.hbm.xml",				"hql/CrazyIdFieldNames.hbm.xml",				"batchfetch/ProductLine.hbm.xml",				"cid/Customer.hbm.xml",				"cid/Order.hbm.xml",				"cid/LineItem.hbm.xml",				"cid/Product.hbm.xml",				"any/Properties.hbm.xml",				"legacy/Commento.hbm.xml",				"legacy/Marelo.hbm.xml"		};	}	public void configure(Configuration cfg) {		super.configure( cfg );		cfg.setProperty( Environment.USE_QUERY_CACHE, "true" );		cfg.setProperty( Environment.GENERATE_STATISTICS, "true" );		cfg.setProperty( Environment.QUERY_TRANSLATOR, ASTQueryTranslatorFactory.class.getName() );	}	public static Test suite() {		return new FunctionalTestClassTestSuite( ASTParserLoadingTest.class );	}	public void testComponentNullnessChecks() {		Session s = openSession();		s.beginTransaction();		Human h = new Human();		h.setName( new Name( "Johnny", 'B', "Goode" ) );		s.save( h );		h = new Human();		h.setName( new Name( "Steve", null, "Ebersole" ) );		s.save( h );		h = new Human();		h.setName( new Name( "Bono", null, null ) );		s.save( h );		h = new Human();		h.setName( new Name( null, null, null ) );		s.save( h );		s.getTransaction().commit();		s.close();		s = openSession();		s.beginTransaction();		List results = s.createQuery( "from Human where name is null" ).list();		assertEquals( 1, results.size() );		results = s.createQuery( "from Human where name is not null" ).list();		assertEquals( 3, results.size() );		s.createQuery( "from Human where ? is null" ).setParameter( 0, null ).list();		s.getTransaction().commit();		s.close();		s = openSession();		s.beginTransaction();		s.createQuery( "delete Human" ).executeUpdate();		s.getTransaction().commit();		s.close();	}	public void testInvalidCollectionDereferencesFail() {		Session s = openSession();		s.beginTransaction();		// control group...		s.createQuery( "from Animal a join a.offspring o where o.description = 'xyz'" ).list();		s.createQuery( "from Animal a join a.offspring o where o.father.description = 'xyz'" ).list();		s.createQuery( "from Animal a join a.offspring o order by o.description" ).list();		s.createQuery( "from Animal a join a.offspring o order by o.father.description" ).list();		try {			s.createQuery( "from Animal a where a.offspring.description = 'xyz'" ).list();			fail( "illegal collection dereference semantic did not cause failure" );		}		catch( QueryException qe ) {			log.trace( "expected failure...", qe );		}		try {			s.createQuery( "from Animal a where a.offspring.father.description = 'xyz'" ).list();			fail( "illegal collection dereference semantic did not cause failure" );		}		catch( QueryException qe ) {			log.trace( "expected failure...", qe );		}		try {			s.createQuery( "from Animal a order by a.offspring.description" ).list();			fail( "illegal collection dereference semantic did not cause failure" );		}		catch( QueryException qe ) {			log.trace( "expected failure...", qe );		}		try {			s.createQuery( "from Animal a order by a.offspring.father.description" ).list();			fail( "illegal collection dereference semantic did not cause failure" );		}		catch( QueryException qe ) {			log.trace( "expected failure...", qe );		}		s.getTransaction().commit();		s.close();	}	/**	 * Copied from {@link HQLTest#testConcatenation}	 */	public void testConcatenation() {		// simple syntax checking...		Session s = openSession();		s.beginTransaction();		s.createQuery( "from Human h where h.nickName = '1' || 'ov' || 'tha' || 'few'" ).list();		s.getTransaction().commit();		s.close();	}	/**	 * Copied from {@link HQLTest#testExpressionWithParamInFunction}	 */	public void testExpressionWithParamInFunction() {		Session s = openSession();		s.beginTransaction();		s.createQuery( "from Animal a where abs(a.bodyWeight-:param) < 2.0" ).setLong( "param", 1 ).list();		s.createQuery( "from Animal a where abs(:param - a.bodyWeight) < 2.0" ).setLong( "param", 1 ).list();		if ( ! ( getDialect() instanceof HSQLDialect ) ) {			// HSQLDB does not like the abs(? - ?) syntax...			s.createQuery( "from Animal where abs(:x - :y) < 2.0" ).setLong( "x", 1 ).setLong( "y", 1 ).list();		}		s.createQuery( "from Animal where lower(upper(:foo)) like 'f%'" ).setString( "foo", "foo" ).list();		s.createQuery( "from Animal a where abs(abs(a.bodyWeight - 1.0 + :param) * abs(length('ffobar')-3)) = 3.0" ).setLong( "param", 1 ).list();		s.createQuery( "from Animal where lower(upper('foo') || upper(:bar)) like 'f%'" ).setString( "bar", "xyz" ).list();		if ( ! ( getDialect() instanceof PostgreSQLDialect || getDialect() instanceof MySQLDialect ) ) {			s.createQuery( "from Animal where abs(cast(1 as float) - cast(:param as float)) = 1.0" ).setLong( "param", 1 ).list();		}		s.getTransaction().commit();		s.close();	}	public void testCrazyIdFieldNames() {		MoreCrazyIdFieldNameStuffEntity top = new MoreCrazyIdFieldNameStuffEntity( "top" );		HeresAnotherCrazyIdFieldName next = new HeresAnotherCrazyIdFieldName( "next" );		top.setHeresAnotherCrazyIdFieldName( next );		MoreCrazyIdFieldNameStuffEntity other = new MoreCrazyIdFieldNameStuffEntity( "other" );		Session s = openSession();		s.beginTransaction();		s.save( next );		s.save( top );		s.save( other );		s.flush();		List results = s.createQuery( "select e.heresAnotherCrazyIdFieldName from MoreCrazyIdFieldNameStuffEntity e where e.heresAnotherCrazyIdFieldName is not null" ).list();		assertEquals( 1, results.size() );		Object result = results.get( 0 );		assertClassAssignability( HeresAnotherCrazyIdFieldName.class, result.getClass() );		assertSame( next, result );		results = s.createQuery( "select e.heresAnotherCrazyIdFieldName.heresAnotherCrazyIdFieldName from MoreCrazyIdFieldNameStuffEntity e where e.heresAnotherCrazyIdFieldName is not null" ).list();		assertEquals( 1, results.size() );		result = results.get( 0 );		assertClassAssignability( Long.class, result.getClass() );		assertEquals( next.getHeresAnotherCrazyIdFieldName(), result );		results = s.createQuery( "select e.heresAnotherCrazyIdFieldName from MoreCrazyIdFieldNameStuffEntity e" ).list();		assertEquals( 1, results.size() );		Iterator itr = s.createQuery( "select e.heresAnotherCrazyIdFieldName from MoreCrazyIdFieldNameStuffEntity e" ).iterate();		assertTrue( itr.hasNext() ); itr.next(); assertFalse( itr.hasNext() );		s.delete( top );		s.delete( next );		s.getTransaction().commit();		s.close();	}	public void testImplicitJoinsInDifferentClauses() {		// HHH-2257 :		// both the classic and ast translators output the same syntactically valid sql		// for all of these cases; the issue is that shallow (iterate) and		// non-shallow (list/scroll) queries return different results because the		// shallow skips the inner join which "weeds out" results from the non-shallow queries.		// The results were initially different depending upon the clause(s) in which the		// implicit join occurred		Session s = openSession();		s.beginTransaction();		SimpleEntityWithAssociation owner = new SimpleEntityWithAssociation( "owner" );		SimpleAssociatedEntity e1 = new SimpleAssociatedEntity( "thing one", owner );		SimpleAssociatedEntity e2 = new SimpleAssociatedEntity( "thing two" );		s.save( e1 );		s.save( e2 );		s.save( owner );		s.getTransaction().commit();		s.close();		checkCounts( "select e.owner from SimpleAssociatedEntity e", 1, "implicit-join in select clause" );		checkCounts( "select e.id, e.owner from SimpleAssociatedEntity e", 1, "implicit-join in select clause" );		// resolved to a "id short cut" when part of the order by clause -> no inner join = no weeding out...		checkCounts( "from SimpleAssociatedEntity e order by e.owner", 2, "implicit-join in order-by clause" );		// resolved to a "id short cut" when part of the group by clause -> no inner join = no weeding out...		checkCounts( "select e.owner.id, count(*) from SimpleAssociatedEntity e group by e.owner", 2, "implicit-join in select and group-by clauses" );	 	s = openSession();		s.beginTransaction();		s.delete( e1 );		s.delete( e2 );		s.delete( owner );		s.getTransaction().commit();		s.close();	}	private void checkCounts(String hql, int expected, String testCondition) {		Session s = openSession();		s.beginTransaction();		int count = determineCount( s.createQuery( hql ).list().iterator() );		assertEquals( "list() [" + testCondition + "]", expected, count );		count = determineCount( s.createQuery( hql ).iterate() );		assertEquals( "iterate() [" + testCondition + "]", expected, count );		s.getTransaction().commit();		s.close();	}	public void testImplicitSelectEntityAssociationInShallowQuery() {		// HHH-2257 :		// both the classic and ast translators output the same syntactically valid sql.		// the issue is that shallow and non-shallow queries return different		// results because the shallow skips the inner join which "weeds out" results		// from the non-shallow queries...		Session s = openSession();		s.beginTransaction();		SimpleEntityWithAssociation owner = new SimpleEntityWithAssociation( "owner" );		SimpleAssociatedEntity e1 = new SimpleAssociatedEntity( "thing one", owner );		SimpleAssociatedEntity e2 = new SimpleAssociatedEntity( "thing two" );		s.save( e1 );		s.save( e2 );		s.save( owner );		s.getTransaction().commit();		s.close();	 	s = openSession();		s.beginTransaction();		int count = determineCount( s.createQuery( "select e.id, e.owner from SimpleAssociatedEntity e" ).list().iterator() );		assertEquals( 1, count ); // thing two would be removed from the result due to the inner join		count = determineCount( s.createQuery( "select e.id, e.owner from SimpleAssociatedEntity e" ).iterate() );		assertEquals( 1, count );		s.getTransaction().commit();		s.close();	 	s = openSession();		s.beginTransaction();		s.delete( e1 );		s.delete( e2 );		s.delete( owner );		s.getTransaction().commit();		s.close();	}	private int determineCount(Iterator iterator) {		int count = 0;		while( iterator.hasNext() ) {			count++;			iterator.next();		}		return count;	}	public void testNestedComponentIsNull() {		// (1) From MapTest originally...		// (2) Was then moved into HQLTest...		// (3) However, a bug fix to EntityType#getIdentifierOrUniqueKeyType (HHH-2138)		// 		caused the classic parser to suddenly start throwing exceptions on

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -