KickJava   Java API By Example, From Geeks To Geeks.

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


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: SetWidget.java,v 1.11 2004/03/22 04:58:13 jackknifebarber Exp $
9  */

10
11 package com.triactive.jdo.test;
12
13 import java.util.HashSet JavaDoc;
14 import java.util.Iterator JavaDoc;
15 import java.util.Set JavaDoc;
16 import javax.jdo.InstanceCallbacks;
17 import javax.jdo.JDOHelper;
18 import javax.jdo.PersistenceManager;
19 import junit.framework.Assert;
20
21
22 public class SetWidget extends Widget implements HasNormalSetField, HasInverseSetField, InstanceCallbacks
23 {
24     private Set JavaDoc normalSet;
25     private Set JavaDoc normalObjectSet;
26     private Set JavaDoc inverseSet;
27     private int numWidgets;
28     private int numElementWidgets;
29
30
31     public SetWidget()
32     {
33         normalSet = new HashSet JavaDoc();
34         normalObjectSet = new HashSet JavaDoc();
35         inverseSet = new HashSet JavaDoc();
36         numWidgets = 0;
37         numElementWidgets = 0;
38     }
39
40
41     public Set JavaDoc getNormalSet()
42     {
43         return normalSet;
44     }
45
46
47     public Set JavaDoc getNormalObjectSet()
48     {
49         return normalObjectSet;
50     }
51
52
53     public Set JavaDoc getInverseSet()
54     {
55         return inverseSet;
56     }
57
58
59     public int getNumWidgets()
60     {
61         return numWidgets;
62     }
63
64
65     public int getNumElementWidgets()
66     {
67         return numElementWidgets;
68     }
69
70
71     public Object JavaDoc[] getSCOFieldValues()
72     {
73         return new Object JavaDoc[] { normalSet, normalObjectSet, inverseSet };
74     }
75
76
77     public Object JavaDoc clone()
78     {
79         SetWidget sw = (SetWidget)super.clone();
80
81         HashSet JavaDoc ns = new HashSet JavaDoc();
82         HashSet JavaDoc nos = new HashSet JavaDoc();
83         HashSet JavaDoc is = new HashSet JavaDoc();
84
85         Iterator JavaDoc i = normalSet.iterator();
86
87         while (i.hasNext())
88         {
89             Widget w = (Widget)((Widget)i.next()).clone();
90
91             ns.add(w);
92             nos.add(w);
93
94             if (w instanceof ElementWidget)
95             {
96                 ElementWidget ew = (ElementWidget)w;
97                 ew.setOwner(sw);
98                 is.add(ew);
99             }
100         }
101
102         sw.normalSet = ns;
103         sw.normalObjectSet = nos;
104         sw.inverseSet = is;
105
106         return sw;
107     }
108
109
110     /**
111      * Fills all of the object's fields with random data values.
112      */

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

130
131     public void fillRandom(int numWidgets, boolean includeSetWidgets)
132     {
133         super.fillRandom();
134
135         /*
136          * Clear normalSet iteratively in order to test remove().
137          */

138         Iterator JavaDoc i = normalSet.iterator();
139
140         while (i.hasNext())
141         {
142             Object JavaDoc obj = i.next();
143             i.remove();
144
145             Assert.assertTrue("normalSet.contains() did not return false after removing existing object", !normalSet.contains(obj));
146             Assert.assertTrue("normalObjectSet.remove() did not return true after removing existing object", normalObjectSet.remove(obj));
147             Assert.assertTrue("normalObjectSet.remove() did not return false attempting to remove non-existent object", !normalObjectSet.remove(obj));
148
149             if (JDOHelper.isPersistent(this))
150             {
151                 JDOHelper.getPersistenceManager(this).deletePersistent(obj);
152
153                 Assert.assertTrue("inverseSet should not contain deleted object", !inverseSet.contains(obj));
154             }
155             else if (obj instanceof ElementWidget)
156                 Assert.assertTrue("inverseSet.remove() did not return true after removing existing object", inverseSet.remove(obj));
157         }
158
159         Assert.assertTrue("normalSet should have been empty", normalSet.isEmpty());
160         Assert.assertTrue("normalObjectSet should have been empty", normalObjectSet.isEmpty());
161         Assert.assertTrue("inverseSet should have been empty", inverseSet.isEmpty());
162
163         /*
164          * Fill up normalSet and normalObjectSet with random Widget objects of random types.
165          * Any ElementWidget objects are added to inverseSet as well.
166          */

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

255
256     public boolean compareTo(Object JavaDoc obj)
257     {
258         validate();
259
260         if (obj == this)
261             return true;
262
263         if (!(obj instanceof SetWidget) || !super.compareTo(obj))
264             return false;
265
266         SetWidget w = (SetWidget)obj;
267
268         w.validate();
269
270         return compareCollection(normalSet, w.normalSet) &&
271                compareCollection(normalObjectSet, w.normalObjectSet) &&
272                compareCollection(inverseSet, w.inverseSet);
273     }
274
275
276     /**
277      * Returns a string representation for this object. All of the field
278      * values are included in the string for debugging purposes.
279      *
280      * @return a string representation for this object.
281      */

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