1 10 11 package com.triactive.jdo.test; 12 13 import java.util.HashSet ; 14 import java.util.Iterator ; 15 import java.util.Set ; 16 import javax.jdo.InstanceCallbacks; 17 import javax.jdo.JDOHelper; 18 import javax.jdo.PersistenceManager; 19 import junit.framework.Assert; 20 21 22 public class SetWidget extends Widget implements HasNormalSetField, HasInverseSetField, InstanceCallbacks 23 { 24 private Set normalSet; 25 private Set normalObjectSet; 26 private Set inverseSet; 27 private int numWidgets; 28 private int numElementWidgets; 29 30 31 public SetWidget() 32 { 33 normalSet = new HashSet (); 34 normalObjectSet = new HashSet (); 35 inverseSet = new HashSet (); 36 numWidgets = 0; 37 numElementWidgets = 0; 38 } 39 40 41 public Set getNormalSet() 42 { 43 return normalSet; 44 } 45 46 47 public Set getNormalObjectSet() 48 { 49 return normalObjectSet; 50 } 51 52 53 public Set getInverseSet() 54 { 55 return inverseSet; 56 } 57 58 59 public int getNumWidgets() 60 { 61 return numWidgets; 62 } 63 64 65 public int getNumElementWidgets() 66 { 67 return numElementWidgets; 68 } 69 70 71 public Object [] getSCOFieldValues() 72 { 73 return new Object [] { normalSet, normalObjectSet, inverseSet }; 74 } 75 76 77 public Object clone() 78 { 79 SetWidget sw = (SetWidget)super.clone(); 80 81 HashSet ns = new HashSet (); 82 HashSet nos = new HashSet (); 83 HashSet is = new HashSet (); 84 85 Iterator i = normalSet.iterator(); 86 87 while (i.hasNext()) 88 { 89 Widget w = (Widget)((Widget)i.next()).clone(); 90 91 ns.add(w); 92 nos.add(w); 93 94 if (w instanceof ElementWidget) 95 { 96 ElementWidget ew = (ElementWidget)w; 97 ew.setOwner(sw); 98 is.add(ew); 99 } 100 } 101 102 sw.normalSet = ns; 103 sw.normalObjectSet = nos; 104 sw.inverseSet = is; 105 106 return sw; 107 } 108 109 110 113 114 public void fillRandom() 115 { 116 fillRandom(r.nextInt(5)); 117 } 118 119 120 public void fillRandom(int numWidgets) 121 { 122 fillRandom(numWidgets, false); 123 } 124 125 126 130 131 public void fillRandom(int numWidgets, boolean includeSetWidgets) 132 { 133 super.fillRandom(); 134 135 138 Iterator i = normalSet.iterator(); 139 140 while (i.hasNext()) 141 { 142 Object obj = i.next(); 143 i.remove(); 144 145 Assert.assertTrue("normalSet.contains() did not return false after removing existing object", !normalSet.contains(obj)); 146 Assert.assertTrue("normalObjectSet.remove() did not return true after removing existing object", normalObjectSet.remove(obj)); 147 Assert.assertTrue("normalObjectSet.remove() did not return false attempting to remove non-existent object", !normalObjectSet.remove(obj)); 148 149 if (JDOHelper.isPersistent(this)) 150 { 151 JDOHelper.getPersistenceManager(this).deletePersistent(obj); 152 153 Assert.assertTrue("inverseSet should not contain deleted object", !inverseSet.contains(obj)); 154 } 155 else if (obj instanceof ElementWidget) 156 Assert.assertTrue("inverseSet.remove() did not return true after removing existing object", inverseSet.remove(obj)); 157 } 158 159 Assert.assertTrue("normalSet should have been empty", normalSet.isEmpty()); 160 Assert.assertTrue("normalObjectSet should have been empty", normalObjectSet.isEmpty()); 161 Assert.assertTrue("inverseSet should have been empty", inverseSet.isEmpty()); 162 163 167 this.numWidgets = numWidgets; 168 numElementWidgets = 0; 169 170 while (numWidgets-- > 0) 171 { 172 Widget obj; 173 174 switch (r.nextInt(includeSetWidgets ? 7 : 6)) 175 { 176 case 0: 177 default: 178 obj = new Widget(); 179 obj.fillRandom(); 180 normalSet.add(obj); 181 normalObjectSet.add(obj); 182 break; 183 184 case 1: 185 obj = new DateWidget(); 186 obj.fillRandom(); 187 normalSet.add(obj); 188 normalObjectSet.add(obj); 189 break; 190 191 case 2: 192 obj = new StringWidget(); 193 obj.fillRandom(); 194 normalSet.add(obj); 195 normalObjectSet.add(obj); 196 break; 197 198 case 3: 199 obj = new BinaryWidget(); 200 obj.fillRandom(); 201 normalSet.add(obj); 202 normalObjectSet.add(obj); 203 break; 204 205 case 4: 206 obj = new ElementWidget(this); 207 obj.fillRandom(); 208 normalSet.add(obj); 209 normalObjectSet.add(obj); 210 inverseSet.add(obj); 211 ++numElementWidgets; 212 break; 213 214 case 5: 215 obj = new FloatWidget(); 216 obj.fillRandom(); 217 normalSet.add(obj); 218 normalObjectSet.add(obj); 219 break; 220 221 case 6: 222 obj = new SetWidget(); 223 obj.fillRandom(); 224 normalSet.add(obj); 225 normalObjectSet.add(obj); 226 break; 227 } 228 } 229 230 validate(); 231 } 232 233 234 private void validate() 235 { 236 Assert.assertEquals("numWidgets != normalSet.size(): " + this, numWidgets, normalSet.size()); 237 Assert.assertEquals("numWidgets != normalObjectSet.size(): " + this, numWidgets, normalObjectSet.size()); 238 Assert.assertEquals("numElementWidgets != inverseSet.size(): " + this, numElementWidgets, inverseSet.size()); 239 Assert.assertTrue("normalSet does not contain all elements of inverseSet: " + this, normalSet.containsAll(inverseSet)); 240 Assert.assertTrue("normalObjectSet does not contain all elements of inverseSet: " + this, normalObjectSet.containsAll(inverseSet)); 241 } 242 243 244 255 256 public boolean compareTo(Object obj) 257 { 258 validate(); 259 260 if (obj == this) 261 return true; 262 263 if (!(obj instanceof SetWidget) || !super.compareTo(obj)) 264 return false; 265 266 SetWidget w = (SetWidget)obj; 267 268 w.validate(); 269 270 return compareCollection(normalSet, w.normalSet) && 271 compareCollection(normalObjectSet, w.normalObjectSet) && 272 compareCollection(inverseSet, w.inverseSet); 273 } 274 275 276 282 283 public String toString() 284 { 285 StringBuffer s = new StringBuffer (super.toString()); 286 287 s.append(" normalSet = ").append(normalSet); 288 s.append('\n'); 289 s.append(" normalObjectSet = ").append(normalObjectSet); 290 s.append('\n'); 291 s.append(" inverseSet = ").append(inverseSet); 292 s.append('\n'); 293 s.append(" numWidgets = ").append(numWidgets); 294 s.append('\n'); 295 s.append(" numElementWidgets = ").append(numElementWidgets); 296 s.append('\n'); 297 298 return s.toString(); 299 } 300 301 302 public void jdoPostLoad() {} 303 public void jdoPreClear() {} 304 public void jdoPreStore() {} 305 306 307 public void jdoPreDelete() 308 { 309 PersistenceManager myPM = JDOHelper.getPersistenceManager(this); 310 Object [] elements = normalSet.toArray(); 311 312 normalSet.clear(); 313 normalObjectSet.clear(); 314 315 myPM.deletePersistentAll(elements); 316 317 if (!inverseSet.isEmpty()) 318 throw new RuntimeException ("Elements still left in inverseSet"); 319 } 320 } 321 | Popular Tags |