KickJava   Java API By Example, From Geeks To Geeks.

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


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: InverseMapFieldTester.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 java.util.Set JavaDoc;
20 import javax.jdo.InstanceCallbacks;
21 import javax.jdo.JDOHelper;
22 import javax.jdo.PersistenceManager;
23 import junit.framework.Assert;
24
25
26 /**
27  * A test object for testing inverse <code>java.util.Map</code> fields.
28  *
29  * @author <a HREF="mailto:mmartin5@austin.rr.com">Mike Martin</a>
30  * @version $Revision: 1.3 $
31  */

32
33 public class InverseMapFieldTester extends TestObject implements InstanceCallbacks
34 {
35     private Set JavaDoc mapValues = new HashSet JavaDoc();
36     private Map JavaDoc inverseMap = new HashMap JavaDoc();
37
38
39     /**
40      * Constructs an empty map field tester object.
41      */

42
43     public InverseMapFieldTester()
44     {
45     }
46
47
48     public Object JavaDoc clone()
49     {
50         InverseMapFieldTester mft = (InverseMapFieldTester)super.clone();
51
52         HashSet JavaDoc mv = new HashSet JavaDoc();
53         HashMap JavaDoc im = new HashMap JavaDoc();
54
55         Iterator JavaDoc i = mapValues.iterator();
56
57         while (i.hasNext())
58         {
59             InverseMapValue imv = (InverseMapValue)((InverseMapValue)i.next()).clone();
60
61             imv.setOwner(mft);
62             mv.add(imv);
63             im.put(imv.getKey(), imv);
64         }
65
66         mft.mapValues = mv;
67         mft.inverseMap = im;
68
69         return mft;
70     }
71
72
73     public void fillRandom()
74     {
75         throw new UnsupportedOperationException JavaDoc();
76     }
77
78
79     public boolean compareTo(Object JavaDoc obj)
80     {
81         throw new UnsupportedOperationException JavaDoc();
82     }
83
84
85     public void jdoPreStore() {}
86     public void jdoPostLoad() {}
87     public void jdoPreClear() {}
88
89     public void jdoPreDelete()
90     {
91         PersistenceManager pm = JDOHelper.getPersistenceManager(this);
92
93         /* Delete all dependent objects. */
94         pm.deletePersistentAll(mapValues);
95     }
96
97
98     public void addMapValue(InverseMapValue imv)
99     {
100         String JavaDoc key = imv.getKey();
101
102         Assert.assertTrue("inverseMap already contains key: " + key, !inverseMap.containsKey(key));
103         Assert.assertTrue("inverseMap already contains value: " + imv, !inverseMap.containsValue(imv));
104
105         Assert.assertTrue("add() returned false adding value: " + imv, mapValues.add(imv));
106         Object JavaDoc oldimv = inverseMap.put(key, imv);
107
108         if (JDOHelper.isPersistent(this))
109             Assert.assertSame("put() should have returned object just persisted with key: " + key, imv, oldimv);
110         else
111             Assert.assertNull("put() returned non-null adding key: " + key, oldimv);
112
113         Assert.assertTrue("inverseMap does not contain key: " + key, inverseMap.containsKey(key));
114         Assert.assertTrue("inverseMap does not contain value: " + imv, inverseMap.containsValue(imv));
115     }
116
117
118     public void removeMapValue(InverseMapValue imv)
119     {
120         Assert.assertSame(this, imv.getOwner());
121
122         String JavaDoc key = imv.getKey();
123
124         if (JDOHelper.isPersistent(imv))
125             JDOHelper.getPersistenceManager(imv).deletePersistent(imv);
126         else
127         {
128             inverseMap.remove(key);
129             mapValues.remove(imv);
130         }
131
132         Assert.assertTrue("inverseMap still contains key: " + key, !inverseMap.containsKey(key));
133         Assert.assertTrue("inverseMap still contains value", !inverseMap.containsValue(imv));
134     }
135
136
137     public void assertMapsEqual(List JavaDoc values)
138     {
139         Assert.assertEquals(values.size(), mapValues.size());
140         Assert.assertEquals(mapValues.size(), inverseMap.size());
141
142         InverseMapValue[] pValues = new InverseMapValue[values.size()];
143         HashMap JavaDoc pValueMap = new HashMap JavaDoc();
144
145         for (int i = 0; i < values.size(); ++i)
146         {
147             InverseMapValue expected = (InverseMapValue)values.get(i);
148             String JavaDoc key = expected.getKey();
149             Assert.assertTrue("inverseMap does not contain key: " + key, inverseMap.containsKey(key));
150
151             pValues[i] = (InverseMapValue)inverseMap.get(expected.getKey());
152             expected.assertEquals(pValues[i]);
153
154             Assert.assertTrue("inverseMap does not contain value: " + pValues[i], inverseMap.containsValue(pValues[i]));
155
156             pValueMap.put(pValues[i].getKey(), pValues[i]);
157         }
158
159         /*
160          * Use containsAll() to assert that the collections returned by keySet()
161          * and values() contain the expected elements.
162          */

163         Assert.assertTrue(inverseMap.keySet().containsAll(pValueMap.keySet()));
164         Assert.assertTrue(inverseMap.values().containsAll(Arrays.asList(pValues)));
165
166         /*
167          * Iterate over the entire entry set of inverseMap and assert that
168          * every expected entry is returned.
169          */

170         Iterator JavaDoc i = inverseMap.entrySet().iterator();
171
172         while (i.hasNext())
173         {
174             Map.Entry JavaDoc entry = (Map.Entry JavaDoc)i.next();
175
176             InverseMapValue imv = (InverseMapValue)pValueMap.get(entry.getKey());
177
178             Assert.assertEquals(entry.getKey(), imv.getKey());
179             Assert.assertEquals(entry.getValue(), imv);
180
181             pValueMap.remove(entry.getKey());
182         }
183
184         Assert.assertEquals(0, pValueMap.size());
185     }
186 }
187
Popular Tags