KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > ojb > odmg > ProjectionAttributeTest


1 package org.apache.ojb.odmg;
2
3 import java.util.Collection JavaDoc;
4 import java.util.Iterator JavaDoc;
5
6 import org.apache.ojb.junit.ODMGTestCase;
7 import org.apache.ojb.odmg.shared.Article;
8 import org.apache.ojb.odmg.shared.Person;
9 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 {
15     private int COUNT = 10;
16
17     public static void main(String JavaDoc[] args)
18     {
19         String JavaDoc[] arr = {ProjectionAttributeTest.class.getName()};
20         junit.textui.TestRunner.main(arr);
21     }
22
23     public ProjectionAttributeTest(String JavaDoc name)
24     {
25         super(name);
26     }
27
28     private void createData(String JavaDoc id) throws Exception JavaDoc
29     {
30         Transaction tx = odmg.newTransaction();
31         tx.begin();
32         for (int i = 0; i < COUNT; i++)
33         {
34             Person aPerson = new PersonImpl();
35             aPerson.setFirstname("firstname" + id +"_" + i);
36             aPerson.setLastname("lastname" + id +"_" + i);
37             database.makePersistent(aPerson);
38         }
39         tx.commit();
40     }
41
42     /**
43      * test getting all (make sure basic operation is still functional)
44      */

45     public void testGetProjectionAttribute() throws Exception JavaDoc
46     {
47         String JavaDoc id = "_" + System.currentTimeMillis();
48         createData(id);
49
50         // 3. Get a list of some articles
51
Transaction tx = odmg.newTransaction();
52         tx.begin();
53
54         OQLQuery query = odmg.newOQLQuery();
55         String JavaDoc sql = "select aPerson.firstname, aPerson.lastname from " + Person.class.getName() + " where firstname like $1";
56         query.create(sql);
57         query.bind("%" + id + "%");
58
59         Collection JavaDoc result = (Collection JavaDoc) query.execute();
60
61         // Iterator over the restricted articles objects
62
Iterator JavaDoc it = result.iterator();
63         int i = 0;
64         while (it.hasNext())
65         {
66             Object JavaDoc[] res = (Object JavaDoc[]) it.next();
67             String JavaDoc firstname = (String JavaDoc) res[0];
68             String JavaDoc lastname = (String JavaDoc) res[1];
69             assertTrue(firstname.startsWith("firstname"));
70             assertTrue(lastname.startsWith("lastname"));
71             i++;
72         }
73         if (i < COUNT)
74             fail("Should have found at least " + COUNT + " items");
75
76         OQLQuery query1 = odmg.newOQLQuery();
77         query1.create("select distinct anArticle.productGroup.groupId from " + Article.class.getName());
78         Collection JavaDoc result1 = (Collection JavaDoc) query1.execute();
79         for (it = result1.iterator(); it.hasNext(); )
80         {
81             it.next();
82         }
83         tx.commit();
84     }
85
86 }
87
Popular Tags