KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > ojb > broker > MultipleTableExtentAwareQueryTest


1 package org.apache.ojb.broker;
2
3 import java.util.Collection JavaDoc;
4 import java.util.Iterator JavaDoc;
5
6 import junit.framework.TestCase;
7
8 import org.apache.ojb.broker.query.Criteria;
9 import org.apache.ojb.broker.query.QueryByCriteria;
10 import org.apache.ojb.broker.query.QueryFactory;
11
12 /**
13  * Test query against extent classes and abstract base class.
14  *
15  * @author <a HREF="mailto:armin@codeAuLait.de">Armin Waibel</a>
16  * @version $Id: MultipleTableExtentAwareQueryTest.java,v 1.2.2.2 2005/08/17 13:54:37 aclute Exp $
17  */

18 public class MultipleTableExtentAwareQueryTest extends TestCase
19 {
20     PersistenceBroker broker;
21
22     public MultipleTableExtentAwareQueryTest(String JavaDoc s)
23     {
24         super(s);
25     }
26
27     public void setUp()
28     {
29         broker = PersistenceBrokerFactory.defaultPersistenceBroker();
30     }
31
32     protected void tearDown() throws Exception JavaDoc
33     {
34         if(broker != null && !broker.isClosed()) broker.close();
35     }
36
37     public static void main(String JavaDoc[] args)
38     {
39         String JavaDoc[] arr = {MultipleTableExtentAwareQueryTest.class.getName()};
40         junit.textui.TestRunner.main(arr);
41     }
42
43     public void testQueryForBaseClass()
44     {
45         int objCount = 5;
46         String JavaDoc name = "testQueryForBaseClass_"+System.currentTimeMillis();
47         broker.beginTransaction();
48         for (int i = objCount-1; i >= 0; i--)
49         {
50             ExtentA a = new ExtentA();
51             a.setName(name);
52             ExtentB b = new ExtentB();
53             b.setName(name);
54             broker.store(a);
55             broker.store(b);
56         }
57         broker.commitTransaction();
58
59         Criteria crit = new Criteria();
60         crit.addEqualTo("name", name);
61         QueryByCriteria query = QueryFactory.newQuery(BaseClass.class, crit);
62         Collection JavaDoc result = broker.getCollectionByQuery(query);
63         assertNotNull(result);
64         assertEquals("Expect all objects extending 'BaseClass'", 2*objCount, result.size());
65
66         int count = broker.getCount(query);
67         assertEquals("Expect all objects extending 'BaseClass'", 2*objCount, count);
68     }
69
70     public void testQueryForExtentsOfAbstractClass()
71     {
72         int objCount = 5;
73         String JavaDoc name = "testQueryForExtentsOfAbstractClass_"+System.currentTimeMillis();
74         broker.beginTransaction();
75         for (int i = objCount-1; i >= 0; i--)
76         {
77             ExtentA a = new ExtentA();
78             a.setName(name);
79             ExtentB b = new ExtentB();
80             b.setName(name);
81             broker.store(a);
82             broker.store(b);
83         }
84         broker.commitTransaction();
85
86         Criteria crit = new Criteria();
87         crit.addEqualTo("name", name);
88         QueryByCriteria query = QueryFactory.newQuery(ExtentA.class, crit);
89         Collection JavaDoc result = broker.getCollectionByQuery(query);
90         assertNotNull(result);
91         assertEquals("Wrong number of objects, expect only classes of type 'ExtentA'", objCount, result.size());
92         assertTrue("Expect only classes of type 'ExtentA'", result.iterator().next() instanceof ExtentA);
93
94         int count = broker.getCount(query);
95         assertNotNull(result);
96         assertEquals("Wrong number of objects, expect only classes of type 'ExtentA'", objCount, count);
97     }
98
99     public void testQueryForExtentsOfRealClass()
100     {
101         int objCount = 5;
102         String JavaDoc name = "testQueryForExtentsOfRealClass_"+System.currentTimeMillis();
103         broker.beginTransaction();
104         for (int i = objCount-1; i >= 0; i--)
105         {
106             ExtentB b = new ExtentB();
107             b.setName(name);
108             ExtentC c = new ExtentC();
109             c.setName(name);
110             ExtentD d = new ExtentD();
111             d.setName(name);
112             broker.store(b);
113             broker.store(c);
114             broker.store(d);
115         }
116         broker.commitTransaction();
117
118         Criteria crit = new Criteria();
119         crit.addEqualTo("name", name);
120         QueryByCriteria query = QueryFactory.newQuery(ExtentD.class, crit);
121         Collection JavaDoc result = broker.getCollectionByQuery(query);
122         assertNotNull(result);
123         assertEquals("Wrong number of objects, expect only classes of type 'ExtentD'", objCount, result.size());
124         assertTrue("Expect only classes of type 'ExtentD'", result.iterator().next() instanceof ExtentD);
125
126         query = QueryFactory.newQuery(ExtentC.class, crit);
127         result = broker.getCollectionByQuery(query);
128         assertNotNull(result);
129         assertEquals("Wrong number of objects, expect only classes of type ExtentC/ExtentD",
130                 objCount*2, result.size());
131
132         int counterB = 0;
133         for (Iterator JavaDoc iterator = result.iterator(); iterator.hasNext();)
134         {
135             Object JavaDoc obj = (ExtentC) iterator.next();
136             if(obj instanceof ExtentB) ++counterB;
137         }
138
139         query = QueryFactory.newQuery(ExtentB.class, crit);
140         result = broker.getCollectionByQuery(query);
141         assertNotNull(result);
142         assertEquals("Wrong number of objects, expect only classes of type ExtentB/ExtentC/ExtentD",
143                 objCount*3, result.size());
144         counterB = 0;
145         int counterC = 0;
146         for (Iterator JavaDoc iterator = result.iterator(); iterator.hasNext();)
147         {
148             Object JavaDoc obj = iterator.next();
149             if(obj instanceof ExtentB) ++counterB;
150             if(obj instanceof ExtentC) ++counterC;
151         }
152         assertEquals(15, counterB);
153         assertEquals(10, counterC);
154         int count = broker.getCount(query);
155         assertEquals("Wrong number of objects, expect only classes of type ExtentB/ExtentC/ExtentD",
156                 objCount*3, count);
157     }
158
159
160
161     //********************************************************
162
// inner classes test objects
163
//********************************************************
164
public static class ExtentA extends BaseClass
165     {
166     }
167
168     public static class ExtentB extends BaseClass
169     {
170     }
171
172     public static class ExtentC extends ExtentB
173     {
174     }
175
176     public static class ExtentD extends ExtentC
177     {
178     }
179
180     public abstract static class BaseClass
181     {
182         private int objId;
183         private String JavaDoc name;
184
185         public int getObjId()
186         {
187             return objId;
188         }
189
190         public void setObjId(int objId)
191         {
192             this.objId = objId;
193         }
194
195         public String JavaDoc getName()
196         {
197             return name;
198         }
199
200         public void setName(String JavaDoc name)
201         {
202             this.name = name;
203         }
204     }
205 }
206
Popular Tags