01: package org.apache.ojb.odmg;
02:
03: import java.util.Collection;
04: import java.util.Iterator;
05:
06: import org.apache.ojb.junit.ODMGTestCase;
07: import org.apache.ojb.odmg.shared.Article;
08: import org.apache.ojb.odmg.shared.Person;
09: import org.apache.ojb.odmg.shared.PersonImpl;
10: import org.odmg.OQLQuery;
11: import org.odmg.Transaction;
12:
13: public class ProjectionAttributeTest extends ODMGTestCase {
14: private int COUNT = 10;
15:
16: public static void main(String[] args) {
17: String[] arr = { ProjectionAttributeTest.class.getName() };
18: junit.textui.TestRunner.main(arr);
19: }
20:
21: public ProjectionAttributeTest(String name) {
22: super (name);
23: }
24:
25: private void createData(String id) throws Exception {
26: Transaction tx = odmg.newTransaction();
27: tx.begin();
28: for (int i = 0; i < COUNT; i++) {
29: Person aPerson = new PersonImpl();
30: aPerson.setFirstname("firstname" + id + "_" + i);
31: aPerson.setLastname("lastname" + id + "_" + i);
32: database.makePersistent(aPerson);
33: }
34: tx.commit();
35: }
36:
37: /**
38: * test getting all (make sure basic operation is still functional)
39: */
40: public void testGetProjectionAttribute() throws Exception {
41: String id = "_" + System.currentTimeMillis();
42: createData(id);
43:
44: // 3. Get a list of some articles
45: Transaction tx = odmg.newTransaction();
46: tx.begin();
47:
48: OQLQuery query = odmg.newOQLQuery();
49: String sql = "select aPerson.firstname, aPerson.lastname from "
50: + Person.class.getName() + " where firstname like $1";
51: query.create(sql);
52: query.bind("%" + id + "%");
53:
54: Collection result = (Collection) query.execute();
55:
56: // Iterator over the restricted articles objects
57: Iterator it = result.iterator();
58: int i = 0;
59: while (it.hasNext()) {
60: Object[] res = (Object[]) it.next();
61: String firstname = (String) res[0];
62: String lastname = (String) res[1];
63: assertTrue(firstname.startsWith("firstname"));
64: assertTrue(lastname.startsWith("lastname"));
65: i++;
66: }
67: if (i < COUNT)
68: fail("Should have found at least " + COUNT + " items");
69:
70: OQLQuery query1 = odmg.newOQLQuery();
71: query1
72: .create("select distinct anArticle.productGroup.groupId from "
73: + Article.class.getName());
74: Collection result1 = (Collection) query1.execute();
75: for (it = result1.iterator(); it.hasNext();) {
76: it.next();
77: }
78: tx.commit();
79: }
80:
81: }
|