1 10 11 package com.triactive.jdo.test; 12 13 import java.util.ArrayList ; 14 import java.util.Collection ; 15 import java.util.HashSet ; 16 import java.util.Iterator ; 17 import java.util.LinkedList ; 18 import java.util.Vector ; 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 normalCollection; 28 private Collection 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 newCollection() 41 { 42 switch (r.nextInt(4)) 43 { 44 case 0: 45 default: 46 return new HashSet (); 47 case 1: 48 return new ArrayList (); 49 case 2: 50 return new LinkedList (); 51 case 3: 52 return new Vector (); 53 } 54 } 55 56 57 public int getNumWidgets() 58 { 59 return numWidgets; 60 } 61 62 63 public Object [] getSCOFieldValues() 64 { 65 return new Object [] { normalCollection, normalObjectCollection }; 66 } 67 68 69 public Object clone() 70 { 71 CollectionWidget sw = (CollectionWidget)super.clone(); 72 73 HashSet ns = new HashSet (); 74 HashSet nos = new HashSet (); 75 76 Iterator 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 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 113 114 public void fillRandom(int numWidgets, boolean includeCollectionWidgets) 115 { 116 super.fillRandom(); 117 118 121 Iterator i = normalCollection.iterator(); 122 123 while (i.hasNext()) 124 { 125 Object 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 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 218 219 public boolean compareTo(Object 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 244 245 public String toString() 246 { 247 StringBuffer s = new StringBuffer (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 [] elements = normalCollection.toArray(); 269 270 normalCollection.clear(); 271 normalObjectCollection.clear(); 272 273 myPM.deletePersistentAll(elements); 274 } 275 } 276 | Popular Tags |