KickJava   Java API By Example, From Geeks To Geeks.

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


1 //$Id: EJBQLTest.java,v 1.21 2005/07/12 20:49:27 oneovthafew Exp $
2
package org.hibernate.test.hql;
3
4 import java.io.ByteArrayOutputStream JavaDoc;
5 import java.io.PrintStream JavaDoc;
6 import java.util.Collections JavaDoc;
7 import java.util.List JavaDoc;
8
9 import junit.framework.Test;
10 import junit.framework.TestSuite;
11
12 import org.hibernate.engine.SessionFactoryImplementor;
13 import org.hibernate.hql.QueryTranslator;
14 import org.hibernate.hql.QueryTranslatorFactory;
15 import org.hibernate.hql.antlr.HqlSqlTokenTypes;
16 import org.hibernate.hql.ast.ASTQueryTranslatorFactory;
17 import org.hibernate.hql.ast.HqlParser;
18 import org.hibernate.hql.ast.QueryTranslatorImpl;
19 import org.hibernate.hql.ast.util.ASTUtil;
20 import org.hibernate.test.TestCase;
21
22 import antlr.RecognitionException;
23 import antlr.TokenStreamException;
24 import antlr.collections.AST;
25
26
27 /**
28  * @author <a HREF="mailto:alex@jboss.org">Alexey Loubyansky</a>
29  */

30 public class EJBQLTest
31         extends TestCase {
32
33     public EJBQLTest(String JavaDoc x) {
34         super( x );
35     }
36
37     public void testEjb3PositionalParameters() throws Exception JavaDoc {
38         QueryTranslatorImpl qt = compile( "from Animal a where a.bodyWeight = ?1" );
39         AST ast = ( AST ) qt.getSqlAST();
40
41         // make certain that the ejb3-positional param got recognized as a named param
42
List JavaDoc namedParams = ASTUtil.collectChildren(
43                 ast,
44                 new ASTUtil.FilterPredicate() {
45                     public boolean exclude(AST n) {
46                         return n.getType() != HqlSqlTokenTypes.NAMED_PARAM;
47                     }
48                 }
49         );
50         assertTrue( "ejb3 positional param not recognized as a named param", namedParams.size() > 0 );
51     }
52
53     /**
54      * SELECT OBJECT(identifier)
55      */

56     public void testSelectObjectClause() throws Exception JavaDoc {
57         //parse("select object(m) from Model m");
58
assertEjbqlEqualsHql( "select object(m) from Model m", "from Model m" );
59     }
60
61     /**
62      * IN(collection_valued_path) identifier
63      */

64     public void testCollectionMemberDeclaration() throws Exception JavaDoc {
65         String JavaDoc hql = "select o from Animal a inner join a.offspring o";
66         String JavaDoc ejbql = "select object(o) from Animal a, in(a.offspring) o";
67         //parse(hql);
68
//parse(ejbql);
69
assertEjbqlEqualsHql( ejbql, hql );
70     }
71
72     /**
73      * collection_valued_path IS [NOT] EMPTY
74      */

75     public void testIsEmpty() throws Exception JavaDoc {
76         //String hql = "from Animal a where not exists (from a.offspring)";
77
String JavaDoc hql = "from Animal a where not exists elements(a.offspring)";
78         String JavaDoc ejbql = "select object(a) from Animal a where a.offspring is empty";
79         //parse(hql);
80
//parse(ejbql);
81
assertEjbqlEqualsHql(ejbql, hql);
82         
83         hql = "from Animal a where exists (from a.mother.father.offspring)";
84         ejbql = "select object(a) from Animal a where a.mother.father.offspring is not empty";
85         assertEjbqlEqualsHql( ejbql, hql );
86     }
87
88     /**
89      * [NOT] MEMBER OF
90      */

91     public void testMemberOf() throws Exception JavaDoc {
92         String JavaDoc hql = "from Animal a where a.mother in (from a.offspring)";
93         //String hql = "from Animal a where a.mother in elements(a.offspring)";
94
String JavaDoc ejbql = "select object(a) from Animal a where a.mother member of a.offspring";
95         //parse(hql);
96
//parse(ejbql);
97
assertEjbqlEqualsHql( ejbql, hql );
98
99         hql = "from Animal a where a.mother not in (from a.offspring)";
100         //hql = "from Animal a where a.mother not in elements(a.offspring)";
101
ejbql = "select object(a) from Animal a where a.mother not member of a.offspring";
102         //parse(hql);
103
//parse(ejbql);
104
assertEjbqlEqualsHql( ejbql, hql );
105     }
106
107     /**
108      * Various functions.
109      * Tests just parsing for now which means it doesn't guarantee that the generated SQL is as expected or even valid.
110      */

111     public void testEJBQLFunctions() throws Exception JavaDoc {
112         String JavaDoc hql = "select object(a) from Animal a where a.description = concat('1', concat('2','3'), '4'||'5')||0";
113         parse( hql, false );
114         System.out.println( "sql: " + toSql( hql ) );
115
116         hql = "from Animal a where substring(a.description, 1, 3) = :p1";
117         parse( hql, false );
118         System.out.println( "sql: " + toSql( hql ) );
119
120         hql = "select substring(a.description, 1, 3) from Animal a";
121         parse( hql, false );
122         System.out.println( "sql: " + toSql( hql ) );
123
124         hql = "from Animal a where lower(a.description) = :p1";
125         parse( hql, false );
126         System.out.println( "sql: " + toSql( hql ) );
127
128         hql = "select lower(a.description) from Animal a";
129         parse( hql, false );
130         System.out.println( "sql: " + toSql( hql ) );
131
132         hql = "from Animal a where upper(a.description) = :p1";
133         parse( hql, false );
134         System.out.println( "sql: " + toSql( hql ) );
135
136         hql = "select upper(a.description) from Animal a";
137         parse( hql, false );
138         System.out.println( "sql: " + toSql( hql ) );
139
140         hql = "from Animal a where length(a.description) = :p1";
141         parse( hql, false );
142         System.out.println( "sql: " + toSql( hql ) );
143
144         hql = "select length(a.description) from Animal a";
145         parse( hql, false );
146         System.out.println( "sql: " + toSql( hql ) );
147
148         hql = "from Animal a where locate(a.description, 'abc', 2) = :p1";
149         parse( hql, false );
150         System.out.println( "sql: " + toSql( hql ) );
151
152         hql = "select locate(a.description, :p1, 2) from Animal a";
153         parse( hql, false );
154         System.out.println( "sql: " + toSql( hql ) );
155
156         hql = "select object(a) from Animal a where trim(trailing '_' from a.description) = :p1";
157         parse( hql, false );
158         System.out.println( "sql: " + toSql( hql ) );
159
160         hql = "select trim(trailing '_' from a.description) from Animal a";
161         parse( hql, false );
162         System.out.println( "sql: " + toSql( hql ) );
163
164         hql = "select object(a) from Animal a where trim(leading '_' from a.description) = :p1";
165         parse( hql, false );
166         System.out.println( "sql: " + toSql( hql ) );
167
168         hql = "select object(a) from Animal a where trim(both a.description) = :p1";
169         parse( hql, false );
170         System.out.println( "sql: " + toSql( hql ) );
171
172         hql = "select object(a) from Animal a where trim(a.description) = :p1";
173         parse( hql, false );
174         System.out.println( "sql: " + toSql( hql ) );
175
176         hql = "select object(a) from Animal a where abs(a.bodyWeight) = sqrt(a.bodyWeight)";
177         parse( hql, false );
178         System.out.println( "sql: " + toSql( hql ) );
179
180         hql = "select object(a) from Animal a where mod(a.bodyWeight, a.mother.bodyWeight) = :p1";
181         parse( hql, false );
182         System.out.println( "sql: " + toSql( hql ) );
183
184         hql = "select object(a) from Animal a where BIT_LENGTH(a.bodyWeight) = :p1";
185         parse( hql, false );
186         System.out.println( "sql: " + toSql( hql ) );
187
188         hql = "select BIT_LENGTH(a.bodyWeight) from Animal a";
189         parse( hql, false );
190         System.out.println( "sql: " + toSql( hql ) );
191
192         hql = "select object(a) from Animal a where CURRENT_DATE = :p1 or CURRENT_TIME = :p2 or CURRENT_TIMESTAMP = :p3";
193         parse( hql, false );
194         System.out.println( "sql: " + toSql( hql ) );
195
196         // todo the following is not supported
197
//hql = "select CURRENT_DATE, CURRENT_TIME, CURRENT_TIMESTAMP from Animal a";
198
//parse(hql, true);
199
//System.out.println("sql: " + toSql(hql));
200

201         hql = "select object(a) from Animal a where a.bodyWeight like '%a%'";
202         parse( hql, false );
203         System.out.println( "sql: " + toSql( hql ) );
204
205         hql = "select object(a) from Animal a where a.bodyWeight not like '%a%'";
206         parse( hql, false );
207         System.out.println( "sql: " + toSql( hql ) );
208
209         hql = "select object(a) from Animal a where a.bodyWeight like '%a%' escape '%'";
210         parse( hql, false );
211         System.out.println( "sql: " + toSql( hql ) );
212     }
213
214     public void testTrueFalse() throws Exception JavaDoc {
215         assertEjbqlEqualsHql( "from Human h where h.pregnant is true", "from Human h where h.pregnant = true" );
216         assertEjbqlEqualsHql( "from Human h where h.pregnant is false", "from Human h where h.pregnant = false" );
217         assertEjbqlEqualsHql( "from Human h where not(h.pregnant is true)", "from Human h where not( h.pregnant=true )" );
218     }
219
220     // Private
221

222     private void assertEjbqlEqualsHql(String JavaDoc ejbql, String JavaDoc hql) {
223         SessionFactoryImplementor factory = getSessionFactoryImplementor();
224         QueryTranslatorFactory ast = new ASTQueryTranslatorFactory();
225
226         QueryTranslator queryTranslator = ast.createQueryTranslator( hql, Collections.EMPTY_MAP, factory );
227         queryTranslator.compile( Collections.EMPTY_MAP, true );
228         String JavaDoc hqlSql = queryTranslator.getSQLString();
229
230         queryTranslator = ast.createQueryTranslator( ejbql, Collections.EMPTY_MAP, factory );
231         queryTranslator.compile( Collections.EMPTY_MAP, true );
232         String JavaDoc ejbqlSql = queryTranslator.getSQLString();
233
234         assertEquals( hqlSql, ejbqlSql );
235     }
236
237     private void assertEjbqlEqualsSql(String JavaDoc sql, String JavaDoc hql) {
238         SessionFactoryImplementor factory = getSessionFactoryImplementor();
239         QueryTranslatorFactory ast = new ASTQueryTranslatorFactory();
240         QueryTranslator queryTranslator = ast.createQueryTranslator( hql, Collections.EMPTY_MAP, factory );
241         queryTranslator.compile( Collections.EMPTY_MAP, true );
242         assertEquals( sql, queryTranslator.getSQLString() );
243     }
244
245     private QueryTranslatorImpl compile(String JavaDoc input) {
246         SessionFactoryImplementor factory = getSessionFactoryImplementor();
247         QueryTranslatorFactory ast = new ASTQueryTranslatorFactory();
248         QueryTranslator queryTranslator = ast.createQueryTranslator( input, Collections.EMPTY_MAP, factory );
249         queryTranslator.compile( Collections.EMPTY_MAP, true );
250
251         return ( QueryTranslatorImpl ) queryTranslator;
252     }
253
254     private AST parse(String JavaDoc input, boolean logging) throws RecognitionException, TokenStreamException {
255         if ( logging ) {
256             System.out.println( "input: ->" + input + "<-" );
257         }
258
259         HqlParser parser = HqlParser.getInstance( input );
260         parser.setFilter( false );
261         parser.statement();
262         AST ast = parser.getAST();
263
264         if ( logging ) {
265             System.out.println( "AST : " + ast.toStringTree() + "" );
266             ByteArrayOutputStream JavaDoc baos = new ByteArrayOutputStream JavaDoc();
267             parser.showAst( ast, new PrintStream JavaDoc( baos ) );
268             System.out.println( baos.toString() );
269         }
270
271         assertEquals( "At least one error occurred during parsing!", 0, parser.getParseErrorHandler().getErrorCount() );
272
273         return ast;
274     }
275
276     private String JavaDoc toSql(String JavaDoc hql) {
277         SessionFactoryImplementor factory = getSessionFactoryImplementor();
278         QueryTranslatorFactory ast = new ASTQueryTranslatorFactory();
279         QueryTranslator queryTranslator = ast.createQueryTranslator( hql, Collections.EMPTY_MAP, factory );
280         queryTranslator.compile( Collections.EMPTY_MAP, true );
281         return queryTranslator.getSQLString();
282     }
283
284     private SessionFactoryImplementor getSessionFactoryImplementor() {
285         SessionFactoryImplementor factory = ( SessionFactoryImplementor ) getSessions();
286         if ( factory == null ) {
287             throw new NullPointerException JavaDoc( "Unable to create factory!" );
288         }
289         return factory;
290     }
291
292     protected String JavaDoc[] getMappings() {
293         return new String JavaDoc[]{
294             "hql/Animal.hbm.xml",
295             "batchfetch/ProductLine.hbm.xml",
296             "cid/Customer.hbm.xml",
297             "cid/Order.hbm.xml",
298             "cid/LineItem.hbm.xml",
299             "cid/Product.hbm.xml",
300             "legacy/Glarch.hbm.xml",
301             "legacy/Fee.hbm.xml",
302             "legacy/Qux.hbm.xml",
303             "legacy/Fum.hbm.xml",
304             "legacy/Holder.hbm.xml",
305             "legacy/One.hbm.xml",
306             "legacy/FooBar.hbm.xml",
307             "legacy/Many.hbm.xml",
308             "legacy/Baz.hbm.xml",
309             "legacy/Simple.hbm.xml",
310             "legacy/Middle.hbm.xml",
311             "legacy/Category.hbm.xml",
312             "legacy/Multi.hbm.xml",
313             "legacy/Commento.hbm.xml",
314             "legacy/Marelo.hbm.xml",
315             "compositeelement/Parent.hbm.xml",
316             "legacy/Container.hbm.xml",
317         };
318     }
319
320     public static Test suite() {
321         return new TestSuite( EJBQLTest.class );
322     }
323 }
324
Popular Tags