001: package test;
002:
003: import org.makumba.ProgrammerError;
004: import org.makumba.providers.QueryAnalysis;
005: import org.makumba.providers.QueryProvider;
006:
007: import junit.framework.Test;
008: import junit.framework.TestCase;
009: import junit.framework.TestResult;
010: import junit.framework.TestSuite;
011:
012: public class hqlanalyzer extends TestCase {
013:
014: public static void main(String[] args) {
015: junit.textui.TestRunner.run(suite());
016: }
017:
018: public static Test suite() {
019: return new TestSuite(hqlanalyzer.class);
020: }
021:
022: public void run(TestResult r) {
023: try {
024: super .run(r);
025:
026: } catch (Throwable t) {
027: t.printStackTrace();
028: }
029: }
030:
031: private QueryProvider qP = QueryProvider.makeQueryAnalzyer("hql");
032:
033: public void testAnalysisComplexSet() {
034:
035: String q1 = "SELECT p.id AS pointer, i.surname as surname, p.address.description as addressdescription FROM test.Person p JOIN p.indiv as i";
036: QueryAnalysis oA = qP.getQueryAnalysis(q1);
037:
038: assertEquals("pointer", oA.getProjectionType()
039: .getFieldDefinition(0).getName());
040: assertEquals("ptr", oA.getProjectionType()
041: .getFieldDefinition(0).getType());
042: assertEquals("id", oA.getProjectionType().getFieldDefinition(0)
043: .getDescription());
044:
045: assertEquals("surname", oA.getProjectionType()
046: .getFieldDefinition(1).getName());
047: assertEquals("char", oA.getProjectionType().getFieldDefinition(
048: 1).getType());
049: assertEquals("surname", oA.getProjectionType()
050: .getFieldDefinition(1).getDescription());
051:
052: assertEquals("addressdescription", oA.getProjectionType()
053: .getFieldDefinition(2).getName());
054: assertEquals("char", oA.getProjectionType().getFieldDefinition(
055: 2).getType());
056: assertEquals("description", oA.getProjectionType()
057: .getFieldDefinition(2).getDescription());
058:
059: }
060:
061: public void testAnalysisSimpleFields() {
062:
063: String q2 = "SELECT p.id AS pointer, p.id AS key, p.birthdate AS birthdate, p.uniqInt, p.hobbies AS text FROM test.Person p";
064: QueryAnalysis oA = qP.getQueryAnalysis(q2);
065:
066: assertEquals("pointer", oA.getProjectionType()
067: .getFieldDefinition(0).getName());
068: assertEquals("ptr", oA.getProjectionType()
069: .getFieldDefinition(0).getType());
070: assertEquals("id", oA.getProjectionType().getFieldDefinition(0)
071: .getDescription());
072:
073: assertEquals("key", oA.getProjectionType()
074: .getFieldDefinition(1).getName());
075: assertEquals("ptr", oA.getProjectionType()
076: .getFieldDefinition(1).getType());
077: assertEquals("id", oA.getProjectionType().getFieldDefinition(1)
078: .getDescription());
079:
080: assertEquals("birthdate", oA.getProjectionType()
081: .getFieldDefinition(2).getName());
082: assertEquals("date", oA.getProjectionType().getFieldDefinition(
083: 2).getType());
084: assertEquals("birthdate", oA.getProjectionType()
085: .getFieldDefinition(2).getDescription());
086:
087: assertEquals("col4", oA.getProjectionType().getFieldDefinition(
088: 3).getName());
089: assertEquals("int", oA.getProjectionType()
090: .getFieldDefinition(3).getType());
091: assertEquals("uniqInt", oA.getProjectionType()
092: .getFieldDefinition(3).getDescription());
093:
094: assertEquals("text", oA.getProjectionType().getFieldDefinition(
095: 4).getName());
096: assertEquals("text", oA.getProjectionType().getFieldDefinition(
097: 4).getType());
098: assertEquals("hobbies", oA.getProjectionType()
099: .getFieldDefinition(4).getDescription());
100: }
101:
102: public void testAnalysisExtenalSetSimple() {
103:
104: String q3 = "SELECT l.name as n FROM test.Person p JOIN p.speaks as l";
105: QueryAnalysis oA = qP.getQueryAnalysis(q3);
106:
107: assertEquals("n", oA.getProjectionType().getFieldDefinition(0)
108: .getName());
109: assertEquals("char", oA.getProjectionType().getFieldDefinition(
110: 0).getType());
111: assertEquals("name", oA.getProjectionType().getFieldDefinition(
112: 0).getDescription());
113:
114: }
115:
116: public void testAnalysisExtenalSetSelectSetPointer() {
117:
118: String q3 = "SELECT l.id FROM test.Person p JOIN p.speaks as l";
119: QueryAnalysis oA = qP.getQueryAnalysis(q3);
120:
121: assertEquals("col1", oA.getProjectionType().getFieldDefinition(
122: 0).getName());
123: assertEquals("ptr", oA.getProjectionType()
124: .getFieldDefinition(0).getType());
125: assertEquals("id", oA.getProjectionType().getFieldDefinition(0)
126: .getDescription());
127:
128: }
129:
130: public void testAnalysisSelectPointer() {
131:
132: String q3 = "SELECT p.id FROM test.Person p";
133: QueryAnalysis oA = qP.getQueryAnalysis(q3);
134:
135: assertEquals("col1", oA.getProjectionType().getFieldDefinition(
136: 0).getName());
137: assertEquals("ptr", oA.getProjectionType()
138: .getFieldDefinition(0).getType());
139: assertEquals("id", oA.getProjectionType().getFieldDefinition(0)
140: .getDescription());
141:
142: }
143:
144: public void testAnalysisSetIntEnum() {
145:
146: String q = "SELECT q.enum as intset FROM test.Person p JOIN p.intSet q";
147: QueryAnalysis oA = qP.getQueryAnalysis(q);
148:
149: assertEquals("intset", oA.getProjectionType()
150: .getFieldDefinition(0).getName());
151: assertEquals("intEnum", oA.getProjectionType()
152: .getFieldDefinition(0).getType());
153: assertEquals("enum", oA.getProjectionType().getFieldDefinition(
154: 0).getDescription());
155:
156: }
157:
158: public void testAnalysisInSet() {
159:
160: String q = "SELECT p.brother as brother, p.age as age FROM test.Person p WHERE p.age IN (0, 1, 3, 2, 5)";
161: QueryAnalysis oA = qP.getQueryAnalysis(q);
162:
163: assertEquals("brother", oA.getProjectionType()
164: .getFieldDefinition(0).getName());
165: assertEquals("ptr", oA.getProjectionType()
166: .getFieldDefinition(0).getType());
167: assertEquals("brother", oA.getProjectionType()
168: .getFieldDefinition(0).getDescription());
169:
170: assertEquals("age", oA.getProjectionType()
171: .getFieldDefinition(1).getName());
172: assertEquals("int", oA.getProjectionType()
173: .getFieldDefinition(1).getType());
174: assertEquals("age", oA.getProjectionType()
175: .getFieldDefinition(1).getDescription());
176:
177: }
178:
179: public void testAnalysisAllSet() {
180:
181: String q = "SELECT p.brother as brother, p.age as age FROM test.Person p WHERE 3 = all elements(p.intSet)";
182: QueryAnalysis oA = qP.getQueryAnalysis(q);
183:
184: assertEquals("brother", oA.getProjectionType()
185: .getFieldDefinition(0).getName());
186: assertEquals("ptr", oA.getProjectionType()
187: .getFieldDefinition(0).getType());
188: assertEquals("brother", oA.getProjectionType()
189: .getFieldDefinition(0).getDescription());
190:
191: assertEquals("age", oA.getProjectionType()
192: .getFieldDefinition(1).getName());
193: assertEquals("int", oA.getProjectionType()
194: .getFieldDefinition(1).getType());
195: assertEquals("age", oA.getProjectionType()
196: .getFieldDefinition(1).getDescription());
197:
198: }
199:
200: public void testAnalysisInSetOnSetEnum() {
201:
202: String q = "SELECT p.brother as brother, p.age as age FROM test.Person p WHERE p.age IN p.intSet";
203: QueryAnalysis oA = qP.getQueryAnalysis(q);
204:
205: assertEquals("brother", oA.getProjectionType()
206: .getFieldDefinition(0).getName());
207: assertEquals("ptr", oA.getProjectionType()
208: .getFieldDefinition(0).getType());
209: assertEquals("brother", oA.getProjectionType()
210: .getFieldDefinition(0).getDescription());
211:
212: assertEquals("age", oA.getProjectionType()
213: .getFieldDefinition(1).getName());
214: assertEquals("int", oA.getProjectionType()
215: .getFieldDefinition(1).getType());
216: assertEquals("age", oA.getProjectionType()
217: .getFieldDefinition(1).getDescription());
218:
219: }
220:
221: public void testAnalysisArithmeticOperationOk() {
222:
223: String q1 = "SELECT p.brother as brother, p.age+17 as agePlus17 FROM test.Person p";
224: QueryAnalysis oA1 = qP.getQueryAnalysis(q1);
225:
226: assertEquals("brother", oA1.getProjectionType()
227: .getFieldDefinition(0).getName());
228: assertEquals("ptr", oA1.getProjectionType().getFieldDefinition(
229: 0).getType());
230: assertEquals("brother", oA1.getProjectionType()
231: .getFieldDefinition(0).getDescription());
232:
233: assertEquals("agePlus17", oA1.getProjectionType()
234: .getFieldDefinition(1).getName());
235: assertEquals("int", oA1.getProjectionType().getFieldDefinition(
236: 1).getType());
237: assertEquals("agePlus17", oA1.getProjectionType()
238: .getFieldDefinition(1).getDescription());
239:
240: String q2 = "SELECT p.brother as brother, p.age+1.2 as agePlus1dot2 FROM test.Person p";
241: QueryAnalysis oA2 = qP.getQueryAnalysis(q2);
242:
243: assertEquals("brother", oA2.getProjectionType()
244: .getFieldDefinition(0).getName());
245: assertEquals("ptr", oA2.getProjectionType().getFieldDefinition(
246: 0).getType());
247: assertEquals("brother", oA2.getProjectionType()
248: .getFieldDefinition(0).getDescription());
249:
250: assertEquals("agePlus1dot2", oA2.getProjectionType()
251: .getFieldDefinition(1).getName());
252: assertEquals("real", oA2.getProjectionType()
253: .getFieldDefinition(1).getType());
254: assertEquals("agePlus1dot2", oA2.getProjectionType()
255: .getFieldDefinition(1).getDescription());
256:
257: String q3 = "SELECT p.brother as brother, true+false as boolean FROM test.Person p";
258: QueryAnalysis oA3 = qP.getQueryAnalysis(q3);
259:
260: assertEquals("brother", oA3.getProjectionType()
261: .getFieldDefinition(0).getName());
262: assertEquals("ptr", oA3.getProjectionType().getFieldDefinition(
263: 0).getType());
264: assertEquals("brother", oA3.getProjectionType()
265: .getFieldDefinition(0).getDescription());
266:
267: assertEquals("boolean", oA3.getProjectionType()
268: .getFieldDefinition(1).getName());
269: assertEquals("setcharEnum", oA3.getProjectionType()
270: .getFieldDefinition(1).getType());
271: assertEquals("boolean", oA3.getProjectionType()
272: .getFieldDefinition(1).getDescription());
273:
274: String q4 = "SELECT p.brother as brother, p.hobbies+p.comment as text FROM test.Person p";
275: QueryAnalysis oA4 = qP.getQueryAnalysis(q4);
276:
277: assertEquals("brother", oA4.getProjectionType()
278: .getFieldDefinition(0).getName());
279: assertEquals("ptr", oA4.getProjectionType().getFieldDefinition(
280: 0).getType());
281: assertEquals("brother", oA4.getProjectionType()
282: .getFieldDefinition(0).getDescription());
283:
284: assertEquals("text", oA4.getProjectionType()
285: .getFieldDefinition(1).getName());
286: assertEquals("text", oA4.getProjectionType()
287: .getFieldDefinition(1).getType());
288: assertEquals("text", oA4.getProjectionType()
289: .getFieldDefinition(1).getDescription());
290: }
291:
292: public void testAnalysisArithmeticOperationParameter() {
293:
294: String q1 = "SELECT p.brother as brother, p.age+:name as param FROM test.Person p";
295: QueryAnalysis oA = qP.getQueryAnalysis(q1);
296:
297: assertEquals("brother", oA.getProjectionType()
298: .getFieldDefinition(0).getName());
299: assertEquals("ptr", oA.getProjectionType()
300: .getFieldDefinition(0).getType());
301: assertEquals("brother", oA.getProjectionType()
302: .getFieldDefinition(0).getDescription());
303:
304: assertEquals("param", oA.getProjectionType()
305: .getFieldDefinition(1).getName());
306: assertEquals("parameter", oA.getProjectionType()
307: .getFieldDefinition(1).getType());
308: assertEquals("param", oA.getProjectionType()
309: .getFieldDefinition(1).getDescription());
310:
311: }
312:
313: public void testAnalysisSubQueries() {
314:
315: String q1 = "SELECT p.brother as brother, p.address.streetno as streetno FROM test.Person p WHERE p.uniqInt IN (SELECT p.id as pid FROM test.Person p WHERE p.age = 17)";
316: QueryAnalysis oA = qP.getQueryAnalysis(q1);
317:
318: //System.out.println(oA.toString());
319:
320: assertEquals("brother", oA.getProjectionType()
321: .getFieldDefinition(0).getName());
322: assertEquals("ptr", oA.getProjectionType()
323: .getFieldDefinition(0).getType());
324: assertEquals("brother", oA.getProjectionType()
325: .getFieldDefinition(0).getDescription());
326:
327: assertEquals("streetno", oA.getProjectionType()
328: .getFieldDefinition(1).getName());
329: assertEquals("char", oA.getProjectionType().getFieldDefinition(
330: 1).getType());
331: assertEquals("streetno", oA.getProjectionType()
332: .getFieldDefinition(1).getDescription());
333:
334: }
335:
336: public void testAnalysisFunctionCalls() {
337:
338: String q1 = "SELECT p.brother as brother, upper(p.address.country) as streetno FROM test.Person p";
339: QueryAnalysis oA = qP.getQueryAnalysis(q1);
340:
341: assertEquals("brother", oA.getProjectionType()
342: .getFieldDefinition(0).getName());
343: assertEquals("ptr", oA.getProjectionType()
344: .getFieldDefinition(0).getType());
345: assertEquals("brother", oA.getProjectionType()
346: .getFieldDefinition(0).getDescription());
347:
348: assertEquals("streetno", oA.getProjectionType()
349: .getFieldDefinition(1).getName());
350: assertEquals("char", oA.getProjectionType().getFieldDefinition(
351: 1).getType());
352: assertEquals("streetno", oA.getProjectionType()
353: .getFieldDefinition(1).getDescription());
354:
355: String q2 = "SELECT p.brother as brother, now() as currenttime FROM test.Person p";
356: QueryAnalysis oA2 = qP.getQueryAnalysis(q2);
357:
358: //System.out.println(oA2.toString());
359:
360: assertEquals("brother", oA2.getProjectionType()
361: .getFieldDefinition(0).getName());
362: assertEquals("ptr", oA2.getProjectionType().getFieldDefinition(
363: 0).getType());
364: assertEquals("brother", oA2.getProjectionType()
365: .getFieldDefinition(0).getDescription());
366:
367: assertEquals("currenttime", oA2.getProjectionType()
368: .getFieldDefinition(1).getName());
369: assertEquals("date", oA2.getProjectionType()
370: .getFieldDefinition(1).getType());
371: assertEquals("currenttime", oA2.getProjectionType()
372: .getFieldDefinition(1).getDescription());
373:
374: }
375:
376: public void testAnalysisSelectWholeObject() {
377:
378: String q1 = "SELECT p as superPointer FROM test.Person p)";
379: QueryAnalysis oA = qP.getQueryAnalysis(q1);
380:
381: //System.out.println(oA.toString());
382:
383: assertEquals("superPointer", oA.getProjectionType()
384: .getFieldDefinition(0).getName());
385: assertEquals("ptr", oA.getProjectionType()
386: .getFieldDefinition(0).getType());
387: assertEquals("p", oA.getProjectionType().getFieldDefinition(0)
388: .getDescription());
389:
390: }
391:
392: public void testAnalysisSelectWholeObjectIndirect() {
393: String q1 = "SELECT p.brother as superPointer FROM test.Person p)";
394: QueryAnalysis oA = qP.getQueryAnalysis(q1);
395:
396: assertEquals("superPointer", oA.getProjectionType()
397: .getFieldDefinition(0).getName());
398: assertEquals("ptr", oA.getProjectionType()
399: .getFieldDefinition(0).getType());
400: assertEquals("brother", oA.getProjectionType()
401: .getFieldDefinition(0).getDescription());
402: }
403:
404: public void testAnalysisWholeObjectWithOtherSelections() {
405: String q1 = "SELECT p.brother AS col1,p AS col2, p.hobbies AS col3, p.birthdate AS col4 FROM test.Person p";
406: QueryAnalysis oA = qP.getQueryAnalysis(q1);
407:
408: assertEquals("col1", oA.getProjectionType().getFieldDefinition(
409: 0).getName());
410: assertEquals("ptr", oA.getProjectionType()
411: .getFieldDefinition(0).getType());
412: assertEquals("brother", oA.getProjectionType()
413: .getFieldDefinition(0).getDescription());
414: assertEquals("col2", oA.getProjectionType().getFieldDefinition(
415: 1).getName());
416: assertEquals("ptr", oA.getProjectionType()
417: .getFieldDefinition(1).getType());
418: assertEquals("p", oA.getProjectionType().getFieldDefinition(1)
419: .getDescription());
420: assertEquals("col3", oA.getProjectionType().getFieldDefinition(
421: 2).getName());
422: assertEquals("text", oA.getProjectionType().getFieldDefinition(
423: 2).getType());
424: assertEquals("hobbies", oA.getProjectionType()
425: .getFieldDefinition(2).getDescription());
426: assertEquals("col4", oA.getProjectionType().getFieldDefinition(
427: 3).getName());
428: assertEquals("date", oA.getProjectionType().getFieldDefinition(
429: 3).getType());
430: assertEquals("birthdate", oA.getProjectionType()
431: .getFieldDefinition(3).getDescription());
432:
433: }
434:
435: public void testAnalysisNoSuchField() {
436:
437: String q1 = "SELECT p as superPointer FROM test.Person p JOIN p.t q)";
438: try {
439: QueryAnalysis oA = qP.getQueryAnalysis(q1);
440: //fail("ProgrammerError expected" );
441: } catch (ProgrammerError e) {
442: assertEquals(e.getMessage(),
443: "No such field \"t\" in Makumba type \"test.Person\"");
444: }
445: }
446:
447: public void test() {
448:
449: String q1 = "SELECT p.TS_create AS col1 FROM test.Person p, p.indiv i WHERE i.name='john'";
450: QueryAnalysis oA = qP.getQueryAnalysis(q1);
451:
452: //System.out.println(oA.toString());
453:
454: //assertEquals("superPointer", oA.getProjectionType().getFieldDefinition(0).getName());
455: //assertEquals("ptr", oA.getProjectionType().getFieldDefinition(0).getType());
456: //assertEquals("p", oA.getProjectionType().getFieldDefinition(0).getDescription());
457:
458: }
459:
460: }
|