1 10 11 package com.triactive.jdo.test; 12 13 import java.util.HashMap ; 14 import java.util.Iterator ; 15 import java.util.Map ; 16 import java.util.Map.Entry; 17 import javax.jdo.InstanceCallbacks; 18 import javax.jdo.JDOHelper; 19 import javax.jdo.PersistenceManager; 20 import junit.framework.Assert; 21 22 23 public class MapWidget extends Widget implements InstanceCallbacks 24 { 25 private Map normalMap = new HashMap (); 26 private Map normalObjectMap = new HashMap (); 27 private Map inverseMap = new HashMap (); 28 private int numWidgets = 0; 29 private int numValueWidgets = 0; 30 31 32 public MapWidget() 33 { 34 } 35 36 37 public Map getNormalMap() 38 { 39 return normalMap; 40 } 41 42 43 public Map getNormalObjectMap() 44 { 45 return normalObjectMap; 46 } 47 48 49 public Map getInverseMap() 50 { 51 return inverseMap; 52 } 53 54 55 public int getNumWidgets() 56 { 57 return numWidgets; 58 } 59 60 61 public int getNumValueWidgets() 62 { 63 return numValueWidgets; 64 } 65 66 67 public Object [] getSCOFieldValues() 68 { 69 return new Object [] { normalMap, normalObjectMap, inverseMap }; 70 } 71 72 73 public Object clone() 74 { 75 MapWidget mw = (MapWidget)super.clone(); 76 77 78 mw.normalMap = new HashMap (); 79 mw.normalObjectMap = new HashMap (); 80 mw.inverseMap = new HashMap (); 81 82 Iterator i = normalMap.entrySet().iterator(); 83 84 while (i.hasNext()) 85 { 86 Entry e = (Entry)i.next(); 87 String ks = (String )e.getKey(); 88 Widget v = (Widget)clone(e.getValue()); 89 90 Integer ki = new Integer (v.getIntField()); 91 92 mw.normalMap.put(ks, v); 93 mw.normalObjectMap.put(ki, v); 94 95 if (v instanceof ValueWidget) 96 { 97 ValueWidget vw = (ValueWidget)v; 98 99 vw.setOwner(mw); 100 mw.inverseMap.put(ki, vw); 101 } 102 } 103 104 return mw; 105 } 106 107 108 111 112 public void fillRandom() 113 { 114 fillRandom(r.nextInt(5)); 115 } 116 117 118 public void fillRandom(int numWidgets) 119 { 120 fillRandom(numWidgets, false); 121 } 122 123 124 128 129 public void fillRandom(int numWidgets, boolean includeMapWidgets) 130 { 131 super.fillRandom(); 132 133 136 Iterator i = normalMap.entrySet().iterator(); 137 138 while (i.hasNext()) 139 { 140 Entry e = (Entry)i.next(); 141 Object skey = e.getKey(); 142 Widget val = (Widget)e.getValue(); 143 144 i.remove(); 145 146 Integer ikey = new Integer (val.getIntField()); 147 148 Assert.assertFalse("normalMap.containsKey() did not return false after removing existing key", normalMap.containsKey(skey)); 149 Assert.assertTrue("normalObjectMap.remove() did not return non-null after removing existing key", normalObjectMap.remove(ikey) != null); 150 Assert.assertTrue("normalObjectMap.remove() did not return null attempting to remove non-existent key", normalObjectMap.remove(ikey) == null); 151 152 if (JDOHelper.isPersistent(this)) 153 { 154 JDOHelper.getPersistenceManager(this).deletePersistent(val); 155 156 Assert.assertFalse("inverseMap should not contain deleted value", inverseMap.containsValue(val)); 157 } 158 else if (val instanceof ValueWidget) 159 Assert.assertTrue("inverseMap.remove() did not return non-null after removing existing key", inverseMap.remove(ikey) != null); 160 } 161 162 Assert.assertTrue("normalMap should have been empty", normalMap.isEmpty()); 163 Assert.assertTrue("normalObjectMap should have been empty", normalObjectMap.isEmpty()); 164 Assert.assertTrue("inverseMap should have been empty", inverseMap.isEmpty()); 165 166 171 this.numWidgets = numWidgets; 172 numValueWidgets = 0; 173 174 while (numWidgets-- > 0) 175 { 176 Widget val; 177 178 switch (r.nextInt(includeMapWidgets ? 7 : 6)) 179 { 180 case 0: 181 default: 182 val = new Widget(); 183 break; 184 case 1: 185 val = new DateWidget(); 186 break; 187 case 2: 188 val = new StringWidget(); 189 break; 190 case 3: 191 val = new BinaryWidget(); 192 break; 193 case 4: 194 val = new ValueWidget(); 195 break; 196 case 5: 197 val = new FloatWidget(); 198 break; 199 case 6: 200 val = new MapWidget(); 201 break; 202 } 203 204 Integer key; 205 206 do 207 { 208 val.fillRandom(); 209 key = new Integer (val.getIntField()); 210 } while (normalObjectMap.containsKey(key)); 211 212 normalMap.put("K" + key, val); 213 normalObjectMap.put(key, val); 214 215 if (val instanceof ValueWidget) 216 { 217 inverseMap.put(key, val); 218 219 if (!JDOHelper.isPersistent(this)) 220 ((ValueWidget)val).setKey(key); 221 222 ++numValueWidgets; 223 } 224 } 225 226 validate(); 227 } 228 229 230 private void validate() 231 { 232 Assert.assertEquals("numWidgets != normalMap.size(): " + this, numWidgets, normalMap.size()); 233 Assert.assertEquals("numWidgets != normalObjectMap.size(): " + this, numWidgets, normalObjectMap.size()); 234 Assert.assertEquals("numValueWidgets != inverseMap.size(): " + this, numValueWidgets, inverseMap.size()); 235 Assert.assertTrue("normalMap does not contain all values of inverseMap: " + this, normalMap.values().containsAll(inverseMap.values())); 236 Assert.assertTrue("normalObjectMap does not contain all values of inverseMap: " + this, normalObjectMap.values().containsAll(inverseMap.values())); 237 } 238 239 240 251 252 public boolean compareTo(Object obj) 253 { 254 validate(); 255 256 if (obj == this) 257 return true; 258 259 if (!(obj instanceof MapWidget) || !super.compareTo(obj)) 260 return false; 261 262 MapWidget w = (MapWidget)obj; 263 264 w.validate(); 265 266 return compareMap(normalMap, w.normalMap) && 267 compareMap(normalObjectMap, w.normalObjectMap) && 268 compareMap(inverseMap, w.inverseMap); 269 } 270 271 272 278 279 public String toString() 280 { 281 StringBuffer s = new StringBuffer (super.toString()); 282 283 s.append(" normalMap = ").append(normalMap); 284 s.append('\n'); 285 s.append(" normalObjectMap = ").append(normalObjectMap); 286 s.append('\n'); 287 s.append(" inverseMap = ").append(inverseMap); 288 s.append('\n'); 289 s.append(" numWidgets = ").append(numWidgets); 290 s.append('\n'); 291 s.append(" numValueWidgets = ").append(numValueWidgets); 292 s.append('\n'); 293 294 return s.toString(); 295 } 296 297 298 public void jdoPostLoad() {} 299 public void jdoPreClear() {} 300 public void jdoPreStore() {} 301 302 303 public void jdoPreDelete() 304 { 305 PersistenceManager myPM = JDOHelper.getPersistenceManager(this); 306 Object [] values = normalMap.values().toArray(); 307 308 normalMap.clear(); 309 normalObjectMap.clear(); 310 311 myPM.deletePersistentAll(values); 312 313 if (!inverseMap.isEmpty()) 314 throw new RuntimeException ("Entries still left in inverseMap"); 315 } 316 } 317 | Popular Tags |