01: package org.xorm.tests;
02:
03: import javax.jdo.PersistenceManager;
04: import javax.jdo.Query;
05:
06: import org.xorm.query.CodeQuery;
07: import org.xorm.tests.model.*;
08: import java.util.Collection;
09:
10: public class TestCodeQuery extends XORMTestCase {
11: public static void main(String[] argv) throws Exception {
12: TestCodeQuery tcq = new TestCodeQuery();
13: tcq.setUp();
14: tcq.testQueries();
15: tcq.tearDown();
16: }
17:
18: public void testQueries() {
19: PersistenceManager mgr = factory.getPersistenceManager();
20: Query q;
21:
22: Class[] queries = new Class[] { Query0.class, Query1.class,
23: Query2.class, Query3.class, Query4.class };
24:
25: Object[] params = new Object[] { new Object[] { "Foo" },
26: new Object[] { new Integer(11), new Float(22.2) },
27: new Object[] { new Double(33.33d), "Bar" },
28: new Object[] {}, new Object[] {} };
29:
30: // Execute each query
31: for (int i = 0; i < queries.length; i++) {
32: q = CodeQuery.parseCodeQuery(mgr, queries[i]);
33: System.out.println(q);
34: // Force the query to execute
35: ((Collection) q.executeWithArray((Object[]) params[i]))
36: .iterator().hasNext();
37: }
38:
39: q = mgr
40: .newQuery(TestInterface.class,
41: "collection.contains(employee) && employee.firstName != \"Bob\"");
42: q.declareVariables("Employee employee");
43: System.out.println(q);
44: ((Collection) q.execute()).iterator().hasNext();
45:
46: }
47:
48: abstract static class Query0 implements TestInterface {
49: public boolean evaluate(String s) {
50: return getString().equals(s);
51: }
52: }
53:
54: abstract static class Query1 implements TestInterface {
55: public boolean evaluate(int ii, float ff) {
56: return (getInt() == ii) && (getFloat() == ff);
57: }
58: }
59:
60: abstract static class Query2 implements TestInterface {
61: public boolean evaluate(double dd, String ss) {
62: return getString().equals(ss) || (getDouble() > dd);
63: }
64: }
65:
66: abstract static class Query3 implements TestInterface {
67: public transient Employee employee;
68:
69: public boolean evaluate() {
70: return getCollection().contains(employee)
71: & !employee.getFirstName().equals("Bob");
72: }
73: }
74:
75: abstract static class Query4 implements TestInterface {
76: public transient Employee employee;
77:
78: public boolean evaluate() {
79: return getCollection().contains(employee)
80: & !employee.getFirstName().equals("Bob")
81: && employee.getLastName().startsWith("Sm")
82: && getBoolean();
83: }
84: }
85:
86: }
|