1 25 26 package org.objectweb.jonas.jtests.clients.entity; 27 28 import java.util.ArrayList ; 29 import java.util.Collection ; 30 import java.util.Enumeration ; 31 import java.util.Hashtable ; 32 33 import javax.naming.NamingException ; 34 import javax.rmi.PortableRemoteObject ; 35 36 import junit.framework.Test; 37 import junit.framework.TestSuite; 38 39 import org.objectweb.jonas.jtests.beans.relation.mnu.AHomeRemote; 40 import org.objectweb.jonas.jtests.beans.relation.mnu.ARemote; 41 import org.objectweb.jonas.jtests.beans.relation.mnu.BHomeRemote; 42 import org.objectweb.jonas.jtests.beans.relation.mnu.BRemote; 43 44 49 public class G_Relation_mnuEC2 extends A_Cmp2Util { 50 51 static Hashtable tbRelationA2B = new Hashtable (); 52 static { 53 tbRelationA2B.put("a0", new String []{}); 54 tbRelationA2B.put("a1", new String []{"b1","b2"}); 55 tbRelationA2B.put("a2", new String []{"b1", "b2", "b3"}); 56 tbRelationA2B.put("a3", new String []{"b2","b3","b4"}); 57 for (Enumeration ea = tbRelationA2B.keys() ; ea.hasMoreElements() ;) { 59 String aname = (String ) (ea.nextElement()); 60 String [] tb = (String [])tbRelationA2B.get(aname); 61 ArrayList col = new ArrayList (tb.length); 62 for (int i=0; i<tb.length; i++ ) { 63 col.add(tb[i]); 64 } 65 tbRelationA2B.put(aname, col); 66 } 67 } 68 69 private static String BEAN_HOME_A = "relation_mnu_AHome"; 70 protected static AHomeRemote ahome = null; 71 private static String BEAN_HOME_B = "relation_mnu_BHome"; 72 protected static BHomeRemote bhome = null; 73 74 75 public G_Relation_mnuEC2(String name) { 76 super(name); 77 } 78 79 80 public void testEmpty() throws Exception { 81 } 82 protected static boolean isInit = false; 83 84 protected void setUp() { 85 super.setUp(); 86 boolean ok = false; 87 int nbtry = 0; 88 while (!ok && nbtry < 3) { 89 if (!isInit) { 90 useBeans("mnu", false); 92 try { 93 ahome = (AHomeRemote) PortableRemoteObject.narrow(ictx.lookup(BEAN_HOME_A), 94 AHomeRemote.class); 95 bhome = (BHomeRemote) PortableRemoteObject.narrow(ictx.lookup(BEAN_HOME_B), 96 BHomeRemote.class); 97 } catch (NamingException e) { 98 fail("Cannot get bean home: "+e.getMessage()); 99 } 100 try { 102 ahome.findByPrimaryKey("a0"); 103 } catch (Exception e) { 104 try { 105 utx.begin(); 106 ARemote a0 = ahome.create("a0"); 107 ARemote a1 = ahome.create("a1"); 108 ARemote a2 = ahome.create("a2"); 109 ARemote a3 = ahome.create("a3"); 110 bhome.create("b0"); 111 bhome.create("b1"); 112 bhome.create("b2"); 113 bhome.create("b3"); 114 bhome.create("b4"); 115 a0.assignB((Collection ) tbRelationA2B.get("a0")); 116 a1.assignB((Collection ) tbRelationA2B.get("a1")); 117 a2.assignB((Collection ) tbRelationA2B.get("a2")); 118 a3.assignB((Collection ) tbRelationA2B.get("a3")); 119 } catch (Exception i) { 120 fail("InitialState creation problem: "+i); 121 } finally { 122 try { 123 utx.commit(); 124 } catch (Exception ii) { 125 } 126 } 127 } 128 isInit = true; 129 } 130 nbtry++; 133 try { 134 if (initStateOK()) { 135 ok = true; 136 } 137 } catch (Exception e) { 138 } 139 if (!ok) { 140 isInit = false; 141 unloadBeans("mnu"); 142 } 143 } 144 } 145 146 150 boolean initStateOK() throws Exception { 151 boolean isOk = true; 152 msgerror = new StringBuffer (); 153 for (Enumeration ea = tbRelationA2B.keys(); ea.hasMoreElements();) { 154 String aname = (String ) (ea.nextElement()); 155 ARemote a = ahome.findByPrimaryKey(aname); 156 Collection colActual = a.retrieveB(); 157 ArrayList colExpected = (ArrayList ) (tbRelationA2B.get(aname)); 158 if (!isCollectionEqual(colExpected, colActual)) { 159 isOk = false; 160 msgerror = msgerror.append("Wrong relation for " + aname 161 +" (expected:" + colExpected 162 + ", found:" + colActual + ")"); 163 } 164 } 165 return isOk; 166 } 167 168 169 172 public void _testBasicGetEmpty(int tx) throws Exception { 173 Collection c; 174 if ((tx == TX_CALL) || (tx == TX_RB)) { 175 utx.begin(); 176 } 177 ARemote a = ahome.findByPrimaryKey("a0"); 178 if (tx == TX_CONT) { 179 c = a.retrieveBInNewTx(); 180 } else { 181 c = a.retrieveB(); 182 } 183 if (tx == TX_CALL) { 184 utx.commit(); 185 } else if (tx == TX_RB) { 186 utx.rollback(); 187 } 188 189 checkIsInitialState(); 190 } 191 public void testBasicGetEmptyTxNo() throws Exception { 192 _testBasicGetEmpty(TX_NO); 193 } 194 public void testBasicGetEmptyTxCall() throws Exception { 195 _testBasicGetEmpty(TX_CALL); 196 } 197 public void testBasicGetEmptyTxCont() throws Exception { 198 _testBasicGetEmpty(TX_CONT); 199 } 200 201 202 205 public void _testBasicSetEmpty(int tx) throws Exception { 206 ArrayList c = new ArrayList (1); 207 c.add("b0"); 208 if ((tx == TX_CALL) || (tx == TX_RB)) { 209 utx.begin(); 210 } 211 ARemote a = ahome.findByPrimaryKey("a0"); 212 if (tx == TX_CONT) { 213 a.assignBInNewTx(c); 214 } else { 215 a.assignB(c); 216 } 217 if (tx == TX_CALL) { 218 utx.commit(); 219 } else if (tx == TX_RB) { 220 utx.rollback(); 221 } 222 Collection c_value = a.retrieveB(); 224 if (tx != TX_RB) { 225 assertTrue("Wrong relations a0 (required:" + c + ", found:" + c_value 226 + ")", isCollectionEqual(c_value, c)); 227 a.assignB(new ArrayList (0)); 229 } 230 checkIsInitialState(); 231 232 } 233 public void testBasicSetEmptyTxNo() throws Exception { 234 _testBasicSetEmpty(TX_NO); 235 } 236 public void testBasicSetEmptyTxCall() throws Exception { 237 _testBasicSetEmpty(TX_CALL); 238 } 239 public void testBasicSetEmptyTxCont() throws Exception { 240 _testBasicSetEmpty(TX_CONT); 241 } 242 251 public void _testBasicSetFirst(int tx) throws Exception { 252 String c = null; 253 if ((tx == TX_CALL) || (tx == TX_RB)) { 254 utx.begin(); 255 } 256 ARemote a1 = ahome.findByPrimaryKey("a1"); 257 ARemote a3 = ahome.findByPrimaryKey("a3"); 258 Collection c1 = a1.retrieveB(); 259 if (tx == TX_CONT) { 260 a1.assignBInNewTx(a3.retrieveB()); 261 } else { 262 a1.assignB(a3.retrieveB()); 263 } 264 if (tx == TX_CALL) { 265 utx.commit(); 266 } else if (tx == TX_RB) { 267 utx.rollback(); 268 } 269 if (tx != TX_RB) { 271 ArrayList c1_att = new ArrayList (3); 272 c1_att.add("b2"); 273 c1_att.add("b3"); 274 c1_att.add("b4"); 275 assertTrue("Wrong test of coherence First before commit: ", isCollectionEqual(a1.retrieveB(),c1_att)); 276 } else { 277 assertTrue("Wrong test of coherence First before roolback: ", isCollectionEqual(a1.retrieveB(),c1)); 278 } 279 if (tx != TX_RB) { 281 a1.assignB(null); 282 a1.assignB(c1); 283 } 284 checkIsInitialState(); 285 } 286 287 288 289 295 public void _testBasicSetSecond(int tx) throws Exception { 296 String c = null; 297 298 if ((tx == TX_CALL) || (tx == TX_RB)) { 299 utx.begin(); 300 } 301 ARemote a1 = ahome.findByPrimaryKey("a1"); 302 Collection c1 = a1.retrieveB(); 303 304 if (tx == TX_CONT) { 305 a1.addInB("b3"); 306 } else { 307 a1.addInB("b3"); 308 } 309 if (tx == TX_CALL) { 310 utx.commit(); 311 } else if (tx == TX_RB) { 312 utx.rollback(); 313 } 314 if (tx != TX_RB) { 316 ArrayList c1_att = new ArrayList (3); 317 c1_att.add("b1"); 318 c1_att.add("b2"); 319 c1_att.add("b3"); 320 assertTrue("Wrong test of coherence Second before commit: ", isCollectionEqual(a1.retrieveB(),c1_att)); 321 } else { 322 assertTrue("Wrong test of coherence Second before roolback: ", isCollectionEqual(a1.retrieveB(),c1)); 323 } 324 if (tx != TX_RB) { 326 a1.assignB(null); 327 a1.assignB(c1); 328 } 329 checkIsInitialState(); 330 } 331 332 333 334 335 336 341 public void _testBasicSetRemove(int tx) throws Exception { 342 String c = null; 343 344 if ((tx == TX_CALL) || (tx == TX_RB)) { 345 utx.begin(); 346 } 347 ARemote a2 = ahome.findByPrimaryKey("a2"); 348 Collection c2 = a2.retrieveB(); 349 if (tx == TX_CONT) { 350 a2.removeFromB("b2"); 351 } else { 352 a2.removeFromB("b2"); 353 } 354 if (tx == TX_CALL) { 355 utx.commit(); 356 } else if (tx == TX_RB) { 357 utx.rollback(); 358 } 359 if (tx != TX_RB) { 361 ArrayList c2_att = new ArrayList (3); 362 c2_att.add("b1"); 363 c2_att.add("b3"); 364 assertTrue("Wrong test of coherence Remove before commit: ", isCollectionEqual(a2.retrieveB(),c2_att)); 365 } else { 366 assertTrue("Wrong test of coherence Remove before roolback: ", isCollectionEqual(a2.retrieveB(),c2)); 367 } 368 if (tx != TX_RB) { 370 a2.assignB(c2); 371 } 372 checkIsInitialState(); 373 } 374 375 376 377 380 public void _testBasicClear(int tx) throws Exception { 381 if ((tx == TX_CALL) || (tx == TX_RB)) { 382 utx.begin(); 383 } 384 ARemote a2 = ahome.findByPrimaryKey("a2"); 385 if (tx == TX_CONT) { 386 a2.clearBInNewTx(); 387 } else { 388 a2.clearB(); 389 } 390 if (tx == TX_CALL) { 391 utx.commit(); 392 } else if (tx == TX_RB) { 393 utx.rollback(); 394 } 395 Collection a2_value = a2.retrieveB(); 397 BRemote b1 = bhome.findByPrimaryKey("b1"); 398 BRemote b2 = bhome.findByPrimaryKey("b2"); 399 BRemote b3 = bhome.findByPrimaryKey("b3"); 400 ArrayList collb1 = new ArrayList (1); 401 collb1.add("a1"); 402 ArrayList collb2 = new ArrayList (2); 403 collb2.add("a1"); 404 collb2.add("a3"); 405 ArrayList collb3 = new ArrayList (1); 406 collb3.add("a3"); 407 Collection col=null; 408 if (tx != TX_RB) { 409 assertTrue("Wrong relations a2 (required: Empty "+", found:" + a2_value 410 + ")", a2_value.isEmpty()); 411 412 ArrayList a2undo = new ArrayList (3); 414 a2undo.add("b1"); 415 a2undo.add("b2"); 416 a2undo.add("b3"); 417 a2.assignB(a2undo); 418 } 419 checkIsInitialState(); 420 } 421 422 423 424 425 428 public void _testCohRemoveInRel(int tx) throws Exception { 429 String bRemoved = "b4"; 430 if ((tx == TX_CALL) || (tx == TX_RB)) { 431 utx.begin(); 432 } 433 ARemote a = ahome.findByPrimaryKey("a3"); 434 if (tx == TX_CONT) { 435 a.removeFromBInNewTx(bRemoved); 436 } else { 437 a.removeFromB(bRemoved); 438 } 439 if (tx == TX_CALL) { 440 utx.commit(); 441 } else if (tx == TX_RB) { 442 utx.rollback(); 443 } 444 Collection ca = a.retrieveB(); 446 if (tx != TX_RB) { 447 assertEquals("Wrong relations size for a3: ", 2, ca.size()); 448 } else { 449 assertEquals("Wrong relations size for a3: ", 3, ca.size()); 450 } 451 if (tx != TX_RB) { 453 a.addInB(bRemoved); 454 } 455 checkIsInitialState(); 457 458 } 459 460 461 public static Test suite() { 462 return new TestSuite(G_Relation_mnuEC2.class); 463 } 464 465 public static void main(String args[]) { 466 String testtorun = null; 467 for (int argn = 0; argn < args.length; argn++) { 469 String s_arg = args[argn]; 470 Integer i_arg; 471 if (s_arg.equals("-n")) { 472 testtorun = args[++argn]; 473 } 474 } 475 if (testtorun == null) { 476 junit.textui.TestRunner.run(suite()); 477 } else { 478 junit.textui.TestRunner.run(new G_Relation_mnuEC2(testtorun)); 479 } 480 } 481 482 } 483 | Popular Tags |