1 25 package org.objectweb.easybeans.tests.transaction.containermanaged.base; 26 27 import static org.testng.Assert.assertTrue; 28 import static org.testng.Assert.fail; 29 30 import java.sql.SQLException ; 31 32 import javax.ejb.EJBException ; 33 import javax.transaction.UserTransaction ; 34 35 import org.objectweb.easybeans.log.JLog; 36 import org.objectweb.easybeans.log.JLogFactory; 37 import org.objectweb.easybeans.tests.common.ejbs.base.transaction.ItfContainerTransaction; 38 import org.objectweb.easybeans.tests.common.exception.AppException; 39 import org.objectweb.easybeans.tests.common.exception.AppRuntimeException; 40 import org.objectweb.easybeans.tests.common.exception.RollbackAppRuntimeException; 41 import org.objectweb.easybeans.tests.common.exception.RollbackApplicationException; 42 import org.objectweb.easybeans.tests.common.helper.EJBHelper; 43 import org.objectweb.easybeans.tests.common.helper.ExceptionHelper; 44 45 51 public abstract class TestExceptionHandleBase { 52 53 56 protected static final String DATABASE_1 = "jdbc_1"; 57 58 61 protected static final String DATABASE_2 = "jdbc_2"; 62 63 67 private ItfContainerTransaction sfsbContainerTransactionApp = null; 68 69 73 private ItfContainerTransaction sfsbContainerTransactionRollback = null; 74 75 79 private ItfContainerTransaction sfsbContainerTransactionApp01 = null; 80 81 85 private ItfContainerTransaction sfsbContainerTransactionApp02 = null; 86 87 90 private ItfContainerTransaction sfsbContainerTransactionRuntime = null; 91 92 95 private static JLog logger = JLogFactory.getLog(TestExceptionHandleBase.class); 96 97 101 public void setup() throws Exception { 102 ExceptionHandleUtil.cleanTransaction(); 104 createBeanApp(); 106 createBeanApp01(); 107 createBeanApp02(); 108 createBeanRollback(); 109 createBeanRuntime(); 110 deleteTable(); 112 } 113 114 119 public abstract void createBeanApp() throws Exception ; 120 121 126 public abstract void createBeanApp01() throws Exception ; 127 128 133 public abstract void createBeanApp02() throws Exception ; 134 135 140 public abstract void createBeanRollback() throws Exception ; 141 142 146 public abstract void createBeanRuntime() throws Exception ; 147 148 154 public void createBeanApp(final Class beanClass) throws Exception { 155 sfsbContainerTransactionApp = EJBHelper.getBeanRemoteInstance(beanClass, ItfContainerTransaction.class); 156 } 157 158 164 public void createBeanApp01(final Class beanClass) throws Exception { 165 sfsbContainerTransactionApp01 = EJBHelper.getBeanRemoteInstance(beanClass, ItfContainerTransaction.class); 166 } 167 168 174 public void createBeanApp02(final Class beanClass) throws Exception { 175 sfsbContainerTransactionApp02 = EJBHelper.getBeanRemoteInstance(beanClass, ItfContainerTransaction.class); 176 } 177 178 184 public void createBeanRollback(final Class beanClass) throws Exception { 185 sfsbContainerTransactionRollback = EJBHelper.getBeanRemoteInstance(beanClass, ItfContainerTransaction.class); 186 } 187 188 193 public void createBeanRuntime(final Class beanClass) throws Exception { 194 sfsbContainerTransactionRuntime = EJBHelper.getBeanRemoteInstance(beanClass, ItfContainerTransaction.class); 195 } 196 197 203 public void testNotUsingClientTransWithAppRollbackException() throws Exception { 204 try { 205 sfsbContainerTransactionRollback.insertCorrectFirstErrorSecond(DATABASE_1, DATABASE_2); 206 fail("The container did not re-throw the application exception."); 207 } catch (RollbackApplicationException e) { 208 logger.debug("The bean threw an expected error during the execution {0}", e); 209 } 210 ExceptionHandleUtil.verifyTable(DATABASE_1, ItfContainerTransaction.TABLE); 212 } 213 214 224 public void testUsingClientTransWithAppRollbackException(final boolean canCommit) throws Exception { 225 UserTransaction utx = ExceptionHandleUtil.getUserTransaction(); 226 utx.begin(); 227 try { 228 sfsbContainerTransactionRollback.insertCorrectFirstErrorSecond(DATABASE_1, DATABASE_2); 229 fail("The container did not re-throw the application exception."); 230 } catch (RollbackApplicationException e) { 231 logger.debug("The bean threw an expected error during the execution {0}", e); 232 } 233 try { 235 utx.commit(); 236 if (!canCommit) { 237 fail("The transaction was marked as rolled back, the client cannot make the commit."); 238 } 239 } catch (Exception e) { 240 logger.debug("The bean threw an expected error during the execution {0}", e); 241 } 242 ExceptionHandleUtil.verifyTable(DATABASE_1, ItfContainerTransaction.TABLE); 244 } 245 246 252 public void testUsingClientTransWithAppException() throws Exception { 253 UserTransaction utx = ExceptionHandleUtil.getUserTransaction(); 254 utx.begin(); 255 try { 256 sfsbContainerTransactionApp.insertCorrectFirstErrorSecond(DATABASE_1, DATABASE_2); 257 fail("The container did not re-throw the application exception."); 258 } catch (AppException e) { 259 logger.debug("The bean threw an expected error during the execution {0}", e); 260 } 261 try { 263 utx.commit(); 264 } catch (Exception e) { 265 logger.debug("The bean threw an expected error during the execution {0}", e); 266 } 267 ExceptionHandleUtil.verifyTable(DATABASE_1, ItfContainerTransaction.TABLE); 269 } 270 271 277 public void testNotUsingClientTransWithAppException() throws Exception { 278 try { 279 sfsbContainerTransactionApp.insertCorrectFirstErrorSecond(DATABASE_1, DATABASE_2); 280 fail("The container did not re-throw the application exception."); 281 } catch (AppException e) { 282 logger.debug("The bean threw an expected error during the execution {0}", e); 283 } 284 ExceptionHandleUtil.verifyTable(DATABASE_1, ItfContainerTransaction.TABLE); 286 } 287 288 294 public void testNotUsingClientTransWithAppRuntimeRollbackException() throws Exception { 295 try { 296 sfsbContainerTransactionApp02.insertCorrectFirstErrorSecond(DATABASE_1, DATABASE_2); 297 fail("The container did not re-throw the application exception."); 298 } catch (RollbackAppRuntimeException e) { 299 logger.debug("The bean threw an expected error during the execution {0}", e); 300 } 301 ExceptionHandleUtil.verifyTable(DATABASE_1, ItfContainerTransaction.TABLE); 303 } 304 305 315 public void testUsingClientTransWithAppRuntimeRollbackException(final boolean canCommit) throws Exception { 316 UserTransaction utx = ExceptionHandleUtil.getUserTransaction(); 317 utx.begin(); 318 try { 319 sfsbContainerTransactionApp02.insertCorrectFirstErrorSecond(DATABASE_1, DATABASE_2); 320 fail("The container did not re-throw the application exception."); 321 } catch (RollbackAppRuntimeException e) { 322 logger.debug("The bean threw an expected error during the execution {0}", e); 323 } 324 try { 326 utx.commit(); 327 if (!canCommit) { 328 fail("The transaction was marked as rolled back, the client cannot make the commit."); 329 } 330 } catch (Exception e) { 331 logger.debug("The bean threw an expected error during the execution {0}", e); 332 } 333 ExceptionHandleUtil.verifyTable(DATABASE_1, ItfContainerTransaction.TABLE); 335 } 336 337 343 public void testNotUsingClientTransWithAppRuntimeException() throws Exception { 344 try { 345 sfsbContainerTransactionApp01.insertCorrectFirstErrorSecond(DATABASE_1, DATABASE_2); 346 fail("The container did not re-throw the application exception."); 347 } catch (AppRuntimeException e) { 348 logger.debug("The bean threw an expected error during the execution {0}", e); 349 } 350 ExceptionHandleUtil.verifyTable(DATABASE_1, ItfContainerTransaction.TABLE); 352 } 353 354 360 public void testUsingClientTransWithAppRuntimeException() throws Exception { 361 UserTransaction utx = ExceptionHandleUtil.getUserTransaction(); 362 utx.begin(); 363 try { 364 sfsbContainerTransactionApp01.insertCorrectFirstErrorSecond(DATABASE_1, DATABASE_2); 365 fail("The container did not re-throw the application exception."); 366 } catch (AppRuntimeException e) { 367 logger.debug("The bean threw an expected error during the execution {0}", e); 368 } 369 try { 371 utx.commit(); 372 } catch (Exception e) { 373 logger.debug("The bean threw an expected error during the execution {0}", e); 374 } 375 ExceptionHandleUtil.verifyTable(DATABASE_1, ItfContainerTransaction.TABLE); 377 } 378 379 384 public void testNotUsingClientTransWithRuntimeException() throws Exception { 385 try { 387 sfsbContainerTransactionRuntime.insertCorrectFirstErrorSecond(DATABASE_1, DATABASE_2); 388 fail("The container did not throw the EJBException."); 389 } catch (EJBException e) { 390 logger.debug("The bean threw an expected error during the execution {0}", e); 391 } 392 393 assertTrue(ExceptionHandleUtil.isDiscarded(sfsbContainerTransactionRuntime), 395 "There was a runtime exception and the container did not discarded the bean."); 396 397 399 ExceptionHandleUtil.verifyTable(DATABASE_1, ItfContainerTransaction.TABLE); 401 402 } 403 404 415 public void testUsingClientTransWithRuntimeException(final Class expectedException, final boolean canCommit) 416 throws Exception { 417 UserTransaction utx = ExceptionHandleUtil.getUserTransaction(); 418 utx.begin(); 419 try { 421 sfsbContainerTransactionRuntime.insertCorrectFirstErrorSecond(DATABASE_1, DATABASE_2); 422 } catch (Exception e) { 423 assertTrue(ExceptionHelper.isEquals(e, expectedException), 424 "The container did not throw the correct exception, the expected exception is " 425 + expectedException.getName() + ", but the container threw " + e.getClass().getName()); 426 } 427 try { 429 utx.commit(); 430 if (!canCommit) { 431 fail("The transaction was marked as rolled back, the client cannot make the commit."); 432 } 433 } catch (Exception e) { 434 logger.debug("The bean threw an expected error during the execution {0}", e); 435 } 436 assertTrue(ExceptionHandleUtil.isDiscarded(sfsbContainerTransactionRuntime), 438 "There was a runtime exception and the container did not discarded the bean."); 439 if (!canCommit) { 441 try { 442 ExceptionHandleUtil.verifyTable(DATABASE_1, ItfContainerTransaction.TABLE); 444 fail("There was an error during the commit and the transaction was not rolled back."); 445 } catch (SQLException e) { 446 logger.debug("The bean threw an expected error during the execution {0}", e); 447 } 448 } 449 450 452 } 453 454 458 public void deleteTable() throws Exception { 459 ExceptionHandleUtil.deleteTable(DATABASE_1, ItfContainerTransaction.TABLE); 461 ExceptionHandleUtil.deleteTable(DATABASE_2, ItfContainerTransaction.TABLE); 462 } 463 464 } 465 | Popular Tags |