001: /*
002: * Copyright 2004 (C) TJDO.
003: * All rights reserved.
004: *
005: * This software is distributed under the terms of the TJDO License version 1.0.
006: * See the terms of the TJDO License in the documentation provided with this software.
007: *
008: * $Id: CollectionWidget.java,v 1.2 2004/03/22 04:58:13 jackknifebarber Exp $
009: */
010:
011: package com.triactive.jdo.test;
012:
013: import java.util.ArrayList;
014: import java.util.Collection;
015: import java.util.HashSet;
016: import java.util.Iterator;
017: import java.util.LinkedList;
018: import java.util.Vector;
019: import javax.jdo.InstanceCallbacks;
020: import javax.jdo.JDOHelper;
021: import javax.jdo.PersistenceManager;
022: import junit.framework.Assert;
023:
024: public class CollectionWidget extends Widget implements HasSCOFields,
025: InstanceCallbacks {
026: private Collection normalCollection;
027: private Collection normalObjectCollection;
028: private int numWidgets;
029:
030: public CollectionWidget() {
031: normalCollection = newCollection();
032: normalObjectCollection = newCollection();
033: numWidgets = 0;
034: }
035:
036: private Collection newCollection() {
037: switch (r.nextInt(4)) {
038: case 0:
039: default:
040: return new HashSet();
041: case 1:
042: return new ArrayList();
043: case 2:
044: return new LinkedList();
045: case 3:
046: return new Vector();
047: }
048: }
049:
050: public int getNumWidgets() {
051: return numWidgets;
052: }
053:
054: public Object[] getSCOFieldValues() {
055: return new Object[] { normalCollection, normalObjectCollection };
056: }
057:
058: public Object clone() {
059: CollectionWidget sw = (CollectionWidget) super .clone();
060:
061: HashSet ns = new HashSet();
062: HashSet nos = new HashSet();
063:
064: Iterator i = normalCollection.iterator();
065:
066: while (i.hasNext()) {
067: Widget w = (Widget) ((Widget) i.next()).clone();
068:
069: ns.add(w);
070: nos.add(w);
071: }
072:
073: sw.normalCollection = ns;
074: sw.normalObjectCollection = nos;
075:
076: return sw;
077: }
078:
079: /**
080: * Fills all of the object's fields with random data values.
081: */
082:
083: public void fillRandom() {
084: fillRandom(r.nextInt(5));
085: }
086:
087: public void fillRandom(int numWidgets) {
088: fillRandom(numWidgets, false);
089: }
090:
091: /**
092: * Fills the collection fields with the given number of random Widget
093: * objects.
094: */
095:
096: public void fillRandom(int numWidgets,
097: boolean includeCollectionWidgets) {
098: super .fillRandom();
099:
100: /*
101: * Clear normalCollection iteratively in order to test remove().
102: */
103: Iterator i = normalCollection.iterator();
104:
105: while (i.hasNext()) {
106: Object obj = i.next();
107: i.remove();
108:
109: Assert
110: .assertTrue(
111: "normalCollection.contains() did not return false after removing existing object",
112: !normalCollection.contains(obj));
113: Assert
114: .assertTrue(
115: "normalObjectCollection.remove() did not return true after removing existing object",
116: normalObjectCollection.remove(obj));
117: Assert
118: .assertTrue(
119: "normalObjectCollection.remove() did not return false attempting to remove non-existent object",
120: !normalObjectCollection.remove(obj));
121:
122: if (JDOHelper.isPersistent(this ))
123: JDOHelper.getPersistenceManager(this ).deletePersistent(
124: obj);
125: }
126:
127: Assert.assertTrue("normalCollection should have been empty",
128: normalCollection.isEmpty());
129: Assert.assertTrue(
130: "normalObjectCollection should have been empty",
131: normalObjectCollection.isEmpty());
132:
133: /*
134: * Fill up normalCollection and normalObjectCollection with random
135: * Widget objects of random types.
136: */
137: this .numWidgets = numWidgets;
138:
139: while (numWidgets-- > 0) {
140: Widget obj;
141:
142: switch (r.nextInt(includeCollectionWidgets ? 6 : 5)) {
143: case 0:
144: default:
145: obj = new Widget();
146: obj.fillRandom();
147: normalCollection.add(obj);
148: normalObjectCollection.add(obj);
149: break;
150:
151: case 1:
152: obj = new DateWidget();
153: obj.fillRandom();
154: normalCollection.add(obj);
155: normalObjectCollection.add(obj);
156: break;
157:
158: case 2:
159: obj = new StringWidget();
160: obj.fillRandom();
161: normalCollection.add(obj);
162: normalObjectCollection.add(obj);
163: break;
164:
165: case 3:
166: obj = new BinaryWidget();
167: obj.fillRandom();
168: normalCollection.add(obj);
169: normalObjectCollection.add(obj);
170: break;
171:
172: case 4:
173: obj = new FloatWidget();
174: obj.fillRandom();
175: normalCollection.add(obj);
176: normalObjectCollection.add(obj);
177: break;
178:
179: case 5:
180: obj = new CollectionWidget();
181: obj.fillRandom();
182: normalCollection.add(obj);
183: normalObjectCollection.add(obj);
184: break;
185: }
186: }
187:
188: validate();
189: }
190:
191: private void validate() {
192: Assert.assertEquals("numWidgets != normalCollection.size(): "
193: + this , numWidgets, normalCollection.size());
194: Assert.assertEquals(
195: "numWidgets != normalObjectCollection.size(): " + this ,
196: numWidgets, normalObjectCollection.size());
197: }
198:
199: /**
200: * Indicates whether some other object is "equal to" this one. By comparing
201: * against an original copy of the object, <code>compareTo()</code> can be
202: * used to verify that the object has been written to a database and read
203: * back correctly.
204: *
205: * @param obj the reference object with which to compare
206: *
207: * @return <code>true</code> if this object is equal to the obj argument;
208: * <code>false</code> otherwise.
209: */
210:
211: public boolean compareTo(Object obj) {
212: validate();
213:
214: if (obj == this )
215: return true;
216:
217: if (!(obj instanceof CollectionWidget) || !super .compareTo(obj))
218: return false;
219:
220: CollectionWidget w = (CollectionWidget) obj;
221:
222: w.validate();
223:
224: return compareCollection(normalCollection, w.normalCollection)
225: && compareCollection(normalObjectCollection,
226: w.normalObjectCollection);
227: }
228:
229: /**
230: * Returns a string representation for this object. All of the field
231: * values are included in the string for debugging purposes.
232: *
233: * @return a string representation for this object.
234: */
235:
236: public String toString() {
237: StringBuffer s = new StringBuffer(super .toString());
238:
239: s.append(" normalCollection = ").append(normalCollection);
240: s.append('\n');
241: s.append(" normalObjectCollection = ").append(
242: normalObjectCollection);
243: s.append('\n');
244: s.append(" numWidgets = ").append(numWidgets);
245: s.append('\n');
246:
247: return s.toString();
248: }
249:
250: public void jdoPostLoad() {
251: }
252:
253: public void jdoPreClear() {
254: }
255:
256: public void jdoPreStore() {
257: }
258:
259: public void jdoPreDelete() {
260: PersistenceManager myPM = JDOHelper.getPersistenceManager(this );
261: Object[] elements = normalCollection.toArray();
262:
263: normalCollection.clear();
264: normalObjectCollection.clear();
265:
266: myPM.deletePersistentAll(elements);
267: }
268: }
|