1 package org.apache.ojb.odmg; 2 3 import java.util.ArrayList ; 4 import java.util.Collection ; 5 import java.util.Iterator ; 6 7 import org.apache.ojb.junit.ODMGTestCase; 8 import org.odmg.DSet; 9 import org.odmg.OQLQuery; 10 import org.odmg.Transaction; 11 12 15 public class DSetTest extends ODMGTestCase 16 { 17 public static void main(String [] args) 18 { 19 String [] arr = {DSetTest.class.getName()}; 20 junit.textui.TestRunner.main(arr); 21 } 22 23 public DSetTest(String name) 24 25 { 26 super(name); 27 } 28 29 protected DListTest.DObject createObject(String name) throws Exception 30 { 31 DListTest.DObject obj = new DListTest.DObject(); 32 obj.setName(name); 33 obj.setRandomName("rnd_"+((int)(Math.random()*1000))); 34 35 return obj; 36 } 37 38 public void testAddingLockupWithTx() throws Exception 39 { 40 final String name = "testAdding_" + System.currentTimeMillis(); 42 43 TransactionExt tx = (TransactionExt) odmg.newTransaction(); 44 tx.begin(); 45 DSet list = odmg.newDSet(); 47 database.bind(list, name); 48 tx.commit(); 49 50 tx.begin(); 51 tx.getBroker().clearCache(); 52 Object obj = database.lookup(name); 53 tx.commit(); 54 assertNotNull("binded DSet not found", obj); 55 56 tx.begin(); 57 for (int i = 0; i < 5; i++) 59 { 60 DListTest.DObject a = createObject(name); 61 list.add(a); 62 } 63 tx.commit(); 64 65 Iterator iter = list.iterator(); 67 while (iter.hasNext()) 68 { 69 DListTest.DObject a = (DListTest.DObject) iter.next(); 70 assertNotNull(a); 71 } 72 73 tx.begin(); 74 tx.getBroker().clearCache(); 75 76 DSet lookedUp = (DSet) database.lookup(name); 78 assertNotNull("binded DSet not found", lookedUp); 79 80 iter = lookedUp.iterator(); 82 Iterator iter1 = list.iterator(); 83 while (iter.hasNext()) 84 { 85 DListTest.DObject a = (DListTest.DObject) iter.next(); 86 DListTest.DObject b = (DListTest.DObject) iter1.next(); 87 assertNotNull(a); 88 assertNotNull(b); 89 assertEquals(a.getId(), b.getId()); 90 } 91 tx.commit(); 92 93 tx.begin(); 95 for (int i = 0; i < 3; i++) 96 { 97 DListTest.DObject a = createObject(name + "_new_entry"); 98 list.add(a); 99 } 100 tx.commit(); 101 102 tx.begin(); 103 tx.getBroker().clearCache(); 104 lookedUp = (DSet) database.lookup(name); 105 iter = lookedUp.iterator(); 106 iter1 = list.iterator(); 107 assertEquals("Wrong number of DListEntry found", 8, list.size()); 108 while (iter.hasNext()) 109 { 110 DListTest.DObject a = (DListTest.DObject) iter.next(); 111 DListTest.DObject b = (DListTest.DObject) iter1.next(); 112 assertNotNull(a); 113 assertNotNull(b); 114 assertEquals(a.getId(), b.getId()); 115 } 116 tx.commit(); 117 assertNotNull("binded DSet not found", lookedUp); 118 } 119 120 public void testReadAndStore() throws Exception 121 { 122 final String name = "testReadAndStore_" + System.currentTimeMillis(); 124 125 Transaction tx = odmg.newTransaction(); 127 tx.begin(); 128 for (int i = 0; i < 5; i++) 130 { 131 tx.lock(createObject(name), Transaction.WRITE); 132 } 133 tx.commit(); 134 135 tx.begin(); 136 OQLQuery q = odmg.newOQLQuery(); 138 q.create("select all from "+DListTest.DObject.class.getName()+" where name=$1"); 139 q.bind(name); 140 Collection ret = (Collection ) q.execute(); 141 assertEquals(5, ret.size()); 143 for (Iterator it = ret.iterator(); it.hasNext(); ) 145 { 146 tx.lock(it.next(), Transaction.READ); 147 } 148 ArrayList result = new ArrayList (); 150 result.addAll(ret); 151 tx.commit(); 152 } 153 154 public void testIterateWithoutTx() throws Exception 155 { 156 final String name = "testAdding_" + System.currentTimeMillis(); 158 159 DSet list = odmg.newDSet(); 161 Transaction tx = odmg.newTransaction(); 162 tx.begin(); 163 for (int i = 0; i < 5; i++) 164 { 165 DListTest.DObject a = createObject(name); 166 list.add(a); 167 } 168 database.bind(list, name); 170 tx.commit(); 171 172 tx = odmg.newTransaction(); 173 tx.begin(); 174 Object obj = database.lookup(name); 175 tx.commit(); 176 assertNotNull("binded DSet not found", obj); 177 178 Iterator iter = list.iterator(); 180 while (iter.hasNext()) 181 { 182 DListTest.DObject a = (DListTest.DObject) iter.next(); 183 assertNotNull(a); 184 } 185 assertEquals(5, list.size()); 186 187 tx = odmg.newTransaction(); 188 tx.begin(); 189 ((TransactionExt) odmg.currentTransaction()).getBroker().clearCache(); 190 DSet lookedUp = (DSet) database.lookup(name); 191 tx.commit(); 192 assertNotNull("binded DSet not found", lookedUp); 193 194 iter = lookedUp.iterator(); 196 Iterator iter1 = list.iterator(); 197 while (iter.hasNext()) 198 { 199 DListTest.DObject a = (DListTest.DObject) iter.next(); 200 DListTest.DObject b = (DListTest.DObject) iter1.next(); 201 assertNotNull(a); 202 assertNotNull(b); 203 assertEquals(a.getId(), b.getId()); 204 } 205 } 206 207 210 public void testRemoving() throws Exception 211 { 212 String name = "testRemoving_" + System.currentTimeMillis(); 214 215 TransactionExt tx = (TransactionExt) odmg.newTransaction(); 216 tx.begin(); 217 DSet list = odmg.newDSet(); 218 database.bind(list, name); 220 221 Object first = null; 222 Object second = null; 223 for (int i = 0; i < 5; i++) 224 { 225 DListTest.DObject a = createObject(name); 226 list.add(a); 227 if(i==1) first = a; 228 if(i==2) second = a; 229 } 230 assertEquals(5, list.size()); 231 tx.commit(); 232 233 tx = (TransactionExt) odmg.newTransaction(); 235 tx.begin(); 236 ((HasBroker) odmg.currentTransaction()).getBroker().clearCache(); 237 DSet lookedUp = (DSet) database.lookup(name); 238 assertNotNull("database lookup does not find the named DSet", lookedUp); 239 assertEquals("Wrong number of list entries", 5, lookedUp.size()); 240 lookedUp.remove(first); 241 lookedUp.remove(second); 242 tx.commit(); 243 244 tx = (TransactionExt) odmg.newTransaction(); 246 tx.begin(); 247 tx.getBroker().clearCache(); 248 lookedUp = (DSet) database.lookup(name); 249 tx.commit(); 250 251 assertEquals(3, lookedUp.size()); 252 } 253 254 255 public void testAdding() throws Exception 256 { 257 String name = "testAdding_" + System.currentTimeMillis(); 259 260 TransactionExt tx = (TransactionExt) odmg.newTransaction(); 261 tx.begin(); 262 DSet list = odmg.newDSet(); 263 database.bind(list, name); 264 tx.commit(); 265 266 tx = (TransactionExt) odmg.newTransaction(); 267 tx.begin(); 268 for (int i = 0; i < 5; i++) 269 { 270 DListTest.DObject a = createObject(name); 271 list.add(a); 272 } 273 274 list.add(createObject(name+"_posNew1")); 275 list.add(createObject(name+"_posNew2")); 276 list.add(createObject(name+"_posNew3")); 277 tx.commit(); 278 279 tx.begin(); 280 tx.getBroker().clearCache(); 281 tx.commit(); 284 285 Iterator iter = list.iterator(); 287 DListTest.DObject a; 288 while (iter.hasNext()) 289 { 290 a = (DListTest.DObject) iter.next(); 291 assertNotNull(a); 292 } 294 assertEquals(8, list.size()); 295 296 297 tx = (TransactionExt) odmg.newTransaction(); 298 tx.begin(); 299 tx.getBroker().clearCache(); 300 DSet lookedUp = (DSet) database.lookup(name); 301 assertNotNull("database lookup does not find DSet", lookedUp); 303 assertEquals(8, lookedUp.size()); 304 iter = lookedUp.iterator(); 305 while (iter.hasNext()) 306 { 307 a = (DListTest.DObject) iter.next(); 308 } 309 tx.commit(); 310 } 311 312 public void testDSet() throws Exception 313 { 314 String name = "testDSet_" + System.currentTimeMillis(); 315 String set_1 = "set_1_" + System.currentTimeMillis(); 316 String set_2 = "set_2_" + System.currentTimeMillis(); 317 318 Transaction tx = odmg.newTransaction(); 319 tx.begin(); 320 321 DListTest.DObject a, b, c, d, e; 322 a = createObject(name); 323 b = createObject(name); 324 c = createObject(name); 325 d = createObject(name); 326 e = createObject(name); 327 328 DSet set1 = odmg.newDSet(); 329 DSet set2 = odmg.newDSet(); 330 331 set1.add(a); 332 set1.add(b); 333 set1.add(c); 334 335 set2.add(b); 336 set2.add(c); 337 set2.add(d); 338 set2.add(e); 339 340 database.bind(set1, set_1); 341 database.bind(set2, set_2); 342 tx.commit(); 343 344 tx = odmg.newTransaction(); 346 tx.begin(); 347 ((HasBroker) tx).getBroker().clearCache(); 348 DSet set1a = (DSet) database.lookup(set_1); 349 DSet set2a = (DSet) database.lookup(set_2); 350 351 assertTrue(set1a.containsAll(set1)); 353 assertTrue(set2a.containsAll(set2)); 354 355 DSet set3 = set1.difference(set2); 357 assertEquals(1, set3.size()); 358 359 set3 = set1.intersection(set2); 360 assertEquals(2, set3.size()); 361 362 set3 = set1.union(set2); 363 assertEquals(5, set3.size()); 364 365 assertTrue(set1.properSubsetOf(set3)); 366 assertTrue(set2.properSubsetOf(set3)); 367 368 assertTrue(set3.properSupersetOf(set1)); 369 assertTrue(set3.properSupersetOf(set2)); 370 371 assertTrue(!set1.properSubsetOf(set2)); 372 373 tx.commit(); 374 } 375 } 376 | Popular Tags |