1 25 26 package org.objectweb.jonas.jtests.clients.entity; 27 28 import java.util.Enumeration ; 29 import java.util.Hashtable ; 30 31 import javax.naming.NamingException ; 32 import javax.rmi.PortableRemoteObject ; 33 34 import junit.framework.Test; 35 import junit.framework.TestSuite; 36 37 import org.objectweb.jonas.jtests.beans.relation.mou.AHomeRemote; 38 import org.objectweb.jonas.jtests.beans.relation.mou.ARemote; 39 import org.objectweb.jonas.jtests.beans.relation.mou.BHomeRemote; 40 41 48 public class G_Relation_mouEC2 extends A_Cmp2Util { 49 50 51 private static String BEAN_HOME_A = "relation_mou_AHome"; 52 protected static AHomeRemote ahome = null; 53 private static String BEAN_HOME_B = "relation_mou_BHome"; 54 protected static BHomeRemote bhome = null; 55 56 static Hashtable tbRelationA2B = new Hashtable (); 57 58 static { 59 tbRelationA2B.put("a20", "b2"); 60 tbRelationA2B.put("a21", "b2"); 61 tbRelationA2B.put("a22", "b2"); 62 tbRelationA2B.put("a0", "b0"); 63 tbRelationA2B.put("a1", "b0"); 64 tbRelationA2B.put("a2", "b0"); 65 tbRelationA2B.put("a3", ""); 66 tbRelationA2B.put("a4", "b4"); 67 tbRelationA2B.put("a5", "b4"); 68 } 69 70 public G_Relation_mouEC2(String name) { 71 super(name); 72 } 73 74 75 public void testEmpty() throws Exception { 76 } 77 protected static boolean isInit = false; 78 79 protected void setUp() { 80 super.setUp(); 81 boolean ok = false; 82 int nbtry = 0; 83 while (!ok && nbtry < 3) { 84 if (!isInit) { 85 useBeans("mou", false); 87 try { 89 ahome = (AHomeRemote) PortableRemoteObject.narrow(ictx.lookup(BEAN_HOME_A), 90 AHomeRemote.class); 91 bhome = (BHomeRemote) PortableRemoteObject.narrow(ictx.lookup(BEAN_HOME_B), 92 BHomeRemote.class); 93 } catch (NamingException e) { 94 fail("Cannot get bean home: " + e.getMessage()); 95 } 96 try { 98 ahome.findByPrimaryKey("a0"); 99 } catch (Exception e) { 100 try { 102 utx.begin(); 103 ARemote a0 = ahome.create("a0"); 104 ARemote a1 = ahome.create("a1"); 105 ARemote a2 = ahome.create("a2"); 106 ARemote a3 = ahome.create("a3"); 107 ARemote a4 = ahome.create("a4"); 108 ARemote a5 = ahome.create("a5"); 109 ARemote a20 = ahome.create("a20"); 110 ARemote a21 = ahome.create("a21"); 111 ARemote a22 = ahome.create("a22"); 112 bhome.create("b0"); 113 bhome.create("b1"); 114 bhome.create("b2"); 115 bhome.create("b4"); 116 a0.assignB((String ) tbRelationA2B.get("a0")); 118 a1.assignB((String ) tbRelationA2B.get("a1")); 119 a2.assignB((String ) tbRelationA2B.get("a2")); 120 a4.assignB((String ) tbRelationA2B.get("a4")); 121 a5.assignB((String ) tbRelationA2B.get("a5")); 122 a20.assignB((String ) tbRelationA2B.get("a20")); 123 a21.assignB((String ) tbRelationA2B.get("a21")); 124 a22.assignB((String ) tbRelationA2B.get("a22")); 125 } catch (Exception e2) { 126 fail("InitialState creation problem: " + e2); 127 } finally { 128 try { 129 utx.commit(); 130 } catch (Exception e3) { 131 } 132 } 133 } 134 isInit = true; 135 } 136 nbtry++; 139 try { 140 if (initStateOK()) { 141 ok = true; 142 } 143 } catch (Exception e) { 144 } 145 if (!ok) { 146 isInit = false; 147 unloadBeans("mou"); 148 } 149 } 150 } 151 152 153 157 boolean initStateOK() throws Exception { 158 boolean isOk = true; 159 msgerror = new StringBuffer (); 160 for (Enumeration ea = tbRelationA2B.keys(); ea.hasMoreElements();) { 161 String aname = (String ) (ea.nextElement()); 162 ARemote a = ahome.findByPrimaryKey(aname); 163 String BActual = a.retrieveB(); 164 if (BActual == null) BActual = ""; 166 String BExpected = (String ) (tbRelationA2B.get(aname)); 167 if (!BActual.equals(BExpected)) { 168 isOk = false; 169 msgerror = msgerror.append("Wrong relation for " + aname 170 + " (expected:" + BExpected 171 + ", found:" + BActual + ")"); 172 } 173 } 174 return isOk; 175 } 176 177 178 179 182 public void _testBasicGetEmpty(int tx) throws Exception { 183 String c = null; 184 if ((tx == TX_CALL) || (tx == TX_RB)) { 185 utx.begin(); 186 } 187 ARemote a = ahome.findByPrimaryKey("a3"); 188 if (tx == TX_CONT) { 189 c = a.retrieveBInNewTx(); 190 } else { 191 c = a.retrieveB(); 192 } 193 if (tx == TX_CALL) { 194 utx.commit(); 195 } else if (tx == TX_RB) { 196 utx.rollback(); 197 } 198 checkIsInitialState(); 199 } 200 public void testBasicGetEmptyTxNo() throws Exception { 201 _testBasicGetEmpty(TX_NO); 202 } 203 204 public void testBasicGetEmptyTxCall() throws Exception { 205 _testBasicGetEmpty(TX_CALL); 206 } 207 208 public void testBasicGetEmptyTxCont() throws Exception { 209 _testBasicGetEmpty(TX_CONT); 210 } 211 212 215 public void _testBasicSetEmpty(int tx) throws Exception { 216 String c = null; 217 if ((tx == TX_CALL) || (tx == TX_RB)) { 218 utx.begin(); 219 } 220 ARemote a = ahome.findByPrimaryKey("a3"); 221 if (tx == TX_CONT) { 222 a.assignBInNewTx("b1"); 223 } else { 224 a.assignB("b1"); 225 } 226 if (tx == TX_CALL) { 227 utx.commit(); 228 } else if (tx == TX_RB) { 229 utx.rollback(); 230 } 231 String idB = a.retrieveB(); 233 if (tx != TX_RB) { 234 assertEquals("Wrong new relation a3->b1 : ", "b1", idB); 235 } else { 236 assertNull("Wrong new relation a3->b1 : ", idB); 237 } 238 if (tx != TX_RB) { 240 a.assignB(null); 241 } 242 checkIsInitialState(); 243 } 244 245 public void testBasicSetEmptyTxNo() throws Exception { 246 _testBasicSetEmpty(TX_NO); 247 } 248 249 public void testBasicSetEmptyTxCall() throws Exception { 250 _testBasicSetEmpty(TX_CALL); 251 } 252 253 public void testBasicSetEmptyTxCont() throws Exception { 254 _testBasicSetEmpty(TX_CONT); 255 } 256 257 260 public void _testBasicGet(int tx) throws Exception { 261 String b = null; 262 if ((tx == TX_CALL) || (tx == TX_RB)) { 263 utx.begin(); 264 } 265 ARemote a1 = ahome.findByPrimaryKey("a1"); 266 267 if (tx == TX_CONT) { 268 b = a1.retrieveBInNewTx(); 269 } else { 270 b = a1.retrieveB(); 271 } 272 if (tx == TX_CALL) { 273 utx.commit(); 274 } else if (tx == TX_RB) { 275 utx.rollback(); 276 } 277 if (tx != TX_RB) { 279 assertEquals("Wrong relation a1->b0 : ", "b0", b); 280 } 281 checkIsInitialState(); 282 } 283 284 285 286 287 288 291 public void _testBasicSetNull(int tx) throws Exception { 292 if ((tx == TX_CALL) || (tx == TX_RB)) { 293 utx.begin(); 294 } 295 ARemote a1 = ahome.findByPrimaryKey("a1"); 296 297 if (tx == TX_CONT) { 298 a1.assignBInNewTx(null); 299 } else { 300 a1.assignB(null); 301 } 302 if (tx == TX_CALL) { 303 utx.commit(); 304 } else if (tx == TX_RB) { 305 utx.rollback(); 306 } 307 if (tx != TX_RB) { 309 String b = a1.retrieveB(); 310 assertNull("Wrong relation a1 : ", b); 311 a1.assignB("b0"); 313 } 314 checkIsInitialState(); 315 } 316 317 318 321 public void _testBasicSet(int tx) throws Exception { 322 String c = null; 323 if ((tx == TX_CALL) || (tx == TX_RB)) { 324 utx.begin(); 325 } 326 ARemote a1 = ahome.findByPrimaryKey("a1"); 327 ARemote a21 = ahome.findByPrimaryKey("a21"); 328 if (tx == TX_CONT) { 329 a1.assignBInNewTx(a21.retrieveB()); 330 } else { 331 a1.assignB(a21.retrieveB()); 332 } 333 if (tx == TX_CALL) { 334 utx.commit(); 335 } else if (tx == TX_RB) { 336 utx.rollback(); 337 } 338 if (tx != TX_RB) { 339 String idB1 = a1.retrieveB(); 341 ARemote a0 = ahome.findByPrimaryKey("a0"); 342 ARemote a2 = ahome.findByPrimaryKey("a2"); 343 ARemote a3 = ahome.findByPrimaryKey("a3"); 344 ARemote a20 = ahome.findByPrimaryKey("a20"); 345 ARemote a22 = ahome.findByPrimaryKey("a22"); 346 String idB0 = a0.retrieveB(); 347 String idB2 = a2.retrieveB(); 348 String idB3 = a3.retrieveB(); 349 String idB20 = a20.retrieveB(); 350 String idB21 = a21.retrieveB(); 351 String idB22 = a22.retrieveB(); 352 assertEquals("Wrong new relation a1->b2 : ", "b2", idB1); 353 assertEquals("Wrong new relation a0->b0 : ", "b0", idB0); 354 assertEquals("Wrong new relation a2->b0 : ", "b0", idB2); 355 assertEquals("Wrong new relation a20->b2 : ", "b2", idB20); 356 assertEquals("Wrong new relation a21->b2 : ", "b2", idB21); 357 assertEquals("Wrong new relation a22->b2 : ", "b2", idB22); 358 a1.assignB("b0"); 360 } 361 checkIsInitialState(); 362 } 363 364 365 366 367 368 371 public void _testCohRemoveA(int tx) throws Exception { 372 if (tx == TX_CONT) { 373 fail("Transaction cannot be initiate by the container for this test"); 376 } 377 if ((tx == TX_CALL) || (tx == TX_RB)) { 378 utx.begin(); 379 } 380 ahome.remove("a0"); 381 if (tx == TX_CALL) { 382 utx.commit(); 383 } else if (tx == TX_RB) { 384 utx.rollback(); 385 } 386 if (tx != TX_RB) { 388 ARemote a0 = ahome.create("a0"); 389 a0.assignBInNewTx("b0"); 390 } 391 checkIsInitialState(); 392 } 393 394 395 public static Test suite() { 396 return new TestSuite(G_Relation_mouEC2.class); 397 } 398 399 public static void main(String args[]) { 400 String testtorun = null; 401 for (int argn = 0; argn < args.length; argn++) { 403 String s_arg = args[argn]; 404 Integer i_arg; 405 if (s_arg.equals("-n")) { 406 testtorun = args[++argn]; 407 } 408 } 409 if (testtorun == null) { 410 junit.textui.TestRunner.run(suite()); 411 } else { 412 junit.textui.TestRunner.run(new G_Relation_mouEC2(testtorun)); 413 } 414 } 415 416 } 417 | Popular Tags |