KickJava   Java API By Example, From Geeks To Geeks.

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


1 /**
2  * Author: Matthew Baird
3  * mattbaird@yahoo.com
4  */

5 package org.apache.ojb.broker;
6
7 import org.apache.ojb.broker.query.Criteria;
8 import org.apache.ojb.broker.query.Query;
9 import org.apache.ojb.broker.query.QueryFactory;
10 import org.apache.ojb.junit.PBTestCase;
11
12 import java.sql.Timestamp JavaDoc;
13 import java.util.Iterator JavaDoc;
14
15 /**
16  * This TestClass tests OJB ability to handle Contract Version Effectiveness patterns.
17  */

18 public class ComplexMultiMappedTableWithCollectionByQueryTest extends PBTestCase
19 {
20     private int COUNT = 10;
21     private static Class JavaDoc CLASS = ComplexMultiMappedTableWithCollectionByQueryTest.class;
22
23     public static void main(String JavaDoc[] args)
24     {
25         String JavaDoc[] arr = {CLASS.getName()};
26         junit.textui.TestRunner.main(arr);
27     }
28
29     /**
30      * Insert the method's description here.
31      * Creation date: (24.12.2000 00:33:40)
32      */

33     public ComplexMultiMappedTableWithCollectionByQueryTest(String JavaDoc name)
34     {
35         super(name);
36     }
37
38     /**
39      * Insert the method's description here.
40      * Creation date: (06.12.2000 21:58:53)
41      */

42     public void setUp() throws Exception JavaDoc
43     {
44         super.setUp();
45         createTestData();
46     }
47
48     private int deleteAllData()
49     {
50         int number_deleted = 0;
51         Criteria crit = new Criteria();
52         Query q = QueryFactory.newQuery(ComplexMultiMapped.PersistentA.class, crit);
53         broker.beginTransaction();
54         Iterator JavaDoc iter = broker.getCollectionByQuery(q).iterator();
55         while (iter.hasNext())
56         {
57             broker.delete(iter.next());
58             number_deleted++;
59         }
60         /**
61          * will delete all B and D and E (both of which extends B)
62          */

63         crit = new Criteria();
64         q = QueryFactory.newQuery(ComplexMultiMapped.PersistentB.class, crit);
65         iter = broker.getCollectionByQuery(q).iterator();
66
67         while (iter.hasNext())
68         {
69             broker.delete(iter.next());
70             number_deleted++;
71         }
72         /**
73          * will delete all C
74          */

75         crit = new Criteria();
76         q = QueryFactory.newQuery(ComplexMultiMapped.PersistentC.class, crit);
77         iter = broker.getCollectionByQuery(q).iterator();
78         while (iter.hasNext())
79         {
80             broker.delete(iter.next());
81             number_deleted++;
82         }
83         broker.commitTransaction();
84         return number_deleted;
85     }
86
87     private void createTestData()
88     {
89         /**
90          * create COUNT of each object
91          */

92         broker.beginTransaction();
93         for (int i = 0; i < COUNT; i++)
94         {
95             ComplexMultiMapped.PersistentA a = new ComplexMultiMapped.PersistentA();
96             a.setValue1("a");
97             a.setValue2(i);
98             a.setValue3(new Timestamp JavaDoc(System.currentTimeMillis()));
99             broker.store(a);
100
101             ComplexMultiMapped.PersistentB b = new ComplexMultiMapped.PersistentB();
102             b.setValue4("b");
103             b.setValue5(i);
104             b.setValue6(new Timestamp JavaDoc(System.currentTimeMillis()));
105             broker.store(b);
106
107             ComplexMultiMapped.PersistentC c = new ComplexMultiMapped.PersistentC();
108             c.setValue1("c");
109             c.setValue2(i);
110             c.setValue3(new Timestamp JavaDoc(System.currentTimeMillis()));
111             c.setValue4("c");
112             c.setValue5(i);
113             c.setValue6(new Timestamp JavaDoc(System.currentTimeMillis()));
114             broker.store(c);
115
116             ComplexMultiMapped.PersistentD d = new ComplexMultiMapped.PersistentD();
117             d.setValue1("d");
118             d.setValue2(i);
119             d.setValue3(new Timestamp JavaDoc(System.currentTimeMillis()));
120             d.setValue4("d");
121             d.setValue5(i);
122             d.setValue6(new Timestamp JavaDoc(System.currentTimeMillis()));
123             broker.store(d);
124
125             ComplexMultiMapped.PersistentE e = new ComplexMultiMapped.PersistentE();
126             e.setValue1("e");
127             e.setValue2(i);
128             e.setValue3(new Timestamp JavaDoc(System.currentTimeMillis()));
129             e.setValue4("e");
130             e.setValue5(i);
131             e.setValue6(new Timestamp JavaDoc(System.currentTimeMillis()));
132             broker.store(e);
133
134             ComplexMultiMapped.PersistentF f = new ComplexMultiMapped.PersistentF();
135             f.setValue1("f");
136             f.setValue2(i);
137             f.setValue3(new Timestamp JavaDoc(System.currentTimeMillis()));
138             f.setValue4("f");
139             f.setValue5(i);
140             f.setValue6(new Timestamp JavaDoc(System.currentTimeMillis()));
141             broker.store(f);
142         }
143         broker.commitTransaction();
144     }
145
146     public void testCreate()
147     {
148         createTestData();
149     }
150
151     public void testGet()
152     {
153         /**
154          * get to a clean, known state.
155          */

156         deleteAllData();
157         /**
158          * now create a bunch of test data.
159          */

160         createTestData();
161
162         Criteria crit = new Criteria();
163         Query q;
164         Iterator JavaDoc iter;
165         int count;
166         /**
167          * check all A's
168          */

169         q = QueryFactory.newQuery(ComplexMultiMapped.PersistentA.class, crit);
170         iter = broker.getCollectionByQuery(q).iterator();
171         ComplexMultiMapped.PersistentA a = null;
172         count = 0;
173         while (iter.hasNext())
174         {
175             a = (ComplexMultiMapped.PersistentA) iter.next();
176             if (!a.getValue1().equals("a"))
177             {
178                 fail("getValue1 should have returned 'a', it in fact returned '" + a.getValue1() + "'");
179             }
180             count++;
181         }
182         if (count != COUNT)
183             fail("should have found " + COUNT + " ComplexMultiMapped.PersistentA's, in fact found " + count);
184
185         assertEquals("counted size", broker.getCount(q), count);
186
187         /**
188          * check all B's
189          */

190         crit = new Criteria();
191         q = QueryFactory.newQuery(ComplexMultiMapped.PersistentB.class, crit);
192         iter = broker.getCollectionByQuery(q).iterator();
193         ComplexMultiMapped.PersistentB b = null;
194         count = 0;
195         while (iter.hasNext())
196         {
197             b = (ComplexMultiMapped.PersistentB) iter.next();
198             if (!b.getValue4().equals("b") && !b.getValue4().equals("d") && !b.getValue4().equals("e") && !b.getValue4().equals("f"))
199             {
200                 fail("getValue4 should have returned 'b' or 'd' or 'e' or 'f' (from extent), it in fact returned '" + b.getValue4() + "'");
201             }
202             count++;
203         }
204         /**
205          * should find ALL b's, d's, e's and f's, so COUNT*4 is the expected result
206          */

207         if (count != COUNT*4)
208             fail("should have found " + (COUNT *4) + " ComplexMultiMapped.PersistentB's, in fact found " + count);
209
210         assertEquals("counted size", broker.getCount(q), count);
211
212         /**
213          * check all C's
214          */

215         crit = new Criteria();
216         q = QueryFactory.newQuery(ComplexMultiMapped.PersistentC.class, crit);
217         iter = broker.getCollectionByQuery(q).iterator();
218         ComplexMultiMapped.PersistentC c = null;
219         count = 0;
220         while (iter.hasNext())
221         {
222             c = (ComplexMultiMapped.PersistentC) iter.next();
223             if (!c.getValue1().equals("c"))
224             {
225                 fail("getValue1 should have returned 'c', it in fact returned '" + c.getValue1() + "'");
226             }
227             count++;
228         }
229         if (count != COUNT)
230             fail("should have found " + COUNT + " ComplexMultiMapped.PersistentC's, in fact found " + count);
231
232         assertEquals("counted size", broker.getCount(q), count);
233
234         /**
235          * check all D's
236          */

237         crit = new Criteria();
238         q = QueryFactory.newQuery(ComplexMultiMapped.PersistentD.class, crit);
239         iter = broker.getCollectionByQuery(q).iterator();
240         ComplexMultiMapped.PersistentD d = null;
241         count = 0;
242         while (iter.hasNext())
243         {
244             d = (ComplexMultiMapped.PersistentD) iter.next();
245             if (!d.getValue1().equals("d"))
246             {
247                 fail("getValue1 should have returned 'd', it in fact returned '" + d.getValue1() + "'");
248             }
249             count++;
250         }
251         if (count != COUNT)
252             fail("should have found " + COUNT + " ComplexMultiMapped.PersistentD's, in fact found " + count);
253
254         assertEquals("counted size", broker.getCount(q), count);
255
256         /**
257          * check all E's
258          */

259         crit = new Criteria();
260         q = QueryFactory.newQuery(ComplexMultiMapped.PersistentE.class, crit);
261         iter = broker.getCollectionByQuery(q).iterator();
262         ComplexMultiMapped.PersistentE e = null;
263         count = 0;
264         while (iter.hasNext())
265         {
266             e = (ComplexMultiMapped.PersistentE) iter.next();
267             if (!e.getValue1().equals("e") && !e.getValue1().equals("f"))
268             {
269                 fail("getValue1 should have returned 'e' or 'f' (extent), it in fact returned '" + e.getValue1() + "'");
270             }
271             count++;
272         }
273         if (count != COUNT *2)
274             fail("should have found " + (COUNT*2) + " ComplexMultiMapped.PersistentE's, in fact found " + count);
275
276         assertEquals("counted size", broker.getCount(q), count);
277
278         /**
279                  * check all F's NEeds to be figured out.
280                 crit = new Criteria();
281                 q = QueryFactory.newQuery(ComplexMultiMapped.PersistentF.class, crit);
282                 iter = broker.getCollectionByQuery(q).iterator();
283                 ComplexMultiMapped.PersistentF f = null;
284                 count = 0;
285                 while (iter.hasNext())
286                 {
287                     f = (ComplexMultiMapped.PersistentF) iter.next();
288                     if (!f.getValue1().equals("f"))
289                     {
290                         fail("getValue1 should have returned 'f', it in fact returned '" + f.getValue1() + "'");
291                     }
292                     count++;
293                 }
294                 if (count != COUNT)
295                     fail("should have found " + COUNT + " ComplexMultiMapped.PersistentF's, in fact found " + count);
296                  */

297
298     }
299
300     public void testDeleteWithData()
301     {
302         /**
303          * put some data in
304          */

305         createTestData();
306         /**
307          * then delete it.
308          */

309         int number_deleted = deleteAllData();
310         /**
311          * we should have the number of classes we put in (4) * the number we put in (COUNT of each)
312          */

313         if (number_deleted < (5*COUNT))
314         {
315             fail("Should have deleted at least " + (4*COUNT) + " actually deleted " + number_deleted);
316         }
317     }
318
319     public void testDeleteWithNoData()
320     {
321         /**
322          * clear all data
323          */

324         deleteAllData();
325         /**
326          * call delete again: there should be nothing.
327          */

328         int number_deleted = deleteAllData();
329         if (number_deleted !=0)
330         {
331             fail("Should have deleted 0, instead deleted " + number_deleted);
332         }
333     }
334 }
335
Popular Tags