KickJava   Java API By Example, From Geeks To Geeks.

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


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.Collection JavaDoc;
14 import java.util.Iterator JavaDoc;
15
16 /**
17  * This TestClass tests OJB ability to handle Contract Version Effectiveness patterns.
18  */

19 public class ComplexMultiMappedTableTest extends PBTestCase
20 {
21     private int COUNT = 10;
22
23     public static void main(String JavaDoc[] args)
24     {
25         String JavaDoc[] arr = {ComplexMultiMappedTableTest.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 ComplexMultiMappedTableTest(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         Collection JavaDoc result;
51         Iterator JavaDoc iter;
52         int number_deleted = 0;
53
54         broker.beginTransaction();
55         Criteria crit = new Criteria();
56         Query q = QueryFactory.newQuery(ComplexMultiMapped.PersistentA.class, crit);
57         result = broker.getCollectionByQuery(q);
58         iter = result.iterator();
59         while (iter.hasNext())
60         {
61             broker.delete(iter.next());
62             number_deleted++;
63         }
64         /**
65          * will delete all B and D and E (both of which extends B)
66          */

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

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

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

161         deleteAllData();
162         /**
163          * now create a bunch of test data.
164          */

165         createTestData();
166
167         Criteria crit = new Criteria();
168         Query q;
169         Iterator JavaDoc iter;
170         int count;
171         /**
172          * check all A's
173          */

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

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

210         if (count != COUNT*4)
211             fail("should have found " + (COUNT *4) + " ComplexMultiMapped.PersistentB's, in fact found " + count);
212
213         /**
214          * check all C's
215          */

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

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

256         crit = new Criteria();
257         q = QueryFactory.newQuery(ComplexMultiMapped.PersistentE.class, crit);
258         iter = broker.getIteratorByQuery(q);
259         ComplexMultiMapped.PersistentE e = null;
260         count = 0;
261         while (iter.hasNext())
262         {
263             e = (ComplexMultiMapped.PersistentE) iter.next();
264             if (!e.getValue1().equals("e") && !e.getValue1().equals("f"))
265             {
266                 fail("getValue1 should have returned 'e' or 'f' (extent), it in fact returned '" + e.getValue1() + "'");
267             }
268             count++;
269         }
270         if (count != COUNT *2)
271             fail("should have found " + (COUNT*2) + " ComplexMultiMapped.PersistentE's, in fact found " + count);
272
273         /**
274                  * check all F's NEEDS TO BE FIGURED OUT.
275                 crit = new Criteria();
276                 q = QueryFactory.newQuery(ComplexMultiMapped.PersistentF.class, crit);
277                 iter = broker.getIteratorByQuery(q);
278                 ComplexMultiMapped.PersistentF f = null;
279                 count = 0;
280                 while (iter.hasNext())
281                 {
282                     f = (ComplexMultiMapped.PersistentF) iter.next();
283                     if (!f.getValue1().equals("f"))
284                     {
285                         fail("getValue1 should have returned 'f', it in fact returned '" + f.getValue1() + "'");
286                     }
287                     count++;
288                 }
289                 if (count != COUNT)
290                     fail("should have found " + COUNT + " ComplexMultiMapped.PersistentF's, in fact found " + count);
291                  */

292
293     }
294
295     public void testDeleteWithData()
296     {
297         /**
298          * put some data in
299          */

300         createTestData();
301         /**
302          * then delete it.
303          */

304         int number_deleted = deleteAllData();
305         /**
306          * we should have the number of classes we put in (4) * the number we put in (COUNT of each)
307          */

308         if (number_deleted < (5*COUNT))
309         {
310             fail("Should have deleted at least " + (4*COUNT) + " actually deleted " + number_deleted);
311         }
312     }
313
314     public void testDeleteWithNoData()
315     {
316         /**
317          * clear all data
318          */

319         deleteAllData();
320         /**
321          * call delete again: there should be nothing.
322          */

323         int number_deleted = deleteAllData();
324         if (number_deleted !=0)
325         {
326             fail("Should have deleted 0, instead deleted " + number_deleted);
327         }
328
329     }
330
331 }
332
Popular Tags