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.mnb.AHomeRemote; 40 import org.objectweb.jonas.jtests.beans.relation.mnb.ARemote; 41 import org.objectweb.jonas.jtests.beans.relation.mnb.BHomeRemote; 42 import org.objectweb.jonas.jtests.beans.relation.mnb.BRemote; 43 44 50 public class G_Relation_mnbEC2 extends A_Cmp2Util { 51 52 static Hashtable tbRelationA2B = new Hashtable (); 53 54 55 static { 56 tbRelationA2B.put("a0", new String []{}); 57 tbRelationA2B.put("a1", new String []{"b1", "b2"}); 58 tbRelationA2B.put("a2", new String []{"b1", "b2", "b3"}); 59 tbRelationA2B.put("a3", new String []{"b2", "b3", "b4"}); 60 for (Enumeration ea = tbRelationA2B.keys(); ea.hasMoreElements();) { 62 String aname = (String ) (ea.nextElement()); 63 String [] tb = (String []) tbRelationA2B.get(aname); 64 ArrayList col = new ArrayList (tb.length); 65 for (int i = 0; i < tb.length; i++) { 66 col.add(tb[i]); 67 } 68 tbRelationA2B.put(aname, col); 69 } 70 } 71 72 static Hashtable tbRelationB2A = new Hashtable (); 73 74 static { 75 tbRelationB2A.put("b0", new String []{}); 76 tbRelationB2A.put("b1", new String []{"a1", "a2"}); 77 tbRelationB2A.put("b2", new String []{"a1", "a2", "a3"}); 78 tbRelationB2A.put("b3", new String []{"a2", "a3"}); 79 tbRelationB2A.put("b4", new String []{"a3"}); 80 for (Enumeration ea1 = tbRelationB2A.keys(); ea1.hasMoreElements();) { 82 String aname1 = (String ) (ea1.nextElement()); 83 String [] tb1 = (String []) tbRelationB2A.get(aname1); 84 ArrayList col1 = new ArrayList (tb1.length); 85 for (int j = 0; j < tb1.length; j++) { 86 col1.add(tb1[j]); 87 } 88 tbRelationB2A.put(aname1, col1); 89 } 90 } 91 92 private static String BEAN_HOME_A = "relation_mnb_AHome"; 93 protected static AHomeRemote ahome = null; 94 private static String BEAN_HOME_B = "relation_mnb_BHome"; 95 protected static BHomeRemote bhome = null; 96 97 98 public G_Relation_mnbEC2(String name) { 99 super(name); 100 } 101 102 103 public void testEmpty() throws Exception { 104 } 105 protected static boolean isInit = false; 106 107 protected void setUp() { 108 super.setUp(); 109 boolean ok = false; 110 int nbtry = 0; 111 while (!ok && nbtry < 3) { 112 if (!isInit) { 113 useBeans("mnb", false); 115 try { 117 ahome = (AHomeRemote) PortableRemoteObject.narrow(ictx.lookup(BEAN_HOME_A), 118 AHomeRemote.class); 119 bhome = (BHomeRemote) PortableRemoteObject.narrow(ictx.lookup(BEAN_HOME_B), 120 BHomeRemote.class); 121 } catch (NamingException e) { 122 fail("Cannot get bean home: " + e.getMessage()); 123 } 124 try { 126 ahome.findByPrimaryKey("a0"); 127 } catch (Exception e) { 128 try { 129 utx.begin(); 131 ahome.create("a0"); 132 ahome.create("a1"); 133 ahome.create("a2"); 134 ahome.create("a3"); 135 bhome.create("b0"); 136 bhome.create("b1"); 137 bhome.create("b2"); 138 bhome.create("b3"); 139 bhome.create("b4"); 140 ARemote a0 = ahome.findByPrimaryKey("a0"); 141 ARemote a1 = ahome.findByPrimaryKey("a1"); 142 ARemote a2 = ahome.findByPrimaryKey("a2"); 143 ARemote a3 = ahome.findByPrimaryKey("a3"); 144 Collection rel_a0 = (Collection ) (tbRelationA2B.get("a0")); 145 a0.assignB(rel_a0); 146 Collection rel_a1 = (Collection ) (tbRelationA2B.get("a1")); 147 a1.assignB(rel_a1); 148 Collection rel_a2 = (Collection ) (tbRelationA2B.get("a2")); 149 a2.assignB(rel_a2); 150 Collection rel_a3 = (Collection ) (tbRelationA2B.get("a3")); 151 a3.assignB(rel_a3); 152 } catch (Exception i) { 153 fail("InitialState creation problem: "+i); 154 } finally { 155 try { 156 utx.commit(); 157 } catch (Exception ii) { 158 } 159 } 160 } 161 isInit = true; 162 } 163 nbtry++; 166 try { 167 if (initStateOK()) { 168 ok = true; 169 } 170 } catch (Exception e) { 171 } 172 if (!ok) { 173 debug("F_Relation_mnbEC2 setUp: unload bean and retry"); 174 isInit = false; 175 unloadBeans("mnb"); 176 } 177 } 178 } 179 180 184 boolean initStateOK() throws Exception { 185 boolean isOk = true; 186 msgerror = new StringBuffer (); 187 for (Enumeration ea = tbRelationA2B.keys(); ea.hasMoreElements();) { 189 String aname = (String ) (ea.nextElement()); 190 ARemote a = ahome.findByPrimaryKey(aname); 191 Collection colActual = a.retrieveB(); 192 ArrayList colExpected = (ArrayList ) (tbRelationA2B.get(aname)); 193 if (!isCollectionEqual(colExpected, colActual)) { 194 isOk = false; 195 msgerror = msgerror.append("Wrong relation for " + aname 196 + " (expected:" + colExpected 197 + ", found:" + colActual + ")"); 198 } 199 } 200 for (Enumeration ea1 = tbRelationB2A.keys(); ea1.hasMoreElements();) { 202 String aname1 = (String ) (ea1.nextElement()); 203 BRemote b = bhome.findByPrimaryKey(aname1); 204 Collection colActual1 = b.retrieveA(); 205 ArrayList colExpected1 = (ArrayList ) (tbRelationB2A.get(aname1)); 206 if (!isCollectionEqual(colExpected1, colActual1)) { 207 isOk = false; 208 msgerror = msgerror.append("Wrong relation for " + aname1 209 + " (expected:" + colExpected1 210 + ", found:" + colActual1 + ")"); 211 } 212 } 213 return isOk; 214 } 215 216 219 public void _testBasicGetEmpty(int tx) throws Exception { 220 Collection c; 221 if ((tx == TX_CALL) || (tx == TX_RB)) { 222 utx.begin(); 223 } 224 ARemote a = ahome.findByPrimaryKey("a0"); 225 if (tx == TX_CONT) { 226 c = a.retrieveBInNewTx(); 227 } else { 228 c = a.retrieveB(); 229 } 230 if (tx == TX_CALL) { 231 utx.commit(); 232 } else if (tx == TX_RB) { 233 utx.rollback(); 234 } 235 checkIsInitialState(); 236 } 237 238 public void testBasicGetEmptyTxNo() throws Exception { 239 _testBasicGetEmpty(TX_NO); 240 } 241 242 public void testBasicGetEmptyTxCall() throws Exception { 243 _testBasicGetEmpty(TX_CALL); 244 } 245 246 public void testBasicGetEmptyTxCont() throws Exception { 247 _testBasicGetEmpty(TX_CONT); 248 } 249 250 253 public void _testBasicSetEmpty(int tx) throws Exception { 254 ArrayList c = new ArrayList (1); 255 c.add("b0"); 256 if ((tx == TX_CALL) || (tx == TX_RB)) { 257 utx.begin(); 258 } 259 ARemote a = ahome.findByPrimaryKey("a0"); 260 if (tx == TX_CONT) { 261 a.assignBInNewTx(c); 262 } else { 263 a.assignB(c); 264 } 265 if (tx == TX_CALL) { 266 utx.commit(); 267 } else if (tx == TX_RB) { 268 utx.rollback(); 269 } 270 Collection c_value = a.retrieveB(); 272 if (tx != TX_RB) { 273 assertTrue("Wrong relations a0 (required:" + c + ", found:" + c_value 274 + ")", isCollectionEqual(c_value, c)); 275 } else { 276 assertTrue("Wrong relation a0->b0 : ", c_value.isEmpty()); 277 } 278 if (tx != TX_RB) { 280 a.assignB(new ArrayList ()); 281 } 282 checkIsInitialState(); 283 } 284 285 public void testBasicSetEmptyTxNo() throws Exception { 286 _testBasicSetEmpty(TX_NO); 287 } 288 289 public void testBasicSetEmptyTxCall() throws Exception { 290 _testBasicSetEmpty(TX_CALL); 291 } 292 293 public void testBasicSetEmptyTxCont() throws Exception { 294 _testBasicSetEmpty(TX_CONT); 295 } 296 297 300 public void _testBasicClear(int tx) throws Exception { 301 if ((tx == TX_CALL) || (tx == TX_RB)) { 302 utx.begin(); 303 } 304 ARemote a2 = ahome.findByPrimaryKey("a2"); 305 if (tx == TX_CONT) { 306 a2.clearBInNewTx(); 307 } else { 308 a2.clearB(); 309 } 310 if (tx == TX_CALL) { 311 utx.commit(); 312 } else if (tx == TX_RB) { 313 utx.rollback(); 314 } 315 Collection a2_value = a2.retrieveB(); 317 BRemote b1 = bhome.findByPrimaryKey("b1"); 318 BRemote b2 = bhome.findByPrimaryKey("b2"); 319 BRemote b3 = bhome.findByPrimaryKey("b3"); 320 ArrayList collb1 = new ArrayList (1); 321 collb1.add("a1"); 322 ArrayList collb2 = new ArrayList (2); 323 collb2.add("a1"); 324 collb2.add("a3"); 325 ArrayList collb3 = new ArrayList (1); 326 collb3.add("a3"); 327 Collection col=null; 328 if (tx != TX_RB) { 329 assertTrue("Wrong relations a2 (required: Empty "+", found:" + a2_value 330 + ")", a2_value.isEmpty()); 331 col = bhome.findByPrimaryKey("b1").retrieveA(); 332 assertTrue("Wrong collection returned by b1.getA(): expected:" + collb1 333 + " found: " + col, isCollectionEqual(collb1, col)); 334 col = bhome.findByPrimaryKey("b2").retrieveA(); 335 assertTrue("Wrong collection returned by b2.getA(): expected:" + collb2 336 + " found: " + col, isCollectionEqual(collb2, col)); 337 col = bhome.findByPrimaryKey("b3").retrieveA(); 338 assertTrue("Wrong collection returned by b3.getA(): expected:" + collb3 339 + " found: " + col, isCollectionEqual(collb3, col)); 340 ArrayList a2undo = new ArrayList (3); 342 a2undo.add("b1"); 343 a2undo.add("b2"); 344 a2undo.add("b3"); 345 a2.assignB(a2undo); 346 } 347 checkIsInitialState(); 348 } 349 350 351 352 353 public static Test suite() { 354 return new TestSuite(G_Relation_mnbEC2.class); 355 } 356 357 public static void main(String args[]) { 358 String testtorun = null; 359 for (int argn = 0; argn < args.length; argn++) { 361 String s_arg = args[argn]; 362 Integer i_arg; 363 if (s_arg.equals("-n")) { 364 testtorun = args[++argn]; 365 } 366 } 367 if (testtorun == null) { 368 junit.textui.TestRunner.run(suite()); 369 } else { 370 junit.textui.TestRunner.run(new G_Relation_mnbEC2(testtorun)); 371 } 372 } 373 374 } 375 | Popular Tags |