KickJava   Java API By Example, From Geeks To Geeks.

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


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: HashtableWidget.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.Hashtable 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 HashtableWidget extends Widget implements InstanceCallbacks
24 {
25     private Hashtable JavaDoc normalMap = new Hashtable JavaDoc();
26     private Hashtable JavaDoc normalObjectMap = new Hashtable JavaDoc();
27     private int numWidgets = 0;
28
29
30     public HashtableWidget()
31     {
32     }
33
34
35     public Map JavaDoc getNormalMap()
36     {
37         return normalMap;
38     }
39
40
41     public Map JavaDoc getNormalObjectMap()
42     {
43         return normalObjectMap;
44     }
45
46
47     public int getNumWidgets()
48     {
49         return numWidgets;
50     }
51
52
53     public Object JavaDoc[] getSCOFieldValues()
54     {
55         return new Object JavaDoc[] { normalMap, normalObjectMap };
56     }
57
58
59     public Object JavaDoc clone()
60     {
61         HashtableWidget mw = (HashtableWidget)super.clone();
62
63         /* Do a deep clone of all the maps. */
64         mw.normalMap = new Hashtable JavaDoc();
65         mw.normalObjectMap = new Hashtable JavaDoc();
66
67         Iterator JavaDoc i = normalMap.entrySet().iterator();
68
69         while (i.hasNext())
70         {
71             Entry e = (Entry)i.next();
72             String JavaDoc ks = (String JavaDoc)e.getKey();
73             Widget v = (Widget)clone(e.getValue());
74
75             Integer JavaDoc ki = new Integer JavaDoc(v.getIntField());
76
77             mw.normalMap.put(ks, v);
78             mw.normalObjectMap.put(ki, v);
79         }
80
81         return mw;
82     }
83
84
85     /**
86      * Fills all of the object's fields with random data values.
87      */

88
89     public void fillRandom()
90     {
91         fillRandom(r.nextInt(5));
92     }
93
94
95     public void fillRandom(int numWidgets)
96     {
97         fillRandom(numWidgets, false);
98     }
99
100
101     /**
102      * Fills the collection fields with the given number of random Widget
103      * objects.
104      */

105
106     public void fillRandom(int numWidgets, boolean includeHtWidgets)
107     {
108         super.fillRandom();
109
110         /*
111          * Clear normalMap iteratively in order to test remove().
112          */

113         Iterator JavaDoc i = normalMap.entrySet().iterator();
114
115         while (i.hasNext())
116         {
117             Entry e = (Entry)i.next();
118             Object JavaDoc skey = e.getKey();
119             Widget val = (Widget)e.getValue();
120
121             i.remove();
122
123             Integer JavaDoc ikey = new Integer JavaDoc(val.getIntField());
124
125             Assert.assertFalse("normalMap.containsKey() did not return false after removing existing key", normalMap.containsKey(skey));
126             Assert.assertTrue("normalObjectMap.remove() did not return non-null after removing existing key", normalObjectMap.remove(ikey) != null);
127             Assert.assertTrue("normalObjectMap.remove() did not return null attempting to remove non-existent key", normalObjectMap.remove(ikey) == null);
128
129             if (JDOHelper.isPersistent(this))
130                 JDOHelper.getPersistenceManager(this).deletePersistent(val);
131         }
132
133         Assert.assertTrue("normalMap should have been empty", normalMap.isEmpty());
134         Assert.assertTrue("normalObjectMap should have been empty", normalObjectMap.isEmpty());
135
136         /*
137          * Fill up normalMap and normalObjectMap with random Widget objects of
138          * random types.
139          */

140         this.numWidgets = numWidgets;
141
142         while (numWidgets-- > 0)
143         {
144             Widget val;
145
146             switch (r.nextInt(includeHtWidgets ? 6 : 5))
147             {
148                 case 0:
149                 default:
150                     val = new Widget();
151                     break;
152                 case 1:
153                     val = new DateWidget();
154                     break;
155                 case 2:
156                     val = new StringWidget();
157                     break;
158                 case 3:
159                     val = new BinaryWidget();
160                     break;
161                 case 4:
162                     val = new FloatWidget();
163                     break;
164                 case 5:
165                     val = new HashtableWidget();
166                     break;
167             }
168
169             Integer JavaDoc key;
170
171             do
172             {
173                 val.fillRandom();
174                 key = new Integer JavaDoc(val.getIntField());
175             } while (normalObjectMap.containsKey(key));
176
177             normalMap.put("K" + key, val);
178             normalObjectMap.put(key, val);
179         }
180
181         validate();
182     }
183
184
185     private void validate()
186     {
187         Assert.assertEquals("numWidgets != normalMap.size(): " + this, numWidgets, normalMap.size());
188         Assert.assertEquals("numWidgets != normalObjectMap.size(): " + this, numWidgets, normalObjectMap.size());
189     }
190
191
192     /**
193      * Indicates whether some other object is "equal to" this one. By comparing
194      * against an original copy of the object, <code>compareTo()</code> can be
195      * used to verify that the object has been written to a database and read
196      * back correctly.
197      *
198      * @param obj the reference object with which to compare
199      *
200      * @return <code>true</code> if this object is equal to the obj argument;
201      * <code>false</code> otherwise.
202      */

203
204     public boolean compareTo(Object JavaDoc obj)
205     {
206         validate();
207
208         if (obj == this)
209             return true;
210
211         if (!(obj instanceof HashtableWidget) || !super.compareTo(obj))
212             return false;
213
214         HashtableWidget w = (HashtableWidget)obj;
215
216         w.validate();
217
218         return compareMap(normalMap, w.normalMap) &&
219                compareMap(normalObjectMap, w.normalObjectMap);
220     }
221
222
223     /**
224      * Returns a string representation for this object. All of the field
225      * values are included in the string for debugging purposes.
226      *
227      * @return a string representation for this object.
228      */

229
230     public String JavaDoc toString()
231     {
232         StringBuffer JavaDoc s = new StringBuffer JavaDoc(super.toString());
233
234         s.append(" normalMap = ").append(normalMap);
235         s.append('\n');
236         s.append(" normalObjectMap = ").append(normalObjectMap);
237         s.append('\n');
238         s.append(" numWidgets = ").append(numWidgets);
239         s.append('\n');
240
241         return s.toString();
242     }
243
244
245     public void jdoPostLoad() {}
246     public void jdoPreClear() {}
247     public void jdoPreStore() {}
248
249
250     public void jdoPreDelete()
251     {
252         PersistenceManager myPM = JDOHelper.getPersistenceManager(this);
253         Object JavaDoc[] values = normalMap.values().toArray();
254
255         normalMap.clear();
256         normalObjectMap.clear();
257
258         myPM.deletePersistentAll(values);
259     }
260 }
261
Popular Tags