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: SetWidget.java,v 1.11 2004/03/22 04:58:13 jackknifebarber Exp $
009: */
010:
011: package com.triactive.jdo.test;
012:
013: import java.util.HashSet;
014: import java.util.Iterator;
015: import java.util.Set;
016: import javax.jdo.InstanceCallbacks;
017: import javax.jdo.JDOHelper;
018: import javax.jdo.PersistenceManager;
019: import junit.framework.Assert;
020:
021: public class SetWidget extends Widget implements HasNormalSetField,
022: HasInverseSetField, InstanceCallbacks {
023: private Set normalSet;
024: private Set normalObjectSet;
025: private Set inverseSet;
026: private int numWidgets;
027: private int numElementWidgets;
028:
029: public SetWidget() {
030: normalSet = new HashSet();
031: normalObjectSet = new HashSet();
032: inverseSet = new HashSet();
033: numWidgets = 0;
034: numElementWidgets = 0;
035: }
036:
037: public Set getNormalSet() {
038: return normalSet;
039: }
040:
041: public Set getNormalObjectSet() {
042: return normalObjectSet;
043: }
044:
045: public Set getInverseSet() {
046: return inverseSet;
047: }
048:
049: public int getNumWidgets() {
050: return numWidgets;
051: }
052:
053: public int getNumElementWidgets() {
054: return numElementWidgets;
055: }
056:
057: public Object[] getSCOFieldValues() {
058: return new Object[] { normalSet, normalObjectSet, inverseSet };
059: }
060:
061: public Object clone() {
062: SetWidget sw = (SetWidget) super .clone();
063:
064: HashSet ns = new HashSet();
065: HashSet nos = new HashSet();
066: HashSet is = new HashSet();
067:
068: Iterator i = normalSet.iterator();
069:
070: while (i.hasNext()) {
071: Widget w = (Widget) ((Widget) i.next()).clone();
072:
073: ns.add(w);
074: nos.add(w);
075:
076: if (w instanceof ElementWidget) {
077: ElementWidget ew = (ElementWidget) w;
078: ew.setOwner(sw);
079: is.add(ew);
080: }
081: }
082:
083: sw.normalSet = ns;
084: sw.normalObjectSet = nos;
085: sw.inverseSet = is;
086:
087: return sw;
088: }
089:
090: /**
091: * Fills all of the object's fields with random data values.
092: */
093:
094: public void fillRandom() {
095: fillRandom(r.nextInt(5));
096: }
097:
098: public void fillRandom(int numWidgets) {
099: fillRandom(numWidgets, false);
100: }
101:
102: /**
103: * Fills the collection fields with the given number of random Widget
104: * objects.
105: */
106:
107: public void fillRandom(int numWidgets, boolean includeSetWidgets) {
108: super .fillRandom();
109:
110: /*
111: * Clear normalSet iteratively in order to test remove().
112: */
113: Iterator i = normalSet.iterator();
114:
115: while (i.hasNext()) {
116: Object obj = i.next();
117: i.remove();
118:
119: Assert
120: .assertTrue(
121: "normalSet.contains() did not return false after removing existing object",
122: !normalSet.contains(obj));
123: Assert
124: .assertTrue(
125: "normalObjectSet.remove() did not return true after removing existing object",
126: normalObjectSet.remove(obj));
127: Assert
128: .assertTrue(
129: "normalObjectSet.remove() did not return false attempting to remove non-existent object",
130: !normalObjectSet.remove(obj));
131:
132: if (JDOHelper.isPersistent(this )) {
133: JDOHelper.getPersistenceManager(this ).deletePersistent(
134: obj);
135:
136: Assert.assertTrue(
137: "inverseSet should not contain deleted object",
138: !inverseSet.contains(obj));
139: } else if (obj instanceof ElementWidget)
140: Assert
141: .assertTrue(
142: "inverseSet.remove() did not return true after removing existing object",
143: inverseSet.remove(obj));
144: }
145:
146: Assert.assertTrue("normalSet should have been empty", normalSet
147: .isEmpty());
148: Assert.assertTrue("normalObjectSet should have been empty",
149: normalObjectSet.isEmpty());
150: Assert.assertTrue("inverseSet should have been empty",
151: inverseSet.isEmpty());
152:
153: /*
154: * Fill up normalSet and normalObjectSet with random Widget objects of random types.
155: * Any ElementWidget objects are added to inverseSet as well.
156: */
157: this .numWidgets = numWidgets;
158: numElementWidgets = 0;
159:
160: while (numWidgets-- > 0) {
161: Widget obj;
162:
163: switch (r.nextInt(includeSetWidgets ? 7 : 6)) {
164: case 0:
165: default:
166: obj = new Widget();
167: obj.fillRandom();
168: normalSet.add(obj);
169: normalObjectSet.add(obj);
170: break;
171:
172: case 1:
173: obj = new DateWidget();
174: obj.fillRandom();
175: normalSet.add(obj);
176: normalObjectSet.add(obj);
177: break;
178:
179: case 2:
180: obj = new StringWidget();
181: obj.fillRandom();
182: normalSet.add(obj);
183: normalObjectSet.add(obj);
184: break;
185:
186: case 3:
187: obj = new BinaryWidget();
188: obj.fillRandom();
189: normalSet.add(obj);
190: normalObjectSet.add(obj);
191: break;
192:
193: case 4:
194: obj = new ElementWidget(this );
195: obj.fillRandom();
196: normalSet.add(obj);
197: normalObjectSet.add(obj);
198: inverseSet.add(obj);
199: ++numElementWidgets;
200: break;
201:
202: case 5:
203: obj = new FloatWidget();
204: obj.fillRandom();
205: normalSet.add(obj);
206: normalObjectSet.add(obj);
207: break;
208:
209: case 6:
210: obj = new SetWidget();
211: obj.fillRandom();
212: normalSet.add(obj);
213: normalObjectSet.add(obj);
214: break;
215: }
216: }
217:
218: validate();
219: }
220:
221: private void validate() {
222: Assert.assertEquals("numWidgets != normalSet.size(): " + this ,
223: numWidgets, normalSet.size());
224: Assert.assertEquals("numWidgets != normalObjectSet.size(): "
225: + this , numWidgets, normalObjectSet.size());
226: Assert.assertEquals("numElementWidgets != inverseSet.size(): "
227: + this , numElementWidgets, inverseSet.size());
228: Assert.assertTrue(
229: "normalSet does not contain all elements of inverseSet: "
230: + this , normalSet.containsAll(inverseSet));
231: Assert
232: .assertTrue(
233: "normalObjectSet does not contain all elements of inverseSet: "
234: + this , normalObjectSet
235: .containsAll(inverseSet));
236: }
237:
238: /**
239: * Indicates whether some other object is "equal to" this one. By comparing
240: * against an original copy of the object, <code>compareTo()</code> can be
241: * used to verify that the object has been written to a database and read
242: * back correctly.
243: *
244: * @param obj the reference object with which to compare
245: *
246: * @return <code>true</code> if this object is equal to the obj argument;
247: * <code>false</code> otherwise.
248: */
249:
250: public boolean compareTo(Object obj) {
251: validate();
252:
253: if (obj == this )
254: return true;
255:
256: if (!(obj instanceof SetWidget) || !super .compareTo(obj))
257: return false;
258:
259: SetWidget w = (SetWidget) obj;
260:
261: w.validate();
262:
263: return compareCollection(normalSet, w.normalSet)
264: && compareCollection(normalObjectSet, w.normalObjectSet)
265: && compareCollection(inverseSet, w.inverseSet);
266: }
267:
268: /**
269: * Returns a string representation for this object. All of the field
270: * values are included in the string for debugging purposes.
271: *
272: * @return a string representation for this object.
273: */
274:
275: public String toString() {
276: StringBuffer s = new StringBuffer(super .toString());
277:
278: s.append(" normalSet = ").append(normalSet);
279: s.append('\n');
280: s.append(" normalObjectSet = ").append(normalObjectSet);
281: s.append('\n');
282: s.append(" inverseSet = ").append(inverseSet);
283: s.append('\n');
284: s.append(" numWidgets = ").append(numWidgets);
285: s.append('\n');
286: s.append(" numElementWidgets = ").append(numElementWidgets);
287: s.append('\n');
288:
289: return s.toString();
290: }
291:
292: public void jdoPostLoad() {
293: }
294:
295: public void jdoPreClear() {
296: }
297:
298: public void jdoPreStore() {
299: }
300:
301: public void jdoPreDelete() {
302: PersistenceManager myPM = JDOHelper.getPersistenceManager(this );
303: Object[] elements = normalSet.toArray();
304:
305: normalSet.clear();
306: normalObjectSet.clear();
307:
308: myPM.deletePersistentAll(elements);
309:
310: if (!inverseSet.isEmpty())
311: throw new RuntimeException(
312: "Elements still left in inverseSet");
313: }
314: }
|