KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > triactive > jdo > test > MapFieldTest


1 /**
2  * Copyright 2002 (C) TJDO.
3  * All rights reserved.
4  *
5  * This software is distributed under the terms of the TJDO License version 1.0.
6  * See the terms of the TJDO License in the documentation provided with this software.
7  *
8  * $Id: MapFieldTest.java,v 1.4 2002/11/24 06:02:48 jackknifebarber Exp $
9  */

10
11 package com.triactive.jdo.test;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.Iterator JavaDoc;
15 import java.util.Map JavaDoc;
16 import java.util.Random JavaDoc;
17 import javax.jdo.PersistenceManager;
18 import javax.jdo.Transaction;
19
20
21 /**
22  * Tests persistent fields of type <code>java.util.Map</code>.
23  *
24  * @author <a HREF="mailto:mmartin5@austin.rr.com">Mike Martin</a>
25  * @version $Revision: 1.4 $
26  */

27
28 public class MapFieldTest extends PersistenceTestCase
29 {
30     private static final int NUM_TESTER_OBJECTS = 5;
31     private static final int NUM_MAP_VALUES = 5;
32
33     protected boolean schemaInitialized = false;
34     /**
35      * Used by the JUnit framework to construct tests.
36      *
37      * @param name Name of the test case.
38      */

39
40     public MapFieldTest(String JavaDoc name)
41     {
42         super(name);
43     }
44
45
46     protected void setUp() throws Exception JavaDoc
47     {
48         super.setUp();
49
50         PersistenceManager pm = pmf.getPersistenceManager();
51         if (!schemaInitialized)
52         {
53             addClassesToSchema(new Class JavaDoc[]
54                 {
55                     Person.class,
56                     MapFieldTester.class,
57                     InverseMapFieldTester.class
58                 }
59             );
60
61             schemaInitialized = true;
62         }
63
64         Transaction tx = pm.currentTransaction();
65         try
66         {
67             /* Delete all MapFieldTester and InverseMapFieldTester objects. */
68             tx.begin();
69
70             Iterator JavaDoc i = pm.getExtent(MapFieldTester.class, true).iterator();
71
72             while (i.hasNext())
73                 pm.deletePersistent(i.next());
74
75             i = pm.getExtent(InverseMapFieldTester.class, true).iterator();
76
77             while (i.hasNext())
78                 pm.deletePersistent(i.next());
79
80             tx.commit();
81         }
82         finally
83         {
84             if (tx.isActive())
85                 tx.rollback();
86
87             pm.close();
88         }
89     }
90
91
92     public void testNormalMapFields() throws Exception JavaDoc
93     {
94         PersistenceManager pm = pmf.getPersistenceManager();
95         Transaction tx = pm.currentTransaction();
96
97         try
98         {
99             /*
100              * Create NUM_TESTER_OBJECTS MapFieldTester objects, each referencing
101              * NUM_MAP_VALUES * 2 Person objects from its map fields.
102              */

103             MapFieldTester mft;
104             Object JavaDoc[] ids = new Object JavaDoc[NUM_TESTER_OBJECTS];
105
106             /*
107              * These two arrays will record transient copies of the added
108              * Person objects.
109              */

110             ArrayList JavaDoc[] husbands = new ArrayList JavaDoc[ids.length];
111             ArrayList JavaDoc[] wives = new ArrayList JavaDoc[ids.length];
112
113             /*
114              * These two will record the same object values, but will be made
115              * persistent when the MapFieldTester object is make persistent.
116              */

117             ArrayList JavaDoc[] pHusbands = new ArrayList JavaDoc[ids.length];
118             ArrayList JavaDoc[] pWives = new ArrayList JavaDoc[ids.length];
119
120             long pNum = 0;
121
122             tx.begin();
123
124             for (int i = 0; i < ids.length; ++i)
125             {
126                 husbands[i] = new ArrayList JavaDoc();
127                 wives[i] = new ArrayList JavaDoc();
128                 pHusbands[i] = new ArrayList JavaDoc();
129                 pWives[i] = new ArrayList JavaDoc();
130
131                 mft = new MapFieldTester();
132
133                 for (int j = 0; j < NUM_MAP_VALUES; ++j)
134                 {
135                     Person h = new Person(pNum, "Fred #" + pNum, "Flintstone", "fred" + pNum + "@bedrock.com");
136                     ++pNum;
137                     Person w = new Person(pNum, "Wilma #" + pNum, "Flintstone", "wilma" + pNum + "@bedrock.com");
138                     ++pNum;
139
140                     husbands[i].add(h.clone());
141                     wives[i].add(w.clone());
142                     pHusbands[i].add(h);
143                     pWives[i].add(w);
144
145                     mft.addPair(h, w);
146                 }
147
148                 pm.makePersistent(mft);
149                 ids[i] = pm.getObjectId(mft);
150             }
151
152             tx.commit();
153
154             /*
155              * Read back and verify the MapFieldTester objects.
156              */

157             tx.begin();
158
159             for (int i = 0; i < ids.length; ++i)
160             {
161                 mft = (MapFieldTester)pm.getObjectById(ids[i], false);
162                 mft.assertMapsEqual(husbands[i], wives[i]);
163             }
164
165             tx.commit();
166
167             /*
168              * Modify the tester objects to add more objects to their maps.
169              */

170             tx.begin();
171
172             for (int i = 0; i < ids.length; ++i)
173             {
174                 mft = (MapFieldTester)pm.getObjectById(ids[i], false);
175
176                 for (int j = 0; j < NUM_MAP_VALUES; ++j)
177                 {
178                     Person h = new Person(pNum, "Barney #" + pNum, "Rubble", "barney" + pNum + "@bedrock.com");
179                     ++pNum;
180                     Person w = new Person(pNum, "Betty #" + pNum, "Rubble", "betty" + pNum + "@bedrock.com");
181                     ++pNum;
182
183                     husbands[i].add(h.clone());
184                     wives[i].add(w.clone());
185                     pHusbands[i].add(h);
186                     pWives[i].add(w);
187
188                     mft.addPair(h, w);
189                 }
190             }
191
192             tx.commit();
193
194             /*
195              * Read back and verify the MapFieldTester objects.
196              */

197             tx.begin();
198
199             for (int i = 0; i < ids.length; ++i)
200             {
201                 mft = (MapFieldTester)pm.getObjectById(ids[i], false);
202                 mft.assertMapsEqual(husbands[i], wives[i]);
203             }
204
205             tx.commit();
206
207             /*
208              * Modify the tester objects to remove random objects from their
209              * maps.
210              */

211             Random JavaDoc r = new Random JavaDoc(0);
212
213             tx.begin();
214
215             for (int i = 0; i < ids.length; ++i)
216             {
217                 mft = (MapFieldTester)pm.getObjectById(ids[i], false);
218
219                 for (int j = 0; j < NUM_MAP_VALUES; ++j)
220                 {
221                     int k = r.nextInt(husbands[i].size());
222
223                     Person h = (Person)pHusbands[i].get(k);
224                     Person w = (Person)pWives[i].get(k);
225
226                     mft.removePair(h, w);
227
228                     husbands[i].remove(k);
229                     wives[i].remove(k);
230                     pHusbands[i].remove(k);
231                     pWives[i].remove(k);
232                 }
233             }
234
235             tx.commit();
236
237             /*
238              * Read back and verify the MapFieldTester objects.
239              */

240             tx.begin();
241
242             for (int i = 0; i < ids.length; ++i)
243             {
244                 mft = (MapFieldTester)pm.getObjectById(ids[i], false);
245                 mft.assertMapsEqual(husbands[i], wives[i]);
246             }
247
248             tx.commit();
249
250             /*
251              * Delete the MapFieldTester objects.
252              */

253             tx.begin();
254
255             for (int i = 0; i < ids.length; ++i)
256             {
257                 mft = (MapFieldTester)pm.getObjectById(ids[i], false);
258                 pm.deletePersistent(mft);
259             }
260
261             tx.commit();
262         }
263         finally
264         {
265             if (tx.isActive())
266                 tx.rollback();
267
268             pm.close();
269         }
270     }
271
272
273     public void testInverseMapFields() throws Exception JavaDoc
274     {
275         PersistenceManager pm = pmf.getPersistenceManager();
276         Transaction tx = pm.currentTransaction();
277
278         try
279         {
280             /*
281              * Create NUM_TESTER_OBJECTS InverseMapFieldTester objects, each
282              * referencing NUM_MAP_VALUES * 2 InverseMapValue objects from its
283              * map fields.
284              */

285             InverseMapFieldTester mft;
286             Object JavaDoc[] ids = new Object JavaDoc[NUM_TESTER_OBJECTS];
287
288             /*
289              * This array will record transient copies of the added
290              * InverseMapValue objects.
291              */

292             ArrayList JavaDoc[] values = new ArrayList JavaDoc[ids.length];
293
294             /*
295              * This one will record the same object values, but will be made
296              * persistent when the InverseMapFieldTester object is made
297              * persistent.
298              */

299             ArrayList JavaDoc[] pValues = new ArrayList JavaDoc[ids.length];
300
301             tx.begin();
302
303             for (int i = 0; i < ids.length; ++i)
304             {
305                 values[i] = new ArrayList JavaDoc();
306                 pValues[i] = new ArrayList JavaDoc();
307
308                 mft = new InverseMapFieldTester();
309
310                 for (int j = 0; j < NUM_MAP_VALUES; ++j)
311                 {
312                     InverseMapValue imv = new InverseMapValue("Key " + j);
313                     imv.fillRandom();
314
315                     values[i].add(imv.clone());
316                     pValues[i].add(imv);
317
318                     mft.addMapValue(imv);
319                 }
320
321                 pm.makePersistent(mft);
322                 ids[i] = pm.getObjectId(mft);
323             }
324
325             tx.commit();
326
327             /*
328              * Read back and verify the InverseMapFieldTester objects.
329              */

330             tx.begin();
331
332             for (int i = 0; i < ids.length; ++i)
333             {
334                 mft = (InverseMapFieldTester)pm.getObjectById(ids[i], false);
335                 mft.assertMapsEqual(values[i]);
336             }
337
338             tx.commit();
339
340             /*
341              * Modify the tester objects to add more objects to their maps.
342              */

343             tx.begin();
344
345             for (int i = 0; i < ids.length; ++i)
346             {
347                 mft = (InverseMapFieldTester)pm.getObjectById(ids[i], false);
348
349                 for (int j = 0; j < NUM_MAP_VALUES; ++j)
350                 {
351                     InverseMapValue imv = new InverseMapValue("Added key " + j);
352                     imv.fillRandom();
353
354                     values[i].add(imv.clone());
355                     pValues[i].add(imv);
356
357                     mft.addMapValue(imv);
358                 }
359             }
360
361             tx.commit();
362
363             /*
364              * Read back and verify the InverseMapFieldTester objects.
365              */

366             tx.begin();
367
368             for (int i = 0; i < ids.length; ++i)
369             {
370                 mft = (InverseMapFieldTester)pm.getObjectById(ids[i], false);
371                 mft.assertMapsEqual(values[i]);
372             }
373
374             tx.commit();
375
376             /*
377              * Modify the tester objects to remove random objects from their
378              * maps.
379              */

380             Random JavaDoc r = new Random JavaDoc(0);
381
382             tx.begin();
383
384             for (int i = 0; i < ids.length; ++i)
385             {
386                 mft = (InverseMapFieldTester)pm.getObjectById(ids[i], false);
387
388                 for (int j = 0; j < NUM_MAP_VALUES; ++j)
389                 {
390                     int k = r.nextInt(values[i].size());
391
392                     InverseMapValue imv = (InverseMapValue)pValues[i].get(k);
393
394                     mft.removeMapValue(imv);
395
396                     values[i].remove(k);
397                     pValues[i].remove(k);
398                 }
399             }
400
401             tx.commit();
402
403             /*
404              * Read back and verify the InverseMapFieldTester objects.
405              */

406             tx.begin();
407
408             for (int i = 0; i < ids.length; ++i)
409             {
410                 mft = (InverseMapFieldTester)pm.getObjectById(ids[i], false);
411                 mft.assertMapsEqual(values[i]);
412             }
413
414             tx.commit();
415
416             /*
417              * Delete the InverseMapFieldTester objects.
418              */

419             tx.begin();
420
421             for (int i = 0; i < ids.length; ++i)
422             {
423                 mft = (InverseMapFieldTester)pm.getObjectById(ids[i], false);
424                 pm.deletePersistent(mft);
425             }
426
427             tx.commit();
428         }
429         finally
430         {
431             if (tx.isActive())
432                 tx.rollback();
433
434             pm.close();
435         }
436     }
437 }
438
Popular Tags