1 10 11 package com.triactive.jdo.test; 12 13 import java.util.ArrayList ; 14 import java.util.Iterator ; 15 import java.util.Map ; 16 import java.util.Random ; 17 import javax.jdo.PersistenceManager; 18 import javax.jdo.Transaction; 19 20 21 27 28 public class MapFieldTest extends PersistenceTestCase 29 { 30 private static final int NUM_TESTER_OBJECTS = 5; 31 private static final int NUM_MAP_VALUES = 5; 32 33 protected boolean schemaInitialized = false; 34 39 40 public MapFieldTest(String name) 41 { 42 super(name); 43 } 44 45 46 protected void setUp() throws Exception 47 { 48 super.setUp(); 49 50 PersistenceManager pm = pmf.getPersistenceManager(); 51 if (!schemaInitialized) 52 { 53 addClassesToSchema(new Class [] 54 { 55 Person.class, 56 MapFieldTester.class, 57 InverseMapFieldTester.class 58 } 59 ); 60 61 schemaInitialized = true; 62 } 63 64 Transaction tx = pm.currentTransaction(); 65 try 66 { 67 68 tx.begin(); 69 70 Iterator i = pm.getExtent(MapFieldTester.class, true).iterator(); 71 72 while (i.hasNext()) 73 pm.deletePersistent(i.next()); 74 75 i = pm.getExtent(InverseMapFieldTester.class, true).iterator(); 76 77 while (i.hasNext()) 78 pm.deletePersistent(i.next()); 79 80 tx.commit(); 81 } 82 finally 83 { 84 if (tx.isActive()) 85 tx.rollback(); 86 87 pm.close(); 88 } 89 } 90 91 92 public void testNormalMapFields() throws Exception 93 { 94 PersistenceManager pm = pmf.getPersistenceManager(); 95 Transaction tx = pm.currentTransaction(); 96 97 try 98 { 99 103 MapFieldTester mft; 104 Object [] ids = new Object [NUM_TESTER_OBJECTS]; 105 106 110 ArrayList [] husbands = new ArrayList [ids.length]; 111 ArrayList [] wives = new ArrayList [ids.length]; 112 113 117 ArrayList [] pHusbands = new ArrayList [ids.length]; 118 ArrayList [] pWives = new ArrayList [ids.length]; 119 120 long pNum = 0; 121 122 tx.begin(); 123 124 for (int i = 0; i < ids.length; ++i) 125 { 126 husbands[i] = new ArrayList (); 127 wives[i] = new ArrayList (); 128 pHusbands[i] = new ArrayList (); 129 pWives[i] = new ArrayList (); 130 131 mft = new MapFieldTester(); 132 133 for (int j = 0; j < NUM_MAP_VALUES; ++j) 134 { 135 Person h = new Person(pNum, "Fred #" + pNum, "Flintstone", "fred" + pNum + "@bedrock.com"); 136 ++pNum; 137 Person w = new Person(pNum, "Wilma #" + pNum, "Flintstone", "wilma" + pNum + "@bedrock.com"); 138 ++pNum; 139 140 husbands[i].add(h.clone()); 141 wives[i].add(w.clone()); 142 pHusbands[i].add(h); 143 pWives[i].add(w); 144 145 mft.addPair(h, w); 146 } 147 148 pm.makePersistent(mft); 149 ids[i] = pm.getObjectId(mft); 150 } 151 152 tx.commit(); 153 154 157 tx.begin(); 158 159 for (int i = 0; i < ids.length; ++i) 160 { 161 mft = (MapFieldTester)pm.getObjectById(ids[i], false); 162 mft.assertMapsEqual(husbands[i], wives[i]); 163 } 164 165 tx.commit(); 166 167 170 tx.begin(); 171 172 for (int i = 0; i < ids.length; ++i) 173 { 174 mft = (MapFieldTester)pm.getObjectById(ids[i], false); 175 176 for (int j = 0; j < NUM_MAP_VALUES; ++j) 177 { 178 Person h = new Person(pNum, "Barney #" + pNum, "Rubble", "barney" + pNum + "@bedrock.com"); 179 ++pNum; 180 Person w = new Person(pNum, "Betty #" + pNum, "Rubble", "betty" + pNum + "@bedrock.com"); 181 ++pNum; 182 183 husbands[i].add(h.clone()); 184 wives[i].add(w.clone()); 185 pHusbands[i].add(h); 186 pWives[i].add(w); 187 188 mft.addPair(h, w); 189 } 190 } 191 192 tx.commit(); 193 194 197 tx.begin(); 198 199 for (int i = 0; i < ids.length; ++i) 200 { 201 mft = (MapFieldTester)pm.getObjectById(ids[i], false); 202 mft.assertMapsEqual(husbands[i], wives[i]); 203 } 204 205 tx.commit(); 206 207 211 Random r = new Random (0); 212 213 tx.begin(); 214 215 for (int i = 0; i < ids.length; ++i) 216 { 217 mft = (MapFieldTester)pm.getObjectById(ids[i], false); 218 219 for (int j = 0; j < NUM_MAP_VALUES; ++j) 220 { 221 int k = r.nextInt(husbands[i].size()); 222 223 Person h = (Person)pHusbands[i].get(k); 224 Person w = (Person)pWives[i].get(k); 225 226 mft.removePair(h, w); 227 228 husbands[i].remove(k); 229 wives[i].remove(k); 230 pHusbands[i].remove(k); 231 pWives[i].remove(k); 232 } 233 } 234 235 tx.commit(); 236 237 240 tx.begin(); 241 242 for (int i = 0; i < ids.length; ++i) 243 { 244 mft = (MapFieldTester)pm.getObjectById(ids[i], false); 245 mft.assertMapsEqual(husbands[i], wives[i]); 246 } 247 248 tx.commit(); 249 250 253 tx.begin(); 254 255 for (int i = 0; i < ids.length; ++i) 256 { 257 mft = (MapFieldTester)pm.getObjectById(ids[i], false); 258 pm.deletePersistent(mft); 259 } 260 261 tx.commit(); 262 } 263 finally 264 { 265 if (tx.isActive()) 266 tx.rollback(); 267 268 pm.close(); 269 } 270 } 271 272 273 public void testInverseMapFields() throws Exception 274 { 275 PersistenceManager pm = pmf.getPersistenceManager(); 276 Transaction tx = pm.currentTransaction(); 277 278 try 279 { 280 285 InverseMapFieldTester mft; 286 Object [] ids = new Object [NUM_TESTER_OBJECTS]; 287 288 292 ArrayList [] values = new ArrayList [ids.length]; 293 294 299 ArrayList [] pValues = new ArrayList [ids.length]; 300 301 tx.begin(); 302 303 for (int i = 0; i < ids.length; ++i) 304 { 305 values[i] = new ArrayList (); 306 pValues[i] = new ArrayList (); 307 308 mft = new InverseMapFieldTester(); 309 310 for (int j = 0; j < NUM_MAP_VALUES; ++j) 311 { 312 InverseMapValue imv = new InverseMapValue("Key " + j); 313 imv.fillRandom(); 314 315 values[i].add(imv.clone()); 316 pValues[i].add(imv); 317 318 mft.addMapValue(imv); 319 } 320 321 pm.makePersistent(mft); 322 ids[i] = pm.getObjectId(mft); 323 } 324 325 tx.commit(); 326 327 330 tx.begin(); 331 332 for (int i = 0; i < ids.length; ++i) 333 { 334 mft = (InverseMapFieldTester)pm.getObjectById(ids[i], false); 335 mft.assertMapsEqual(values[i]); 336 } 337 338 tx.commit(); 339 340 343 tx.begin(); 344 345 for (int i = 0; i < ids.length; ++i) 346 { 347 mft = (InverseMapFieldTester)pm.getObjectById(ids[i], false); 348 349 for (int j = 0; j < NUM_MAP_VALUES; ++j) 350 { 351 InverseMapValue imv = new InverseMapValue("Added key " + j); 352 imv.fillRandom(); 353 354 values[i].add(imv.clone()); 355 pValues[i].add(imv); 356 357 mft.addMapValue(imv); 358 } 359 } 360 361 tx.commit(); 362 363 366 tx.begin(); 367 368 for (int i = 0; i < ids.length; ++i) 369 { 370 mft = (InverseMapFieldTester)pm.getObjectById(ids[i], false); 371 mft.assertMapsEqual(values[i]); 372 } 373 374 tx.commit(); 375 376 380 Random r = new Random (0); 381 382 tx.begin(); 383 384 for (int i = 0; i < ids.length; ++i) 385 { 386 mft = (InverseMapFieldTester)pm.getObjectById(ids[i], false); 387 388 for (int j = 0; j < NUM_MAP_VALUES; ++j) 389 { 390 int k = r.nextInt(values[i].size()); 391 392 InverseMapValue imv = (InverseMapValue)pValues[i].get(k); 393 394 mft.removeMapValue(imv); 395 396 values[i].remove(k); 397 pValues[i].remove(k); 398 } 399 } 400 401 tx.commit(); 402 403 406 tx.begin(); 407 408 for (int i = 0; i < ids.length; ++i) 409 { 410 mft = (InverseMapFieldTester)pm.getObjectById(ids[i], false); 411 mft.assertMapsEqual(values[i]); 412 } 413 414 tx.commit(); 415 416 419 tx.begin(); 420 421 for (int i = 0; i < ids.length; ++i) 422 { 423 mft = (InverseMapFieldTester)pm.getObjectById(ids[i], false); 424 pm.deletePersistent(mft); 425 } 426 427 tx.commit(); 428 } 429 finally 430 { 431 if (tx.isActive()) 432 tx.rollback(); 433 434 pm.close(); 435 } 436 } 437 } 438 | Popular Tags |