1 25 package org.objectweb.easybeans.tests.transaction.beanmanaged; 26 27 import java.sql.SQLException ; 28 29 import javax.ejb.EJBException ; 30 import javax.naming.NamingException ; 31 import javax.transaction.Status ; 32 import javax.transaction.UserTransaction ; 33 34 import org.objectweb.easybeans.log.JLog; 35 import org.objectweb.easybeans.log.JLogFactory; 36 import org.objectweb.easybeans.tests.common.asserts.Assert; 37 import org.objectweb.easybeans.tests.common.ejbs.stateful.beanmanaged.transaction.ItfBeanManagedTransaction; 38 import org.objectweb.easybeans.tests.common.ejbs.stateful.beanmanaged.transaction.SFSBBeanManagedTransaction; 39 import org.objectweb.easybeans.tests.common.ejbs.stateless.containermanaged.ItfDatabaseManager; 40 import org.objectweb.easybeans.tests.common.ejbs.stateless.containermanaged.SLSBDatabaseManager; 41 import org.objectweb.easybeans.tests.common.exception.TransactionException; 42 import org.objectweb.easybeans.tests.common.helper.EJBHelper; 43 import org.objectweb.easybeans.tests.common.helper.EmbeddedHelper; 44 import org.objectweb.easybeans.tests.common.helper.ExceptionHelper; 45 import org.objectweb.easybeans.tests.common.helper.TransactionHelper; 46 import org.testng.annotations.AfterClass; 47 import org.testng.annotations.BeforeClass; 48 import org.testng.annotations.BeforeMethod; 49 import org.testng.annotations.Test; 50 51 63 public class TestTransactionCommitSFSB { 64 65 68 private ItfBeanManagedTransaction sfsbBeanManagedTransaction; 69 70 73 private ItfDatabaseManager slsbDatabaseManager; 74 75 78 private static final String DATABASE = "jdbc_1"; 79 80 83 private static JLog logger = JLogFactory.getLog(TestTransactionCommitSFSB.class); 84 85 89 @BeforeClass 90 public void setup() throws Exception { 91 EmbeddedHelper.bindDatasource(); 94 95 slsbDatabaseManager = EJBHelper.getBeanRemoteInstance(SLSBDatabaseManager.class, ItfDatabaseManager.class); 97 } 98 99 106 @Test 107 public void testTransInTwoMethods() throws Exception { 108 try { 109 sfsbBeanManagedTransaction.insertTableWithoutCommitTransaction(); 111 sfsbBeanManagedTransaction.dropTableWithoutBeginTransaction(); 113 } catch (TransactionException e) { 114 throw e.getParentException(); 115 } 116 } 117 118 126 @Test(expectedExceptions = javax.transaction.NotSupportedException .class) 127 public void testBeginTwiceSameTrans() throws Exception { 128 try { 129 sfsbBeanManagedTransaction.insertTableWithoutCommitTransaction(); 131 sfsbBeanManagedTransaction.dropTableWithBeginCommitTransaction(); 133 } catch (TransactionException e) { 134 throw e.getParentException(); 135 } 136 137 } 138 139 148 @Test(expectedExceptions = javax.transaction.NotSupportedException .class) 149 public void testBeginTwoTransSameMethod() throws Exception { 150 try { 151 sfsbBeanManagedTransaction.insertTableWithNestedTrans(); 153 } catch (TransactionException e) { 154 throw e.getParentException(); 155 } 156 } 157 158 167 @Test(expectedExceptions = javax.transaction.NotSupportedException .class) 168 public void testBeginTwoTransDifMethod() throws Exception { 169 try { 170 sfsbBeanManagedTransaction.insertTableWithoutCommitTransaction(); 172 sfsbBeanManagedTransaction.insertTableWithNewTransaction(); 174 } catch (TransactionException e) { 175 throw e.getParentException(); 176 } 177 } 178 179 187 @Test 188 public void testUsingClientTransaction() throws Exception { 189 sfsbBeanManagedTransaction.insertTableWithoutCommitTransaction(); 191 UserTransaction utx = TransactionHelper.getInternalUserTransaction(); 193 utx.begin(); 195 sfsbBeanManagedTransaction.setRollback(); 199 try { 201 utx.commit(); 202 } catch (Exception e) { 203 logger.debug("Error when the transaction made a rollback {0}", e); 204 } 205 Integer [] expected = {new Integer (Status.STATUS_COMMITTED), new Integer (Status.STATUS_NO_TRANSACTION) }; 206 207 Assert.assertEquals(new Integer (utx.getStatus()), expected, 209 "After the commit the transaction must be commited or not_transaction"); 210 211 } 212 213 219 @Test 220 public void testSetRollbackOnly() throws Exception { 221 try{ 223 sfsbBeanManagedTransaction.setRollbackOnly(); 224 }catch(EJBException e){ 225 ExceptionHelper.checkCause(e, java.lang.IllegalStateException .class); 226 } 227 } 228 234 @Test 235 public void testgetRollbackOnly() throws Exception { 236 try{ 238 sfsbBeanManagedTransaction.getRollbackOnly(); 239 }catch(EJBException e){ 240 ExceptionHelper.checkCause(e, java.lang.IllegalStateException .class); 241 } 242 } 243 250 @Test 251 public void testTransInSameMethod() throws Exception { 252 try { 253 sfsbBeanManagedTransaction.insertTableWithBeginCommitTransaction(); 254 } catch (TransactionException e) { 255 throw e.getParentException(); 256 } 257 slsbDatabaseManager.verifyTable(DATABASE, ItfBeanManagedTransaction.TABLE); 259 260 } 261 262 271 @Test(expectedExceptions = SQLException .class) 272 public void testRollbackInSameMethod() throws Exception { 273 sfsbBeanManagedTransaction.insertTableWithBeginRollback(); 274 275 slsbDatabaseManager.verifyTable(DATABASE, ItfBeanManagedTransaction.TABLE); 278 279 } 280 281 285 @AfterClass 286 public void tierDown() throws Exception { 287 EmbeddedHelper.unbindDatasource(); 291 } 292 293 296 @BeforeMethod 297 public void deletesTable() { 298 try { 300 slsbDatabaseManager.deleteTable(DATABASE, ItfBeanManagedTransaction.TABLE); 302 } catch (SQLException e) { 303 logger.debug("The table delete threw an error during the execution {0}", e); 304 } catch (NamingException e) { 305 logger.debug("The table delete threw an error during the execution {0}", e); 306 } 307 } 308 309 313 @BeforeMethod 314 public void createBean() throws Exception { 315 sfsbBeanManagedTransaction = EJBHelper.getBeanRemoteInstance(SFSBBeanManagedTransaction.class, 316 ItfBeanManagedTransaction.class); 317 sfsbBeanManagedTransaction.startup(ItfBeanManagedTransaction.CREATE_TABLE, DATABASE); 318 } 319 } 320 | Popular Tags |