KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hibernate > test > hql > HQLTest


1 //$Id: HQLTest.java,v 1.158 2005/07/20 19:37:05 steveebersole Exp $
2
package org.hibernate.test.hql;
3
4 import java.io.PrintWriter JavaDoc;
5 import java.io.StringWriter JavaDoc;
6 import java.util.ArrayList JavaDoc;
7 import java.util.HashMap JavaDoc;
8 import java.util.List JavaDoc;
9
10 import junit.framework.Test;
11 import junit.framework.TestSuite;
12
13 import org.hibernate.dialect.HSQLDialect;
14 import org.hibernate.dialect.MySQLDialect;
15 import org.hibernate.dialect.Oracle9Dialect;
16 import org.hibernate.dialect.function.SQLFunction;
17 import org.hibernate.hql.ast.DetailedSemanticException;
18 import org.hibernate.hql.ast.QuerySyntaxException;
19 import org.hibernate.hql.ast.tree.ConstructorNode;
20 import org.hibernate.hql.ast.tree.DotNode;
21 import org.hibernate.hql.ast.tree.IndexNode;
22 import org.hibernate.hql.ast.tree.SelectClause;
23
24 import antlr.RecognitionException;
25
26 /**
27  * Tests cases where the AST based query translator and the 'classic' query translator generate identical SQL.
28  *
29  * @author Gavin King
30  */

31 public class HQLTest extends QueryTranslatorTestCase {
32
33     public HQLTest(String JavaDoc x) {
34         super( x );
35         SelectClause.VERSION2_SQL = true;
36     }
37
38     protected boolean dropAfterFailure() {
39         return false;
40     }
41
42     //FAILING TESTS:
43

44     public void testCollectionOrderBy() {
45         assertTranslation("from Animal a join a.offspring o order by a.description");
46         assertTranslation("from Animal a join fetch a.offspring order by a.description");
47         assertTranslation("from Animal a join a.offspring o order by a.description, o.description");
48     }
49     
50     public void testExpressionWithParamInFunction() {
51         assertTranslation("from Animal a where abs(a.bodyWeight-:param) < 2.0");
52         assertTranslation("from Animal a where abs(:param - a.bodyWeight) < 2.0");
53         assertTranslation("from Animal where abs(:x - :y) < 2.0");
54         assertTranslation("from Animal where abs(cast(1 as float) - cast(:param as float)) = 1.0");
55         assertTranslation("from Animal where lower(upper(:foo)) like 'f%'");
56         assertTranslation("from Animal a where abs(abs(a.bodyWeight - 1.0 + :param) * abs(length('ffobar')-3)) = 3.0");
57         if ( getDialect() instanceof MySQLDialect ) return;
58         assertTranslation("from Animal where lower(upper('foo') || upper(:bar)) like 'f%'");
59     }
60     
61     public void testCompositeKeysWithPropertyNamedId() {
62         assertTranslation( "select e.id.id from EntityWithCrazyCompositeKey e" );
63         assertTranslation( "select max(e.id.id) from EntityWithCrazyCompositeKey e" );
64     }
65
66     public void testMaxindexHqlFunctionInElementAccessor() {
67         //TODO: broken SQL
68
assertTranslation( "select c from ContainerX c where c.manyToMany[ maxindex(c.manyToMany) ].count = 2" );
69         assertTranslation( "select c from Container c where c.manyToMany[ maxIndex(c.manyToMany) ].count = 2" );
70     }
71
72     public void testMultipleElementAccessorOperators() throws Exception JavaDoc {
73         //TODO: broken SQL
74
assertTranslation( "select c from ContainerX c where c.oneToMany[ c.manyToMany[0].count ].name = 's'" );
75         assertTranslation( "select c from ContainerX c where c.manyToMany[ c.oneToMany[0].count ].name = 's'" );
76     }
77
78     /*public void testSelectMaxElements() throws Exception {
79         //TODO: this is almost correct, but missing a select-clause column alias!
80         assertTranslation("select max( elements(one.manies) ) from org.hibernate.test.legacy.One one");
81     }*/

82
83     public void testKeyManyToOneJoin() {
84         //TODO: new parser generates unnecessary joins (though the query results are correct)
85
assertTranslation( "from Order o left join fetch o.lineItems li left join fetch li.product p" );
86         assertTranslation( "from Outer o where o.id.master.id.sup.dudu is not null" );
87         assertTranslation( "from Outer o where o.id.master.id.sup.dudu is not null" );
88     }
89
90     public void testDuplicateExplicitJoin() throws Exception JavaDoc {
91         //very minor issue with select clause:
92
assertTranslation( "from Animal a join a.mother m1 join a.mother m2" );
93         assertTranslation( "from Zoo zoo join zoo.animals an join zoo.mammals m" );
94         assertTranslation( "from Zoo zoo join zoo.mammals an join zoo.mammals m" );
95     }
96
97     // TESTS THAT FAIL ONLY ON DIALECTS WITH THETA-STYLE OUTERJOINS:
98

99     public void testIndexWithExplicitJoin() throws Exception JavaDoc {
100         //TODO: broken on dialects with theta-style outerjoins:
101
assertTranslation( "from Zoo zoo join zoo.animals an where zoo.mammals[ index(an) ] = an" );
102         assertTranslation( "from Zoo zoo join zoo.mammals dog where zoo.mammals[ index(dog) ] = dog" );
103         assertTranslation( "from Zoo zoo join zoo.mammals dog where dog = zoo.mammals[ index(dog) ]" );
104     }
105
106     public void testOneToManyMapIndex() throws Exception JavaDoc {
107         //TODO: this breaks on dialects with theta-style outerjoins:
108
assertTranslation( "from Zoo zoo where zoo.mammals['dog'].description like '%black%'" );
109         assertTranslation( "from Zoo zoo where zoo.mammals['dog'].father.description like '%black%'" );
110         assertTranslation( "from Zoo zoo where zoo.mammals['dog'].father.id = 1234" );
111         assertTranslation( "from Zoo zoo where zoo.animals['1234'].description like '%black%'" );
112     }
113
114     public void testExplicitJoinMapIndex() throws Exception JavaDoc {
115         //TODO: this breaks on dialects with theta-style outerjoins:
116
assertTranslation( "from Zoo zoo, Dog dog where zoo.mammals['dog'] = dog" );
117         assertTranslation( "from Zoo zoo join zoo.mammals dog where zoo.mammals['dog'] = dog" );
118     }
119
120     public void testIndexFunction() throws Exception JavaDoc {
121         // Instead of doing the pre-processor trick like the existing QueryTranslator, this
122
// is handled by MethodNode.
123
//TODO: broken on dialects with theta-style outerjoins:
124
assertTranslation( "from Zoo zoo join zoo.mammals dog where index(dog) = 'dog'" );
125         assertTranslation( "from Zoo zoo join zoo.animals an where index(an) = '1234'" );
126     }
127
128     public void testSelectCollectionOfValues() throws Exception JavaDoc {
129         //TODO: broken on dialects with theta-style joins
130
///old parser had a bug where the collection element was not included in return types!
131
assertTranslation( "select baz, date from Baz baz join baz.stringDateMap date where index(date) = 'foo'" );
132     }
133
134     public void testCollectionOfValues() throws Exception JavaDoc {
135         //old parser had a bug where the collection element was not returned!
136
//TODO: broken on dialects with theta-style joins
137
assertTranslation( "from Baz baz join baz.stringDateMap date where index(date) = 'foo'" );
138     }
139
140     public void testHHH719() throws Exception JavaDoc {
141         assertTranslation("from Baz b order by org.bazco.SpecialFunction(b.id)");
142     }
143
144     //PASSING TESTS:
145

146     public void testExplicitJoinsInSubquery() {
147         // test for HHH-557,
148
// TODO : this passes regardless because the only difference between the two sqls is one extra comma
149
// (commas are eaten by the tokenizer during asserTranslation when building the token maps).
150
assertTranslation(
151                 "from org.hibernate.test.hql.Animal as animal " +
152                 "where animal.id in (" +
153                 " select a.id " +
154                 " from org.hibernate.test.hql.Animal as a " +
155                 " left join a.mother as mo" +
156                 ")"
157         );
158     }
159
160     public void testImplicitJoinsInGroupBy() {
161         assertTranslation(
162                 "select o.mother.bodyWeight, count(distinct o) " +
163                 "from Animal an " +
164                 " join an.offspring as o " +
165                 "group by o.mother.bodyWeight"
166         );
167     }
168
169     public void testCrazyIdFieldNames() {
170         DotNode.useThetaStyleImplicitJoins = true;
171         // only regress against non-scalar forms as there appears to be a bug in the classic translator
172
// in regards to this issue also. Specifically, it interprets the wrong return type, though it gets
173
// the sql "correct" :/
174

175         String JavaDoc hql = "select e.heresAnotherCrazyIdFieldName from MoreCrazyIdFieldNameStuffEntity e where e.heresAnotherCrazyIdFieldName is not null";
176         assertTranslation( hql, new HashMap JavaDoc(), false, ( String JavaDoc ) null );
177
178         hql = "select e.heresAnotherCrazyIdFieldName.heresAnotherCrazyIdFieldName from MoreCrazyIdFieldNameStuffEntity e where e.heresAnotherCrazyIdFieldName is not null";
179         assertTranslation( hql, new HashMap JavaDoc(), false, ( String JavaDoc ) null );
180
181         DotNode.useThetaStyleImplicitJoins = false;
182     }
183     
184     public void testSizeFunctionAndProperty() {
185         assertTranslation("from Animal a where a.offspring.size > 0");
186         assertTranslation("from Animal a join a.offspring where a.offspring.size > 1");
187         assertTranslation("from Animal a where size(a.offspring) > 0");
188         assertTranslation("from Animal a join a.offspring o where size(a.offspring) > 1");
189         assertTranslation("from Animal a where size(a.offspring) > 1 and size(a.offspring) < 100");
190         
191         assertTranslation("from Human a where a.family.size > 0");
192         assertTranslation("from Human a join a.family where a.family.size > 1");
193         assertTranslation("from Human a where size(a.family) > 0");
194         assertTranslation("from Human a join a.family o where size(a.family) > 1");
195         assertTranslation("from Human a where a.family.size > 0 and a.family.size < 100");
196 }
197
198     // Do the simplest test first!
199
public void testFromOnly() throws Exception JavaDoc {
200         // 2004-06-21 [jsd] This test now works with the new AST based QueryTranslatorImpl.
201
assertTranslation( "from Animal" );
202         assertTranslation( "from Model" );
203     }
204
205     public void testJoinPathEndingInValueCollection() {
206         assertTranslation( "select h from Human as h join h.nickNames as nn where h.nickName=:nn1 and (nn=:nn2 or nn=:nn3)" );
207     }
208
209     public void testSerialJoinPathEndingInValueCollection() {
210         // HHH-242
211
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)" );
212     }
213
214     public void testImplicitJoinContainedByCollectionFunction() {
215         // HHH-281 : Implied joins in a collection function (i.e., indices or elements)
216
assertTranslation( "from Human as h where 'shipping' in indices(h.father.addresses)" );
217         assertTranslation( "from Human as h where 'shipping' in indices(h.father.father.addresses)" );
218         assertTranslation( "from Human as h where 'sparky' in elements(h.father.nickNames)" );
219         assertTranslation( "from Human as h where 'sparky' in elements(h.father.father.nickNames)" );
220     }
221
222
223     public void testImpliedJoinInSubselectFrom() {
224         // HHH-276 : Implied joins in a from in a subselect.
225
assertTranslation( "from Animal a where exists( from a.mother.offspring )" );
226     }
227
228     public void testSubselectImplicitJoins() {
229         // HHH-276 : Implied joins in a from in a subselect.
230
assertTranslation( "from Simple s where s = some( select sim from Simple sim where sim.other.count=s.other.count )" );
231     }
232
233
234     public void testCollectionOfValuesSize() throws Exception JavaDoc {
235         //SQL *was* missing a comma
236
assertTranslation( "select size(baz.stringDateMap) from org.hibernate.test.legacy.Baz baz" );
237     }
238
239     public void testCollectionFunctions() throws Exception JavaDoc {
240         //these are both broken, a join that belongs in the subselect finds its way into the main query
241
assertTranslation( "from Zoo zoo where size(zoo.animals) > 100" );
242         assertTranslation( "from Zoo zoo where maxindex(zoo.mammals) = 'dog'" );
243     }
244
245     public void testImplicitJoinInExplicitJoin() throws Exception JavaDoc {
246         assertTranslation( "from Animal an inner join an.mother.mother gm" );
247         assertTranslation( "from Animal an inner join an.mother.mother.mother ggm" );
248         assertTranslation( "from Animal an inner join an.mother.mother.mother.mother gggm" );
249     }
250
251     public void testImpliedManyToManyProperty() throws Exception JavaDoc {
252         //missing a table join (SQL correct for a one-to-many, not for a many-to-many)
253
assertTranslation( "select c from ContainerX c where c.manyToMany[0].name = 's'" );
254     }
255
256     public void testCollectionSize() throws Exception JavaDoc {
257         //SQL is correct, query spaces *was* missing a table
258
assertTranslation( "select size(zoo.animals) from Zoo zoo" );
259     }
260
261     /*public void testCollectionIndexFunctionsInSelect() throws Exception {
262         assertTranslation("select maxindex(zoo.animals) from Zoo zoo");
263         assertTranslation("select minindex(zoo.animals) from Zoo zoo");
264         assertTranslation("select indices(zoo.animals) from Zoo zoo");
265     }
266
267     public void testCollectionElementFunctionsInSelect() throws Exception {
268         assertTranslation("select maxelement(zoo.animals) from Zoo zoo");
269         assertTranslation("select minelement(zoo.animals) from Zoo zoo");
270         assertTranslation("select elements(zoo.animals) from Zoo zoo");
271     }*/

272     
273     public void testFetchCollectionOfValues() throws Exception JavaDoc {
274         assertTranslation( "from Baz baz left join fetch baz.stringSet" );
275     }
276
277     public void testFetchList() throws Exception JavaDoc {
278         assertTranslation( "from User u join fetch u.permissions" );
279     }
280
281     public void testCollectionFetchWithExplicitThetaJoin() {
282         assertTranslation( "select m from Master m1, Master m left join fetch m.details where m.name=m1.name" );
283     }
284
285     /*public void testListElementFunctionInSelect() throws Exception {
286         //wrong pk column in select clause! (easy fix?)
287         assertTranslation("select maxelement(u.permissions) from User u");
288         assertTranslation("select elements(u.permissions) from User u");
289     }*/

290
291     public void testListElementFunctionInWhere() throws Exception JavaDoc {
292         assertTranslation( "from User u where 'read' in elements(u.permissions)" );
293         assertTranslation( "from User u where 'write' <> all elements(u.permissions)" );
294     }
295     
296     /*public void testManyToManyElementFunctionInSelect() throws Exception {
297         assertTranslation("select maxelement(human.friends) from Human human");
298         assertTranslation("select elements(human.friends) from Human human");
299     }*/

300
301     public void testManyToManyMaxElementFunctionInWhere() throws Exception JavaDoc {
302         //completely broken!!
303
assertTranslation( "from Human human where 5 = maxelement(human.friends)" );
304     }
305
306     public void testCollectionIndexFunctionsInWhere() throws Exception JavaDoc {
307         assertTranslation( "from Zoo zoo where 4 = maxindex(zoo.animals)" );
308         assertTranslation( "from Zoo zoo where 2 = minindex(zoo.animals)" );
309     }
310
311     public void testCollectionIndicesInWhere() throws Exception JavaDoc {
312         assertTranslation( "from Zoo zoo where 4 > some indices(zoo.animals)" );
313         assertTranslation( "from Zoo zoo where 4 > all indices(zoo.animals)" );
314     }
315
316     public void testIndicesInWhere() throws Exception JavaDoc {
317         assertTranslation( "from Zoo zoo where 4 in indices(zoo.animals)" );
318         assertTranslation( "from Zoo zoo where exists indices(zoo.animals)" );
319     }
320
321     public void testCollectionElementInWhere() throws Exception JavaDoc {
322         assertTranslation( "from Zoo zoo where 4 > some elements(zoo.animals)" );
323         assertTranslation( "from Zoo zoo where 4 > all elements(zoo.animals)" );
324     }
325
326     public void testElementsInWhere() throws Exception JavaDoc {
327         assertTranslation( "from Zoo zoo where 4 in elements(zoo.animals)" );
328         assertTranslation( "from Zoo zoo where exists elements(zoo.animals)" );
329     }
330
331     public void testNull() throws Exception JavaDoc {
332         assertTranslation( "from Human h where h.nickName is null" );
333         assertTranslation( "from Human h where h.nickName is not null" );
334     }
335
336     public void testSubstitutions() throws Exception JavaDoc {
337         assertTranslation( "from Human h where h.pregnant = true" );
338         assertTranslation( "from Human h where h.pregnant = foo" );
339     }
340
341     public void testWhere() throws Exception JavaDoc {
342         assertTranslation( "from Animal an where an.bodyWeight > 10" );
343         // 2004-06-26 [jsd] This one requires NOT GT => LE transform.
344
assertTranslation( "from Animal an where not an.bodyWeight > 10" );
345         assertTranslation( "from Animal an where an.bodyWeight between 0 and 10" );
346         assertTranslation( "from Animal an where an.bodyWeight not between 0 and 10" );
347         assertTranslation( "from Animal an where sqrt(an.bodyWeight)/2 > 10" );
348         // 2004-06-27 [jsd] Recognize 'is null' properly. Generate 'and' and 'or' as well.
349
assertTranslation( "from Animal an where (an.bodyWeight > 10 and an.bodyWeight < 100) or an.bodyWeight is null" );
350     }
351     
352     public void testEscapedQuote() throws Exception JavaDoc {
353         assertTranslation( "from Human h where h.nickName='1 ov''tha''few'");
354     }
355
356     public void testCaseWhenElse() {
357         assertTranslation( "from Human h where case when h.nickName='1ovthafew' then 'Gavin' when h.nickName='turin' then 'Christian' else h.nickName end = h.name.first" );
358     }
359     
360     public void testCaseExprWhenElse() {
361         assertTranslation( "from Human h where case h.nickName when '1ovthafew' then 'Gavin' when 'turin' then 'Christian' else h.nickName end = h.name.first" );
362     }
363
364     public void testInvalidHql() throws Exception JavaDoc {
365         Exception JavaDoc newException = compileBadHql( "from Animal foo where an.bodyWeight > 10", false );
366         assertTrue( "Wrong exception type!", newException instanceof QuerySyntaxException );
367         newException = compileBadHql( "select an.name from Animal foo", false );
368         assertTrue( "Wrong exception type!", newException instanceof QuerySyntaxException );
369         newException = compileBadHql( "from Animal foo where an.verybogus > 10", false );
370         assertTrue( "Wrong exception type!", newException instanceof QuerySyntaxException );
371         newException = compileBadHql( "select an.boguspropertyname from Animal foo", false );
372         assertTrue( "Wrong exception type!", newException instanceof QuerySyntaxException );
373         newException = compileBadHql( "select an.name", false );
374         assertTrue( "Wrong exception type!", newException instanceof QuerySyntaxException );
375         newException = compileBadHql( "from Animal an where (((an.bodyWeight > 10 and an.bodyWeight < 100)) or an.bodyWeight is null", false );
376         assertTrue( "Wrong exception type!", newException instanceof QuerySyntaxException );
377         newException = compileBadHql( "from Animal an where an.bodyWeight is null where an.bodyWeight is null", false );
378         assertTrue( "Wrong exception type!", newException instanceof QuerySyntaxException );
379         newException = compileBadHql( "from where name='foo'", false );
380         assertTrue( "Wrong exception type!", newException instanceof QuerySyntaxException );
381         newException = compileBadHql( "from NonexistentClass where name='foo'", false );
382         assertTrue( "Wrong exception type!", newException instanceof QuerySyntaxException );
383         newException = compileBadHql( "select new FOO_BOGUS_Animal(an.description, an.bodyWeight) from Animal an", false );
384         assertTrue( "Wrong exception type!", newException instanceof QuerySyntaxException );
385         newException = compileBadHql( "select new Animal(an.description, an.bodyWeight, 666) from Animal an", false );
386         assertTrue( "Wrong exception type!", newException instanceof QuerySyntaxException );
387
388     }
389
390     public void testWhereBetween() throws Exception JavaDoc {
391         // 2004-08-31 [jsd] This "just worked"! Woohoo!
392
assertTranslation( "from Animal an where an.bodyWeight between 1 and 10" );
393     }
394
395     public void testConcatenation() {
396         if ( getDialect() instanceof MySQLDialect ) return; //MySQL uses concat(x, y, z)
397
assertTranslation("from Human h where h.nickName = '1' || 'ov' || 'tha' || 'few'");
398     }
399
400     public void testWhereLike() throws Exception JavaDoc {
401         assertTranslation( "from Animal a where a.description like '%black%'" );
402         assertTranslation( "from Animal an where an.description like '%fat%'" );
403         assertTranslation( "from Animal an where lower(an.description) like '%fat%'" );
404     }
405
406     public void testWhereIn() throws Exception JavaDoc {
407         assertTranslation( "from Animal an where an.description in ('fat', 'skinny')" );
408     }
409
410     public void testLiteralInFunction() throws Exception JavaDoc {
411         assertTranslation( "from Animal an where an.bodyWeight > abs(5)" );
412         assertTranslation( "from Animal an where an.bodyWeight > abs(-5)" );
413     }
414
415     public void testExpressionInFunction() throws Exception JavaDoc {
416         assertTranslation( "from Animal an where an.bodyWeight > abs(3-5)" );
417         assertTranslation( "from Animal an where an.bodyWeight > abs(3/5)" );
418         assertTranslation( "from Animal an where an.bodyWeight > abs(3+5)" );
419         assertTranslation( "from Animal an where an.bodyWeight > abs(3*5)" );
420         SQLFunction concat = (SQLFunction) getDialect().getFunctions().get("concat");
421         List JavaDoc list = new ArrayList JavaDoc(); list.add("'fat'"); list.add("'skinny'");
422         assertTranslation( "from Animal an where an.description = " + concat.render(list, getSessionFactoryImplementor()) );
423     }
424
425     public void testNotOrWhereClause() {
426         assertTranslation( "from Simple s where 'foo'='bar' or not 'foo'='foo'" );
427         assertTranslation( "from Simple s where 'foo'='bar' or not ('foo'='foo')" );
428         assertTranslation( "from Simple s where not ( 'foo'='bar' or 'foo'='foo' )" );
429         assertTranslation( "from Simple s where not ( 'foo'='bar' and 'foo'='foo' )" );
430         assertTranslation( "from Simple s where not ( 'foo'='bar' and 'foo'='foo' ) or not ('x'='y')" );
431         assertTranslation( "from Simple s where not ( 'foo'='bar' or 'foo'='foo' ) and not ('x'='y')" );
432         assertTranslation( "from Simple s where not ( 'foo'='bar' or 'foo'='foo' ) and 'x'='y'" );
433         assertTranslation( "from Simple s where not ( 'foo'='bar' and 'foo'='foo' ) or 'x'='y'" );
434         assertTranslation( "from Simple s where 'foo'='bar' and 'foo'='foo' or not 'x'='y'" );
435         assertTranslation( "from Simple s where 'foo'='bar' or 'foo'='foo' and not 'x'='y'" );
436         assertTranslation( "from Simple s where ('foo'='bar' and 'foo'='foo') or 'x'='y'" );
437         assertTranslation( "from Simple s where ('foo'='bar' or 'foo'='foo') and 'x'='y'" );
438         assertTranslation( "from Simple s where not( upper( s.name ) ='yada' or 1=2 or 'foo'='bar' or not('foo'='foo') or 'foo' like 'bar' )" );
439     }
440
441     public void testComplexExpressionInFunction() throws Exception JavaDoc {
442         assertTranslation( "from Animal an where an.bodyWeight > abs((3-5)/4)" );
443     }
444
445     public void testStandardFunctions() throws Exception JavaDoc {
446         assertTranslation( "from Animal where current_date = current_time" );
447         assertTranslation( "from Animal a where upper(a.description) = 'FAT'" );
448         assertTranslation( "select lower(a.description) from Animal a" );
449     }
450
451     public void testOrderBy() throws Exception JavaDoc {
452         assertTranslation( "from Animal an order by an.bodyWeight" );
453         assertTranslation( "from Animal an order by an.bodyWeight asc" );
454         assertTranslation( "from Animal an order by an.bodyWeight desc" );
455         assertTranslation( "from Human h order by sqrt(h.bodyWeight), year(h.birthdate)" );
456         assertTranslation( "from Animal an order by sqrt(an.bodyWeight*4)/2" );
457         assertTranslation( "from Animal an order by an.mother.bodyWeight" );
458         assertTranslation( "from Animal an order by an.bodyWeight, an.description" );
459         assertTranslation( "from Animal an order by an.bodyWeight asc, an.description desc" );
460     }
461
462     public void testGroupByFunction() {
463         if ( getDialect() instanceof Oracle9Dialect ) return;
464         assertTranslation( "select count(*) from Human h group by year(h.birthdate)" );
465         assertTranslation( "select count(*) from Human h group by trunc( sqrt(h.bodyWeight*4)/2 )" );
466         assertTranslation( "select count(*) from Human h group by year(sysdate)" );
467     }
468
469     public void testPolymorphism() throws Exception JavaDoc {
470         HashMap JavaDoc replacements = new HashMap JavaDoc();
471         replacements.put( "false", "0" );
472         replacements.put( "true", "1" );
473         assertTranslation( "from Mammal" );
474         assertTranslation( "from Dog" );
475         assertTranslation( "from Mammal m where m.pregnant = false and m.bodyWeight > 10", replacements );
476         assertTranslation( "from Dog d where d.pregnant = false and d.bodyWeight > 10", replacements );
477     }
478
479     public void testTokenReplacement() throws Exception JavaDoc {
480         HashMap JavaDoc replacements = new HashMap JavaDoc();
481         replacements.put( "false", "0" );
482         replacements.put( "true", "1" );
483         assertTranslation( "from Mammal m where m.pregnant = false and m.bodyWeight > 10",
484                 replacements );
485     }
486
487     public void testProduct() throws Exception JavaDoc {
488         HashMap JavaDoc replacements = new HashMap JavaDoc();
489         replacements.put( "false", "0" );
490         replacements.put( "true", "1" );
491         assertTranslation( "from Animal, Animal" );
492         assertTranslation( "from Animal x, Animal y where x.bodyWeight = y.bodyWeight" );
493         assertTranslation( "from Animal x, Mammal y where x.bodyWeight = y.bodyWeight and not y.pregnant = true", replacements );
494         assertTranslation( "from Mammal, Mammal" );
495     }
496
497     public void testJoinedSubclassProduct() throws Exception JavaDoc {
498         assertTranslation( "from PettingZoo, PettingZoo" ); //product of two subclasses
499
}
500
501     public void testProjectProduct() throws Exception JavaDoc {
502         assertTranslation( "select x from Human x, Human y where x.nickName = y.nickName" );
503         assertTranslation( "select x, y from Human x, Human y where x.nickName = y.nickName" );
504     }
505
506     public void testExplicitEntityJoins() throws Exception JavaDoc {
507         assertTranslation( "from Animal an inner join an.mother mo" );
508         assertTranslation( "from Animal an left outer join an.mother mo" );
509         assertTranslation( "from Animal an left outer join fetch an.mother" );
510     }
511
512     public void testMultipleExplicitEntityJoins() throws Exception JavaDoc {
513         assertTranslation( "from Animal an inner join an.mother mo inner join mo.mother gm" );
514         assertTranslation( "from Animal an left outer join an.mother mo left outer join mo.mother gm" );
515         assertTranslation( "from Animal an inner join an.mother m inner join an.father f" );
516         assertTranslation( "from Animal an left join fetch an.mother m left join fetch an.father f" );
517     }
518
519     public void testMultipleExplicitJoins() throws Exception JavaDoc {
520         assertTranslation( "from Animal an inner join an.mother mo inner join an.offspring os" );
521         assertTranslation( "from Animal an left outer join an.mother mo left outer join an.offspring os" );
522     }
523
524     public void testExplicitEntityJoinsWithRestriction() throws Exception JavaDoc {
525         assertTranslation( "from Animal an inner join an.mother mo where an.bodyWeight < mo.bodyWeight" );
526     }
527
528     public void testIdProperty() throws Exception JavaDoc {
529         assertTranslation( "from Animal a where a.mother.id = 12" );
530     }
531
532     public void testSubclassAssociation() throws Exception JavaDoc {
533         assertTranslation( "from DomesticAnimal da join da.owner o where o.nickName = 'Gavin'" );
534         assertTranslation( "from DomesticAnimal da left join fetch da.owner" );
535         assertTranslation( "from Human h join h.pets p where p.pregnant = 1" );
536         assertTranslation( "from Human h join h.pets p where p.bodyWeight > 100" );
537         assertTranslation( "from Human h left join fetch h.pets" );
538     }
539
540     public void testExplicitCollectionJoins() throws Exception JavaDoc {
541         assertTranslation( "from Animal an inner join an.offspring os" );
542         assertTranslation( "from Animal an left outer join an.offspring os" );
543     }
544
545     public void testExplicitOuterJoinFetch() throws Exception JavaDoc {
546         assertTranslation( "from Animal an left outer join fetch an.offspring" );
547     }
548
549     public void testExplicitOuterJoinFetchWithSelect() throws Exception JavaDoc {
550         assertTranslation( "select an from Animal an left outer join fetch an.offspring" );
551     }
552
553     public void testExplicitJoins() throws Exception JavaDoc {
554         HashMap JavaDoc replacements = new HashMap JavaDoc();
555         replacements.put( "false", "0" );
556         replacements.put( "true", "1" );
557         assertTranslation( "from Zoo zoo join zoo.mammals mam where mam.pregnant = true and mam.description like '%white%'", replacements );
558         assertTranslation( "from Zoo zoo join zoo.animals an where an.description like '%white%'" );
559     }
560
561     /**
562      * Test for HHH-559
563      */

564     public void testMultibyteCharacterConstant() throws Exception JavaDoc {
565         assertTranslation( "from Zoo zoo join zoo.animals an where an.description like '%\u4e2d%'" );
566     }
567
568     public void testImplicitJoins() throws Exception JavaDoc {
569         // Two dots...
570
assertTranslation( "from Animal an where an.mother.bodyWeight > ?" );
571         assertTranslation( "from Animal an where an.mother.bodyWeight > 10" );
572         assertTranslation( "from Dog dog where dog.mother.bodyWeight > 10" );
573         // Three dots...
574
assertTranslation( "from Animal an where an.mother.mother.bodyWeight > 10" );
575         // The new QT doesn't throw an exception here, so this belongs in ASTQueryTranslator test. [jsd]
576
// assertTranslation( "from Animal an where an.offspring.mother.bodyWeight > 10" );
577
// Is not null (unary postfix operator)
578
assertTranslation( "from Animal an where an.mother is not null" );
579         // ID property shortut (no implicit join)
580
assertTranslation( "from Animal an where an.mother.id = 123" );
581     }
582
583     public void testImplicitJoinInSelect() {
584         assertTranslation( "select foo, foo.long from Foo foo" );
585         DotNode.useThetaStyleImplicitJoins = true;
586         assertTranslation( "select foo.foo from Foo foo" );
587         assertTranslation( "select foo, foo.foo from Foo foo" );
588         assertTranslation( "select foo.foo from Foo foo where foo.foo is not null" );
589         DotNode.useThetaStyleImplicitJoins = false;
590     }
591
592     public void testSelectExpressions() {
593         DotNode.useThetaStyleImplicitJoins = true;
594         assertTranslation( "select an.mother.mother from Animal an" );
595         assertTranslation( "select an.mother.mother.mother from Animal an" );
596         assertTranslation( "select an.mother.mother.bodyWeight from Animal an" );
597         assertTranslation( "select an.mother.zoo.id from Animal an" );
598         assertTranslation( "select user.human.zoo.id from User user" );
599         assertTranslation( "select u.userName, u.human.name.first from User u" );
600         assertTranslation( "select u.human.name.last, u.human.name.first from User u" );
601         assertTranslation( "select bar.baz.name from Bar bar" );
602         assertTranslation( "select bar.baz.name, bar.baz.count from Bar bar" );
603         DotNode.useThetaStyleImplicitJoins = false;
604     }
605
606     public void testSelectStandardFunctionsNoParens() throws Exception JavaDoc {
607         assertTranslation( "select current_date, current_time, current_timestamp from Animal" );
608     }
609
610     public void testMapIndex() throws Exception JavaDoc {
611         assertTranslation( "from User u where u.permissions['hibernate']='read'" );
612     }
613
614     /*public void testCollectionFunctionsInSelect() {
615         //sql is correct, just different order in select clause
616         assertTranslation("select baz, size(baz.stringSet), count( distinct elements(baz.stringSet) ), max( elements(baz.stringSet) ) from Baz baz group by baz");
617     }
618
619     public void testSelectElements() throws Exception {
620         assertTranslation( "select elements(fum1.friends) from org.hibernate.test.legacy.Fum fum1" );
621         assertTranslation( "select elements(one.manies) from org.hibernate.test.legacy.One one" );
622     }*/

623
624     public void testNamedParameters() throws Exception JavaDoc {
625         assertTranslation( "from Animal an where an.mother.bodyWeight > :weight" );
626     }
627
628     // Second set of examples....
629

630     public void testClassProperty() throws Exception JavaDoc {
631         assertTranslation( "from Animal a where a.mother.class = Reptile" );
632     }
633
634     public void testComponent() throws Exception JavaDoc {
635         assertTranslation( "from Human h where h.name.first = 'Gavin'" );
636     }
637
638     public void testSelectEntity() throws Exception JavaDoc {
639         assertTranslation( "select an from Animal an inner join an.mother mo where an.bodyWeight < mo.bodyWeight" );
640         assertTranslation( "select mo, an from Animal an inner join an.mother mo where an.bodyWeight < mo.bodyWeight" );
641     }
642
643     public void testValueAggregate() {
644         assertTranslation( "select max(p), min(p) from User u join u.permissions p" );
645     }
646
647     public void testAggregation() throws Exception JavaDoc {
648         assertTranslation( "select count(an) from Animal an" );
649         assertTranslation( "select count(*) from Animal an" );
650         assertTranslation( "select count(distinct an) from Animal an" );
651         assertTranslation( "select count(distinct an.id) from Animal an" );
652         assertTranslation( "select count(all an.id) from Animal an" );
653     }
654
655     public void testSelectProperty() throws Exception JavaDoc {
656         assertTranslation( "select an.bodyWeight, mo.bodyWeight from Animal an inner join an.mother mo where an.bodyWeight < mo.bodyWeight" );
657     }
658
659     public void testSelectEntityProperty() throws Exception JavaDoc {
660         DotNode.useThetaStyleImplicitJoins = true;
661         assertTranslation( "select an.mother from Animal an" );
662         assertTranslation( "select an, an.mother from Animal an" );
663         DotNode.useThetaStyleImplicitJoins = false;
664     }
665
666     public void testSelectDistinctAll() throws Exception JavaDoc {
667         assertTranslation( "select distinct an.description, an.bodyWeight from Animal an" );
668         assertTranslation( "select all an from Animal an" );
669     }
670
671     public void testSelectAssociatedEntityId() throws Exception JavaDoc {
672         assertTranslation( "select an.mother.id from Animal an" );
673     }
674
675     public void testGroupBy() throws Exception JavaDoc {
676         assertTranslation( "select an.mother.id, max(an.bodyWeight) from Animal an group by an.mother.id" );
677         assertTranslation( "select an.mother.id, max(an.bodyWeight) from Animal an group by an.mother.id having max(an.bodyWeight)>1.0" );
678     }
679
680     public void testGroupByMultiple() throws Exception JavaDoc {
681         assertTranslation( "select s.id, s.count, count(t), max(t.date) from org.hibernate.test.legacy.Simple s, org.hibernate.test.legacy.Simple t where s.count = t.count group by s.id, s.count order by s.count" );
682     }
683
684     public void testManyToMany() throws Exception JavaDoc {
685         assertTranslation( "from Human h join h.friends f where f.nickName = 'Gavin'" );
686         assertTranslation( "from Human h join h.friends f where f.bodyWeight > 100" );
687     }
688
689     public void testManyToManyElementFunctionInWhere() throws Exception JavaDoc {
690         assertTranslation( "from Human human where human in elements(human.friends)" );
691         assertTranslation( "from Human human where human = some elements(human.friends)" );
692     }
693
694     public void testManyToManyElementFunctionInWhere2() throws Exception JavaDoc {
695         assertTranslation( "from Human h1, Human h2 where h2 in elements(h1.family)" );
696         assertTranslation( "from Human h1, Human h2 where 'father' in indices(h1.family)" );
697     }
698
699     public void testManyToManyFetch() throws Exception JavaDoc {
700         assertTranslation( "from Human h left join fetch h.friends" );
701     }
702
703     public void testManyToManyIndexAccessor() throws Exception JavaDoc {
704         // From ParentChildTest.testCollectionQuery()
705
assertTranslation( "select c from ContainerX c, Simple s where c.manyToMany[2] = s" );
706         assertTranslation( "select s from ContainerX c, Simple s where c.manyToMany[2] = s" );
707         assertTranslation( "from ContainerX c, Simple s where c.manyToMany[2] = s" );
708         //would be nice to have:
709
//assertTranslation( "select c.manyToMany[2] from ContainerX c" );
710
}
711
712     public void testSelectNew() throws Exception JavaDoc {
713         assertTranslation( "select new Animal(an.description, an.bodyWeight) from Animal an" );
714         assertTranslation( "select new org.hibernate.test.hql.Animal(an.description, an.bodyWeight) from Animal an" );
715     }
716
717     public void testSimpleCorrelatedSubselect() throws Exception JavaDoc {
718         assertTranslation( "from Animal a where a.bodyWeight = (select o.bodyWeight from a.offspring o)" );
719         assertTranslation( "from Animal a where a = (from a.offspring o)" );
720     }
721
722     public void testSimpleUncorrelatedSubselect() throws Exception JavaDoc {
723         assertTranslation( "from Animal a where a.bodyWeight = (select an.bodyWeight from Animal an)" );
724         assertTranslation( "from Animal a where a = (from Animal an)" );
725     }
726
727     public void testSimpleCorrelatedSubselect2() throws Exception JavaDoc {
728         assertTranslation( "from Animal a where a = (select o from a.offspring o)" );
729         assertTranslation( "from Animal a where a in (select o from a.offspring o)" );
730     }
731
732     public void testSimpleUncorrelatedSubselect2() throws Exception JavaDoc {
733         assertTranslation( "from Animal a where a = (select an from Animal an)" );
734         assertTranslation( "from Animal a where a in (select an from Animal an)" );
735     }
736
737     public void testUncorrelatedSubselect2() throws Exception JavaDoc {
738         assertTranslation( "from Animal a where a.bodyWeight = (select max(an.bodyWeight) from Animal an)" );
739     }
740
741     public void testCorrelatedSubselect2() throws Exception JavaDoc {
742         assertTranslation( "from Animal a where a.bodyWeight > (select max(o.bodyWeight) from a.offspring o)" );
743     }
744
745     public void testManyToManyJoinInSubselect() throws Exception JavaDoc {
746         assertTranslation( "select foo from Foo foo where foo in (select elt from Baz baz join baz.fooArray elt)" );
747     }
748
749     public void testImplicitJoinInSubselect() throws Exception JavaDoc {
750         assertTranslation( "from Animal a where a = (select an.mother from Animal an)" );
751         assertTranslation( "from Animal a where a.id = (select an.mother.id from Animal an)" );
752     }
753
754     public void testManyToOneSubselect() {
755         //TODO: the join in the subselect also shows up in the outer query!
756
assertTranslation( "from Animal a where 'foo' in (select m.description from a.mother m)" );
757     }
758
759     public void testPositionalParameters() throws Exception JavaDoc {
760         assertTranslation( "from Animal an where an.bodyWeight > ?" );
761     }
762
763     public void testKeywordPropertyName() throws Exception JavaDoc {
764         assertTranslation( "from Glarch g order by g.order asc" );
765         assertTranslation( "select g.order from Glarch g where g.order = 3" );
766     }
767
768     public void testJavaConstant() throws Exception JavaDoc {
769         assertTranslation( "from org.hibernate.test.legacy.Category c where c.name = org.hibernate.test.legacy.Category.ROOT_CATEGORY" );
770         assertTranslation( "from org.hibernate.test.legacy.Category c where c.id = org.hibernate.test.legacy.Category.ROOT_ID" );
771         // todo : additional desired functionality
772
//assertTranslation( "from Category c where c.name = Category.ROOT_CATEGORY" );
773
//assertTranslation( "select c.name, Category.ROOT_ID from Category as c");
774
}
775
776     public void testClassName() throws Exception JavaDoc {
777         assertTranslation( "from Zoo zoo where zoo.class = PettingZoo" );
778         assertTranslation( "from DomesticAnimal an where an.class = Dog" );
779         assertTranslation( "from Animal an where an.class = Dog" );
780     }
781
782     public void testSelectDialectFunction() throws Exception JavaDoc {
783         // From SQLFunctionsTest.testDialectSQLFunctions...
784
if ( getDialect() instanceof HSQLDialect ) {
785             assertTranslation( "select mod(s.count, 2) from org.hibernate.test.legacy.Simple as s where s.id = 10" );
786             //assertTranslation( "from org.hibernate.test.legacy.Simple as s where mod(s.count, 2) = 0" );
787
}
788         assertTranslation( "select upper(human.name.first) from Human human" );
789         assertTranslation( "from Human human where lower(human.name.first) like 'gav%'" );
790         assertTranslation( "select upper(a.description) from Animal a" );
791         assertTranslation( "select max(a.bodyWeight) from Animal a" );
792     }
793
794     public void testTwoJoins() throws Exception JavaDoc {
795         assertTranslation( "from Human human join human.friends, Human h join h.mother" );
796         assertTranslation( "from Human human join human.friends f, Animal an join an.mother m where f=m" );
797         assertTranslation( "from Baz baz left join baz.fooToGlarch, Bar bar join bar.foo" );
798     }
799
800     public void testToOneToManyManyJoinSequence() throws Exception JavaDoc {
801         assertTranslation( "from Dog d join d.owner h join h.friends f where f.name.first like 'joe%'" );
802     }
803
804     public void testToOneToManyJoinSequence() throws Exception JavaDoc {
805         assertTranslation( "from Animal a join a.mother m join m.offspring" );
806         assertTranslation( "from Dog d join d.owner m join m.offspring" );
807         assertTranslation( "from Animal a join a.mother m join m.offspring o where o.bodyWeight > a.bodyWeight" );
808     }
809
810     public void testSubclassExplicitJoin() throws Exception JavaDoc {
811         assertTranslation( "from DomesticAnimal da join da.owner o where o.nickName = 'gavin'" );
812         assertTranslation( "from DomesticAnimal da join da.owner o where o.bodyWeight > 0" );
813     }
814
815     public void testMultipleExplicitCollectionJoins() throws Exception JavaDoc {
816         assertTranslation( "from Animal an inner join an.offspring os join os.offspring gc" );
817         assertTranslation( "from Animal an left outer join an.offspring os left outer join os.offspring gc" );
818     }
819
820     public void testSelectDistinctComposite() throws Exception JavaDoc {
821         // This is from CompositeElementTest.testHandSQL.
822
assertTranslation( "select distinct p from org.hibernate.test.compositeelement.Parent p join p.children c where c.name like 'Child%'" );
823     }
824
825     public void testDotComponent() throws Exception JavaDoc {
826         // from FumTest.testListIdentifiers()
827
assertTranslation( "select fum.id from org.hibernate.test.legacy.Fum as fum where not fum.fum='FRIEND'" );
828     }
829
830     public void testOrderByCount() throws Exception JavaDoc {
831         assertTranslation( "from Animal an group by an.zoo.id order by an.zoo.id, count(*)" );
832     }
833
834     public void testHavingCount() throws Exception JavaDoc {
835         assertTranslation( "from Animal an group by an.zoo.id having count(an.zoo.id) > 1" );
836     }
837
838     public void selectWhereElements() throws Exception JavaDoc {
839         assertTranslation( "select foo from Foo foo, Baz baz where foo in elements(baz.fooArray)" );
840     }
841
842     public void testCollectionOfComponents() throws Exception JavaDoc {
843         assertTranslation( "from Baz baz inner join baz.components comp where comp.name='foo'" );
844     }
845
846     public void testNestedComponentIsNull() {
847         // From MapTest...
848
assertTranslation( "from Commento c where c.marelo.commento.mcompr is null" );
849     }
850
851     public void testOneToOneJoinedFetch() throws Exception JavaDoc {
852         // From OneToOneTest.testOneToOneOnSubclass
853
assertTranslation( "from org.hibernate.test.onetoone.joined.Person p join fetch p.address left join fetch p.mailingAddress" );
854     }
855
856     public void testSubclassImplicitJoin() throws Exception JavaDoc {
857         assertTranslation( "from DomesticAnimal da where da.owner.nickName like 'Gavin%'" );
858         assertTranslation( "from DomesticAnimal da where da.owner.nickName = 'gavin'" );
859         assertTranslation( "from DomesticAnimal da where da.owner.bodyWeight > 0" );
860     }
861
862     public void testComponent2() throws Exception JavaDoc {
863         assertTranslation( "from Dog dog where dog.owner.name.first = 'Gavin'" );
864     }
865
866     public void testOneToOne() throws Exception JavaDoc {
867         assertTranslation( "from User u where u.human.nickName='Steve'" );
868         assertTranslation( "from User u where u.human.name.first='Steve'" );
869     }
870
871     public void testSelectClauseImplicitJoin() throws Exception JavaDoc {
872         //assertTranslation( "select d.owner.mother from Dog d" ); //bug in old qt
873
assertTranslation( "select d.owner.mother.description from Dog d" );
874     }
875
876     public void testFromClauseImplicitJoin() throws Exception JavaDoc {
877         assertTranslation( "from DomesticAnimal da join da.owner.mother m where m.bodyWeight > 10" );
878     }
879
880     public void testJoinedSubclassWithOrCondition() {
881         assertTranslation( "from Animal an where (an.bodyWeight > 10 and an.bodyWeight < 100) or an.bodyWeight is null" );
882     }
883
884     public void testImplicitJoinInFrom() {
885         assertTranslation( "from Human h join h.mother.mother.offspring o" );
886     }
887
888     public void testDuplicateImplicitJoinInSelect() {
889         // This test causes failures on theta-join dialects because the SQL is different. The old parser
890
// duplicates the condition, whereas the new parser does not. The queries are semantically the
891
// same however.
892
assertTranslation( "select an.mother.bodyWeight from Animal an join an.mother m where an.mother.bodyWeight > 10" );
893         assertTranslation( "select an.mother.bodyWeight from Animal an where an.mother.bodyWeight > 10" );
894         //assertTranslation("select an.mother from Animal an where an.mother.bodyWeight is not null");
895
assertTranslation( "select an.mother.bodyWeight from Animal an order by an.mother.bodyWeight" );
896     }
897
898     public void testConstructorNode() throws Exception JavaDoc {
899         ConstructorNode n = new ConstructorNode();
900         assertNull( n.getFromElement() );
901         assertFalse( n.isReturnableEntity() );
902     }
903
904     public void testIndexNode() throws Exception JavaDoc {
905         IndexNode n = new IndexNode();
906         Exception JavaDoc ex = null;
907         try {
908             n.setScalarColumnText( 0 );
909         }
910         catch ( UnsupportedOperationException JavaDoc e ) {
911             ex = e;
912         }
913         assertNotNull( ex );
914     }
915
916     public void testExceptions() throws Exception JavaDoc {
917         DetailedSemanticException dse = new DetailedSemanticException( "test" );
918         dse.printStackTrace();
919         dse.printStackTrace( new PrintWriter JavaDoc( new StringWriter JavaDoc() ) );
920         QuerySyntaxException qse = new QuerySyntaxException( new RecognitionException( "test" ), "from bozo b where b.clown = true" );
921         assertNotNull( qse.getMessage() );
922     }
923
924     public static Test suite() {
925         return new TestSuite( HQLTest.class );
926     }
927
928
929     public void testSelectProperty2() throws Exception JavaDoc {
930         assertTranslation( "select an, mo.bodyWeight from Animal an inner join an.mother mo where an.bodyWeight < mo.bodyWeight" );
931         assertTranslation( "select an, mo, an.bodyWeight, mo.bodyWeight from Animal an inner join an.mother mo where an.bodyWeight < mo.bodyWeight" );
932     }
933
934     public void testSubclassWhere() throws Exception JavaDoc {
935         // TODO: The classic QT generates lots of extra parens, etc.
936
assertTranslation( "from PettingZoo pz1, PettingZoo pz2 where pz1.id = pz2.id" );
937         assertTranslation( "from PettingZoo pz1, PettingZoo pz2 where pz1.id = pz2" );
938         assertTranslation( "from PettingZoo pz where pz.id > 0 " );
939     }
940
941     public void testNestedImplicitJoinsInSelect() throws Exception JavaDoc {
942         // NOTE: This test is not likely to generate the exact SQL because of the where clause. The synthetic
943
// theta style joins come out differently in the new QT.
944
// From FooBarTest.testQuery()
945
// Missing the foo2_ join, and foo3_ should include subclasses, but it doesn't.
946
// assertTranslation("select foo.foo.foo.foo.string from org.hibernate.test.legacy.Foo foo where foo.foo.foo = 'bar'");
947
assertTranslation( "select foo.foo.foo.foo.string from org.hibernate.test.legacy.Foo foo" );
948     }
949
950     public void testNestedComponent() throws Exception JavaDoc {
951         // From FooBarTest.testQuery()
952
//an extra set of parens in new SQL
953
assertTranslation( "from org.hibernate.test.legacy.Foo foo where foo.component.subcomponent.name='bar'" );
954     }
955
956     public void testNull2() throws Exception JavaDoc {
957         //old parser generates useless extra parens
958
assertTranslation( "from Human h where not( h.nickName is null )" );
959         assertTranslation( "from Human h where not( h.nickName is not null )" );
960     }
961
962     public void testUnknownFailureFromMultiTableTest() {
963         assertTranslation( "from Lower s where s.yetanother.name='name'" );
964     }
965
966     public void testJoinInSubselect() throws Exception JavaDoc {
967         //new parser uses ANSI-style inner join syntax
968
DotNode.useThetaStyleImplicitJoins = true;
969         assertTranslation( "from Animal a where a in (select m from Animal an join an.mother m)" );
970         assertTranslation( "from Animal a where a in (select o from Animal an join an.offspring o)" );
971         DotNode.useThetaStyleImplicitJoins = false;
972     }
973     
974     public void testJoinedSubclassImplicitJoin() throws Exception JavaDoc {
975         // From MultiTableTest.testQueries()
976
// TODO: This produces the proper from clause now, but the parens in the where clause are different.
977
assertTranslation( "from org.hibernate.test.legacy.Lower s where s.yetanother.name='name'" );
978     }
979
980     public void testProjectProductJoinedSubclass() throws Exception JavaDoc {
981         // TODO: The old QT generates the discriminator and the theta join in a strange order, and with two extra sets of parens, this is okay, right?
982
assertTranslation( "select zoo from Zoo zoo, PettingZoo pz where zoo=pz" );
983         assertTranslation( "select zoo, pz from Zoo zoo, PettingZoo pz where zoo=pz" );
984     }
985
986     public void testCorrelatedSubselect1() throws Exception JavaDoc {
987         // The old translator generates the theta join before the condition in the sub query.
988
// TODO: Decide if we want to bother generating the theta join in the same order (non simple).
989
assertTranslation( "from Animal a where exists (from a.offspring o where o.bodyWeight>10)" );
990     }
991
992     public void testOuterAliasInSubselect() {
993         assertTranslation( "from Human h where h = (from Animal an where an = h)" );
994     }
995
996     public void testFetch() throws Exception JavaDoc {
997         assertTranslation( "from Zoo zoo left join zoo.mammals" );
998         assertTranslation( "from Zoo zoo left join fetch zoo.mammals" );
999     }
1000
1001    public void testOneToManyElementFunctionInWhere() throws Exception JavaDoc {
1002        assertTranslation( "from Zoo zoo where 'dog' in indices(zoo.mammals)" );
1003        assertTranslation( "from Zoo zoo, Dog dog where dog in elements(zoo.mammals)" );
1004    }
1005
1006    /*public void testManyToManyElementFunctionInSelect() throws Exception {
1007        assertTranslation("select elements(zoo.mammals) from Zoo zoo");
1008        assertTranslation("select indices(zoo.mammals) from Zoo zoo");
1009    }*/

1010    
1011    public void testManyToManyInJoin() throws Exception JavaDoc {
1012        assertTranslation( "select x.id from Human h1 join h1.family x" );
1013        //assertTranslation("select index(h2) from Human h1 join h1.family h2");
1014
}
1015
1016    public void testManyToManyInSubselect() throws Exception JavaDoc {
1017        assertTranslation( "from Human h1, Human h2 where h2 in (select x.id from h1.family x)" );
1018        assertTranslation( "from Human h1, Human h2 where 'father' in indices(h1.family)" );
1019    }
1020
1021    public void testOneToManyIndexAccess() throws Exception JavaDoc {
1022        assertTranslation( "from Zoo zoo where zoo.mammals['dog'] is not null" );
1023    }
1024
1025    public void testImpliedSelect() throws Exception JavaDoc {
1026        assertTranslation( "select zoo from Zoo zoo" );
1027        assertTranslation( "from Zoo zoo" );
1028        assertTranslation( "from Zoo zoo join zoo.mammals m" );
1029        assertTranslation( "from Zoo" );
1030        assertTranslation( "from Zoo zoo join zoo.mammals" );
1031    }
1032    
1033    public void testVectorSubselect() {
1034        assertTranslation( "from Animal a where ('foo', 'bar') in (select m.description, m.bodyWeight from a.mother m)" );
1035    }
1036
1037    public void testWierdSubselectImplicitJoinStuff() {
1038        //note that the new qt used to eliminate unnecessary join, but no more
1039
assertTranslation("from Simple s where s = some( select sim from Simple sim where sim.other.count=s.other.count ) and s.other.count > 0");
1040    }
1041    
1042    /*public void testSelectElementsOfCollectionOfValues() throws Exception {
1043        // From FooBarTest.testQuery()
1044        // TODO: This produces the where clause in a different order, but it seems okay.
1045        assertTranslation("select foo.component.name, elements(foo.component.importantDates) from org.hibernate.test.legacy.Foo foo where foo.foo.id=?");
1046    }*/

1047
1048    //public void testMultiTableElements() throws Exception {
1049
/*
1050    HQL : select elements(ls.bag), elements(ls.set) from org.hibernate.test.legacy.Lower ls
1051    OLD SQL:
1052    select top2_.id1_ as col_0_0_, top4_.id1_ as col_1_0_
1053    from leafsubclass lower0_ inner join rootclass lower0_1_ on lower0_.id__=lower0_1_.id1_, simple_simple bag1_, rootclass top2_, rootclass set3_, rootclass top4_
1054    where lower0_1_.id1_ is not null and lower0_.id__=bag1_.simple1 and bag1_.simple2=top2_.id1_ and lower0_.id__=set3_.parent and set3_.id1_=top4_.id1_
1055    */

1056
1057    //assertTranslation("select elements(ls.bag), elements(ls.set) from org.hibernate.test.legacy.Lower ls");
1058
//}
1059

1060    public void testCollectionsInSelect2() throws Exception JavaDoc {
1061        // This one looks okay now, it just generates extra parens in the where clause.
1062
assertTranslation( "select foo.string from Bar bar left join bar.baz.fooArray foo where bar.string = foo.string" );
1063    }
1064
1065
1066    //public void testCollectionsInSelect() throws Exception {
1067
// From FooBarTest.testCollectionsInSelect
1068
/*
1069    HQL : select baz, baz.stringSet.size, count( distinct elements(baz.stringSet) ), max( elements(baz.stringSet) ) from org.hibernate.test.legacy.Baz baz group by baz
1070    OLD SQL:
1071    select
1072        baz0_.baz_id_column_ as baz_id_c1_, baz0_.count_count as count_co2_37_, baz0_.name_b as name_b37_, baz0_.foo as foo37_, baz0_.superBaz as superBaz37_, baz0_.str as str37_, baz0_.baz_id_column_ as col_0_0_,
1073        count(*) as col_1_0_,
1074        count(distinct stringset2_.element) as col_2_0_, max(stringset3_.element) as col_3_0_
1075    from baz baz0_, stringSet stringset1_, stringSet stringset2_, stringSet stringset3_
1076    where baz0_.baz_id_column_=stringset1_.id_ and baz0_.baz_id_column_=stringset2_.id_ and baz0_.baz_id_column_=stringset3_.id_
1077    group by baz0_.baz_id_column_
1078
1079    NEW SQL:
1080    select
1081        // TODO: Remove the extra 'id' column select.
1082        baz0_.baz_id_column_ as col_0_0_,
1083        // TODO: Figure out how the classic translator knows to use count(*)
1084        (select count(*) from stringSet stringset1_ where baz0_.baz_id_column_=stringset1_.id_) as col_1_0_,
1085        // This is also correct.
1086        count(distinct stringset2_.element) as col_2_0_, max(stringset3_.element) as col_3_0_,
1087        // The properties of baz are correct, they're just in the wrong place.
1088        baz0_.baz_id_column_ as baz_id_c1_, baz0_.count_count as count_co2_37_, baz0_.name_b as name_b37_, baz0_.foo as foo37_, baz0_.superBaz as superBaz37_, baz0_.str as str37_
1089// FROM is okay.
1090    from baz baz0_ stringSet stringset1_, stringSet stringset3_, stringSet stringset2_
1091// WHERE is okay.
1092    where (baz0_.baz_id_column_=stringset1_.id_ and baz0_.baz_id_column_=stringset2_.id_ baz0_.baz_id_column_=stringset3_.id_)
1093// GROUP BY is okay.
1094    group by baz0_.baz_id_column_
1095    */

1096    //assertTranslation( "select baz, size(baz.stringSet), count( distinct elements(baz.stringSet) ), max( elements(baz.stringSet) ) from org.hibernate.test.legacy.Baz baz group by baz");
1097

1098    //}
1099

1100}
1101
Popular Tags