1 25 26 package org.objectweb.jonas.jtests.clients.entity; 27 28 import java.util.Collection ; 29 import java.util.Enumeration ; 30 import java.util.Hashtable ; 31 32 import javax.ejb.ObjectNotFoundException ; 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.s2pkcomp.AHomeRemote; 40 import org.objectweb.jonas.jtests.beans.relation.s2pkcomp.ARemote; 41 import org.objectweb.jonas.jtests.beans.relation.s2pkcomp.BHomeRemote; 42 import org.objectweb.jonas.jtests.beans.relation.s2pkcomp.BRemote; 43 import org.objectweb.jonas.jtests.beans.relation.s2pkcomp.Pk; 44 45 51 public class F_Relation_s2pkcompEC2 extends A_Cmp2Util { 52 53 54 private static String BEAN_HOME_A = "relation_s2pkcomp_AHome"; 55 private static String BEAN_HOME_B = "relation_s2pkcomp_BHome"; 56 protected static AHomeRemote ahome = null; 57 protected static BHomeRemote bhome = null; 58 59 static Hashtable a2b = new Hashtable (); 60 static Hashtable b2a = new Hashtable (); 61 static Pk pkNull = new Pk("null", 0); 62 63 static { 64 a2b.put(new Pk("a",1), new Pk("b",1)); 65 a2b.put(new Pk("a",2), new Pk("b",2)); 66 a2b.put(new Pk("a",3), pkNull); 67 b2a.put(new Pk("b",1), new Pk("a",1)); 68 b2a.put(new Pk("b",2), new Pk("a",2)); 69 b2a.put(new Pk("b",3), pkNull); 70 } 71 72 public F_Relation_s2pkcompEC2(String name) { 73 super(name); 74 } 75 76 protected static boolean isInit = false; 77 78 protected void setUp() { 79 super.setUp(); 80 boolean ok = false; 81 int nbtry = 0; 82 while (!ok && nbtry < 3) { 83 if (!isInit) { 84 useBeans("s2pkcomp", false); 86 try { 88 ahome = (AHomeRemote) PortableRemoteObject.narrow(ictx.lookup(BEAN_HOME_A), 89 AHomeRemote.class); 90 bhome = (BHomeRemote) PortableRemoteObject.narrow(ictx.lookup(BEAN_HOME_B), 91 BHomeRemote.class); 92 } catch (NamingException e) { 93 fail("Cannot get bean home: " + e.getMessage()); 94 } 95 try { 97 ahome.findByPrimaryKey(new Pk("a", 2)); 98 } catch (Exception e) { 99 try { 101 utx.begin(); 102 ARemote a1 = ahome.create("a", 1); 103 ARemote a2 = ahome.create("a", 2); 104 ahome.create("a", 3); 105 bhome.create("b", 1); 106 bhome.create("b", 2); 107 bhome.create("b", 3); 108 a1.assignB(new Pk("b", 1)); 109 a2.assignB(new Pk("b", 2)); 110 } catch (Exception i) { 111 fail("InitialState creation problem: "+i); 112 } finally { 113 try { 114 utx.commit(); 115 } catch (Exception ii) { 116 } 117 } 118 } 119 isInit = true; 120 } 121 nbtry++; 124 try { 125 if (initStateOK()) { 126 ok = true; 127 } 128 } catch (Exception e) { 129 } 130 if (!ok) { 131 isInit = false; 132 unloadBeans("s2pkcomp"); 133 } 134 } 135 } 136 137 141 boolean initStateOK() throws Exception { 142 boolean isOk = true; 143 msgerror = new StringBuffer (); 144 for (Enumeration ea = a2b.keys(); ea.hasMoreElements();) { 146 Pk apk = (Pk) (ea.nextElement()); 147 ARemote a = ahome.findByPrimaryKey(apk); 148 Pk bpkActual = a.retrieveB(); 149 Pk bpkExpected = (Pk)(a2b.get(apk)); 150 if (pkNull.equals(bpkExpected)) { 151 if (bpkActual!=null) { 152 isOk = false; 153 msgerror.append("\nWrong relation for " + apk.toString() 154 + " (expected: null" 155 + ", found:" + bpkActual.toString() + ")"); 156 } 157 } else { 158 if (!bpkExpected.equals(bpkActual)) { 159 isOk = false; 160 msgerror.append("\nWrong relation for " + apk.toString() 161 + " (expected:" + bpkExpected.toString() 162 + ", found:" + bpkActual.toString() + ")"); 163 } 164 } 165 } 166 for (Enumeration eb = b2a.keys(); eb.hasMoreElements();) { 168 Pk bpk = (Pk) (eb.nextElement()); 169 BRemote b = bhome.findByPrimaryKey(bpk); 170 Pk apkActual = b.retrieveA(); 171 Pk apkExpected = (Pk)(b2a.get(bpk)); 172 if (pkNull.equals(apkExpected)) { 173 if (apkActual!=null) { 174 isOk = false; 175 msgerror.append("\nWrong relation for " + bpk.toString() 176 + " (expected: null" 177 + ", found:" + apkActual.toString() + ")"); 178 } 179 } else { 180 if (!apkExpected.equals(apkActual)) { 181 isOk = false; 182 msgerror.append("\nWrong relation for " + bpk.toString() 183 + " (expected:" + apkExpected.toString() 184 + ", found:" + apkActual.toString() + ")"); 185 } 186 } 187 } 188 return isOk; 189 } 190 191 195 public void _testCohRemoveA(int tx) throws Exception { 196 if (tx == TX_CONT) { 197 fail("Transaction cannot be initiate by the container for this test"); 200 } 201 if ((tx == TX_CALL) || (tx == TX_RB)) { 202 utx.begin(); 203 } 204 BRemote b1 = bhome.findByPrimaryKey(new Pk("b", 1)); 205 ahome.remove(new Pk("a", 1)); 207 if (tx == TX_CALL) { 208 utx.commit(); 209 } else if (tx == TX_RB) { 210 utx.rollback(); 211 } 212 if (tx != TX_RB) { 213 assertNull("Bad coherence of relation : null expected for b1.retreiveB() found :"+b1.retrieveA(), b1.retrieveA()); 215 boolean not_found=false; 216 try { 217 ARemote a1 = ahome.findByPrimaryKey(new Pk("a", 1)); 218 } catch (ObjectNotFoundException e) { 219 not_found=true; 220 } 221 assertTrue("a-1 is not removed",not_found); 222 ahome.create("a", 1); 224 ARemote a1 = ahome.findByPrimaryKey(new Pk("a", 1)); 225 a1.assignB(new Pk("b", 1)); 226 } 227 checkIsInitialState(); 228 } 229 230 public void testCohRemoveATxNo() throws Exception { 231 _testCohRemoveA(TX_NO); 232 } 233 public void testCohRemoveATxCall() throws Exception { 234 _testCohRemoveA(TX_CALL); 235 } 236 public void testCohRemoveATxRb() throws Exception { 237 _testCohRemoveA(TX_RB); 238 } 239 240 246 public void _testCohBeanRemoveB(int tx) throws Exception { 247 if (tx == TX_CONT) { 248 fail("Transaction cannot be initiate by the container for this test"); 251 } 252 if ((tx == TX_CALL) || (tx == TX_RB)) { 253 utx.begin(); 254 } 255 ARemote a1 = ahome.findByPrimaryKey(new Pk("a", 1)); 256 BRemote b1 = bhome.findByPrimaryKey(new Pk("b", 1)); 257 b1.remove(); 259 if (tx == TX_CALL) { 260 utx.commit(); 261 } else if (tx == TX_RB) { 262 utx.rollback(); 263 } 264 if (tx != TX_RB) { 265 assertNull("Bad coherence of relation : null expected for a1.retreiveB() found :"+a1.retrieveB(), a1.retrieveB()); 267 boolean not_found=false; 268 try { 269 b1 = bhome.findByPrimaryKey(new Pk("b", 1)); 270 } catch (ObjectNotFoundException e) { 271 not_found=true; 272 } 273 assertTrue("b-1 is not removed", not_found); 274 bhome.create("b", 1); 276 a1.assignB(new Pk("b", 1)); 277 } 278 checkIsInitialState(); 279 } 280 281 public void testCohBeanRemoveBTxNo() throws Exception { 282 _testCohBeanRemoveB(TX_NO); 283 } 284 public void testCohBeanRemoveBTxCall() throws Exception { 285 _testCohBeanRemoveB(TX_CALL); 286 } 287 public void testCohBeanRemoveBTxRb() throws Exception { 288 _testCohBeanRemoveB(TX_RB); 289 } 290 291 295 public void _testFindByBIsNull(int tx) throws Exception { 296 if ((tx == TX_CALL) || (tx == TX_RB)) { 297 utx.begin(); 298 } 299 Collection ca = ahome.findByBIsNull(); 300 if (tx == TX_CALL) { 301 utx.commit(); 302 } else if (tx == TX_RB) { 303 utx.rollback(); 304 } 305 assertEquals("Bad result for findByBIsNull: ", 1, ca.size()); 307 } 308 309 public void testFindByBIsNullTxNo() throws Exception { 310 _testFindByBIsNull(TX_NO); 311 } 312 public void _testFindByBIsNullTxCall() throws Exception { 314 _testFindByBIsNull(TX_CALL); 315 } 316 public void _testFindByBIsNullTxRb() throws Exception { 318 _testFindByBIsNull(TX_RB); 319 } 320 321 324 public void _testFindAById1Id2(int tx) throws Exception { 325 if ((tx == TX_CALL) || (tx == TX_RB)) { 326 utx.begin(); 327 } 328 ARemote a1 = ahome.findById1Id2("a", 1); 329 if (tx == TX_CALL) { 330 utx.commit(); 331 } else if (tx == TX_RB) { 332 utx.rollback(); 333 } 334 Pk pkA1 = a1.getId(); 336 assertEquals("Bad id1 of a1: ", "a", pkA1.id1); 337 assertEquals("Bad id2 of a1: ", 1, pkA1.id2); 338 } 339 340 public void testFindAById1Id2TxNo() throws Exception { 341 _testFindAById1Id2(TX_NO); 342 } 343 public void testFindAById1Id2TxCall() throws Exception { 344 _testFindAById1Id2(TX_CALL); 345 } 346 public void testFindAById1Id2TxRb() throws Exception { 347 _testFindAById1Id2(TX_RB); 348 } 349 350 public static Test suite() { 351 return new TestSuite(F_Relation_s2pkcompEC2.class); 352 } 353 354 public static void main (String args[]) { 355 String testtorun = null; 356 for (int argn = 0; argn < args.length; argn++) { 358 String s_arg = args[argn]; 359 Integer i_arg; 360 if (s_arg.equals("-n")) { 361 testtorun = args[++argn]; 362 } 363 } 364 if (testtorun == null) { 365 junit.textui.TestRunner.run(suite()); 366 } else { 367 junit.textui.TestRunner.run(new F_Relation_s2pkcompEC2(testtorun)); 368 } 369 } 370 } 371 372 | Popular Tags |