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: HashtableWidget.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.Hashtable;
014: import java.util.Iterator;
015: import java.util.Map;
016: import java.util.Map.Entry;
017: import javax.jdo.InstanceCallbacks;
018: import javax.jdo.JDOHelper;
019: import javax.jdo.PersistenceManager;
020: import junit.framework.Assert;
021:
022: public class HashtableWidget extends Widget implements
023: InstanceCallbacks {
024: private Hashtable normalMap = new Hashtable();
025: private Hashtable normalObjectMap = new Hashtable();
026: private int numWidgets = 0;
027:
028: public HashtableWidget() {
029: }
030:
031: public Map getNormalMap() {
032: return normalMap;
033: }
034:
035: public Map getNormalObjectMap() {
036: return normalObjectMap;
037: }
038:
039: public int getNumWidgets() {
040: return numWidgets;
041: }
042:
043: public Object[] getSCOFieldValues() {
044: return new Object[] { normalMap, normalObjectMap };
045: }
046:
047: public Object clone() {
048: HashtableWidget mw = (HashtableWidget) super .clone();
049:
050: /* Do a deep clone of all the maps. */
051: mw.normalMap = new Hashtable();
052: mw.normalObjectMap = new Hashtable();
053:
054: Iterator i = normalMap.entrySet().iterator();
055:
056: while (i.hasNext()) {
057: Entry e = (Entry) i.next();
058: String ks = (String) e.getKey();
059: Widget v = (Widget) clone(e.getValue());
060:
061: Integer ki = new Integer(v.getIntField());
062:
063: mw.normalMap.put(ks, v);
064: mw.normalObjectMap.put(ki, v);
065: }
066:
067: return mw;
068: }
069:
070: /**
071: * Fills all of the object's fields with random data values.
072: */
073:
074: public void fillRandom() {
075: fillRandom(r.nextInt(5));
076: }
077:
078: public void fillRandom(int numWidgets) {
079: fillRandom(numWidgets, false);
080: }
081:
082: /**
083: * Fills the collection fields with the given number of random Widget
084: * objects.
085: */
086:
087: public void fillRandom(int numWidgets, boolean includeHtWidgets) {
088: super .fillRandom();
089:
090: /*
091: * Clear normalMap iteratively in order to test remove().
092: */
093: Iterator i = normalMap.entrySet().iterator();
094:
095: while (i.hasNext()) {
096: Entry e = (Entry) i.next();
097: Object skey = e.getKey();
098: Widget val = (Widget) e.getValue();
099:
100: i.remove();
101:
102: Integer ikey = new Integer(val.getIntField());
103:
104: Assert
105: .assertFalse(
106: "normalMap.containsKey() did not return false after removing existing key",
107: normalMap.containsKey(skey));
108: Assert
109: .assertTrue(
110: "normalObjectMap.remove() did not return non-null after removing existing key",
111: normalObjectMap.remove(ikey) != null);
112: Assert
113: .assertTrue(
114: "normalObjectMap.remove() did not return null attempting to remove non-existent key",
115: normalObjectMap.remove(ikey) == null);
116:
117: if (JDOHelper.isPersistent(this ))
118: JDOHelper.getPersistenceManager(this ).deletePersistent(
119: val);
120: }
121:
122: Assert.assertTrue("normalMap should have been empty", normalMap
123: .isEmpty());
124: Assert.assertTrue("normalObjectMap should have been empty",
125: normalObjectMap.isEmpty());
126:
127: /*
128: * Fill up normalMap and normalObjectMap with random Widget objects of
129: * random types.
130: */
131: this .numWidgets = numWidgets;
132:
133: while (numWidgets-- > 0) {
134: Widget val;
135:
136: switch (r.nextInt(includeHtWidgets ? 6 : 5)) {
137: case 0:
138: default:
139: val = new Widget();
140: break;
141: case 1:
142: val = new DateWidget();
143: break;
144: case 2:
145: val = new StringWidget();
146: break;
147: case 3:
148: val = new BinaryWidget();
149: break;
150: case 4:
151: val = new FloatWidget();
152: break;
153: case 5:
154: val = new HashtableWidget();
155: break;
156: }
157:
158: Integer key;
159:
160: do {
161: val.fillRandom();
162: key = new Integer(val.getIntField());
163: } while (normalObjectMap.containsKey(key));
164:
165: normalMap.put("K" + key, val);
166: normalObjectMap.put(key, val);
167: }
168:
169: validate();
170: }
171:
172: private void validate() {
173: Assert.assertEquals("numWidgets != normalMap.size(): " + this ,
174: numWidgets, normalMap.size());
175: Assert.assertEquals("numWidgets != normalObjectMap.size(): "
176: + this , numWidgets, normalObjectMap.size());
177: }
178:
179: /**
180: * Indicates whether some other object is "equal to" this one. By comparing
181: * against an original copy of the object, <code>compareTo()</code> can be
182: * used to verify that the object has been written to a database and read
183: * back correctly.
184: *
185: * @param obj the reference object with which to compare
186: *
187: * @return <code>true</code> if this object is equal to the obj argument;
188: * <code>false</code> otherwise.
189: */
190:
191: public boolean compareTo(Object obj) {
192: validate();
193:
194: if (obj == this )
195: return true;
196:
197: if (!(obj instanceof HashtableWidget) || !super .compareTo(obj))
198: return false;
199:
200: HashtableWidget w = (HashtableWidget) obj;
201:
202: w.validate();
203:
204: return compareMap(normalMap, w.normalMap)
205: && compareMap(normalObjectMap, w.normalObjectMap);
206: }
207:
208: /**
209: * Returns a string representation for this object. All of the field
210: * values are included in the string for debugging purposes.
211: *
212: * @return a string representation for this object.
213: */
214:
215: public String toString() {
216: StringBuffer s = new StringBuffer(super .toString());
217:
218: s.append(" normalMap = ").append(normalMap);
219: s.append('\n');
220: s.append(" normalObjectMap = ").append(normalObjectMap);
221: s.append('\n');
222: s.append(" numWidgets = ").append(numWidgets);
223: s.append('\n');
224:
225: return s.toString();
226: }
227:
228: public void jdoPostLoad() {
229: }
230:
231: public void jdoPreClear() {
232: }
233:
234: public void jdoPreStore() {
235: }
236:
237: public void jdoPreDelete() {
238: PersistenceManager myPM = JDOHelper.getPersistenceManager(this );
239: Object[] values = normalMap.values().toArray();
240:
241: normalMap.clear();
242: normalObjectMap.clear();
243:
244: myPM.deletePersistentAll(values);
245: }
246: }
|