1 5 package org.apache.ojb.odmg; 6 7 import java.util.Iterator ; 8 import java.util.Collection ; 9 import java.io.Serializable ; 10 11 import org.apache.ojb.junit.ODMGTestCase; 12 import org.odmg.ODMGException; 13 import org.odmg.OQLQuery; 14 import org.odmg.Transaction; 15 16 20 public class BidirectionalAssociationTest extends ODMGTestCase 21 { 22 public static void main(String [] args) 23 { 24 String [] arr = {BidirectionalAssociationTest.class.getName()}; 25 junit.textui.TestRunner.main(arr); 26 } 27 28 public void testDeleteA() throws Exception 29 { 30 33 createWithUpdate(); 34 deleteA(); 35 } 36 37 public void testDeleteB() throws Exception 38 { 39 42 createWithUpdate(); 43 deleteB(); 44 } 45 46 public void testCreateWitUpdate() throws Exception 47 { 48 createWithUpdate(); 49 } 50 51 54 public void createWithUpdate() throws ODMGException 55 { 56 Transaction tx = odmg.newTransaction(); 57 long currentTime = System.currentTimeMillis(); 58 59 ObjectA a = new ObjectA(); 60 a.setPk("A" + currentTime); 61 ObjectB b = new ObjectB(); 62 b.setPk("B" + currentTime); 63 64 tx.begin(); 65 database.makePersistent(a); 66 database.makePersistent(b); 67 tx.commit(); 68 69 tx.begin(); 70 tx.lock(a, Transaction.WRITE); 71 tx.lock(b, Transaction.WRITE); 72 a.setRelatedB(b); 73 b.setRelatedA(a); 74 tx.commit(); 75 76 79 tx.begin(); 80 OQLQuery query = odmg.newOQLQuery(); 81 int i = 0; 82 query.create("select bidirectionalAssociationObjectA from " + ObjectA.class.getName() + " where pk=$1"); 83 query.bind("A"+currentTime); 84 Collection all = (Collection ) query.execute(); 85 Iterator it = all.iterator(); 86 while (it.hasNext()) 87 { 88 i++; 89 a = (ObjectA) it.next(); 90 if (a.getRelatedB() == null) 91 fail("a should have had a related b"); 92 } 93 if (i > 1) 94 fail("should have found only one bidirectionalAssociationObjectA, instead found: " + i); 95 96 query = odmg.newOQLQuery(); 97 i = 0; 98 query.create("select bidirectionalAssociationObjectB from " + ObjectB.class.getName() + " where pk=$1"); 99 query.bind("B"+currentTime); 100 all = (Collection ) query.execute(); 101 it = all.iterator(); 102 while (it.hasNext()) 103 { 104 i++; 105 b = (ObjectB) it.next(); 106 if (b.getRelatedA() == null) 107 fail("b should have had a related a"); 108 109 } 110 if (i > 1) 111 fail("should have found only one bidirectionalAssociationObjectB, instead found: " + i); 112 tx.commit(); 113 } 114 115 119 public void testCreateWithoutUpdate() throws ODMGException 120 { 121 Transaction tx = odmg.newTransaction(); 122 long currentTime = System.currentTimeMillis(); 123 ObjectA a = new ObjectA(); 124 a.setPk("A" + currentTime); 125 ObjectB b = new ObjectB(); 126 b.setPk("B" + currentTime); 127 128 tx.begin(); 129 b.setRelatedA(a); 130 a.setRelatedB(b); 131 database.makePersistent(b); 134 tx.commit(); 137 138 141 tx.begin(); 142 OQLQuery query = odmg.newOQLQuery(); 143 int i = 0; 144 query.create("select bidirectionalAssociationObjectA from " + ObjectA.class.getName() + " where pk=$1"); 145 query.bind("A"+currentTime); 146 Collection all = (Collection ) query.execute(); 147 Iterator it = all.iterator(); 148 while (it.hasNext()) 149 { 150 i++; 151 it.next(); 152 } 153 if (i > 1) 154 fail("should have found only one bidirectionalAssociationObjectA, instead found: " + i); 155 156 query = odmg.newOQLQuery(); 157 i = 0; 158 query.create("select bidirectionalAssociationObjectB from " + ObjectB.class.getName() + " where pk=$1"); 159 query.bind("B"+currentTime); 160 all = (Collection ) query.execute(); 161 it = all.iterator(); 162 while (it.hasNext()) 163 { 164 i++; 165 it.next(); 166 } 167 if (i > 1) 168 fail("should have found only one bidirectionalAssociationObjectB, instead found: " + i); 169 170 } 171 172 176 public void testGetA() throws Exception 177 { 178 181 deleteA(); 182 deleteB(); 183 createWithUpdate(); 184 185 OQLQuery query = odmg.newOQLQuery(); 186 int i = 0; 187 query.create("select allA from " + ObjectA.class.getName()); 188 Transaction tx = odmg.newTransaction(); 189 tx.begin(); 190 Collection all = (Collection ) query.execute(); 191 Iterator it = all.iterator(); 192 ObjectA temp = null; 193 while (it.hasNext()) 194 { 195 temp = (ObjectA) it.next(); 196 if (temp.getRelatedB() == null) 197 fail("should have relatedB"); 198 i++; 199 } 200 tx.commit(); 201 if (i == 0) 202 fail("Should have found at least 1 bidirectionalAssociationObjectA object"); 203 } 204 205 public void testGetB() throws Exception 206 { 207 210 deleteA(); 211 deleteB(); 212 createWithUpdate(); 213 214 OQLQuery query = odmg.newOQLQuery(); 215 int i = 0; 216 query.create("select bidirectionalAssociationObjectB from " + ObjectB.class.getName()); 217 Transaction tx = odmg.newTransaction(); 218 tx.begin(); 219 Collection all = (Collection ) query.execute(); 220 Iterator it = all.iterator(); 221 ObjectB temp = null; 222 while (it.hasNext()) 223 { 224 temp = (ObjectB) it.next(); 225 if (temp.getRelatedA() == null) 226 fail("should have relatedA"); 227 i++; 228 } 229 tx.commit(); 230 if (i == 0) 231 fail("Should have found at least 1 bidirectionalAssociationObjectA object"); 232 } 233 234 238 public void deleteA() throws Exception 239 { 240 ObjectA a; 241 ObjectB b; 242 243 OQLQuery query = odmg.newOQLQuery(); 244 query.create("select bidirectionalAssociationObjectA from " + ObjectA.class.getName()); 245 TransactionExt tx = (TransactionExt) odmg.newTransaction(); 246 tx.begin(); 247 Collection all = (Collection ) query.execute(); 248 Iterator it = all.iterator(); 249 250 while (it.hasNext()) 251 { 252 a = (ObjectA)it.next(); 253 b = a.getRelatedB(); 254 if (b != null) 255 { 256 tx.lock(b, Transaction.WRITE); 257 b.setRelatedA(null); } 259 database.deletePersistent(a); 260 } 261 tx.commit(); 262 } 263 264 268 public void deleteB() throws Exception 269 { 270 ObjectA a; 271 ObjectB b; 272 273 OQLQuery query = odmg.newOQLQuery(); 274 query.create("select bidirectionalAssociationObjectB from " + ObjectB.class.getName()); 275 Transaction tx = odmg.newTransaction(); 276 tx.begin(); 277 Collection all = (Collection ) query.execute(); 278 Iterator it = all.iterator(); 279 280 while (it.hasNext()) 281 { 282 b = (ObjectB)it.next(); 283 a = b.getRelatedA(); 284 if (a != null) 285 { 286 tx.lock(a, Transaction.WRITE); 287 a.setRelatedB(null); } 289 database.deletePersistent(b); 290 } 291 292 tx.commit(); 293 } 294 295 299 public BidirectionalAssociationTest(String name) 300 { 301 super(name); 302 } 303 304 public void tearDown() throws Exception 305 { 306 super.tearDown(); 307 } 308 309 310 311 public static class ObjectA implements Serializable 312 { 313 private String pk; 314 private String fkToB; 315 private ObjectB relatedB; 316 317 public ObjectA() 318 { 319 } 320 321 public String getPk() 322 { 323 return pk; 324 } 325 326 public void setPk(String pk) 327 { 328 this.pk = pk; 329 } 330 331 public String getFkToB() 332 { 333 return fkToB; 334 } 335 336 public void setFkToB(String fkToB) 337 { 338 this.fkToB = fkToB; 339 } 340 341 public ObjectB getRelatedB() 342 { 343 return relatedB; 344 } 345 346 public void setRelatedB(ObjectB relatedB) 347 { 348 this.relatedB = relatedB; 349 } 350 } 351 352 353 public static class ObjectB implements Serializable 354 { 355 private String pk; 356 private String fkToA; 357 private ObjectA relatedA; 358 359 public ObjectB() 360 { 361 } 362 363 public String getPk() 364 { 365 return pk; 366 } 367 368 public void setPk(String pk) 369 { 370 this.pk = pk; 371 } 372 373 public String getFkToA() 374 { 375 return fkToA; 376 } 377 378 public void setFkToA(String fkToA) 379 { 380 this.fkToA = fkToA; 381 } 382 383 public ObjectA getRelatedA() 384 { 385 return relatedA; 386 } 387 388 public void setRelatedA(ObjectA relatedA) 389 { 390 this.relatedA = relatedA; 391 } 392 } 393 } 394 | Popular Tags |