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.omu.AHomeRemote; 40 import org.objectweb.jonas.jtests.beans.relation.omu.ARemote; 41 import org.objectweb.jonas.jtests.beans.relation.omu.BHomeRemote; 42 43 44 48 49 public class G_Relation_omuEC2 extends A_Cmp2Util { 50 51 52 private static String BEAN_HOME_A = "relation_omu_AHome"; 53 protected static AHomeRemote ahome = null; 54 private static String BEAN_HOME_B = "relation_omu_BHome"; 55 protected static BHomeRemote bhome = null; 56 57 static Hashtable tbRelationA2B = new Hashtable (); 58 59 static { 60 tbRelationA2B.put("as0", new String []{}); 61 tbRelationA2B.put("as1", new String []{"bs1_1"}); 62 tbRelationA2B.put("as2", new String []{"bs2_1", "bs2_2", "bs2_3"}); 63 tbRelationA2B.put("a0", new String []{}); 64 tbRelationA2B.put("a1", new String []{"b1_1", "b1_2", "b1_3"}); 65 tbRelationA2B.put("a2", new String []{"b2_1", "b2_2", "b2_3"}); 66 tbRelationA2B.put("a3", new String []{"b3_1"}); 67 for (Enumeration ea = tbRelationA2B.keys(); ea.hasMoreElements();) { 69 String aname = (String ) (ea.nextElement()); 70 String [] tb = (String []) tbRelationA2B.get(aname); 71 ArrayList col = new ArrayList (tb.length); 72 for (int i = 0; i < tb.length; i++) { 73 col.add(tb[i]); 74 } 75 tbRelationA2B.put(aname, col); 76 } 77 } 78 79 public G_Relation_omuEC2(String name) { 80 super(name); 81 } 83 84 85 public void testEmpty() throws Exception { 86 } 87 protected static boolean isInit = false; 88 89 protected void setUp() { 90 super.setUp(); 91 boolean ok = false; 92 int nbtry = 0; 93 while (!ok && nbtry < 3) { 94 if (!isInit) { 95 useBeans("omu", false); 97 try { 98 ahome = (AHomeRemote) PortableRemoteObject.narrow(ictx.lookup(BEAN_HOME_A), 99 AHomeRemote.class); 100 bhome = (BHomeRemote) PortableRemoteObject.narrow(ictx.lookup(BEAN_HOME_B), 101 BHomeRemote.class); 102 } catch (NamingException e) { 103 fail("Cannot get bean home: " + e.getMessage()); 104 } 105 try { 107 ahome.findByPrimaryKey("a0"); 108 } catch (Exception e) { 109 try { 110 utx.begin(); 111 bhome.create("bs0"); 112 bhome.create("bs1_1"); 113 bhome.create("bs2_1"); 114 bhome.create("bs2_2"); 115 bhome.create("bs2_3"); 116 bhome.create("bs3"); 117 bhome.create("b0"); 118 bhome.create("b1_1"); 119 bhome.create("b1_2"); 120 bhome.create("b1_3"); 121 bhome.create("b2_1"); 122 bhome.create("b2_2"); 123 bhome.create("b2_3"); 124 bhome.create("b3_1"); 125 ARemote as0 = ahome.create("as0"); 126 ARemote as1 = ahome.create("as1"); 127 ARemote as2 = ahome.create("as2"); 128 ARemote a0 = ahome.create("a0"); 129 ARemote a1 = ahome.create("a1"); 130 ARemote a2 = ahome.create("a2"); 131 ARemote a3 = ahome.create("a3"); 132 as1.assignB((Collection )tbRelationA2B.get("as1")); 133 as2.assignB((Collection )tbRelationA2B.get("as2")); 134 a1.assignB((Collection )tbRelationA2B.get("a1")); 135 a2.assignB((Collection )tbRelationA2B.get("a2")); 136 a3.assignB((Collection )tbRelationA2B.get("a3")); 137 } catch (Exception i) { 138 fail("InitialState creation problem:: "+i); 139 } finally { 140 try { 141 utx.commit(); 142 } catch (Exception ii) { 143 } 144 } 145 } 146 isInit = true; 147 } 148 nbtry++; 151 try { 152 if (initStateOK()) { 153 ok = true; 154 } 155 } catch (Exception e) { 156 } 157 if (!ok) { 158 isInit = false; 159 unloadBeans("omu"); 160 } 161 } 162 } 163 164 168 boolean initStateOK() throws Exception { 169 boolean isOk = true; 170 msgerror = new StringBuffer (); 171 for (Enumeration ea = tbRelationA2B.keys(); ea.hasMoreElements();) { 172 String aname = (String ) (ea.nextElement()); 173 ARemote a = ahome.findByPrimaryKey(aname); 174 Collection colActual = a.retrieveB(); 175 ArrayList colExpected = (ArrayList ) (tbRelationA2B.get(aname)); 176 debug("Relation for " + aname + " :: expected:" + colExpected + ", found:" + colActual ); 177 if (!isCollectionEqual(colExpected, colActual)) { 178 isOk = false; 179 msgerror = msgerror.append("Wrong relation for " + aname 180 + " (expected:" + colExpected 181 + ", found:" + colActual + ")"); 182 } 183 } 184 return isOk; 185 } 186 187 188 189 192 public void _testBasicGetEmpty(int tx) throws Exception { 193 Collection c = null; 194 if ((tx == TX_CALL) || (tx == TX_RB)) { 195 utx.begin(); 196 } 197 ARemote a = ahome.findByPrimaryKey("as0"); 198 if (tx == TX_CONT) { 199 c = a.retrieveBInNewTx(); 200 } else { 201 c = a.retrieveB(); 202 } 203 if (tx == TX_CALL) { 204 utx.commit(); 205 } else if (tx == TX_RB) { 206 utx.rollback(); 207 } 208 checkIsInitialState(); 209 } 210 211 public void testBasicGetEmptyTxNo() throws Exception { 212 _testBasicGetEmpty(TX_NO); 213 } 214 215 public void testBasicGetEmptyTxCall() throws Exception { 216 _testBasicGetEmpty(TX_CALL); 217 } 218 219 public void testBasicGetEmptyTxCont() throws Exception { 220 _testBasicGetEmpty(TX_CONT); 221 } 222 223 226 public void _testBasicGetOne(int tx) throws Exception { 227 228 Collection c = null; 229 if ((tx == TX_CALL) || (tx == TX_RB)) { 230 utx.begin(); 231 } 232 ARemote a = ahome.findByPrimaryKey("as1"); 233 if (tx == TX_CONT) { 234 c = a.retrieveBInNewTx(); 235 } else { 236 c = a.retrieveB(); 237 } 238 if (tx == TX_CALL) { 239 utx.commit(); 240 } else if (tx == TX_RB) { 241 utx.rollback(); 242 } 243 assertEquals("Wrong relations size for as1: ", 1, c.size()); 245 String s = "bs1_1"; 246 assertTrue("Relation missing " + s + " for as1: ", c.contains(s)); 247 checkIsInitialState(); 248 } 249 250 251 254 public void _testBasicGetMany(int tx) throws Exception { 255 256 Collection c = null; 257 if ((tx == TX_CALL) || (tx == TX_RB)) { 258 utx.begin(); 259 } 260 ARemote a = ahome.findByPrimaryKey("as2"); 261 if (tx == TX_CONT) { 262 c = a.retrieveBInNewTx(); 263 } else { 264 c = a.retrieveB(); 265 } 266 if (tx == TX_CALL) { 267 utx.commit(); 268 } else if (tx == TX_RB) { 269 utx.rollback(); 270 } 271 assertEquals("Relations size of: ", 3, c.size()); 273 String s = "bs2_1"; 274 assertTrue("Relation missing " + s + ": ", c.contains(s)); 275 s = "bs2_2"; 276 assertTrue("Relation missing " + s + ": ", c.contains(s)); 277 s = "bs2_3"; 278 assertTrue("Relation missing " + s + ": ", c.contains(s)); 279 checkIsInitialState(); 280 } 281 282 283 public void _testBasicSetEmpty(int tx) throws Exception { 284 ArrayList ce = new ArrayList (); 285 if ((tx == TX_CALL) || (tx == TX_RB)) { 286 utx.begin(); 287 } 288 ARemote a = ahome.findByPrimaryKey("as1"); 289 if (tx == TX_CONT) { 290 a.assignBInNewTx(ce); 291 } else { 292 a.assignB(ce); 293 } 294 if (tx == TX_CALL) { 295 utx.commit(); 296 } else if (tx == TX_RB) { 297 utx.rollback(); 298 } 299 Collection ca = a.retrieveB(); 301 if (tx != TX_RB) { 302 assertEquals("Relations size of: ", 0, ca.size()); 303 } else { 304 assertEquals("Relations size of: ", 1, ca.size()); 305 } 306 if (tx != TX_RB) { 308 a.addInB("bs1_1"); 309 ca = a.retrieveB(); 311 assertEquals("After undo, Relations size of: ", 1, ca.size()); 312 } 313 checkIsInitialState(); 314 } 315 316 public void testBasicSetEmptyTxNo() throws Exception { 317 _testBasicSetEmpty(TX_NO); 318 } 319 320 public void testBasicSetEmptyTxCall() throws Exception { 321 _testBasicSetEmpty(TX_CALL); 322 } 323 324 public void testBasicSetEmptyTxCont() throws Exception { 325 _testBasicSetEmpty(TX_CONT); 326 } 327 328 329 330 331 public static Test suite() { 332 return new TestSuite(G_Relation_omuEC2.class); 333 } 334 335 public static void main(String args[]) { 336 String testtorun = null; 337 for (int argn = 0; argn < args.length; argn++) { 339 String s_arg = args[argn]; 340 Integer i_arg; 341 if (s_arg.equals("-n")) { 342 testtorun = args[++argn]; 343 } 344 } 345 if (testtorun == null) { 346 junit.textui.TestRunner.run(suite()); 347 } else { 348 junit.textui.TestRunner.run(new G_Relation_omuEC2(testtorun)); 349 } 350 } 351 352 } 353 | Popular Tags |