1 22 package org.jboss.test.cmp2.relationship.oneToManyBidirectional; 23 24 import java.util.Collection ; 25 import java.util.Iterator ; 26 import javax.naming.InitialContext ; 27 import junit.framework.Test; 28 import net.sourceforge.junitejb.EJBTestCase; 29 import org.jboss.test.JBossTestCase; 30 31 public class ABTest extends EJBTestCase { 32 static org.jboss.logging.Logger log = 33 org.jboss.logging.Logger.getLogger(ABTest.class); 34 35 public static Test suite() throws Exception { 36 return JBossTestCase.getDeploySetup(ABTest.class, "cmp2-relationship.jar"); 37 } 38 39 public ABTest(String name) { 40 super(name); 41 } 42 43 private AHome getTableAHome() { 44 try { 45 InitialContext jndiContext = new InitialContext (); 46 47 return (AHome) jndiContext.lookup("relation/oneToMany/bidirectional/table/A"); 48 } catch(Exception e) { 49 log.debug("failed", e); 50 fail("Exception in getTableAHome: " + e.getMessage()); 51 } 52 return null; 53 } 54 55 private BHome getTableBHome() { 56 try { 57 InitialContext jndiContext = new InitialContext (); 58 59 return (BHome) jndiContext.lookup("relation/oneToMany/bidirectional/table/B"); 60 } catch(Exception e) { 61 log.debug("failed", e); 62 fail("Exception in getTableBHome: " + e.getMessage()); 63 } 64 return null; 65 } 66 67 private AHome getFKAHome() { 68 try { 69 InitialContext jndiContext = new InitialContext (); 70 71 return (AHome) jndiContext.lookup("relation/oneToMany/bidirectional/fk/A"); 72 } catch(Exception e) { 73 log.debug("failed", e); 74 fail("Exception in getFKAHome: " + e.getMessage()); 75 } 76 return null; 77 } 78 79 private BHome getFKBHome() { 80 try { 81 InitialContext jndiContext = new InitialContext (); 82 83 return (BHome) jndiContext.lookup("relation/oneToMany/bidirectional/fk/B"); 84 } catch(Exception e) { 85 log.debug("failed", e); 86 fail("Exception in getFKBHome: " + e.getMessage()); 87 } 88 return null; 89 } 90 91 public void test_a1SetB_a2GetB_Table() throws Exception { 93 AHome aHome = getTableAHome(); 94 BHome bHome = getTableBHome(); 95 a1SetB_a2GetB(aHome, bHome); 96 } 97 98 public void test_a1SetB_a2GetB_FK() throws Exception { 100 AHome aHome = getFKAHome(); 101 BHome bHome = getFKBHome(); 102 a1SetB_a2GetB(aHome, bHome); 103 } 104 105 private void a1SetB_a2GetB(AHome aHome, BHome bHome) throws Exception { 107 A a1 = aHome.create(new Integer (1)); 109 A a2 = aHome.create(new Integer (2)); 110 111 Collection b1 = a1.getB(); 112 Collection b2 = a2.getB(); 113 114 B[] b1x = new B[20]; 115 B[] b2x = new B[30]; 116 117 for(int i=0; i<b1x.length; i++) { 118 b1x[i] = bHome.create(new Integer (10000 + i)); 119 b1.add(b1x[i]); 120 } 121 122 for(int i=0; i<b2x.length; i++) { 123 b2x[i] = bHome.create(new Integer (20000 + i)); 124 b2.add(b2x[i]); 125 } 126 127 for(int i=0; i<b1x.length; i++) { 129 assertTrue(b1.contains(b1x[i])); 130 } 131 132 for(int i=0; i<b2x.length; i++) { 134 assertTrue(b2.contains(b2x[i])); 135 } 136 137 a1.setB(a2.getB()); 139 140 142 assertTrue(a2.getB().isEmpty()); 144 145 assertTrue(b2.isEmpty()); 147 148 assertTrue(b1 == a1.getB()); 150 151 assertTrue(b2 == a2.getB()); 153 154 for(int i=0; i<b2x.length; i++) { 159 assertTrue(a1.getB().contains(b2x[i])); 160 } 161 162 for(int i=0; i<b1x.length; i++) { 167 assertTrue(b1x[i].getA() == null); 168 } 169 170 171 for(int i=0; i<b2x.length; i++) { 176 assertTrue(a1.isIdentical(b2x[i].getA())); 177 } 178 } 179 180 public void test_b2mSetA_b1nGetA_Table() throws Exception { 182 AHome aHome = getTableAHome(); 183 BHome bHome = getTableBHome(); 184 b2mSetA_b1nGetA(aHome, bHome); 185 } 186 187 public void test_b2mSetA_b1nGetA_FK() throws Exception { 189 AHome aHome = getFKAHome(); 190 BHome bHome = getFKBHome(); 191 b2mSetA_b1nGetA(aHome, bHome); 192 } 193 194 public void b2mSetA_b1nGetA(AHome aHome, BHome bHome) throws Exception { 196 A a1 = aHome.create(new Integer (1)); 198 A a2 = aHome.create(new Integer (2)); 199 200 Collection b1 = a1.getB(); 201 Collection b2 = a2.getB(); 202 203 B[] b1x = new B[20]; 204 B[] b2x = new B[30]; 205 206 for(int i=0; i<b1x.length; i++) { 207 b1x[i] = bHome.create(new Integer (10000 + i)); 208 b1.add(b1x[i]); 209 } 210 211 for(int i=0; i<b2x.length; i++) { 212 b2x[i] = bHome.create(new Integer (20000 + i)); 213 b2.add(b2x[i]); 214 } 215 216 for(int i=0; i<b1x.length; i++) { 218 assertTrue(b1.contains(b1x[i])); 219 } 220 221 for(int i=0; i<b2x.length; i++) { 223 assertTrue(b2.contains(b2x[i])); 224 } 225 226 228 b2x[b2x.length-1].setA(b1x[b1x.length-1].getA()); 230 231 233 for(int i=0; i<b1x.length; i++) { 238 assertTrue(b1.contains(b1x[i])); 239 } 240 241 assertTrue(b1.contains(b2x[b2x.length-1])); 243 244 for(int i=0; i<b2x.length-1; i++) { 249 assertTrue(b2.contains(b2x[i])); 250 } 251 252 for(int i=0; i<b1x.length; i++) { 257 assertTrue(a1.isIdentical(b1x[i].getA())); 258 } 259 260 for(int i=0; i<b2x.length-1; i++) { 265 assertTrue(a2.isIdentical(b2x[i].getA())); 266 } 267 268 assertTrue(a1.isIdentical(b2x[b2x.length-1].getA())); 270 } 271 272 public void test_a1GetB_addB2m_Table() throws Exception { 274 AHome aHome = getTableAHome(); 275 BHome bHome = getTableBHome(); 276 a1GetB_addB2m(aHome, bHome); 277 } 278 279 public void test_a1GetB_addB2m_FK() throws Exception { 281 AHome aHome = getFKAHome(); 282 BHome bHome = getFKBHome(); 283 a1GetB_addB2m(aHome, bHome); 284 } 285 286 private void a1GetB_addB2m(AHome aHome, BHome bHome) throws Exception { 288 A a1 = aHome.create(new Integer (1)); 290 A a2 = aHome.create(new Integer (2)); 291 292 Collection b1 = a1.getB(); 293 Collection b2 = a2.getB(); 294 295 B[] b1x = new B[20]; 296 B[] b2x = new B[30]; 297 298 for(int i=0; i<b1x.length; i++) { 299 b1x[i] = bHome.create(new Integer (10000 + i)); 300 b1.add(b1x[i]); 301 } 302 303 for(int i=0; i<b2x.length; i++) { 304 b2x[i] = bHome.create(new Integer (20000 + i)); 305 b2.add(b2x[i]); 306 } 307 308 for(int i=0; i<b1x.length; i++) { 310 assertTrue(b1.contains(b1x[i])); 311 } 312 313 for(int i=0; i<b2x.length; i++) { 315 assertTrue(b2.contains(b2x[i])); 316 } 317 318 320 a1.getB().add(b2x[b2x.length-1]); 322 323 325 for(int i=0; i<b1x.length; i++) { 330 assertTrue(b1.contains(b1x[i])); 331 } 332 333 assertTrue(b1.contains(b2x[b2x.length-1])); 335 336 for(int i=0; i<b2x.length-1; i++) { 341 assertTrue(b2.contains(b2x[i])); 342 } 343 344 for(int i=0; i<b1x.length; i++) { 349 assertTrue(a1.isIdentical(b1x[i].getA())); 350 } 351 352 for(int i=0; i<b2x.length-1; i++) { 357 assertTrue(a2.isIdentical(b2x[i].getA())); 358 } 359 360 assertTrue(a1.isIdentical(b2x[b2x.length-1].getA())); 362 } 363 364 public void test_a1GetB_removeB1n_Table() throws Exception { 366 AHome aHome = getTableAHome(); 367 BHome bHome = getTableBHome(); 368 a1GetB_removeB1n(aHome, bHome); 369 } 370 371 public void test_a1GetB_removeB1n_FK() throws Exception { 373 AHome aHome = getFKAHome(); 374 BHome bHome = getFKBHome(); 375 a1GetB_removeB1n(aHome, bHome); 376 } 377 378 private void a1GetB_removeB1n(AHome aHome, BHome bHome) throws Exception { 380 A a1 = aHome.create(new Integer (1)); 382 A a2 = aHome.create(new Integer (2)); 383 384 Collection b1 = a1.getB(); 385 Collection b2 = a2.getB(); 386 387 B[] b1x = new B[20]; 388 B[] b2x = new B[30]; 389 390 for(int i=0; i<b1x.length; i++) { 391 b1x[i] = bHome.create(new Integer (10000 + i)); 392 b1.add(b1x[i]); 393 } 394 395 for(int i=0; i<b2x.length; i++) { 396 b2x[i] = bHome.create(new Integer (20000 + i)); 397 b2.add(b2x[i]); 398 } 399 400 for(int i=0; i<b1x.length; i++) { 402 assertTrue(b1.contains(b1x[i])); 403 } 404 405 for(int i=0; i<b2x.length; i++) { 407 assertTrue(b2.contains(b2x[i])); 408 } 409 410 412 a1.getB().remove(b1x[b1x.length-1]); 414 415 417 assertTrue(b1x[b1x.length-1].getA() == null); 419 420 assertTrue(b1 == a1.getB()); 422 423 for(int i=0; i<b1x.length-1; i++) { 428 assertTrue(b1.contains(b1x[i])); 429 } 430 431 assertTrue(!(b1.contains(b1x[b1x.length-1]))); 433 } 434 435 public void setUpEJB() throws Exception { 436 AHome aHome; 437 BHome bHome; 438 439 aHome = getTableAHome(); 440 bHome = getTableBHome(); 441 deleteAllAsAndBs(aHome, bHome); 442 443 aHome = getFKAHome(); 444 bHome = getFKBHome(); 445 deleteAllAsAndBs(aHome, bHome); 446 } 447 448 public void tearDownEJB() throws Exception { 449 } 450 451 public void deleteAllAsAndBs(AHome aHome, BHome bHome) throws Exception { 452 Iterator currentAs = aHome.findAll().iterator(); 454 while(currentAs.hasNext()) { 455 A a = (A)currentAs.next(); 456 a.remove(); 457 } 458 459 Iterator currentBs = bHome.findAll().iterator(); 461 while(currentBs.hasNext()) { 462 B b = (B)currentBs.next(); 463 b.remove(); 464 } 465 } 466 } 467 468 469 470 | Popular Tags |