KickJava   Java API By Example, From Geeks To Geeks.

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


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: MapFieldTester.java,v 1.3 2002/11/08 05:06:27 jackknifebarber Exp $
9  */

10
11 package com.triactive.jdo.test;
12
13 import java.util.Arrays JavaDoc;
14 import java.util.HashMap JavaDoc;
15 import java.util.HashSet JavaDoc;
16 import java.util.Iterator JavaDoc;
17 import java.util.List JavaDoc;
18 import java.util.Map JavaDoc;
19 import javax.jdo.InstanceCallbacks;
20 import javax.jdo.JDOHelper;
21 import javax.jdo.PersistenceManager;
22 import junit.framework.Assert;
23
24
25 /**
26  * A test object for testing <code>java.util.Map</code> fields.
27  *
28  * @author <a HREF="mailto:mmartin5@austin.rr.com">Mike Martin</a>
29  * @version $Revision: 1.3 $
30  */

31
32 public class MapFieldTester implements InstanceCallbacks
33 {
34     private Map JavaDoc personsByNumber = new HashMap JavaDoc();
35     private Map JavaDoc husbandsByWife = new HashMap JavaDoc();
36     private Map JavaDoc wivesByHusband = new HashMap JavaDoc();
37
38
39     /**
40      * Constructs an empty map field tester object.
41      */

42
43     public MapFieldTester()
44     {
45     }
46
47
48     /**
49      * Adds the two <code>Person</code> objects to the test <code>Map</code>
50      * fields. A test failure is reported if either of the objects are already
51      * present in the maps.
52      *
53      * @param husband A test <code>Person</code> object. Can contain any
54      * arbitrary field values.
55      * @param wife A test <code>Person</code> object. Can contain any
56      * arbitrary field values.
57      * @param a Used to report test assertion failures.
58      */

59
60     public void addPair(Person husband, Person wife)
61     {
62         Long JavaDoc hNum = new Long JavaDoc(husband.getPersonNum());
63         Long JavaDoc wNum = new Long JavaDoc(wife.getPersonNum());
64
65         Assert.assertEquals(null, personsByNumber.put(hNum, husband));
66         Assert.assertEquals(null, personsByNumber.put(wNum, wife));
67         Assert.assertTrue(personsByNumber.containsKey(hNum));
68         Assert.assertTrue(personsByNumber.containsKey(wNum));
69         Assert.assertTrue(personsByNumber.containsValue(husband));
70         Assert.assertTrue(personsByNumber.containsValue(wife));
71
72         Assert.assertEquals(null, husbandsByWife.put(wife, husband));
73         Assert.assertEquals(null, wivesByHusband.put(husband, wife));
74         Assert.assertTrue(husbandsByWife.containsKey(wife));
75         Assert.assertTrue(husbandsByWife.containsValue(husband));
76         Assert.assertTrue(wivesByHusband.containsKey(husband));
77         Assert.assertTrue(wivesByHusband.containsValue(wife));
78     }
79
80
81     /**
82      * Adds the two <code>Person</code> objects to the test <code>Map</code>
83      * fields. A test failure is reported if either of the objects are already
84      * present in the maps.
85      *
86      * @param husband A test <code>Person</code> object. Can contain any
87      * arbitrary field values.
88      * @param wife A test <code>Person</code> object. Can contain any
89      * arbitrary field values.
90      * @param a Used to report test assertion failures.
91      */

92
93     public void removePair(Person husband, Person wife)
94     {
95         Long JavaDoc hNum = new Long JavaDoc(husband.getPersonNum());
96         Long JavaDoc wNum = new Long JavaDoc(wife.getPersonNum());
97
98         Assert.assertEquals(husband, personsByNumber.remove(hNum));
99         Assert.assertEquals(wife, personsByNumber.remove(wNum));
100         Assert.assertTrue(!personsByNumber.containsKey(hNum));
101         Assert.assertTrue(!personsByNumber.containsKey(wNum));
102         Assert.assertTrue(!personsByNumber.containsValue(husband));
103         Assert.assertTrue(!personsByNumber.containsValue(wife));
104
105         Assert.assertEquals(husband, husbandsByWife.remove(wife));
106         Assert.assertEquals(wife, wivesByHusband.remove(husband));
107         Assert.assertTrue(!husbandsByWife.containsKey(wife));
108         Assert.assertTrue(!husbandsByWife.containsValue(husband));
109         Assert.assertTrue(!wivesByHusband.containsKey(husband));
110         Assert.assertTrue(!wivesByHusband.containsValue(wife));
111
112         if (JDOHelper.isPersistent(husband))
113             JDOHelper.getPersistenceManager(husband).deletePersistent(husband);
114
115         if (JDOHelper.isPersistent(wife))
116             JDOHelper.getPersistenceManager(wife).deletePersistent(wife);
117     }
118
119
120     /**
121      * Compares the values in the given arrays of test <code>Person</code>
122      * objects to the objects in the map fields. The map fields are validated
123      * using a variety of techniques that exercise most of the <code>Map</code>
124      * interface to assert that the map contents are correct.
125      *
126      * @param husbands A set of husband objects that are expected to equal the
127      * husband objects in the map fields.
128      * @param wives A set of wife objects that are expected to equal the
129      * wife objects in the map fields. Must be the same length
130      * as the husband array.
131      * @param a Used to report test assertion failures.
132      */

133
134     public void assertMapsEqual(List JavaDoc husbands, List JavaDoc wives)
135     {
136         Assert.assertEquals(husbands.size(), wives.size());
137         Assert.assertEquals(husbands.size() + wives.size(), personsByNumber.size());
138         Assert.assertEquals(husbands.size(), husbandsByWife.size());
139         Assert.assertEquals(wives.size(), wivesByHusband.size());
140
141         Person[] pHusbands = new Person[husbands.size()];
142         Person[] pWives = new Person[wives.size()];
143         HashMap JavaDoc personsByNumber1 = new HashMap JavaDoc();
144
145         for (int i = 0; i < husbands.size(); ++i)
146         {
147             Person expected;
148
149             expected = (Person)husbands.get(i);
150             pHusbands[i] = (Person)personsByNumber.get(new Long JavaDoc(expected.getPersonNum()));
151             expected.assertEquals(pHusbands[i]);
152             Assert.assertTrue(husbandsByWife.containsValue(pHusbands[i]));
153             Assert.assertTrue(wivesByHusband.containsKey(pHusbands[i]));
154
155             expected = (Person)wives.get(i);
156             pWives[i] = (Person)personsByNumber.get(new Long JavaDoc(expected.getPersonNum()));
157             expected.assertEquals(pWives[i]);
158             Assert.assertTrue(husbandsByWife.containsKey(pWives[i]));
159             Assert.assertTrue(wivesByHusband.containsValue(pWives[i]));
160
161             Assert.assertEquals(JDOHelper.getObjectId(pHusbands[i]),
162                                 JDOHelper.getObjectId(husbandsByWife.get(pWives[i])));
163
164             Assert.assertEquals(JDOHelper.getObjectId(pWives[i]),
165                                 JDOHelper.getObjectId(wivesByHusband.get(pHusbands[i])));
166
167             personsByNumber1.put(new Long JavaDoc(pHusbands[i].getPersonNum()), pHusbands[i]);
168             personsByNumber1.put(new Long JavaDoc(pWives[i].getPersonNum()), 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(Arrays.asList(pWives)));
176         Assert.assertTrue(husbandsByWife.values().containsAll(Arrays.asList(pHusbands)));
177         Assert.assertTrue(wivesByHusband.keySet().containsAll(Arrays.asList(pHusbands)));
178         Assert.assertTrue(wivesByHusband.values().containsAll(Arrays.asList(pWives)));
179
180         Assert.assertEquals(personsByNumber, personsByNumber1);
181
182         /*
183          * Iterate over the entire entry set of personsByNumber and assert that
184          * every expected entry is returned.
185          */

186         Iterator JavaDoc i = personsByNumber.entrySet().iterator();
187
188         while (i.hasNext())
189         {
190             Map.Entry JavaDoc entry = (Map.Entry JavaDoc)i.next();
191
192             Person p = (Person)personsByNumber1.get(entry.getKey());
193
194             Assert.assertEquals(entry.getKey(), new Long JavaDoc(p.getPersonNum()));
195             Assert.assertEquals(entry.getValue(), p);
196
197             personsByNumber1.remove(entry.getKey());
198         }
199
200         Assert.assertEquals(0, personsByNumber1.size());
201     }
202
203     public int hashCode()
204     {
205         Object JavaDoc id = JDOHelper.getObjectId(this);
206
207         return id == null ? super.hashCode() : id.hashCode();
208     }
209
210     public boolean equals(Object JavaDoc o)
211     {
212         if (o == this)
213             return true;
214
215         Object JavaDoc id = JDOHelper.getObjectId(this);
216
217         return id == null ? super.equals(o) : id.equals(JDOHelper.getObjectId(o));
218     }
219
220     public void jdoPreStore() {}
221     public void jdoPostLoad() {}
222     public void jdoPreClear() {}
223
224     public void jdoPreDelete()
225     {
226         PersistenceManager pm = JDOHelper.getPersistenceManager(this);
227
228         /* Delete all dependent Person objects. */
229         HashSet JavaDoc persons = new HashSet JavaDoc(personsByNumber.values());
230
231         personsByNumber.clear();
232         husbandsByWife.clear();
233         wivesByHusband.clear();
234
235         pm.deletePersistentAll(persons);
236     }
237 }
238
Popular Tags