1 25 26 package org.objectweb.jonas.jtests.clients.entity; 27 28 import javax.naming.NamingException ; 29 import javax.rmi.PortableRemoteObject ; 30 31 import junit.framework.Test; 32 import junit.framework.TestSuite; 33 34 import org.objectweb.jonas.jtests.beans.relation.oob.AHomeRemote; 35 import org.objectweb.jonas.jtests.beans.relation.oob.ARemote; 36 import org.objectweb.jonas.jtests.beans.relation.oob.BHomeRemote; 37 import org.objectweb.jonas.jtests.beans.relation.oob.BRemote; 38 39 43 public class G_Relation_oobEC2 extends A_Cmp2Util { 44 45 private static String BEAN_HOME_A = "relation_oob_AHome"; 46 private static String BEAN_HOME_B = "relation_oob_BHome"; 47 protected static AHomeRemote ahome = null; 48 protected static BHomeRemote bhome = null; 49 50 51 public G_Relation_oobEC2(String name) { 52 super(name); 53 } 54 55 56 public void testEmpty() throws Exception { 57 } 58 59 protected static boolean isInit = false; 60 61 protected void setUp() { 62 super.setUp(); 63 boolean ok = false; 64 int nbtry = 0; 65 while (!ok && nbtry < 3) { 66 if (!isInit) { 67 useBeans("oob", false); 69 try { 71 ahome = (AHomeRemote) PortableRemoteObject.narrow(ictx.lookup(BEAN_HOME_A), 72 AHomeRemote.class); 73 bhome = (BHomeRemote) PortableRemoteObject.narrow(ictx.lookup(BEAN_HOME_B), 74 BHomeRemote.class); 75 } catch (NamingException e) { 76 fail("Cannot get bean home: " + e.getMessage()); 77 } 78 try { 80 ahome.findByPrimaryKey("a2"); 81 } catch (Exception e) { 82 try { 84 utx.begin(); 85 ARemote a1 = ahome.create("a1"); 86 ARemote a2 = ahome.create("a2"); 87 ahome.create("a3"); 88 bhome.create("b1"); 89 bhome.create("b2"); 90 bhome.create("b3"); 91 a1.assignB("b1"); 92 a2.assignB("b2"); 93 } catch (Exception i) { 94 fail("InitialState creation problem: " + i); 95 } finally { 96 try { 97 utx.commit(); 98 } catch (Exception ii) { 99 } 100 } 101 } 102 isInit = true; 103 } 104 nbtry++; 107 try { 108 if (initStateOK()) { 109 ok = true; 110 } 111 } catch (Exception e) { 112 } 113 if (!ok) { 114 isInit = false; 115 unloadBeans("oob"); 116 } 117 } 118 } 119 120 124 boolean initStateOK() throws Exception { 125 boolean isOk = true; 126 127 msgerror = new StringBuffer (); 128 129 ARemote a1 = ahome.findByPrimaryKey("a1"); 130 BRemote b1 = bhome.findByPrimaryKey("b1"); 131 ARemote a2 = ahome.findByPrimaryKey("a2"); 132 BRemote b2 = bhome.findByPrimaryKey("b2"); 133 ARemote a3 = ahome.findByPrimaryKey("a3"); 134 BRemote b3 = bhome.findByPrimaryKey("b3"); 135 String idB1 = a1.retrieveB(); 136 String idB2 = a2.retrieveB(); 137 String idB3 = a3.retrieveB(); 138 String idA1 = b1.retrieveA(); 139 String idA2 = b2.retrieveA(); 140 String idA3 = b3.retrieveA(); 141 142 if (idB1 != null && !idB1.equals("b1")) { 143 isOk = false; 144 msgerror.append("\nWrong relation for a1->b1" 145 + "(expected: A1.retrieveB()='b1'" 146 + ", found:" + idB1 + ")"); 147 } 148 if (idB2 != null && !idB2.equals("b2")) { 149 isOk = false; 150 msgerror.append("\nWrong relation for a2->b2" 151 + "(expected: A2.retrieveB()='b2'" 152 + ", found:" + idB2 + ")"); 153 } 154 if (idA1 != null && !idA1.equals("a1")) { 155 isOk = false; 156 msgerror.append("\nWrong relation for b1->a1" 157 + "(expected: B1.retrieveA()='a1'" 158 + ", found:" + idA1 + ")"); 159 } 160 if (idA2 != null && !idA2.equals("a2")) { 161 isOk = false; 162 msgerror.append("\nWrong relation for b2->a2" 163 + "(expected: B2.retrieveA()='a2'" 164 + ", found:" + idA2 + ")"); 165 } 166 if (idA3 != null) { 167 isOk = false; 168 msgerror.append("\nWrong relation for a3->b3" 169 + "(expected: A3.retrieveB()=null" 170 + ", found:" + idA3 + ")"); 171 } 172 if (idB3 != null) { 173 isOk = false; 174 msgerror.append("\nWrong relation for b3->a3" 175 + "(expected: B3.retrieveA()=null" 176 + ", found:" + idB3 + ")"); 177 } 178 return isOk; 179 } 180 181 182 185 public void _testBasicGetEmpty(int tx) throws Exception { 186 String idB = null; 187 if ((tx == TX_CALL) || (tx == TX_RB)) { 188 utx.begin(); 189 } 190 ARemote a = ahome.findByPrimaryKey("a3"); 191 if (tx == TX_CONT) { 192 idB = a.retrieveBInNewTx(); 193 } else { 194 idB = a.retrieveB(); 195 } 196 if (tx == TX_CALL) { 197 utx.commit(); 198 } else if (tx == TX_RB) { 199 utx.rollback(); 200 } 201 checkIsInitialState(); 202 } 203 204 205 206 209 public void _testBasicSetEmpty(int tx) throws Exception { 210 if ((tx == TX_CALL) || (tx == TX_RB)) { 211 utx.begin(); 212 } 213 ARemote a = ahome.findByPrimaryKey("a3"); 214 if (tx == TX_CONT) { 215 a.assignBInNewTx("b3"); 216 } else { 217 a.assignB("b3"); 218 } 219 if (tx == TX_CALL) { 220 utx.commit(); 221 } else if (tx == TX_RB) { 222 utx.rollback(); 223 } 224 String idB = a.retrieveB(); 226 if (tx != TX_RB) { 227 assertEquals("Wrong assign null for relation a3->b3 : ","b3", idB); 228 } else { 229 assertNull("Wrong assign null for relation a3->b3 : ", idB); 230 } 231 if (tx != TX_RB) { 233 a.assignB(null); 234 } 235 checkIsInitialState(); 236 } 237 238 public void testBasicSetEmptyTxNo() throws Exception { 239 _testBasicSetEmpty(TX_NO); 240 } 241 public void testBasicSetEmptyTxCall() throws Exception { 242 _testBasicSetEmpty(TX_CALL); 243 } 244 public void testBasicSetEmptyTxCont() throws Exception { 245 _testBasicSetEmpty(TX_CONT); 246 } 247 248 251 public void _testBasicSetEmptyNull(int tx) throws Exception { 252 if ((tx == TX_CALL) || (tx == TX_RB)) { 253 utx.begin(); 254 } 255 ARemote a = ahome.findByPrimaryKey("a1"); 256 if (tx == TX_CONT) { 257 a.assignBInNewTx(null); 258 } else { 259 a.assignB(null); 260 } 261 if (tx == TX_CALL) { 262 utx.commit(); 263 } else if (tx == TX_RB) { 264 utx.rollback(); 265 } 266 String idB = a.retrieveB(); 268 if (tx == TX_RB) { 269 assertEquals("Wrong assign null for relation a1->b1: ", "b1", idB); 270 } else { 271 assertNull("Wrong assign null for relation a1->b1: " + idB, idB); 272 } 273 if (tx != TX_RB) { 275 a.assignB("b1"); 276 } 277 checkIsInitialState(); 278 } 279 280 281 285 public void _testCohSetOne(int tx) throws Exception { 286 if ((tx == TX_CALL) || (tx == TX_RB)) { 287 utx.begin(); 288 } 289 ARemote a1 = ahome.findByPrimaryKey("a1"); 290 BRemote b1 = bhome.findByPrimaryKey("b1"); 291 ARemote a2 = ahome.findByPrimaryKey("a2"); 292 BRemote b2 = bhome.findByPrimaryKey("b2"); 293 ARemote a3 = ahome.findByPrimaryKey("a3"); 294 BRemote b3 = bhome.findByPrimaryKey("b3"); 295 if (tx == TX_CONT) { 297 a1.assignBInNewTx("b2"); 298 } else { 299 a1.assignB("b2"); 300 } 301 if (tx == TX_CALL) { 302 utx.commit(); 303 } else if (tx == TX_RB) { 304 utx.rollback(); 305 } 306 if (tx != TX_RB) { 307 assertNull("Bad coherence of relation null : expected for a2.retreiveB() found :" + a2.retrieveB(), a2.retrieveB()); 309 assertNull("Bad coherence of relation null : expected for b1.retreiveA() found :" + b1.retrieveA(), b1.retrieveA()); 310 assertEquals("Bad coherence of relation a1 : expected for b2.retreiveA() found :" + b2.retrieveA(), "a1", b2.retrieveA()); 311 assertEquals("Bad coherence of relation b2 : expected for a1.retreiveB() found :" + a1.retrieveB(), "b2", a1.retrieveB()); 312 313 a1.assignB("b1"); 315 a2.assignB("b2"); 316 } 317 checkIsInitialState(); 318 } 319 320 321 322 323 public static Test suite() { 324 return new TestSuite(G_Relation_oobEC2.class); 325 } 326 327 public static void main (String args[]) { 328 String testtorun = null; 329 for (int argn = 0; argn < args.length; argn++) { 331 String s_arg = args[argn]; 332 Integer i_arg; 333 if (s_arg.equals("-n")) { 334 testtorun = args[++argn]; 335 } 336 } 337 if (testtorun == null) { 338 junit.textui.TestRunner.run(suite()); 339 } else { 340 junit.textui.TestRunner.run(new G_Relation_oobEC2(testtorun)); 341 } 342 } 343 } 344 | Popular Tags |