001: /**
002: * Copyright 2002 (C) TJDO.
003: * All rights reserved.
004: *
005: * This software is distributed under the terms of the TJDO License version 1.0.
006: * See the terms of the TJDO License in the documentation provided with this software.
007: *
008: * $Id: MapFieldTest.java,v 1.4 2002/11/24 06:02:48 jackknifebarber Exp $
009: */package com.triactive.jdo.test;
010:
011: import java.util.ArrayList;
012: import java.util.Iterator;
013: import java.util.Map;
014: import java.util.Random;
015: import javax.jdo.PersistenceManager;
016: import javax.jdo.Transaction;
017:
018: /**
019: * Tests persistent fields of type <code>java.util.Map</code>.
020: *
021: * @author <a href="mailto:mmartin5@austin.rr.com">Mike Martin</a>
022: * @version $Revision: 1.4 $
023: */
024:
025: public class MapFieldTest extends PersistenceTestCase {
026: private static final int NUM_TESTER_OBJECTS = 5;
027: private static final int NUM_MAP_VALUES = 5;
028:
029: protected boolean schemaInitialized = false;
030:
031: /**
032: * Used by the JUnit framework to construct tests.
033: *
034: * @param name Name of the test case.
035: */
036:
037: public MapFieldTest(String name) {
038: super (name);
039: }
040:
041: protected void setUp() throws Exception {
042: super .setUp();
043:
044: PersistenceManager pm = pmf.getPersistenceManager();
045: if (!schemaInitialized) {
046: addClassesToSchema(new Class[] { Person.class,
047: MapFieldTester.class, InverseMapFieldTester.class });
048:
049: schemaInitialized = true;
050: }
051:
052: Transaction tx = pm.currentTransaction();
053: try {
054: /* Delete all MapFieldTester and InverseMapFieldTester objects. */
055: tx.begin();
056:
057: Iterator i = pm.getExtent(MapFieldTester.class, true)
058: .iterator();
059:
060: while (i.hasNext())
061: pm.deletePersistent(i.next());
062:
063: i = pm.getExtent(InverseMapFieldTester.class, true)
064: .iterator();
065:
066: while (i.hasNext())
067: pm.deletePersistent(i.next());
068:
069: tx.commit();
070: } finally {
071: if (tx.isActive())
072: tx.rollback();
073:
074: pm.close();
075: }
076: }
077:
078: public void testNormalMapFields() throws Exception {
079: PersistenceManager pm = pmf.getPersistenceManager();
080: Transaction tx = pm.currentTransaction();
081:
082: try {
083: /*
084: * Create NUM_TESTER_OBJECTS MapFieldTester objects, each referencing
085: * NUM_MAP_VALUES * 2 Person objects from its map fields.
086: */
087: MapFieldTester mft;
088: Object[] ids = new Object[NUM_TESTER_OBJECTS];
089:
090: /*
091: * These two arrays will record transient copies of the added
092: * Person objects.
093: */
094: ArrayList[] husbands = new ArrayList[ids.length];
095: ArrayList[] wives = new ArrayList[ids.length];
096:
097: /*
098: * These two will record the same object values, but will be made
099: * persistent when the MapFieldTester object is make persistent.
100: */
101: ArrayList[] pHusbands = new ArrayList[ids.length];
102: ArrayList[] pWives = new ArrayList[ids.length];
103:
104: long pNum = 0;
105:
106: tx.begin();
107:
108: for (int i = 0; i < ids.length; ++i) {
109: husbands[i] = new ArrayList();
110: wives[i] = new ArrayList();
111: pHusbands[i] = new ArrayList();
112: pWives[i] = new ArrayList();
113:
114: mft = new MapFieldTester();
115:
116: for (int j = 0; j < NUM_MAP_VALUES; ++j) {
117: Person h = new Person(pNum, "Fred #" + pNum,
118: "Flintstone", "fred" + pNum
119: + "@bedrock.com");
120: ++pNum;
121: Person w = new Person(pNum, "Wilma #" + pNum,
122: "Flintstone", "wilma" + pNum
123: + "@bedrock.com");
124: ++pNum;
125:
126: husbands[i].add(h.clone());
127: wives[i].add(w.clone());
128: pHusbands[i].add(h);
129: pWives[i].add(w);
130:
131: mft.addPair(h, w);
132: }
133:
134: pm.makePersistent(mft);
135: ids[i] = pm.getObjectId(mft);
136: }
137:
138: tx.commit();
139:
140: /*
141: * Read back and verify the MapFieldTester objects.
142: */
143: tx.begin();
144:
145: for (int i = 0; i < ids.length; ++i) {
146: mft = (MapFieldTester) pm.getObjectById(ids[i], false);
147: mft.assertMapsEqual(husbands[i], wives[i]);
148: }
149:
150: tx.commit();
151:
152: /*
153: * Modify the tester objects to add more objects to their maps.
154: */
155: tx.begin();
156:
157: for (int i = 0; i < ids.length; ++i) {
158: mft = (MapFieldTester) pm.getObjectById(ids[i], false);
159:
160: for (int j = 0; j < NUM_MAP_VALUES; ++j) {
161: Person h = new Person(pNum, "Barney #" + pNum,
162: "Rubble", "barney" + pNum + "@bedrock.com");
163: ++pNum;
164: Person w = new Person(pNum, "Betty #" + pNum,
165: "Rubble", "betty" + pNum + "@bedrock.com");
166: ++pNum;
167:
168: husbands[i].add(h.clone());
169: wives[i].add(w.clone());
170: pHusbands[i].add(h);
171: pWives[i].add(w);
172:
173: mft.addPair(h, w);
174: }
175: }
176:
177: tx.commit();
178:
179: /*
180: * Read back and verify the MapFieldTester objects.
181: */
182: tx.begin();
183:
184: for (int i = 0; i < ids.length; ++i) {
185: mft = (MapFieldTester) pm.getObjectById(ids[i], false);
186: mft.assertMapsEqual(husbands[i], wives[i]);
187: }
188:
189: tx.commit();
190:
191: /*
192: * Modify the tester objects to remove random objects from their
193: * maps.
194: */
195: Random r = new Random(0);
196:
197: tx.begin();
198:
199: for (int i = 0; i < ids.length; ++i) {
200: mft = (MapFieldTester) pm.getObjectById(ids[i], false);
201:
202: for (int j = 0; j < NUM_MAP_VALUES; ++j) {
203: int k = r.nextInt(husbands[i].size());
204:
205: Person h = (Person) pHusbands[i].get(k);
206: Person w = (Person) pWives[i].get(k);
207:
208: mft.removePair(h, w);
209:
210: husbands[i].remove(k);
211: wives[i].remove(k);
212: pHusbands[i].remove(k);
213: pWives[i].remove(k);
214: }
215: }
216:
217: tx.commit();
218:
219: /*
220: * Read back and verify the MapFieldTester objects.
221: */
222: tx.begin();
223:
224: for (int i = 0; i < ids.length; ++i) {
225: mft = (MapFieldTester) pm.getObjectById(ids[i], false);
226: mft.assertMapsEqual(husbands[i], wives[i]);
227: }
228:
229: tx.commit();
230:
231: /*
232: * Delete the MapFieldTester objects.
233: */
234: tx.begin();
235:
236: for (int i = 0; i < ids.length; ++i) {
237: mft = (MapFieldTester) pm.getObjectById(ids[i], false);
238: pm.deletePersistent(mft);
239: }
240:
241: tx.commit();
242: } finally {
243: if (tx.isActive())
244: tx.rollback();
245:
246: pm.close();
247: }
248: }
249:
250: public void testInverseMapFields() throws Exception {
251: PersistenceManager pm = pmf.getPersistenceManager();
252: Transaction tx = pm.currentTransaction();
253:
254: try {
255: /*
256: * Create NUM_TESTER_OBJECTS InverseMapFieldTester objects, each
257: * referencing NUM_MAP_VALUES * 2 InverseMapValue objects from its
258: * map fields.
259: */
260: InverseMapFieldTester mft;
261: Object[] ids = new Object[NUM_TESTER_OBJECTS];
262:
263: /*
264: * This array will record transient copies of the added
265: * InverseMapValue objects.
266: */
267: ArrayList[] values = new ArrayList[ids.length];
268:
269: /*
270: * This one will record the same object values, but will be made
271: * persistent when the InverseMapFieldTester object is made
272: * persistent.
273: */
274: ArrayList[] pValues = new ArrayList[ids.length];
275:
276: tx.begin();
277:
278: for (int i = 0; i < ids.length; ++i) {
279: values[i] = new ArrayList();
280: pValues[i] = new ArrayList();
281:
282: mft = new InverseMapFieldTester();
283:
284: for (int j = 0; j < NUM_MAP_VALUES; ++j) {
285: InverseMapValue imv = new InverseMapValue("Key "
286: + j);
287: imv.fillRandom();
288:
289: values[i].add(imv.clone());
290: pValues[i].add(imv);
291:
292: mft.addMapValue(imv);
293: }
294:
295: pm.makePersistent(mft);
296: ids[i] = pm.getObjectId(mft);
297: }
298:
299: tx.commit();
300:
301: /*
302: * Read back and verify the InverseMapFieldTester objects.
303: */
304: tx.begin();
305:
306: for (int i = 0; i < ids.length; ++i) {
307: mft = (InverseMapFieldTester) pm.getObjectById(ids[i],
308: false);
309: mft.assertMapsEqual(values[i]);
310: }
311:
312: tx.commit();
313:
314: /*
315: * Modify the tester objects to add more objects to their maps.
316: */
317: tx.begin();
318:
319: for (int i = 0; i < ids.length; ++i) {
320: mft = (InverseMapFieldTester) pm.getObjectById(ids[i],
321: false);
322:
323: for (int j = 0; j < NUM_MAP_VALUES; ++j) {
324: InverseMapValue imv = new InverseMapValue(
325: "Added key " + j);
326: imv.fillRandom();
327:
328: values[i].add(imv.clone());
329: pValues[i].add(imv);
330:
331: mft.addMapValue(imv);
332: }
333: }
334:
335: tx.commit();
336:
337: /*
338: * Read back and verify the InverseMapFieldTester objects.
339: */
340: tx.begin();
341:
342: for (int i = 0; i < ids.length; ++i) {
343: mft = (InverseMapFieldTester) pm.getObjectById(ids[i],
344: false);
345: mft.assertMapsEqual(values[i]);
346: }
347:
348: tx.commit();
349:
350: /*
351: * Modify the tester objects to remove random objects from their
352: * maps.
353: */
354: Random r = new Random(0);
355:
356: tx.begin();
357:
358: for (int i = 0; i < ids.length; ++i) {
359: mft = (InverseMapFieldTester) pm.getObjectById(ids[i],
360: false);
361:
362: for (int j = 0; j < NUM_MAP_VALUES; ++j) {
363: int k = r.nextInt(values[i].size());
364:
365: InverseMapValue imv = (InverseMapValue) pValues[i]
366: .get(k);
367:
368: mft.removeMapValue(imv);
369:
370: values[i].remove(k);
371: pValues[i].remove(k);
372: }
373: }
374:
375: tx.commit();
376:
377: /*
378: * Read back and verify the InverseMapFieldTester objects.
379: */
380: tx.begin();
381:
382: for (int i = 0; i < ids.length; ++i) {
383: mft = (InverseMapFieldTester) pm.getObjectById(ids[i],
384: false);
385: mft.assertMapsEqual(values[i]);
386: }
387:
388: tx.commit();
389:
390: /*
391: * Delete the InverseMapFieldTester objects.
392: */
393: tx.begin();
394:
395: for (int i = 0; i < ids.length; ++i) {
396: mft = (InverseMapFieldTester) pm.getObjectById(ids[i],
397: false);
398: pm.deletePersistent(mft);
399: }
400:
401: tx.commit();
402: } finally {
403: if (tx.isActive())
404: tx.rollback();
405:
406: pm.close();
407: }
408: }
409: }
|