📄 hqltest.java
字号:
//$Id: HQLTest.java,v 1.138 2005/04/13 06:24:30 oneovthafew Exp $package org.hibernate.test.hql;import java.io.PrintWriter;import java.io.StringWriter;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import junit.framework.Test;import junit.framework.TestSuite;import org.hibernate.dialect.HSQLDialect;import org.hibernate.dialect.function.SQLFunction;import org.hibernate.hql.ast.ConstructorNode;import org.hibernate.hql.ast.DetailedSemanticException;import org.hibernate.hql.ast.IndexNode;import org.hibernate.hql.ast.QuerySyntaxError;import antlr.RecognitionException;/** * Tests cases where the AST based query translator and the 'classic' query translator generate identical SQL. * * @author Gavin King */public class HQLTest extends QueryTranslatorTestCase { public HQLTest(String x) { super( x ); } protected boolean dropAfterFailure() { return false; } //FAILING TESTS: public void testMaxindexHqlFunctionInElementAccessor() { //TODO: broken SQL assertTranslation( "select c from ContainerX c where c.manyToMany[ maxindex(c.manyToMany) ].count = 2" ); assertTranslation( "select c from Container c where c.manyToMany[ maxIndex(c.manyToMany) ].count = 2" ); } public void testMultipleElementAccessorOperators() throws Exception { //TODO: broken SQL assertTranslation( "select c from ContainerX c where c.oneToMany[ c.manyToMany[0].count ].name = 's'" ); assertTranslation( "select c from ContainerX c where c.manyToMany[ c.oneToMany[0].count ].name = 's'" ); } /*public void testSelectMaxElements() throws Exception { //TODO: this is almost correct, but missing a select-clause column alias! assertTranslation("select max( elements(one.manies) ) from org.hibernate.test.legacy.One one"); }*/ public void testKeyManyToOneJoin() { //TODO: new parser generates unnecessary joins (though the query results are correct) assertTranslation( "from Order o left join fetch o.lineItems li left join fetch li.product p" ); assertTranslation( "from Outer o where o.id.master.id.sup.dudu is not null" ); assertTranslation( "from Outer o where o.id.master.id.sup.dudu is not null" ); } public void testDuplicateExplicitJoin() throws Exception { //very minor issue with select clause: assertTranslation( "from Animal a join a.mother m1 join a.mother m2" ); assertTranslation( "from Zoo zoo join zoo.animals an join zoo.mammals m" ); assertTranslation( "from Zoo zoo join zoo.mammals an join zoo.mammals m" ); } // TESTS THAT FAIL ONLY ON DIALECTS WITH THETA-STYLE OUTERJOINS: public void testIndexWithExplicitJoin() throws Exception { //TODO: broken on dialects with theta-style outerjoins: assertTranslation( "from Zoo zoo join zoo.animals an where zoo.mammals[ index(an) ] = an" ); assertTranslation( "from Zoo zoo join zoo.mammals dog where zoo.mammals[ index(dog) ] = dog" ); assertTranslation( "from Zoo zoo join zoo.mammals dog where dog = zoo.mammals[ index(dog) ]" ); } public void testOneToManyMapIndex() throws Exception { //TODO: this breaks on dialects with theta-style outerjoins: assertTranslation( "from Zoo zoo where zoo.mammals['dog'].description like '%black%'" ); assertTranslation( "from Zoo zoo where zoo.mammals['dog'].father.description like '%black%'" ); assertTranslation( "from Zoo zoo where zoo.mammals['dog'].father.id = 1234" ); assertTranslation( "from Zoo zoo where zoo.animals['1234'].description like '%black%'" ); } public void testExplicitJoinMapIndex() throws Exception { //TODO: this breaks on dialects with theta-style outerjoins: assertTranslation( "from Zoo zoo, Dog dog where zoo.mammals['dog'] = dog" ); assertTranslation( "from Zoo zoo join zoo.mammals dog where zoo.mammals['dog'] = dog" ); } public void testIndexFunction() throws Exception { // Instead of doing the pre-processor trick like the existing QueryTranslator, this // is handled by MethodNode. //TODO: broken on dialects with theta-style outerjoins: assertTranslation( "from Zoo zoo join zoo.mammals dog where index(dog) = 'dog'" ); assertTranslation( "from Zoo zoo join zoo.animals an where index(an) = '1234'" ); } public void testSelectCollectionOfValues() throws Exception { //TODO: broken on dialects with theta-style joins ///old parser had a bug where the collection element was not included in return types! assertTranslation( "select baz, date from Baz baz join baz.stringDateMap date where index(date) = 'foo'" ); } public void testCollectionOfValues() throws Exception { //old parser had a bug where the collection element was not returned! //TODO: broken on dialects with theta-style joins assertTranslation( "from Baz baz join baz.stringDateMap date where index(date) = 'foo'" ); } //PASSING TESTS: // Do the simplest test first! public void testFromOnly() throws Exception { // 2004-06-21 [jsd] This test now works with the new AST based QueryTranslatorImpl. assertTranslation( "from Animal" ); assertTranslation( "from Model" ); } public void testJoinPathEndingInValueCollection() { assertTranslation( "select h from Human as h join h.nickNames as nn where h.nickName=:nn1 and (nn=:nn2 or nn=:nn3)" ); } public void testSerialJoinPathEndingInValueCollection() { // HHH-242 assertTranslation( "select h from Human as h join h.friends as f join f.nickNames as nn where h.nickName=:nn1 and (nn=:nn2 or nn=:nn3)" ); } public void testImplicitJoinContainedByCollectionFunction() { // HHH-281 : Implied joins in a collection function (i.e., indices or elements) assertTranslation( "from Human as h where 'shipping' in indices(h.father.addresses)" ); assertTranslation( "from Human as h where 'shipping' in indices(h.father.father.addresses)" ); assertTranslation( "from Human as h where 'sparky' in elements(h.father.nickNames)" ); assertTranslation( "from Human as h where 'sparky' in elements(h.father.father.nickNames)" ); } public void testImpliedJoinInSubselectFrom() { // HHH-276 : Implied joins in a from in a subselect. assertTranslation( "from Animal a where exists( from a.mother.offspring )" ); } public void testSubselectImplicitJoins() { // HHH-276 : Implied joins in a from in a subselect. assertTranslation( "from Simple s where s = some( select sim from Simple sim where sim.other.count=s.other.count )" ); } public void testCollectionOfValuesSize() throws Exception { //SQL *was* missing a comma assertTranslation( "select size(baz.stringDateMap) from org.hibernate.test.legacy.Baz baz" ); } public void testCollectionFunctions() throws Exception { //these are both broken, a join that belongs in the subselect finds its way into the main query assertTranslation( "from Zoo zoo where size(zoo.animals) > 100" ); assertTranslation( "from Zoo zoo where maxindex(zoo.mammals) = 'dog'" ); } public void testImplicitJoinInExplicitJoin() throws Exception { assertTranslation( "from Animal an inner join an.mother.mother gm" ); assertTranslation( "from Animal an inner join an.mother.mother.mother ggm" ); assertTranslation( "from Animal an inner join an.mother.mother.mother.mother gggm" ); } public void testImpliedManyToManyProperty() throws Exception { //missing a table join (SQL correct for a one-to-many, not for a many-to-many) assertTranslation( "select c from ContainerX c where c.manyToMany[0].name = 's'" ); } public void testCollectionSize() throws Exception { //SQL is correct, query spaces *was* missing a table assertTranslation( "select size(zoo.animals) from Zoo zoo" ); } /*public void testCollectionIndexFunctionsInSelect() throws Exception { assertTranslation("select maxindex(zoo.animals) from Zoo zoo"); assertTranslation("select minindex(zoo.animals) from Zoo zoo"); assertTranslation("select indices(zoo.animals) from Zoo zoo"); } public void testCollectionElementFunctionsInSelect() throws Exception { assertTranslation("select maxelement(zoo.animals) from Zoo zoo"); assertTranslation("select minelement(zoo.animals) from Zoo zoo"); assertTranslation("select elements(zoo.animals) from Zoo zoo"); }*/ public void testFetchCollectionOfValues() throws Exception { assertTranslation( "from Baz baz left join fetch baz.stringSet" ); } public void testFetchList() throws Exception { assertTranslation( "from User u join fetch u.permissions" ); } public void testCollectionFetchWithExplicitThetaJoin() { assertTranslation( "select m from Master m1, Master m left join fetch m.details where m.name=m1.name" ); } /*public void testListElementFunctionInSelect() throws Exception { //wrong pk column in select clause! (easy fix?) assertTranslation("select maxelement(u.permissions) from User u"); assertTranslation("select elements(u.permissions) from User u"); }*/ public void testListElementFunctionInWhere() throws Exception { assertTranslation( "from User u where 'read' in elements(u.permissions)" ); assertTranslation( "from User u where 'write' <> all elements(u.permissions)" ); } /*public void testManyToManyElementFunctionInSelect() throws Exception { assertTranslation("select maxelement(human.friends) from Human human"); assertTranslation("select elements(human.friends) from Human human"); }*/ public void testManyToManyMaxElementFunctionInWhere() throws Exception { //completely broken!! assertTranslation( "from Human human where 5 = maxelement(human.friends)" ); } public void testCollectionIndexFunctionsInWhere() throws Exception { assertTranslation( "from Zoo zoo where 4 = maxindex(zoo.animals)" ); assertTranslation( "from Zoo zoo where 2 = minindex(zoo.animals)" ); } public void testCollectionIndicesInWhere() throws Exception { assertTranslation( "from Zoo zoo where 4 > some indices(zoo.animals)" ); assertTranslation( "from Zoo zoo where 4 > all indices(zoo.animals)" ); } public void testIndicesInWhere() throws Exception { assertTranslation( "from Zoo zoo where 4 in indices(zoo.animals)" ); assertTranslation( "from Zoo zoo where exists indices(zoo.animals)" ); } public void testCollectionElementInWhere() throws Exception { assertTranslation( "from Zoo zoo where 4 > some elements(zoo.animals)" ); assertTranslation( "from Zoo zoo where 4 > all elements(zoo.animals)" ); } public void testElementsInWhere() throws Exception { assertTranslation( "from Zoo zoo where 4 in elements(zoo.animals)" ); assertTranslation( "from Zoo zoo where exists elements(zoo.animals)" ); } public void testNull() throws Exception { assertTranslation( "from Human h where h.nickName is null" );
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -