1 package org.apache.ojb.odmg; 2 3 import java.util.ArrayList ; 4 import java.util.Collection ; 5 import java.util.Iterator ; 6 import java.util.List ; 7 import java.util.Collections ; 8 import java.util.Comparator ; 9 10 import org.apache.commons.lang.builder.ToStringBuilder; 11 import org.apache.commons.lang.builder.HashCodeBuilder; 12 import org.apache.commons.lang.builder.EqualsBuilder; 13 import org.apache.ojb.junit.ODMGTestCase; 14 import org.odmg.DBag; 15 import org.odmg.DList; 16 import org.odmg.OQLQuery; 17 import org.odmg.Transaction; 18 19 22 public class DListTest extends ODMGTestCase 23 { 24 public static void main(String [] args) 25 { 26 String [] arr = {DListTest.class.getName()}; 27 junit.textui.TestRunner.main(arr); 28 } 29 30 public DListTest(String name) 31 32 { 33 super(name); 34 } 35 36 protected DObject createObject(String name) throws Exception 37 { 38 DObject obj = new DObject(); 39 obj.setName(name); 40 obj.setRandomName("rnd_"+((int)(Math.random()*1000))); 41 42 return obj; 43 } 44 45 public void testAddingLockupWithTx() throws Exception 46 { 47 final String name = "testAdding_" + System.currentTimeMillis(); 49 50 TransactionExt tx = (TransactionExt) odmg.newTransaction(); 51 tx.begin(); 52 DList list = odmg.newDList(); 54 database.bind(list, name); 55 tx.commit(); 56 57 tx.begin(); 58 tx.getBroker().clearCache(); 59 Object obj = database.lookup(name); 60 tx.commit(); 61 assertNotNull("binded DList not found", obj); 62 63 tx.begin(); 64 for (int i = 0; i < 5; i++) 66 { 67 DObject a = createObject(name); 68 list.add(a); 69 } 70 tx.commit(); 71 72 Iterator iter = list.iterator(); 74 while (iter.hasNext()) 75 { 76 DObject a = (DObject) iter.next(); 77 assertNotNull(a); 78 } 79 80 tx.begin(); 81 tx.getBroker().clearCache(); 82 83 DList lookedUp = (DList) database.lookup(name); 85 assertNotNull("binded DList not found", lookedUp); 86 87 iter = lookedUp.iterator(); 89 Iterator iter1 = list.iterator(); 90 while (iter.hasNext()) 91 { 92 DObject a = (DObject) iter.next(); 93 DObject b = (DObject) iter1.next(); 94 assertNotNull(a); 95 assertNotNull(b); 96 assertEquals(a.getId(), b.getId()); 97 } 98 tx.commit(); 99 100 tx.begin(); 102 for (int i = 0; i < 3; i++) 103 { 104 DObject a = createObject(name + "_new_entry"); 105 list.add(a); 106 } 107 tx.commit(); 108 109 tx.begin(); 110 tx.getBroker().clearCache(); 111 lookedUp = (DList) database.lookup(name); 112 iter = lookedUp.iterator(); 113 iter1 = list.iterator(); 114 assertEquals("Wrong number of DListEntry found", 8, list.size()); 115 while (iter.hasNext()) 116 { 117 DObject a = (DObject) iter.next(); 118 DObject b = (DObject) iter1.next(); 119 assertNotNull(a); 120 assertNotNull(b); 121 assertEquals(a.getId(), b.getId()); 122 } 123 tx.commit(); 124 assertNotNull("binded DList not found", lookedUp); 125 } 126 127 public void testRemoveAdd() throws Exception 128 { 129 final String name = "testRemoveAdd_" + System.currentTimeMillis(); 131 132 TransactionExt tx = (TransactionExt) odmg.newTransaction(); 133 tx.begin(); 134 DList list = odmg.newDList(); 136 database.bind(list, name); 137 138 for (int i = 0; i < 5; i++) 140 { 141 DObject a = createObject(name); 142 list.add(a); 143 } 144 tx.commit(); 145 146 Iterator iter = list.iterator(); 148 while (iter.hasNext()) 149 { 150 DObject a = (DObject) iter.next(); 151 assertNotNull(a); 152 } 153 154 tx.begin(); 155 tx.getBroker().clearCache(); 156 157 DList lookedUp = (DList) database.lookup(name); 159 assertNotNull("binded DList not found", lookedUp); 160 161 iter = lookedUp.iterator(); 163 Iterator iter1 = list.iterator(); 164 while (iter.hasNext()) 165 { 166 DObject a = (DObject) iter.next(); 167 DObject b = (DObject) iter1.next(); 168 assertNotNull(a); 169 assertNotNull(b); 170 assertEquals(a.getId(), b.getId()); 171 } 172 tx.commit(); 173 174 tx.begin(); 176 for (int i = 0; i < 3; i++) 177 { 178 DObject a = createObject(name + "_new_entry_NOT_PERSIST"); 179 list.add(a); 180 list.remove(list.size()-1); 181 } 182 tx.commit(); 183 184 185 tx.begin(); 186 tx.getBroker().clearCache(); 187 lookedUp = (DList) database.lookup(name); 188 iter = lookedUp.iterator(); 189 iter1 = list.iterator(); 190 assertEquals("Wrong number of DListEntry found", 5, list.size()); 191 while (iter.hasNext()) 192 { 193 DObject a = (DObject) iter.next(); 194 DObject b = (DObject) iter1.next(); 195 assertNotNull(a); 196 assertNotNull(b); 197 assertEquals(a.getId(), b.getId()); 198 } 199 tx.commit(); 200 assertNotNull("binded DList not found", lookedUp); 201 202 203 tx.begin(); 204 for (int i = 0; i < 3; i++) 205 { 206 DObject a = createObject(name + "_new_entry_new_persist"); 207 list.add(a); 208 list.remove(0); 209 } 210 tx.commit(); 211 212 tx.begin(); 213 tx.getBroker().clearCache(); 214 lookedUp = (DList) database.lookup(name); 215 iter = lookedUp.iterator(); 216 iter1 = list.iterator(); 217 assertEquals("Wrong number of DListEntry found", 5, list.size()); 218 while (iter.hasNext()) 219 { 220 DObject a = (DObject) iter.next(); 221 DObject b = (DObject) iter1.next(); 222 assertNotNull(a); 223 assertNotNull(b); 224 assertEquals(a.getId(), b.getId()); 225 } 226 tx.commit(); 227 assertNotNull("binded DList not found", lookedUp); 228 } 229 230 public void testReadAndStore() throws Exception 231 { 232 final String name = "testReadAndStore_" + System.currentTimeMillis(); 234 235 Transaction tx = odmg.newTransaction(); 237 tx.begin(); 238 for (int i = 0; i < 5; i++) 240 { 241 tx.lock(createObject(name), Transaction.WRITE); 242 } 243 tx.commit(); 244 245 tx.begin(); 246 OQLQuery q = odmg.newOQLQuery(); 248 q.create("select all from "+DObject.class.getName()+" where name=$1"); 249 q.bind(name); 250 Collection ret = (Collection ) q.execute(); 251 assertEquals(5, ret.size()); 253 for (Iterator it = ret.iterator(); it.hasNext(); ) 255 { 256 tx.lock(it.next(), Transaction.READ); 257 } 258 ArrayList result = new ArrayList (); 260 result.addAll(ret); 261 tx.commit(); 262 } 263 264 public void testIterateWithoutTx() throws Exception 265 { 266 final String name = "testAdding_" + System.currentTimeMillis(); 268 269 List list = odmg.newDList(); 271 TransactionExt tx = (TransactionExt) odmg.newTransaction(); 272 tx.begin(); 273 for (int i = 0; i < 5; i++) 274 { 275 DObject a = createObject(name); 276 list.add(a); 277 } 278 database.bind(list, name); 280 tx.commit(); 281 282 tx = (TransactionExt) odmg.newTransaction(); 283 tx.begin(); 284 Object obj = database.lookup(name); 285 tx.commit(); 286 assertNotNull("binded DList not found", obj); 287 288 Iterator iter = list.iterator(); 290 while (iter.hasNext()) 291 { 292 DObject a = (DObject) iter.next(); 293 assertNotNull(a); 294 } 295 assertEquals(5, list.size()); 296 297 tx = (TransactionExt) odmg.newTransaction(); 298 tx.begin(); 299 tx.getBroker().clearCache(); 300 301 List lookedUp = (List ) database.lookup(name); 302 tx.commit(); 303 assertNotNull("binded DList not found", lookedUp); 304 305 list = new ArrayList (list); 307 lookedUp = new ArrayList (lookedUp); 308 309 Collections.sort(list, new Comparator (){ 310 public int compare(Object o1, Object o2) 311 { 312 DObject d1 = (DObject) o1; 313 DObject d2 = (DObject) o2; 314 return d1.getId().compareTo(d2.getId()); 315 } 316 }); 317 318 Collections.sort(lookedUp, new Comparator (){ 319 public int compare(Object o1, Object o2) 320 { 321 DObject d1 = (DObject) o1; 322 DObject d2 = (DObject) o2; 323 return d1.getId().compareTo(d2.getId()); 324 } 325 }); 326 327 assertEquals(list.size(), lookedUp.size()); 328 329 for(int i = 0; i < lookedUp.size(); i++) 330 { 331 DObject a = (DObject) lookedUp.get(i); 332 DObject aa = (DObject) list.get(i); 333 assertNotNull(a); 334 assertNotNull(aa); 335 assertEquals(a.getId(), aa.getId()); 336 } 337 } 338 339 342 public void testRemoving() throws Exception 343 { 344 String name = "testRemoving_" + System.currentTimeMillis(); 346 347 Transaction tx = odmg.newTransaction(); 348 tx.begin(); 349 DList list = odmg.newDList(); 350 database.bind(list, name); 352 353 for (int i = 0; i < 5; i++) 354 { 355 DObject a = createObject(name); 356 list.add(a); 357 } 358 assertEquals(5, list.size()); 359 tx.commit(); 360 361 tx = odmg.newTransaction(); 363 tx.begin(); 364 ((HasBroker) odmg.currentTransaction()).getBroker().clearCache(); 365 DList lookedUp = (DList) database.lookup(name); 366 assertNotNull("database lookup does not find the named DList", lookedUp); 367 assertEquals("Wrong number of list entries", 5, lookedUp.size()); 368 lookedUp.remove(2); 369 lookedUp.remove(1); 370 tx.commit(); 371 372 tx = odmg.newTransaction(); 374 tx.begin(); 375 ((HasBroker) odmg.currentTransaction()).getBroker().clearCache(); 376 lookedUp = (DList) database.lookup(name); 377 tx.commit(); 378 379 assertEquals(3, lookedUp.size()); 380 } 381 382 383 public void testAddingWithIndex() throws Exception 384 { 385 String name = "testAddingWithIndex_" + System.currentTimeMillis(); 387 388 Transaction tx = odmg.newTransaction(); 389 tx.begin(); 390 DList list = odmg.newDList(); 391 database.bind(list, name); 392 tx.commit(); 393 394 tx = odmg.newTransaction(); 395 tx.begin(); 396 for (int i = 0; i < 5; i++) 397 { 398 DObject a = createObject(name); 399 list.add(a); 400 } 401 402 list.add(2, createObject(name+"_pos2")); 403 list.add(0, createObject(name+"_pos0")); 404 list.add(7, createObject(name+"_pos7")); 405 tx.commit(); 406 407 tx.begin(); 408 ((TransactionImpl) tx).getBroker().clearCache(); 409 tx.commit(); 412 413 Iterator iter = list.iterator(); 415 DObject a; 416 while (iter.hasNext()) 417 { 418 a = (DObject) iter.next(); 419 assertNotNull(a); 420 } 422 423 424 tx = odmg.newTransaction(); 425 tx.begin(); 426 ((TransactionImpl) tx).getBroker().clearCache(); 427 DList lookedUp = (DList) database.lookup(name); 428 assertNotNull("database lookup does not find DList", lookedUp); 430 assertEquals(8, lookedUp.size()); 431 iter = lookedUp.iterator(); 432 while (iter.hasNext()) 433 { 434 a = (DObject) iter.next(); 435 } 436 tx.commit(); 437 } 438 439 public void testDBag() throws Exception 440 { 441 String name = "testDBag_" + System.currentTimeMillis(); 442 443 Transaction tx = odmg.newTransaction(); 444 tx.begin(); 445 DBag bag1 = odmg.newDBag(); 446 DBag bag2 = odmg.newDBag(); 447 DObject a, b, c, d, e; 448 a = createObject(name); 449 b = createObject(name); 450 c = createObject(name); 451 d = createObject(name); 452 e = createObject(name); 453 bag1.add(a); 454 bag1.add(b); 455 bag1.add(c); 456 bag2.add(b); 457 bag2.add(c); 458 bag2.add(d); 459 bag2.add(e); 460 DBag bag3 = bag1.difference(bag2); 461 assertEquals("should contain only 1 element", 1, bag3.size()); 462 463 bag3 = bag1.intersection(bag2); 464 assertEquals("should contain two elements", 2, bag3.size()); 465 466 tx.commit(); 467 } 468 469 public static class DObject 470 { 471 Integer id; 472 String name; 473 String randomName; 474 475 public DObject() 476 { 477 } 478 479 public boolean equals(Object obj) 480 { 481 if(obj instanceof DObject) 482 { 483 DObject target = ((DObject)obj); 484 return new EqualsBuilder() 485 .append(id, target.getId()) 486 .append(name, target.getName()) 487 .append(randomName, target.getRandomName()) 488 .isEquals(); 489 } 490 else 491 { 492 return false; 493 } 494 } 495 496 public int hashCode() 497 { 498 return new HashCodeBuilder().append(id).append(name).append(randomName).hashCode(); 499 } 500 501 public String toString() 502 { 503 ToStringBuilder buf = new ToStringBuilder(this); 504 buf.append("id", id); 505 buf.append("name", name); 506 buf.append("randonName", randomName); 507 return buf.toString(); 508 } 509 510 public Integer getId() 511 { 512 return id; 513 } 514 515 public void setId(Integer id) 516 { 517 this.id = id; 518 } 519 520 public String getName() 521 { 522 return name; 523 } 524 525 public void setName(String name) 526 { 527 this.name = name; 528 } 529 530 public String getRandomName() 531 { 532 return randomName; 533 } 534 535 public void setRandomName(String randomName) 536 { 537 this.randomName = randomName; 538 } 539 } 540 } 541 | Popular Tags |