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: MapFieldTester.java,v 1.3 2002/11/08 05:06:27 jackknifebarber Exp $
009: */
010:
011: package com.triactive.jdo.test;
012:
013: import java.util.Arrays;
014: import java.util.HashMap;
015: import java.util.HashSet;
016: import java.util.Iterator;
017: import java.util.List;
018: import java.util.Map;
019: import javax.jdo.InstanceCallbacks;
020: import javax.jdo.JDOHelper;
021: import javax.jdo.PersistenceManager;
022: import junit.framework.Assert;
023:
024: /**
025: * A test object for testing <code>java.util.Map</code> fields.
026: *
027: * @author <a href="mailto:mmartin5@austin.rr.com">Mike Martin</a>
028: * @version $Revision: 1.3 $
029: */
030:
031: public class MapFieldTester implements InstanceCallbacks {
032: private Map personsByNumber = new HashMap();
033: private Map husbandsByWife = new HashMap();
034: private Map wivesByHusband = new HashMap();
035:
036: /**
037: * Constructs an empty map field tester object.
038: */
039:
040: public MapFieldTester() {
041: }
042:
043: /**
044: * Adds the two <code>Person</code> objects to the test <code>Map</code>
045: * fields. A test failure is reported if either of the objects are already
046: * present in the maps.
047: *
048: * @param husband A test <code>Person</code> object. Can contain any
049: * arbitrary field values.
050: * @param wife A test <code>Person</code> object. Can contain any
051: * arbitrary field values.
052: * @param a Used to report test assertion failures.
053: */
054:
055: public void addPair(Person husband, Person wife) {
056: Long hNum = new Long(husband.getPersonNum());
057: Long wNum = new Long(wife.getPersonNum());
058:
059: Assert.assertEquals(null, personsByNumber.put(hNum, husband));
060: Assert.assertEquals(null, personsByNumber.put(wNum, wife));
061: Assert.assertTrue(personsByNumber.containsKey(hNum));
062: Assert.assertTrue(personsByNumber.containsKey(wNum));
063: Assert.assertTrue(personsByNumber.containsValue(husband));
064: Assert.assertTrue(personsByNumber.containsValue(wife));
065:
066: Assert.assertEquals(null, husbandsByWife.put(wife, husband));
067: Assert.assertEquals(null, wivesByHusband.put(husband, wife));
068: Assert.assertTrue(husbandsByWife.containsKey(wife));
069: Assert.assertTrue(husbandsByWife.containsValue(husband));
070: Assert.assertTrue(wivesByHusband.containsKey(husband));
071: Assert.assertTrue(wivesByHusband.containsValue(wife));
072: }
073:
074: /**
075: * Adds the two <code>Person</code> objects to the test <code>Map</code>
076: * fields. A test failure is reported if either of the objects are already
077: * present in the maps.
078: *
079: * @param husband A test <code>Person</code> object. Can contain any
080: * arbitrary field values.
081: * @param wife A test <code>Person</code> object. Can contain any
082: * arbitrary field values.
083: * @param a Used to report test assertion failures.
084: */
085:
086: public void removePair(Person husband, Person wife) {
087: Long hNum = new Long(husband.getPersonNum());
088: Long wNum = new Long(wife.getPersonNum());
089:
090: Assert.assertEquals(husband, personsByNumber.remove(hNum));
091: Assert.assertEquals(wife, personsByNumber.remove(wNum));
092: Assert.assertTrue(!personsByNumber.containsKey(hNum));
093: Assert.assertTrue(!personsByNumber.containsKey(wNum));
094: Assert.assertTrue(!personsByNumber.containsValue(husband));
095: Assert.assertTrue(!personsByNumber.containsValue(wife));
096:
097: Assert.assertEquals(husband, husbandsByWife.remove(wife));
098: Assert.assertEquals(wife, wivesByHusband.remove(husband));
099: Assert.assertTrue(!husbandsByWife.containsKey(wife));
100: Assert.assertTrue(!husbandsByWife.containsValue(husband));
101: Assert.assertTrue(!wivesByHusband.containsKey(husband));
102: Assert.assertTrue(!wivesByHusband.containsValue(wife));
103:
104: if (JDOHelper.isPersistent(husband))
105: JDOHelper.getPersistenceManager(husband).deletePersistent(
106: husband);
107:
108: if (JDOHelper.isPersistent(wife))
109: JDOHelper.getPersistenceManager(wife)
110: .deletePersistent(wife);
111: }
112:
113: /**
114: * Compares the values in the given arrays of test <code>Person</code>
115: * objects to the objects in the map fields. The map fields are validated
116: * using a variety of techniques that exercise most of the <code>Map</code>
117: * interface to assert that the map contents are correct.
118: *
119: * @param husbands A set of husband objects that are expected to equal the
120: * husband objects in the map fields.
121: * @param wives A set of wife objects that are expected to equal the
122: * wife objects in the map fields. Must be the same length
123: * as the husband array.
124: * @param a Used to report test assertion failures.
125: */
126:
127: public void assertMapsEqual(List husbands, List wives) {
128: Assert.assertEquals(husbands.size(), wives.size());
129: Assert.assertEquals(husbands.size() + wives.size(),
130: personsByNumber.size());
131: Assert.assertEquals(husbands.size(), husbandsByWife.size());
132: Assert.assertEquals(wives.size(), wivesByHusband.size());
133:
134: Person[] pHusbands = new Person[husbands.size()];
135: Person[] pWives = new Person[wives.size()];
136: HashMap personsByNumber1 = new HashMap();
137:
138: for (int i = 0; i < husbands.size(); ++i) {
139: Person expected;
140:
141: expected = (Person) husbands.get(i);
142: pHusbands[i] = (Person) personsByNumber.get(new Long(
143: expected.getPersonNum()));
144: expected.assertEquals(pHusbands[i]);
145: Assert.assertTrue(husbandsByWife
146: .containsValue(pHusbands[i]));
147: Assert.assertTrue(wivesByHusband.containsKey(pHusbands[i]));
148:
149: expected = (Person) wives.get(i);
150: pWives[i] = (Person) personsByNumber.get(new Long(expected
151: .getPersonNum()));
152: expected.assertEquals(pWives[i]);
153: Assert.assertTrue(husbandsByWife.containsKey(pWives[i]));
154: Assert.assertTrue(wivesByHusband.containsValue(pWives[i]));
155:
156: Assert
157: .assertEquals(JDOHelper.getObjectId(pHusbands[i]),
158: JDOHelper.getObjectId(husbandsByWife
159: .get(pWives[i])));
160:
161: Assert.assertEquals(JDOHelper.getObjectId(pWives[i]),
162: JDOHelper.getObjectId(wivesByHusband
163: .get(pHusbands[i])));
164:
165: personsByNumber1.put(new Long(pHusbands[i].getPersonNum()),
166: pHusbands[i]);
167: personsByNumber1.put(new Long(pWives[i].getPersonNum()),
168: pWives[i]);
169: }
170:
171: /*
172: * Use containsAll() to assert that the collections returned by keySet()
173: * and values() contain the expected elements.
174: */
175: Assert.assertTrue(husbandsByWife.keySet().containsAll(
176: Arrays.asList(pWives)));
177: Assert.assertTrue(husbandsByWife.values().containsAll(
178: Arrays.asList(pHusbands)));
179: Assert.assertTrue(wivesByHusband.keySet().containsAll(
180: Arrays.asList(pHusbands)));
181: Assert.assertTrue(wivesByHusband.values().containsAll(
182: Arrays.asList(pWives)));
183:
184: Assert.assertEquals(personsByNumber, personsByNumber1);
185:
186: /*
187: * Iterate over the entire entry set of personsByNumber and assert that
188: * every expected entry is returned.
189: */
190: Iterator i = personsByNumber.entrySet().iterator();
191:
192: while (i.hasNext()) {
193: Map.Entry entry = (Map.Entry) i.next();
194:
195: Person p = (Person) personsByNumber1.get(entry.getKey());
196:
197: Assert.assertEquals(entry.getKey(), new Long(p
198: .getPersonNum()));
199: Assert.assertEquals(entry.getValue(), p);
200:
201: personsByNumber1.remove(entry.getKey());
202: }
203:
204: Assert.assertEquals(0, personsByNumber1.size());
205: }
206:
207: public int hashCode() {
208: Object id = JDOHelper.getObjectId(this );
209:
210: return id == null ? super .hashCode() : id.hashCode();
211: }
212:
213: public boolean equals(Object o) {
214: if (o == this )
215: return true;
216:
217: Object id = JDOHelper.getObjectId(this );
218:
219: return id == null ? super .equals(o) : id.equals(JDOHelper
220: .getObjectId(o));
221: }
222:
223: public void jdoPreStore() {
224: }
225:
226: public void jdoPostLoad() {
227: }
228:
229: public void jdoPreClear() {
230: }
231:
232: public void jdoPreDelete() {
233: PersistenceManager pm = JDOHelper.getPersistenceManager(this );
234:
235: /* Delete all dependent Person objects. */
236: HashSet persons = new HashSet(personsByNumber.values());
237:
238: personsByNumber.clear();
239: husbandsByWife.clear();
240: wivesByHusband.clear();
241:
242: pm.deletePersistentAll(persons);
243: }
244: }
|