1 25 26 package org.objectweb.jonas.jtests.clients.exception; 27 28 import java.rmi.RemoteException ; 29 30 import javax.ejb.FinderException ; 31 import javax.ejb.RemoveException ; 32 import javax.transaction.RollbackException ; 33 import javax.transaction.Status ; 34 import javax.transaction.TransactionRolledbackException ; 35 36 import junit.framework.Assert; 37 38 import org.objectweb.jonas.jtests.beans.beanexc.AccountE; 39 import org.objectweb.jonas.jtests.beans.beanexc.AccountEHome; 40 import org.objectweb.jonas.jtests.beans.beanexc.AccountPK; 41 import org.objectweb.jonas.jtests.beans.beanexc.AccountS; 42 import org.objectweb.jonas.jtests.beans.beanexc.AppException; 43 44 public abstract class A_CatcherEntity extends A_Catcher { 45 46 public A_CatcherEntity(String name) { 47 super(name); 48 } 49 50 53 public AccountE getAccount(int i) { 54 AccountE ac = null; 55 try { 56 ac = getHome().findByNoAccount(i); 57 } catch (FinderException e) { 58 fail("FinderException in getAccount :" + e); 59 } catch (RemoteException e) { 60 fail("RemoteException in getAccount :" + e); 61 } 62 return ac; 63 } 64 65 69 public AccountS getAccountS(int i) { 70 AccountS acs = null; 71 try { 72 acs = getHome().findByNoAccount(i); 73 } catch (FinderException e) { 74 fail("FinderException in getAccountS :" + e); 75 } catch (RemoteException e) { 76 fail("RemoteException in getAccountS :" + e); 77 } 78 return acs; 79 } 80 81 84 public abstract AccountEHome getHome(); 85 86 87 91 100 public void testApplicationCallerTx2() throws Exception { 101 AccountE ac = getAccount(87); 102 long oldbalance = ac.getBalance(); 103 utx.begin(); 104 try { 105 ac.doAppException_1(); 107 fail("No AppException"); 108 } catch (AppException e) { 109 assertTrue(utx.getStatus() == Status.STATUS_ACTIVE); 110 assertEquals("On AppException", 5000, ac.getBalance()); 111 } finally { 112 utx.commit(); 113 assertEquals("After commit", 5000, ac.getBalance()); 114 } 115 } 116 117 126 public void testApplicationContTx2() throws Exception { 127 AccountE ac = getAccount(82); 128 long oldbalance = ac.getBalance(); 129 try { 130 ac.doAppException_2(false); 133 fail("No AppException"); 134 } catch (AppException e) { 135 assertEquals(50, ac.getBalance()); 136 } 137 } 138 139 148 public void testApplicationContTxRb2() throws Exception { 149 AccountE ac = getAccount(82); 150 long oldbalance = ac.getBalance(); 151 try { 152 ac.doAppException_2(true); 155 fail("No AppException"); 156 } catch (AppException e) { 157 assertEquals(oldbalance, ac.getBalance()); 158 } 159 } 160 161 170 public void testApplicationHomeUserTx() throws Exception { 171 utx.begin(); 172 try { 173 getHome().create(true); 175 fail("No AppException"); 176 } catch (AppException e) { 177 } finally { 178 utx.commit(); 179 } 180 try { 181 getHome().findByNoAccount(1951); 182 fail("bean has been created"); 183 } catch (FinderException e) { 184 } 185 } 186 187 197 public void testApplicationHomeContTx() throws Exception { 198 try { 199 getHome().create(0); 201 fail("No AppException"); 202 } catch (AppException e) { 203 } 204 try { 205 getHome().findByNoAccount(1951); 206 fail("bean has been created"); 207 } catch (FinderException e) { 208 } 209 } 210 211 217 public void testApplicationHomeNoTx() throws Exception { 218 try { 219 getHome().create(true); 223 fail("No AppException"); 224 } catch (AppException e) { 225 } 226 } 231 232 239 public void testApplicationRemovePkContTx() throws Exception { 240 int nac = 999990; 241 try { 242 AccountPK pk = new AccountPK(nac); 243 getHome().remove(pk); 245 fail("No RemoveException"); 246 } catch (RemoveException e) { 247 } 248 AccountE ac = getAccount(nac); 250 Assert.assertEquals("For exception in ejbRemove", ac.getCustomer()); 251 } 252 253 260 public void testApplicationRemovePkUserTx() throws Exception { 261 int nac = 999991; 262 utx.begin(); 263 try { 264 AccountPK pk = new AccountPK(nac); 265 getHome().remove(pk); 267 fail("No RemoveException"); 268 } catch (RemoveException e) { 269 } finally { 270 utx.commit(); 271 } 272 AccountE ac = getAccount(nac); 274 Assert.assertEquals("For exception in ejbRemove", ac.getCustomer()); 275 } 276 277 285 public void testApplicationRemoveThisContTx() throws Exception { 286 int nac = 999992; 287 AccountE ac; 288 try { 289 ac = getAccount(nac); 290 ac.remove(); 292 fail("No RemoveException"); 293 } catch (RemoveException e) { 294 } 295 ac = getAccount(nac); 297 Assert.assertEquals("For exception in ejbRemove", ac.getCustomer()); 298 } 299 300 308 public void testApplicationRemoveThisUserTx() throws Exception { 309 int nac = 999993; 310 AccountE ac; 311 utx.begin(); 312 try { 313 ac = getAccount(nac); 314 ac.remove(); 316 fail("No RemoveException"); 317 } catch (RemoveException e) { 318 } finally { 319 utx.commit(); 320 } 321 ac = getAccount(nac); 323 Assert.assertEquals("For exception in ejbRemove", ac.getCustomer()); 324 } 325 326 330 338 public void testRuntimeContTx() throws Exception { 339 AccountE ac = getAccount(81); 340 try { 341 ac.doFailedEjbStore_2(); 342 fail("ejbStore should raise TransactionRolledbackException"); 343 } catch (RemoteException e) { 344 assertTransactionRolledback(e); 345 } 346 } 347 348 355 public void testRuntimeCallerTx() throws Exception { 356 AccountE ac = getAccount(81); 357 utx.begin(); 358 ac.doFailedEjbStore_1(); 359 try { 360 utx.commit(); 361 fail("commit should raise RollbackException"); 362 } catch (RollbackException e) { 363 } 364 } 365 366 370 379 public void testDestroyedAfterEJBContTx() throws Exception { 380 AccountS ac = getAccountS(101); 381 try { 382 ac.doEJBException_2(); 383 fail("No RemoteException"); 384 } catch (RemoteException e) { 385 } 386 assertFalse("instance hasn't been destroyed", ac.iAmDestroyed()); 387 } 388 389 398 public void testDestroyedAfterEJBUserTx() throws Exception { 399 AccountS ac = getAccountS(101); 400 utx.begin(); 401 try { 402 try { 403 ac.doEJBException_sup(); 404 fail("No RemoteException"); 405 } catch (RemoteException e) { 406 assertTransactionRolledback(e); 407 } 408 try { 409 assertTrue("instance should be destroyed", ac.iAmDestroyed()); 410 fail("business method on discarded instance succeeded"); 411 } catch (RemoteException e) { 412 } 413 } finally { 414 utx.rollback(); 415 } 416 } 417 418 426 public void testDestroyedAfterEJBNoTx() throws Exception { 427 AccountS ac = getAccountS(101); 428 try { 429 ac.doEJBException_sup(); 430 fail("No RemoteException"); 431 } catch (RemoteException e) { 432 } 433 assertFalse("instance hasn't been destroyed", ac.iAmDestroyed()); 434 } 435 436 public void testDiscardedInstances() throws Exception { 437 AccountS ac = getAccountS(101); 438 utx.begin(); 439 try { 440 try { 441 ac.doEJBException_sup(); 442 fail("No RemoteException"); 443 } catch (RemoteException e) { 444 assertTransactionRolledback(e); 445 } 446 try { 447 assertTrue("instance should be destroyed", ac.iAmDestroyed()); 448 fail("business method on discarded instance succeeded"); 449 } catch (RemoteException e) { 450 } 451 } finally { 452 utx.rollback(); 453 } 454 try { 455 ac.doEJBException_2(); 456 fail("No RemoteException"); 457 } catch (RemoteException e) { 458 } 459 assertFalse("instance hasn't been destroyed (cont tx)", ac.iAmDestroyed()); 460 } 461 462 466 473 public void testUncheckedHomeContTx() throws Exception { 474 try { 475 getHome().create(1); fail("No RemoteException"); 477 } catch (RemoteException e) { 478 } 479 try { 481 AccountE ac = getHome().findByNoAccount(1951); 482 fail("rollback should have removed bean"); 483 } catch (FinderException e) { 484 } 485 } 486 487 497 public void testUncheckedHomeUserTx() throws Exception { 498 utx.begin(); 499 try { 500 getHome().create(1); fail("No RemoteException"); 502 } catch (RemoteException e) { 503 assertTransactionRolledback(e); 504 } finally { 505 utx.rollback(); 506 } 507 try { 509 getHome().findByNoAccount(1951); 510 fail("rollback should have removed bean"); 511 } catch (FinderException e) { 512 } 513 } 514 515 private void assertTransactionRolledback(RemoteException e) 516 throws RemoteException { 517 if ((!(e instanceof java.rmi.ServerException ) 519 || !(e.detail instanceof TransactionRolledbackException )) && !(e instanceof TransactionRolledbackException )) { 520 throw e; 521 } 522 } 523 } 524 | Popular Tags |