KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Copyright 2004 (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: MapWidget.java,v 1.2 2004/03/22 04:58:13 jackknifebarber Exp $
9  */

10
11 package com.triactive.jdo.test;
12
13 import java.util.HashMap JavaDoc;
14 import java.util.Iterator JavaDoc;
15 import java.util.Map JavaDoc;
16 import java.util.Map.Entry;
17 import javax.jdo.InstanceCallbacks;
18 import javax.jdo.JDOHelper;
19 import javax.jdo.PersistenceManager;
20 import junit.framework.Assert;
21
22
23 public class MapWidget extends Widget implements InstanceCallbacks
24 {
25     private Map JavaDoc normalMap = new HashMap JavaDoc();
26     private Map JavaDoc normalObjectMap = new HashMap JavaDoc();
27     private Map JavaDoc inverseMap = new HashMap JavaDoc();
28     private int numWidgets = 0;
29     private int numValueWidgets = 0;
30
31
32     public MapWidget()
33     {
34     }
35
36
37     public Map JavaDoc getNormalMap()
38     {
39         return normalMap;
40     }
41
42
43     public Map JavaDoc getNormalObjectMap()
44     {
45         return normalObjectMap;
46     }
47
48
49     public Map JavaDoc getInverseMap()
50     {
51         return inverseMap;
52     }
53
54
55     public int getNumWidgets()
56     {
57         return numWidgets;
58     }
59
60
61     public int getNumValueWidgets()
62     {
63         return numValueWidgets;
64     }
65
66
67     public Object JavaDoc[] getSCOFieldValues()
68     {
69         return new Object JavaDoc[] { normalMap, normalObjectMap, inverseMap };
70     }
71
72
73     public Object JavaDoc clone()
74     {
75         MapWidget mw = (MapWidget)super.clone();
76
77         /* Do a deep clone of all the maps. */
78         mw.normalMap = new HashMap JavaDoc();
79         mw.normalObjectMap = new HashMap JavaDoc();
80         mw.inverseMap = new HashMap JavaDoc();
81
82         Iterator JavaDoc i = normalMap.entrySet().iterator();
83
84         while (i.hasNext())
85         {
86             Entry e = (Entry)i.next();
87             String JavaDoc ks = (String JavaDoc)e.getKey();
88             Widget v = (Widget)clone(e.getValue());
89
90             Integer JavaDoc ki = new Integer JavaDoc(v.getIntField());
91
92             mw.normalMap.put(ks, v);
93             mw.normalObjectMap.put(ki, v);
94
95             if (v instanceof ValueWidget)
96             {
97                 ValueWidget vw = (ValueWidget)v;
98
99                 vw.setOwner(mw);
100                 mw.inverseMap.put(ki, vw);
101             }
102         }
103
104         return mw;
105     }
106
107
108     /**
109      * Fills all of the object's fields with random data values.
110      */

111
112     public void fillRandom()
113     {
114         fillRandom(r.nextInt(5));
115     }
116
117
118     public void fillRandom(int numWidgets)
119     {
120         fillRandom(numWidgets, false);
121     }
122
123
124     /**
125      * Fills the collection fields with the given number of random Widget
126      * objects.
127      */

128
129     public void fillRandom(int numWidgets, boolean includeMapWidgets)
130     {
131         super.fillRandom();
132
133         /*
134          * Clear normalMap iteratively in order to test remove().
135          */

136         Iterator JavaDoc i = normalMap.entrySet().iterator();
137
138         while (i.hasNext())
139         {
140             Entry e = (Entry)i.next();
141             Object JavaDoc skey = e.getKey();
142             Widget val = (Widget)e.getValue();
143
144             i.remove();
145
146             Integer JavaDoc ikey = new Integer JavaDoc(val.getIntField());
147
148             Assert.assertFalse("normalMap.containsKey() did not return false after removing existing key", normalMap.containsKey(skey));
149             Assert.assertTrue("normalObjectMap.remove() did not return non-null after removing existing key", normalObjectMap.remove(ikey) != null);
150             Assert.assertTrue("normalObjectMap.remove() did not return null attempting to remove non-existent key", normalObjectMap.remove(ikey) == null);
151
152             if (JDOHelper.isPersistent(this))
153             {
154                 JDOHelper.getPersistenceManager(this).deletePersistent(val);
155
156                 Assert.assertFalse("inverseMap should not contain deleted value", inverseMap.containsValue(val));
157             }
158             else if (val instanceof ValueWidget)
159                 Assert.assertTrue("inverseMap.remove() did not return non-null after removing existing key", inverseMap.remove(ikey) != null);
160         }
161
162         Assert.assertTrue("normalMap should have been empty", normalMap.isEmpty());
163         Assert.assertTrue("normalObjectMap should have been empty", normalObjectMap.isEmpty());
164         Assert.assertTrue("inverseMap should have been empty", inverseMap.isEmpty());
165
166         /*
167          * Fill up normalMap and normalObjectMap with random Widget objects of
168          * random types. Any ValueWidget objects are added to inverseMap as
169          * well.
170          */

171         this.numWidgets = numWidgets;
172         numValueWidgets = 0;
173
174         while (numWidgets-- > 0)
175         {
176             Widget val;
177
178             switch (r.nextInt(includeMapWidgets ? 7 : 6))
179             {
180                 case 0:
181                 default:
182                     val = new Widget();
183                     break;
184                 case 1:
185                     val = new DateWidget();
186                     break;
187                 case 2:
188                     val = new StringWidget();
189                     break;
190                 case 3:
191                     val = new BinaryWidget();
192                     break;
193                 case 4:
194                     val = new ValueWidget();
195                     break;
196                 case 5:
197                     val = new FloatWidget();
198                     break;
199                 case 6:
200                     val = new MapWidget();
201                     break;
202             }
203
204             Integer JavaDoc key;
205
206             do
207             {
208                 val.fillRandom();
209                 key = new Integer JavaDoc(val.getIntField());
210             } while (normalObjectMap.containsKey(key));
211
212             normalMap.put("K" + key, val);
213             normalObjectMap.put(key, val);
214
215             if (val instanceof ValueWidget)
216             {
217                 inverseMap.put(key, val);
218
219                 if (!JDOHelper.isPersistent(this))
220                     ((ValueWidget)val).setKey(key);
221
222                 ++numValueWidgets;
223             }
224         }
225
226         validate();
227     }
228
229
230     private void validate()
231     {
232         Assert.assertEquals("numWidgets != normalMap.size(): " + this, numWidgets, normalMap.size());
233         Assert.assertEquals("numWidgets != normalObjectMap.size(): " + this, numWidgets, normalObjectMap.size());
234         Assert.assertEquals("numValueWidgets != inverseMap.size(): " + this, numValueWidgets, inverseMap.size());
235         Assert.assertTrue("normalMap does not contain all values of inverseMap: " + this, normalMap.values().containsAll(inverseMap.values()));
236         Assert.assertTrue("normalObjectMap does not contain all values of inverseMap: " + this, normalObjectMap.values().containsAll(inverseMap.values()));
237     }
238
239
240     /**
241      * Indicates whether some other object is "equal to" this one. By comparing
242      * against an original copy of the object, <code>compareTo()</code> can be
243      * used to verify that the object has been written to a database and read
244      * back correctly.
245      *
246      * @param obj the reference object with which to compare
247      *
248      * @return <code>true</code> if this object is equal to the obj argument;
249      * <code>false</code> otherwise.
250      */

251
252     public boolean compareTo(Object JavaDoc obj)
253     {
254         validate();
255
256         if (obj == this)
257             return true;
258
259         if (!(obj instanceof MapWidget) || !super.compareTo(obj))
260             return false;
261
262         MapWidget w = (MapWidget)obj;
263
264         w.validate();
265
266         return compareMap(normalMap, w.normalMap) &&
267                compareMap(normalObjectMap, w.normalObjectMap) &&
268                compareMap(inverseMap, w.inverseMap);
269     }
270
271
272     /**
273      * Returns a string representation for this object. All of the field
274      * values are included in the string for debugging purposes.
275      *
276      * @return a string representation for this object.
277      */

278
279     public String JavaDoc toString()
280     {
281         StringBuffer JavaDoc s = new StringBuffer JavaDoc(super.toString());
282
283         s.append(" normalMap = ").append(normalMap);
284         s.append('\n');
285         s.append(" normalObjectMap = ").append(normalObjectMap);
286         s.append('\n');
287         s.append(" inverseMap = ").append(inverseMap);
288         s.append('\n');
289         s.append(" numWidgets = ").append(numWidgets);
290         s.append('\n');
291         s.append(" numValueWidgets = ").append(numValueWidgets);
292         s.append('\n');
293
294         return s.toString();
295     }
296
297
298     public void jdoPostLoad() {}
299     public void jdoPreClear() {}
300     public void jdoPreStore() {}
301
302
303     public void jdoPreDelete()
304     {
305         PersistenceManager myPM = JDOHelper.getPersistenceManager(this);
306         Object JavaDoc[] values = normalMap.values().toArray();
307
308         normalMap.clear();
309         normalObjectMap.clear();
310
311         myPM.deletePersistentAll(values);
312
313         if (!inverseMap.isEmpty())
314             throw new RuntimeException JavaDoc("Entries still left in inverseMap");
315     }
316 }
317
Popular Tags