1 22 package org.jboss.test.cmp2.relationship.manyToManyBidirectional; 23 24 import java.util.Collection ; 25 import java.util.Iterator ; 26 import javax.naming.InitialContext ; 27 import junit.framework.Test; 28 import junit.framework.TestCase; 29 import net.sourceforge.junitejb.EJBTestCase; 30 import org.jboss.test.JBossTestCase; 31 32 public class ABTest extends EJBTestCase { 33 34 static org.jboss.logging.Logger log = 35 org.jboss.logging.Logger.getLogger(ABTest.class); 36 37 public static Test suite() throws Exception { 38 return JBossTestCase.getDeploySetup(ABTest.class, "cmp2-relationship.jar"); 39 } 40 41 public ABTest(String name) { 42 super(name); 43 } 44 45 private AHome getAHome() { 46 try { 47 InitialContext jndiContext = new InitialContext (); 48 49 return (AHome) jndiContext.lookup("relation/manyToMany/bidirectional/A"); 50 } catch(Exception e) { 51 log.debug("failed", e); 52 fail("Exception in getAHome: " + e.getMessage()); 53 } 54 return null; 55 } 56 57 private BHome getBHome() { 58 try { 59 InitialContext jndiContext = new InitialContext (); 60 61 return (BHome) jndiContext.lookup("relation/manyToMany/bidirectional/B"); 62 } catch(Exception e) { 63 log.debug("failed", e); 64 fail("Exception in getBHome: " + e.getMessage()); 65 } 66 return null; 67 } 68 69 public void test_a1SetB_a3GetB() throws Exception { 71 AHome aHome = getAHome(); 72 BHome bHome = getBHome(); 73 74 A a1 = aHome.create(new Integer (1)); 76 A a2 = aHome.create(new Integer (2)); 77 A a3 = aHome.create(new Integer (3)); 78 A a4 = aHome.create(new Integer (4)); 79 A a5 = aHome.create(new Integer (5)); 80 81 B b1 = bHome.create(new Integer (-1)); 82 B b2 = bHome.create(new Integer (-2)); 83 B b3 = bHome.create(new Integer (-3)); 84 B b4 = bHome.create(new Integer (-4)); 85 B b5 = bHome.create(new Integer (-5)); 86 87 a1.getB().add(b1); 88 a1.getB().add(b2); 89 a2.getB().add(b1); 90 a2.getB().add(b2); 91 a2.getB().add(b3); 92 a3.getB().add(b2); 93 a3.getB().add(b3); 94 a3.getB().add(b4); 95 a4.getB().add(b3); 96 a4.getB().add(b4); 97 a4.getB().add(b5); 98 a5.getB().add(b4); 99 a5.getB().add(b5); 100 101 assertTrue(a1.getB().contains(b1)); 102 assertTrue(a1.getB().contains(b2)); 103 assertTrue(a2.getB().contains(b1)); 104 assertTrue(a2.getB().contains(b2)); 105 assertTrue(a2.getB().contains(b3)); 106 assertTrue(a3.getB().contains(b2)); 107 assertTrue(a3.getB().contains(b3)); 108 assertTrue(a3.getB().contains(b4)); 109 assertTrue(a4.getB().contains(b3)); 110 assertTrue(a4.getB().contains(b4)); 111 assertTrue(a4.getB().contains(b5)); 112 assertTrue(a5.getB().contains(b4)); 113 assertTrue(a5.getB().contains(b5)); 114 115 assertTrue(b1.getA().contains(a1)); 116 assertTrue(b1.getA().contains(a2)); 117 assertTrue(b2.getA().contains(a1)); 118 assertTrue(b2.getA().contains(a2)); 119 assertTrue(b2.getA().contains(a3)); 120 assertTrue(b3.getA().contains(a2)); 121 assertTrue(b3.getA().contains(a3)); 122 assertTrue(b3.getA().contains(a4)); 123 assertTrue(b4.getA().contains(a3)); 124 assertTrue(b4.getA().contains(a4)); 125 assertTrue(b4.getA().contains(a5)); 126 assertTrue(b5.getA().contains(a4)); 127 assertTrue(b5.getA().contains(a5)); 128 129 a1.setB(a3.getB()); 131 132 assertTrue(!a1.getB().contains(b1)); 134 assertTrue(a1.getB().contains(b2)); 135 assertTrue(a1.getB().contains(b3)); 136 assertTrue(a1.getB().contains(b4)); 137 138 assertTrue(a2.getB().contains(b1)); 139 assertTrue(a2.getB().contains(b2)); 140 assertTrue(a2.getB().contains(b3)); 141 142 assertTrue(a3.getB().contains(b2)); 143 assertTrue(a3.getB().contains(b3)); 144 assertTrue(a3.getB().contains(b4)); 145 146 assertTrue(a4.getB().contains(b3)); 147 assertTrue(a4.getB().contains(b4)); 148 assertTrue(a4.getB().contains(b5)); 149 150 assertTrue(a5.getB().contains(b4)); 151 assertTrue(a5.getB().contains(b5)); 152 153 154 assertTrue(!b1.getA().contains(a1)); 155 assertTrue(b1.getA().contains(a2)); 156 157 assertTrue(b2.getA().contains(a1)); 158 assertTrue(b2.getA().contains(a2)); 159 assertTrue(b2.getA().contains(a3)); 160 161 assertTrue(b3.getA().contains(a1)); 162 assertTrue(b3.getA().contains(a2)); 163 assertTrue(b3.getA().contains(a3)); 164 assertTrue(b3.getA().contains(a4)); 165 166 assertTrue(b4.getA().contains(a1)); 167 assertTrue(b4.getA().contains(a3)); 168 assertTrue(b4.getA().contains(a4)); 169 assertTrue(b4.getA().contains(a5)); 170 171 assertTrue(b5.getA().contains(a4)); 172 assertTrue(b5.getA().contains(a5)); 173 } 174 175 public void test_a1GetB_addB3() throws Exception { 177 AHome aHome = getAHome(); 178 BHome bHome = getBHome(); 179 180 A a1 = aHome.create(new Integer (1)); 182 A a2 = aHome.create(new Integer (2)); 183 A a3 = aHome.create(new Integer (3)); 184 A a4 = aHome.create(new Integer (4)); 185 A a5 = aHome.create(new Integer (5)); 186 187 B b1 = bHome.create(new Integer (-1)); 188 B b2 = bHome.create(new Integer (-2)); 189 B b3 = bHome.create(new Integer (-3)); 190 B b4 = bHome.create(new Integer (-4)); 191 B b5 = bHome.create(new Integer (-5)); 192 193 a1.getB().add(b1); 194 a1.getB().add(b2); 195 a2.getB().add(b1); 196 a2.getB().add(b2); 197 a2.getB().add(b3); 198 a3.getB().add(b2); 199 a3.getB().add(b3); 200 a3.getB().add(b4); 201 a4.getB().add(b3); 202 a4.getB().add(b4); 203 a4.getB().add(b5); 204 a5.getB().add(b4); 205 a5.getB().add(b5); 206 207 assertTrue(a1.getB().contains(b1)); 208 assertTrue(a1.getB().contains(b2)); 209 assertTrue(a2.getB().contains(b1)); 210 assertTrue(a2.getB().contains(b2)); 211 assertTrue(a2.getB().contains(b3)); 212 assertTrue(a3.getB().contains(b2)); 213 assertTrue(a3.getB().contains(b3)); 214 assertTrue(a3.getB().contains(b4)); 215 assertTrue(a4.getB().contains(b3)); 216 assertTrue(a4.getB().contains(b4)); 217 assertTrue(a4.getB().contains(b5)); 218 assertTrue(a5.getB().contains(b4)); 219 assertTrue(a5.getB().contains(b5)); 220 221 assertTrue(b1.getA().contains(a1)); 222 assertTrue(b1.getA().contains(a2)); 223 assertTrue(b2.getA().contains(a1)); 224 assertTrue(b2.getA().contains(a2)); 225 assertTrue(b2.getA().contains(a3)); 226 assertTrue(b3.getA().contains(a2)); 227 assertTrue(b3.getA().contains(a3)); 228 assertTrue(b3.getA().contains(a4)); 229 assertTrue(b4.getA().contains(a3)); 230 assertTrue(b4.getA().contains(a4)); 231 assertTrue(b4.getA().contains(a5)); 232 assertTrue(b5.getA().contains(a4)); 233 assertTrue(b5.getA().contains(a5)); 234 235 a1.getB().add(b3); 237 238 assertTrue(a1.getB().contains(b1)); 240 assertTrue(a1.getB().contains(b2)); 241 assertTrue(a1.getB().contains(b3)); 242 243 assertTrue(a2.getB().contains(b1)); 244 assertTrue(a2.getB().contains(b2)); 245 assertTrue(a2.getB().contains(b3)); 246 247 assertTrue(a3.getB().contains(b2)); 248 assertTrue(a3.getB().contains(b3)); 249 assertTrue(a3.getB().contains(b4)); 250 251 assertTrue(a4.getB().contains(b3)); 252 assertTrue(a4.getB().contains(b4)); 253 assertTrue(a4.getB().contains(b5)); 254 255 assertTrue(a5.getB().contains(b4)); 256 assertTrue(a5.getB().contains(b5)); 257 258 259 assertTrue(b1.getA().contains(a1)); 260 assertTrue(b1.getA().contains(a2)); 261 262 assertTrue(b2.getA().contains(a1)); 263 assertTrue(b2.getA().contains(a2)); 264 assertTrue(b2.getA().contains(a3)); 265 266 assertTrue(b3.getA().contains(a1)); 267 assertTrue(b3.getA().contains(a2)); 268 assertTrue(b3.getA().contains(a3)); 269 assertTrue(b3.getA().contains(a4)); 270 271 assertTrue(b4.getA().contains(a3)); 272 assertTrue(b4.getA().contains(a4)); 273 assertTrue(b4.getA().contains(a5)); 274 275 assertTrue(b5.getA().contains(a4)); 276 assertTrue(b5.getA().contains(a5)); 277 } 278 279 public void test_a2GetB_removeB2() throws Exception { 281 AHome aHome = getAHome(); 282 BHome bHome = getBHome(); 283 284 A a1 = aHome.create(new Integer (1)); 286 A a2 = aHome.create(new Integer (2)); 287 A a3 = aHome.create(new Integer (3)); 288 A a4 = aHome.create(new Integer (4)); 289 A a5 = aHome.create(new Integer (5)); 290 291 B b1 = bHome.create(new Integer (-1)); 292 B b2 = bHome.create(new Integer (-2)); 293 B b3 = bHome.create(new Integer (-3)); 294 B b4 = bHome.create(new Integer (-4)); 295 B b5 = bHome.create(new Integer (-5)); 296 297 a1.getB().add(b1); 298 a1.getB().add(b2); 299 a2.getB().add(b1); 300 a2.getB().add(b2); 301 a2.getB().add(b3); 302 a3.getB().add(b2); 303 a3.getB().add(b3); 304 a3.getB().add(b4); 305 a4.getB().add(b3); 306 a4.getB().add(b4); 307 a4.getB().add(b5); 308 a5.getB().add(b4); 309 a5.getB().add(b5); 310 311 assertTrue(a1.getB().contains(b1)); 312 assertTrue(a1.getB().contains(b2)); 313 assertTrue(a2.getB().contains(b1)); 314 assertTrue(a2.getB().contains(b2)); 315 assertTrue(a2.getB().contains(b3)); 316 assertTrue(a3.getB().contains(b2)); 317 assertTrue(a3.getB().contains(b3)); 318 assertTrue(a3.getB().contains(b4)); 319 assertTrue(a4.getB().contains(b3)); 320 assertTrue(a4.getB().contains(b4)); 321 assertTrue(a4.getB().contains(b5)); 322 assertTrue(a5.getB().contains(b4)); 323 assertTrue(a5.getB().contains(b5)); 324 325 assertTrue(b1.getA().contains(a1)); 326 assertTrue(b1.getA().contains(a2)); 327 assertTrue(b2.getA().contains(a1)); 328 assertTrue(b2.getA().contains(a2)); 329 assertTrue(b2.getA().contains(a3)); 330 assertTrue(b3.getA().contains(a2)); 331 assertTrue(b3.getA().contains(a3)); 332 assertTrue(b3.getA().contains(a4)); 333 assertTrue(b4.getA().contains(a3)); 334 assertTrue(b4.getA().contains(a4)); 335 assertTrue(b4.getA().contains(a5)); 336 assertTrue(b5.getA().contains(a4)); 337 assertTrue(b5.getA().contains(a5)); 338 339 a2.getB().remove(b2); 341 342 assertTrue(a1.getB().contains(b1)); 344 assertTrue(a1.getB().contains(b2)); 345 346 assertTrue(a2.getB().contains(b1)); 347 assertTrue(!a2.getB().contains(b2)); 348 assertTrue(a2.getB().contains(b3)); 349 350 assertTrue(a3.getB().contains(b2)); 351 assertTrue(a3.getB().contains(b3)); 352 assertTrue(a3.getB().contains(b4)); 353 354 assertTrue(a4.getB().contains(b3)); 355 assertTrue(a4.getB().contains(b4)); 356 assertTrue(a4.getB().contains(b5)); 357 358 assertTrue(a5.getB().contains(b4)); 359 assertTrue(a5.getB().contains(b5)); 360 361 362 assertTrue(b1.getA().contains(a1)); 363 assertTrue(b1.getA().contains(a2)); 364 365 assertTrue(b2.getA().contains(a1)); 366 assertTrue(!b2.getA().contains(a2)); 367 assertTrue(b2.getA().contains(a3)); 368 369 assertTrue(b3.getA().contains(a2)); 370 assertTrue(b3.getA().contains(a3)); 371 assertTrue(b3.getA().contains(a4)); 372 373 assertTrue(b4.getA().contains(a3)); 374 assertTrue(b4.getA().contains(a4)); 375 assertTrue(b4.getA().contains(a5)); 376 377 assertTrue(b5.getA().contains(a4)); 378 assertTrue(b5.getA().contains(a5)); 379 } 380 381 public void setUpEJB() throws Exception { 382 deleteAllAsAndBs(getAHome(), getBHome()); 383 } 384 385 public void tearDownEJB() throws Exception { 386 } 387 388 public void deleteAllAsAndBs(AHome aHome, BHome bHome) throws Exception { 389 Iterator currentAs = aHome.findAll().iterator(); 391 while(currentAs.hasNext()) { 392 A a = (A)currentAs.next(); 393 a.remove(); 394 } 395 396 Iterator currentBs = bHome.findAll().iterator(); 398 while(currentBs.hasNext()) { 399 B b = (B)currentBs.next(); 400 b.remove(); 401 } 402 } 403 } 404 405 406 407 | Popular Tags |