KickJava   Java API By Example, From Geeks To Geeks.

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


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: CollectionWidget.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.ArrayList JavaDoc;
14 import java.util.Collection JavaDoc;
15 import java.util.HashSet JavaDoc;
16 import java.util.Iterator JavaDoc;
17 import java.util.LinkedList JavaDoc;
18 import java.util.Vector JavaDoc;
19 import javax.jdo.InstanceCallbacks;
20 import javax.jdo.JDOHelper;
21 import javax.jdo.PersistenceManager;
22 import junit.framework.Assert;
23
24
25 public class CollectionWidget extends Widget implements HasSCOFields, InstanceCallbacks
26 {
27     private Collection JavaDoc normalCollection;
28     private Collection JavaDoc normalObjectCollection;
29     private int numWidgets;
30
31
32     public CollectionWidget()
33     {
34         normalCollection = newCollection();
35         normalObjectCollection = newCollection();
36         numWidgets = 0;
37     }
38
39
40     private Collection JavaDoc newCollection()
41     {
42         switch (r.nextInt(4))
43         {
44             case 0:
45             default:
46                 return new HashSet JavaDoc();
47             case 1:
48                 return new ArrayList JavaDoc();
49             case 2:
50                 return new LinkedList JavaDoc();
51             case 3:
52                 return new Vector JavaDoc();
53         }
54     }
55
56
57     public int getNumWidgets()
58     {
59         return numWidgets;
60     }
61
62
63     public Object JavaDoc[] getSCOFieldValues()
64     {
65         return new Object JavaDoc[] { normalCollection, normalObjectCollection };
66     }
67
68
69     public Object JavaDoc clone()
70     {
71         CollectionWidget sw = (CollectionWidget)super.clone();
72
73         HashSet JavaDoc ns = new HashSet JavaDoc();
74         HashSet JavaDoc nos = new HashSet JavaDoc();
75
76         Iterator JavaDoc i = normalCollection.iterator();
77
78         while (i.hasNext())
79         {
80             Widget w = (Widget)((Widget)i.next()).clone();
81
82             ns.add(w);
83             nos.add(w);
84         }
85
86         sw.normalCollection = ns;
87         sw.normalObjectCollection = nos;
88
89         return sw;
90     }
91
92
93     /**
94      * Fills all of the object's fields with random data values.
95      */

96
97     public void fillRandom()
98     {
99         fillRandom(r.nextInt(5));
100     }
101
102
103     public void fillRandom(int numWidgets)
104     {
105         fillRandom(numWidgets, false);
106     }
107
108
109     /**
110      * Fills the collection fields with the given number of random Widget
111      * objects.
112      */

113
114     public void fillRandom(int numWidgets, boolean includeCollectionWidgets)
115     {
116         super.fillRandom();
117
118         /*
119          * Clear normalCollection iteratively in order to test remove().
120          */

121         Iterator JavaDoc i = normalCollection.iterator();
122
123         while (i.hasNext())
124         {
125             Object JavaDoc obj = i.next();
126             i.remove();
127
128             Assert.assertTrue("normalCollection.contains() did not return false after removing existing object", !normalCollection.contains(obj));
129             Assert.assertTrue("normalObjectCollection.remove() did not return true after removing existing object", normalObjectCollection.remove(obj));
130             Assert.assertTrue("normalObjectCollection.remove() did not return false attempting to remove non-existent object", !normalObjectCollection.remove(obj));
131
132             if (JDOHelper.isPersistent(this))
133                 JDOHelper.getPersistenceManager(this).deletePersistent(obj);
134         }
135
136         Assert.assertTrue("normalCollection should have been empty", normalCollection.isEmpty());
137         Assert.assertTrue("normalObjectCollection should have been empty", normalObjectCollection.isEmpty());
138
139         /*
140          * Fill up normalCollection and normalObjectCollection with random
141          * Widget objects of random types.
142          */

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

218
219     public boolean compareTo(Object JavaDoc obj)
220     {
221         validate();
222
223         if (obj == this)
224             return true;
225
226         if (!(obj instanceof CollectionWidget) || !super.compareTo(obj))
227             return false;
228
229         CollectionWidget w = (CollectionWidget)obj;
230
231         w.validate();
232
233         return compareCollection(normalCollection, w.normalCollection) &&
234                compareCollection(normalObjectCollection, w.normalObjectCollection);
235     }
236
237
238     /**
239      * Returns a string representation for this object. All of the field
240      * values are included in the string for debugging purposes.
241      *
242      * @return a string representation for this object.
243      */

244
245     public String JavaDoc toString()
246     {
247         StringBuffer JavaDoc s = new StringBuffer JavaDoc(super.toString());
248
249         s.append(" normalCollection = ").append(normalCollection);
250         s.append('\n');
251         s.append(" normalObjectCollection = ").append(normalObjectCollection);
252         s.append('\n');
253         s.append(" numWidgets = ").append(numWidgets);
254         s.append('\n');
255
256         return s.toString();
257     }
258
259
260     public void jdoPostLoad() {}
261     public void jdoPreClear() {}
262     public void jdoPreStore() {}
263
264
265     public void jdoPreDelete()
266     {
267         PersistenceManager myPM = JDOHelper.getPersistenceManager(this);
268         Object JavaDoc[] elements = normalCollection.toArray();
269
270         normalCollection.clear();
271         normalObjectCollection.clear();
272
273         myPM.deletePersistentAll(elements);
274     }
275 }
276
Popular Tags