KickJava   Java API By Example, From Geeks To Geeks.

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


1 // $Id: ASTQueryTranslatorTest.java,v 1.53 2005/07/10 16:51:17 oneovthafew Exp $
2
package org.hibernate.test.hql;
3
4 import junit.framework.Test;
5 import junit.framework.TestSuite;
6
7 /**
8  * Tests cases where the AST based QueryTranslator does not generate identical SQL.
9  *
10  * @author josh Dec 6, 2004 9:07:58 AM
11  */

12 public class ASTQueryTranslatorTest extends QueryTranslatorTestCase {
13
14     public ASTQueryTranslatorTest(String JavaDoc x) {
15         super( x );
16     }
17
18     public static Test suite() {
19         return new TestSuite( ASTQueryTranslatorTest.class );
20     }
21
22     protected boolean dropAfterFailure() {
23         return false;
24     }
25
26     // ##### TESTS THAT DON'T PASS BECAUSE THEY GENERATE DIFFERENT, POSSIBLY VALID SQL #####
27

28     public void testSelectManyToOne() {
29         assertTranslation("select distinct a.zoo from Animal a where a.zoo is not null");
30         assertTranslation("select a.zoo from Animal a");
31     }
32
33     public void testSelectExpression() {
34         //old qt cant handle select-clause expressions
35
assertTranslation("select a.bodyWeight + m.bodyWeight from Animal a join a.mother m");
36     }
37     
38     public void testFetchProperties() {
39         //not implemented in old qt
40
assertTranslation("from Animal a fetch all properties join a.offspring o fetch all properties");
41     }
42     
43     public void testOldSyntax() {
44         //generates ANSI join instead of theta join
45
assertTranslation("from a in class Animal, o in elements(a.offspring), h in class Human");
46     }
47     
48     public void testImplicitJoinInsideOutsideSubselect() {
49         // new qt re-uses the joins defined by 's.other.count' path when referenced in the subquery,
50
// whereas the old qt reuses only the "root table" (not the already defined join to 'other'):
51
// OLD SQL :
52
// select simple0_.id_ as col_0_0_
53
// from Simple simple0_,
54
// Simple simple1_
55
// where (simple1_.count_>0
56
// and simple0_.other=simple1_.id_)
57
// and (simple0_.id_=some(
58
// select simple2_.id_
59
// from Simple simple2_,
60
// Simple simple3_,
61
// Simple simple4_
62
// where (simple3_.count_=simple4_.count_
63
// and simple2_.other=simple3_.id_
64
// and simple0_.other=simple4_.id_)
65
// ))
66
// NEW SQL :
67
// select simple0_.id_ as col_0_0_
68
// from Simple simple0_,
69
// Simple simple1_
70
// where (simple1_.count_>0
71
// and simple0_.id_=some(
72
// select simple2_.id_
73
// from Simple simple2_,
74
// Simple simple3_
75
// where (simple3_.count_=simple1_.count_
76
// and simple2_.other=simple3_.id_)
77
// )
78
// and simple0_.other=simple1_.id_)
79
assertTranslation( "from Simple s where s = some( from Simple sim where sim.other.count=s.other.count ) and s.other.count > 0" );
80         assertTranslation( "from Simple s where s.other.count > 0 and s = some( from Simple sim where sim.other.count=s.other.count )" );
81     }
82
83     public void testNakedPropertyRef() {
84         // this is needed for ejb3 selects and bulk statements
85
// Note: these all fail because the old parser did not have this
86
// feature, it just "passes the tokens through" to the SQL.
87
assertTranslation( "from Animal where bodyWeight = bodyWeight" );
88         assertTranslation( "select bodyWeight from Animal" );
89         assertTranslation( "select max(bodyWeight) from Animal" );
90     }
91
92     public void testNakedComponentPropertyRef() {
93         // this is needed for ejb3 selects and bulk statements
94
// Note: these all fail because the old parser did not have this
95
// feature, it just "passes the tokens through" to the SQL.
96
assertTranslation( "from Human where name.first = 'Gavin'" );
97         assertTranslation( "select name from Human" );
98         assertTranslation( "select upper(h.name.first) from Human as h" );
99         assertTranslation( "select upper(name.first) from Human" );
100     }
101
102     public void testNakedMapIndex() throws Exception JavaDoc {
103         assertTranslation( "from Zoo where mammals['dog'].description like '%black%'" );
104     }
105
106     public void testDuplicateImplicitJoinInWhere() {
107         //new qt has more organized joins
108
assertTranslation("from Human h where h.mother.bodyWeight>10 and h.mother.bodyWeight<10");
109     }
110     
111     public void testWhereExpressions() {
112         assertTranslation("from User u where u.userName='gavin' and u.human.name.first='Gavin'");
113         //new qt has more organized joins
114
assertTranslation("from User u where u.human.name.last='King' and u.human.name.first='Gavin'");
115         assertTranslation("from Bar bar where bar.baz.name='josh'");
116         assertTranslation("from Bar bar where bar.baz.name='josh' and not bar.baz.name='gavin'");
117     }
118     
119     public void testImplicitJoinInSelect() {
120         //slightly diff select clause, both correct
121
assertTranslation("select foo.long, foo.foo from Foo foo");
122     }
123     
124     public void testSelectStandardFunctions() throws Exception JavaDoc {
125         //old parser throws an exception
126
assertTranslation( "select current_date(), current_time(), current_timestamp() from Animal" );
127     }
128
129     public void testSelectClauseImplicitJoin() throws Exception JavaDoc {
130         //the old query translator has a bug which results in the wrong return type!
131
assertTranslation( "select d.owner.mother from Dog d" );
132     }
133
134     public void testComplexWhereExpression() throws Exception JavaDoc {
135         // classic QT generates lots of extra parens and some extra theta joins.
136
assertTranslation( "select distinct s from Simple s\n" +
137                 "where ( ( s.other.count + 3 ) = (15*2)/2 and s.count = 69) or ( ( s.other.count + 2 ) / 7 ) = 2" );
138     }
139
140     public void testDuplicateImplicitJoin() throws Exception JavaDoc {
141         // old qt generates an extra theta join
142
assertTranslation( "from Animal an where an.mother.bodyWeight > 10 and an.mother.bodyWeight < 20" );
143     }
144
145     public void testKeywordClassNameAndAlias() throws Exception JavaDoc {
146         // The old QT throws an exception, the new one doesn't, which is better
147
assertTranslation( "from Order order" );
148     }
149
150     public void testComponent3() throws Exception JavaDoc {
151         // The old translator generates duplicate inner joins *and* duplicate theta join clauses in the where statement in this case.
152
assertTranslation( "from Dog dog where dog.owner.name.first = 'Gavin' and dog.owner.name.last='King' and dog.owner.bodyWeight<70" );
153     }
154
155     public void testUncorrelatedSubselectWithJoin() throws Exception JavaDoc {
156         // The old translator generates unnecessary inner joins for the Animal subclass of zoo.mammals
157
// The new one is working fine now!
158
assertTranslation( "from Animal a where a in (select mam from Zoo zoo join zoo.mammals mam)" );
159     }
160
161     public void testFetch() throws Exception JavaDoc {
162         //SQL is correct, new qt is not throwing an exception when it should be (minor issue)
163
assertTranslation("from Customer cust left join fetch cust.billingAddress where cust.customerId='abc123'");
164     }
165
166     public void testImplicitJoin() throws Exception JavaDoc {
167         //old qt generates an exception, the new one doesnt
168
//this is actually invalid HQL, an implicit join on a many-valued association
169
assertTranslation( "from Animal an where an.offspring.mother.bodyWeight > 10" );
170     }
171
172 }
173
Popular Tags