1 18 package org.objectweb.speedo.runtime.userid; 19 20 import junit.framework.Assert; 21 import org.objectweb.speedo.SpeedoTestHelper; 22 import org.objectweb.speedo.pobjects.userid.AutoIncFieldId; 23 import org.objectweb.speedo.pobjects.userid.AutoIncId; 24 import org.objectweb.speedo.pobjects.userid.BasicB; 25 import org.objectweb.speedo.pobjects.userid.IntUserId; 26 import org.objectweb.speedo.pobjects.userid.Ref2AutoIncFieldId; 27 import org.objectweb.speedo.pobjects.userid.StringLongId; 28 import org.objectweb.speedo.pobjects.userid.StringUserId; 29 import org.objectweb.speedo.pobjects.userid.InnerUserId; 30 import org.objectweb.speedo.pobjects.userid.Ref2BasicB; 31 import org.objectweb.util.monolog.api.BasicLevel; 32 33 import javax.jdo.JDOException; 34 import javax.jdo.JDOHelper; 35 import javax.jdo.PersistenceManager; 36 import java.io.File ; 37 import java.io.FileInputStream ; 38 import java.io.FileOutputStream ; 39 import java.io.ObjectInputStream ; 40 import java.io.ObjectOutputStream ; 41 import javax.jdo.Query; 42 import java.util.ArrayList ; 43 import java.util.Collection ; 44 import java.util.Iterator ; 45 46 50 public class TestUserId extends SpeedoTestHelper { 51 52 private final static String OID_FILE_NAME = "TestUserId_test1.id"; 53 54 public TestUserId(String s) { 55 super(s); 56 } 57 58 protected String getLoggerName() { 59 return LOG_NAME + "rt.userid"; 60 } 61 62 public void testGetUnexistingObject() { 63 PersistenceManager pm = pmf.getPersistenceManager(); 64 try { 65 pm.getObjectById(new StringLongId("azerty", 217), false); 66 fail("Object found !"); 67 } catch (JDOException e) { 68 } catch (Exception e) { 70 logger.log(BasicLevel.ERROR, e.getMessage(), e); 71 fail("Object found !"); 72 } finally { 73 pm.close(); 74 } 75 } 76 public void test1MakePersistent() { 77 try { 78 PersistenceManager pm = pmf.getPersistenceManager(); 79 BasicB bb = new BasicB("azerty", 217, "blabla", 456); 80 pm.makePersistent(bb); 81 StringLongId id = (StringLongId) pm.getObjectId(bb); 82 logger.log(BasicLevel.DEBUG, "UserId=" + id); 83 pm.close(); 84 Assert.assertNotNull("Null Object id", id); 85 ObjectOutputStream os = new ObjectOutputStream ( 86 new FileOutputStream (OID_FILE_NAME)); 87 os.writeObject(id); 88 os.close(); 89 } catch (Exception e) { 90 logger.log(BasicLevel.ERROR, "Exception", e); 91 fail(e.getMessage()); 92 } 93 } 94 95 public void test2LoadNDelete() { 96 try { 97 PersistenceManager pm = pmf.getPersistenceManager(); 98 ObjectInputStream is = new ObjectInputStream ( 99 new FileInputStream (OID_FILE_NAME)); 100 StringLongId id = (StringLongId) is.readObject(); 101 is.close(); 102 new File (OID_FILE_NAME).delete(); 103 BasicB bb = (BasicB) pm.getObjectById(id, true); 104 Assert.assertNotNull("Null Object", bb); 105 pm.currentTransaction().begin(); 106 pm.deletePersistent(bb); 107 pm.currentTransaction().commit(); 108 pm.close(); 109 } catch (Exception e) { 110 logger.log(BasicLevel.ERROR, "Exception", e); 111 fail(e.getMessage()); 112 } 113 } 114 115 public void testUserId1() { 116 try { 117 String id1 = "wxcvb"; 118 int id2 = 3; 119 String f1 = "sdgh"; 120 int f2 = 68746; 121 122 PersistenceManager pm = pmf.getPersistenceManager(); 123 BasicB bb = new BasicB(id1, id2, f1, f2); 124 pm.makePersistent(bb); 125 StringLongId id = (StringLongId) pm.getObjectId(bb); 126 logger.log(BasicLevel.DEBUG, "UserId=" + id); 127 Assert.assertNotNull("identifier is null", id); 128 String strid = id.toString(); 129 bb = null; 130 pm.close(); 131 132 pm = pmf.getPersistenceManager(); 133 Object o = pm.newObjectIdInstance(BasicB.class, strid); 134 bb = (BasicB) pm.getObjectById(o, false); 135 Assert.assertNotNull("returned object is null", bb); 136 Assert.assertEquals("Bad field 'id1' value", id1, bb.getId1()); 137 Assert.assertEquals("Bad field 'di2' value", id2, bb.getId2()); 138 Assert.assertEquals("Bad field 'f1' value", f1, bb.readF1()); 139 Assert.assertEquals("Bad field 'f1' value", f2, bb.readF2()); 140 141 Query q = pm.newQuery(BasicB.class); 142 Iterator it = ((Collection ) q.execute()).iterator(); 143 while(it.hasNext()) { 144 BasicB _bb = (BasicB) it.next(); 145 StringLongId oid = (StringLongId) pm.getObjectId(_bb); 146 assertTrue(_bb == pm.getObjectById(oid, false)); 147 } 148 q.closeAll(); 149 150 pm.currentTransaction().begin(); 151 pm.deletePersistent(bb); 152 pm.currentTransaction().commit(); 153 pm.close(); 154 } catch (Exception e) { 155 logger.log(BasicLevel.ERROR, "Exception", e); 156 fail(e.getMessage()); 157 } 158 } 159 160 public void testUserId2() { 161 try { 162 String id1 = "querty"; 163 int id2 = 12000; 164 String f1 = "blabla"; 165 int f2 = 456; 166 167 PersistenceManager pm = pmf.getPersistenceManager(); 168 BasicB bb = new BasicB(id1, id2, f1, f2); 169 pm.makePersistent(bb); 170 StringLongId id = (StringLongId) pm.getObjectId(bb); 171 logger.log(BasicLevel.DEBUG, "UserId=" + id); 172 Assert.assertNotNull("identifier is null", id); 173 bb = null; 174 pm.close(); 175 176 pm = pmf.getPersistenceManager(); 177 id = new StringLongId(id1, id2); 178 bb = (BasicB) pm.getObjectById(id, false); 179 Assert.assertNotNull("returned object is null", bb); 180 Assert.assertEquals("Bad field 'id1' value", id1, bb.getId1()); 181 Assert.assertEquals("Bad field 'di2' value", id2, bb.getId2()); 182 Assert.assertEquals("Bad field 'f1' value", f1, bb.readF1()); 183 Assert.assertEquals("Bad field 'f1' value", f2, bb.readF2()); 184 pm.currentTransaction().begin(); 185 pm.deletePersistent(bb); 186 pm.currentTransaction().commit(); 187 pm.close(); 188 } catch (Exception e) { 189 logger.log(BasicLevel.ERROR, "Exception", e); 190 fail(e.getMessage()); 191 } 192 } 193 194 public void testInnerUserId() { 195 try { 196 String f1 = "testInnerUserId"; 197 int f2 = 12; 198 InnerUserId.Oid iuio = new InnerUserId.Oid(0,0); 199 PersistenceManager pm = pmf.getPersistenceManager(); 200 InnerUserId iui = new InnerUserId(f1, f2, iuio); 201 pm.makePersistent(iui); 202 InnerUserId.Oid iuio2 = (InnerUserId.Oid) pm.getObjectId(iui); 203 logger.log(BasicLevel.DEBUG, "UserId=" + iuio2); 204 Assert.assertNotNull("identifier is null", iuio2); 205 iui = null; 206 pm.close(); 207 208 pm = pmf.getPersistenceManager(); 209 iuio2 = new InnerUserId.Oid(0, 0); 210 iui = (InnerUserId) pm.getObjectById(iuio2, false); 211 Assert.assertNotNull("returned object is null", iui); 212 Assert.assertEquals("Bad field 'oid1' value", iuio.oid1, iui.getOid1()); 213 Assert.assertEquals("Bad field 'oid2' value", iuio.oid2, iui.getOid2()); 214 Assert.assertEquals("Bad field 'f1' value", f1, iui.getF1()); 215 Assert.assertEquals("Bad field 'f2' value", f2, iui.getF2()); 216 pm.currentTransaction().begin(); 217 pm.deletePersistent(iui); 218 pm.currentTransaction().commit(); 219 pm.close(); 220 } catch (Exception e) { 221 logger.log(BasicLevel.ERROR, "Exception", e); 222 fail(e.getMessage()); 223 } 224 } 225 226 public void testStringId() { 227 try { 228 String name = "testStringId1"; 229 int f1 = 12; 230 Object id; 231 String strid; 232 PersistenceManager pm = pmf.getPersistenceManager(); 233 StringUserId sui = new StringUserId(name, f1); 234 pm.makePersistent(sui); 235 id = pm.getObjectId(sui); 236 Assert.assertNotNull("identifier is null", id); 237 strid = id.toString(); 238 id = null; 239 pm.close(); 240 sui = null; pm = pmf.getPersistenceManager(); 242 id = pm.newObjectIdInstance(StringUserId.class, strid); 243 sui = (StringUserId) pm.getObjectById(id, false); 244 Assert.assertNotNull("returned object is null", sui); 245 Assert.assertEquals("Bad field 'name' value", name, sui.getName()); 246 Assert.assertEquals("Bad field 'f1' value", f1, sui.getF1()); 247 pm.currentTransaction().begin(); 248 pm.deletePersistent(sui); 249 pm.currentTransaction().commit(); 250 pm.close(); 251 } catch (Exception e) { 252 logger.log(BasicLevel.ERROR, "Exception", e); 253 fail(e.getMessage()); 254 } 255 } 256 257 public void testStringId2() { 258 try { 259 String name = "testStringId2"; 260 int f1 = 12; 261 Object id; 262 PersistenceManager pm = pmf.getPersistenceManager(); 263 StringUserId sui = new StringUserId(name, f1); 264 pm.makePersistent(sui); 265 id = pm.getObjectId(sui); 266 Assert.assertNotNull("identifier is null", id); 267 id = null; 268 pm.close(); 269 sui = null; pm = pmf.getPersistenceManager(); 271 id = pm.newObjectIdInstance(StringUserId.class, name); 272 sui = (StringUserId) pm.getObjectById(id, false); 273 Assert.assertNotNull("returned object is null", sui); 274 Assert.assertEquals("Bad field 'name' value", name, sui.getName()); 275 Assert.assertEquals("Bad field 'f1' value", f1, sui.getF1()); 276 pm.currentTransaction().begin(); 277 pm.deletePersistent(sui); 278 pm.currentTransaction().commit(); 279 pm.close(); 280 } catch (Exception e) { 281 logger.log(BasicLevel.ERROR, "Exception", e); 282 fail(e.getMessage()); 283 } 284 } 285 286 public void testIntId() { 287 try { 288 String f1 = "testInt"; 289 int name = 13; 290 Object id; 291 PersistenceManager pm = pmf.getPersistenceManager(); 292 IntUserId iui = new IntUserId(name, f1); 293 pm.makePersistent(iui); 294 id = pm.getObjectId(iui); 295 Assert.assertNotNull("identifier is null", id); 296 pm.close(); 297 iui = null; pm = pmf.getPersistenceManager(); 299 id = pm.newObjectIdInstance(IntUserId.class, id.toString()); 300 Assert.assertNotNull("built identifier is null", id); 301 iui = (IntUserId) pm.getObjectById(id, false); 302 Assert.assertNotNull("returned object is null", iui); 303 Assert.assertEquals("Bad field 'name' value", name, iui.getName()); 304 Assert.assertEquals("Bad field 'f1' value", f1, iui.getF1()); 305 pm.currentTransaction().begin(); 306 pm.deletePersistent(iui); 307 pm.currentTransaction().commit(); 308 pm.close(); 309 } catch (Exception e) { 310 logger.log(BasicLevel.ERROR, "Exception", e); 311 fail(e.getMessage()); 312 } 313 } 314 315 public void testRdbSequence1() { 316 try { 317 PersistenceManager pm = pmf.getPersistenceManager(); 318 AutoIncId aii = new AutoIncId(); 319 pm.makePersistent(aii); 320 Object id = pm.getObjectId(aii); 321 logger.log(BasicLevel.DEBUG, "id(AutoIncId): " +id); 322 Assert.assertNotNull("identifier is null", id); 323 pm.close(); 324 pm = pmf.getPersistenceManager(); 325 id = pm.newObjectIdInstance(AutoIncId.class, id.toString()); 326 Assert.assertNotNull("built identifier is null", id); 327 aii = (AutoIncId) pm.getObjectById(id, false); 328 Assert.assertNotNull("returned object is null", aii); 329 pm.currentTransaction().begin(); 330 pm.deletePersistent(aii); 331 pm.currentTransaction().commit(); 332 pm.close(); 333 } catch (Exception e) { 334 logger.log(BasicLevel.ERROR, "Exception", e); 335 fail(e.getMessage()); 336 } 337 } 338 339 public void testRdbSequence2() { 340 try { 341 PersistenceManager pm = pmf.getPersistenceManager(); 342 AutoIncFieldId aifi = new AutoIncFieldId(); 343 pm.makePersistent(aifi); 344 long id = aifi.getId(); 345 logger.log(BasicLevel.DEBUG, "id(AutoIncId): " +id); 346 pm.close(); 347 348 aifi = null; 349 pm = pmf.getPersistenceManager(); 350 pm.evictAll(); 351 pm.close(); 352 353 pm = pmf.getPersistenceManager(); 354 Object oid = pm.newObjectIdInstance(AutoIncFieldId.class, "" + id); 355 Assert.assertNotNull("built identifier is null", oid); 356 aifi = (AutoIncFieldId) pm.getObjectById(oid, false); 357 Assert.assertNotNull("returned object is null", aifi); 358 pm.currentTransaction().begin(); 359 pm.deletePersistent(aifi); 360 pm.currentTransaction().commit(); 361 pm.close(); 362 } catch (Exception e) { 363 logger.log(BasicLevel.ERROR, "Exception", e); 364 fail(e.getMessage()); 365 } 366 } 367 368 public void testRdbSequenceUserField() { 369 try { 370 PersistenceManager pm = pmf.getPersistenceManager(); 371 AutoIncFieldId aifi = new AutoIncFieldId(); 372 Assert.assertEquals("Bad id value before 'makePersistent'", -1, aifi.getId()); 373 pm.makePersistent(aifi); 374 long lid = aifi.getId(); 375 Assert.assertTrue("Bad id value after 'makePersistent': " + lid, 0 < lid); 376 logger.log(BasicLevel.DEBUG, "long id: " +lid); 377 Object id = pm.getObjectId(aifi); 378 logger.log(BasicLevel.DEBUG, "id(AutoIncId): " +id); 379 Assert.assertNotNull("identifier is null", id); 380 pm.close(); 381 382 pm = pmf.getPersistenceManager(); 383 id = pm.newObjectIdInstance(AutoIncFieldId.class, id.toString()); 384 Assert.assertNotNull("built identifier is null (toString)", id); 385 aifi = (AutoIncFieldId) pm.getObjectById(id, false); 386 Assert.assertNotNull("returned object is null (toString)", aifi); 387 pm.close(); 388 389 pm = pmf.getPersistenceManager(); 390 id = pm.newObjectIdInstance(AutoIncFieldId.class, "" + lid); 391 Assert.assertNotNull("built identifier is null (long value)", id); 392 aifi = (AutoIncFieldId) pm.getObjectById(id, false); 393 Assert.assertNotNull("returned object is null (long value)", aifi); 394 pm.currentTransaction().begin(); 395 pm.deletePersistent(aifi); 396 pm.currentTransaction().commit(); 397 pm.close(); 398 } catch (Exception e) { 399 logger.log(BasicLevel.ERROR, "Exception", e); 400 fail(e.getMessage()); 401 } 402 } 403 404 public void testRdbSequenceRefToUserField() { 405 try { 406 PersistenceManager pm = pmf.getPersistenceManager(); 407 pm.currentTransaction().begin(); 408 ArrayList al = new ArrayList (); 409 al.add(new AutoIncFieldId()); 410 al.add(new AutoIncFieldId()); 411 Ref2AutoIncFieldId ref = new Ref2AutoIncFieldId(new AutoIncFieldId(), al); 412 pm.makePersistent(ref); 413 pm.currentTransaction().commit(); 414 pm.currentTransaction().begin(); 415 pm.deletePersistentAll(al); 416 pm.deletePersistent(ref.getSimpleRef()); 417 pm.deletePersistent(ref); 418 pm.currentTransaction().commit(); 419 pm.close(); 420 } catch (Exception e) { 421 logger.log(BasicLevel.ERROR, "Exception", e); 422 fail(e.getMessage()); 423 } 424 } 425 public void testRef2BasicB() { 426 PersistenceManager pm = pmf.getPersistenceManager(); 427 pm.currentTransaction().begin(); 428 final String r2bb_id = "testRef2BasicB_r2bb_id"; 429 final String bb_id = "testRef2BasicB_bb_id"; 430 BasicB bb = new BasicB(bb_id, 1); 431 Ref2BasicB r2bb = new Ref2BasicB(r2bb_id); 432 pm.makePersistent(r2bb); 433 r2bb.setBasicb(bb); 434 pm.getObjectId(bb); 435 pm.currentTransaction().commit(); 436 pm.evict(r2bb); 437 pm.evict(bb); 438 pm.close(); 439 440 pm = pmf.getPersistenceManager(); 441 pm.currentTransaction().begin(); 442 Object o = pm.getObjectById( 443 pm.newObjectIdInstance(Ref2BasicB.class, r2bb_id), false); 444 assertTrue("Bad object ", o==r2bb); 445 pm.deletePersistent(r2bb.getBasicb()); 446 pm.deletePersistent(r2bb); 447 pm.currentTransaction().commit(); 448 pm.close(); 449 } 450 451 public void testTwiceMakePersistent() { 452 try { 453 String name = "testTwiceMakePersistent"; 454 String strid; 455 PersistenceManager pm = pmf.getPersistenceManager(); 456 StringUserId sui = new StringUserId(name, 123456); 457 pm.currentTransaction().begin(); 458 pm.makePersistent(sui); 459 pm.currentTransaction().commit(); 460 461 StringUserId sui2 = new StringUserId(name, 654321); 462 pm.currentTransaction().begin(); 463 try { 464 pm.makePersistent(sui2); 465 fail("Same identifier not detected"); 466 } catch (JDOException e) { 467 assertTrue("Object becomes persistent", !JDOHelper.isPersistent(sui2)); 468 assertEquals("Bad f1 value in non peristent object", 654321, sui2.getF1()); 469 } 470 pm.currentTransaction().commit(); 471 472 sui = null; 473 pm.evictAll(); 474 pm.currentTransaction().begin(); 475 try { 476 pm.makePersistent(sui2); 477 pm.currentTransaction().commit(); 478 fail("Same identifier not detected with empty cache"); 479 } catch (JDOException e) { 480 if (pm.currentTransaction().isActive()) { 481 pm.currentTransaction().rollback(); 482 } 483 assertTrue("Object becomes persistent", !JDOHelper.isPersistent(sui2)); 484 assertEquals("Bad f1 value in non peristent object with empty cache", 485 654321, sui2.getF1()); 486 } 487 488 pm.currentTransaction().begin(); 489 sui = (StringUserId) pm.getObjectById( 490 pm.newObjectIdInstance(StringUserId.class, name), false); 491 pm.deletePersistent(sui); 492 pm.currentTransaction().commit(); 493 pm.close(); 494 } catch (Exception e) { 495 logger.log(BasicLevel.ERROR, "Exception", e); 496 fail(e.getMessage()); 497 } 498 499 } 500 } 501 | Popular Tags |