1 25 package org.objectweb.easybeans.tests.transaction.containermanaged.base; 26 27 import java.sql.SQLException ; 28 29 import javax.naming.NamingException ; 30 import javax.transaction.Status ; 31 import javax.transaction.UserTransaction ; 32 33 import org.objectweb.easybeans.log.JLog; 34 import org.objectweb.easybeans.log.JLogFactory; 35 import org.objectweb.easybeans.tests.common.ejbs.base.transaction.ItfContainerTransaction; 36 import org.objectweb.easybeans.tests.common.ejbs.stateless.containermanaged.ItfDatabaseManager; 37 import org.objectweb.easybeans.tests.common.ejbs.stateless.containermanaged.SLSBDatabaseManager; 38 import org.objectweb.easybeans.tests.common.exception.TransactionException; 39 import org.objectweb.easybeans.tests.common.helper.EJBHelper; 40 import org.objectweb.easybeans.tests.common.helper.EmbeddedHelper; 41 import org.objectweb.easybeans.tests.common.helper.TransactionHelper; 42 import org.objectweb.easybeans.tests.common.interfaces.ItfTestContainerManaged; 43 44 50 public class TestContainerTransactionBase implements ItfTestContainerManaged { 51 52 55 protected static final String DATABASE_1 = "jdbc_1"; 56 57 60 protected static final String DATABASE_2 = "jdbc_2"; 61 62 65 private ItfContainerTransaction sfsbContainerTransaction = null; 66 67 70 private ItfDatabaseManager slsbDatabaseManager; 71 72 73 76 private static JLog logger = JLogFactory.getLog(TestContainerTransactionBase.class); 77 78 85 protected void verifyTable(final String database, final String tableName) throws NamingException , SQLException { 86 slsbDatabaseManager.verifyTable(database, tableName); 87 } 88 89 93 public void setup() throws Exception { 94 EmbeddedHelper.bindDatasource(); 97 slsbDatabaseManager = EJBHelper.getBeanRemoteInstance(SLSBDatabaseManager.class, ItfDatabaseManager.class); 99 cleanTransaction(); 101 } 102 103 108 public void createBean(final Class beanClass) throws Exception { 109 sfsbContainerTransaction = EJBHelper.getBeanRemoteInstance(beanClass, ItfContainerTransaction.class); 110 } 111 112 119 private void executeErrorTransaction() throws Exception { 120 UserTransaction utx = getUserTransaction(); 121 utx.begin(); 123 sfsbContainerTransaction.insertCorrectTableInBothDB(DATABASE_1, DATABASE_2); 125 utx.rollback(); 127 } 128 129 133 public void testSetRollbackOnly() throws Exception { 134 try { 136 sfsbContainerTransaction.setRollbackOnly(DATABASE_1, DATABASE_2); 137 } catch (TransactionException e) { 138 throw e.getParentException(); 139 } 140 145 verifyTable(DATABASE_1, ItfContainerTransaction.TABLE); 146 verifyTable(DATABASE_2, ItfContainerTransaction.TABLE); 147 } 148 149 153 public void testGetRollbackOnly() throws Exception { 154 try { 155 sfsbContainerTransaction.getRollbackOnly(); 156 } catch (TransactionException e) { 157 throw e.getParentException(); 158 } 159 } 160 161 165 public void testUsingClientTrans() throws Exception { 166 executeErrorTransaction(); 167 168 verifyTable(DATABASE_1, ItfContainerTransaction.TABLE); 170 verifyTable(DATABASE_2, ItfContainerTransaction.TABLE); 172 } 173 174 177 public void deleteTable() { 178 deleteTable(DATABASE_1, ItfContainerTransaction.TABLE); 180 deleteTable(DATABASE_2, ItfContainerTransaction.TABLE); 181 } 182 183 188 protected void deleteTable(final String dbName, final String tableName) { 189 try { 191 slsbDatabaseManager.deleteTable(dbName, tableName); 192 } catch (SQLException e) { 193 logger.debug("The table delete threw an error during the execution {0}", e); 194 } catch (NamingException e) { 195 logger.debug("The table delete threw an error during the execution {0}", e); 196 } 197 } 198 199 203 protected ItfContainerTransaction getBean() { 204 return sfsbContainerTransaction; 205 } 206 207 208 216 public void testGetUserTransactionWithLookup() throws Exception { 217 try { 218 sfsbContainerTransaction.getUserTransactionWithLookup(); 219 } catch (TransactionException e) { 220 throw e.getParentException(); 221 } 222 } 223 224 232 public void testGetUserTransactionWithEJBContext() throws Exception { 233 try { 234 sfsbContainerTransaction.getUserTransactionWithEJBContext(); 235 } catch (TransactionException e) { 236 throw e.getParentException(); 237 } 238 } 239 240 245 protected UserTransaction getUserTransaction() throws Exception { 246 UserTransaction utx = TransactionHelper.getInternalUserTransaction(); 247 logger.debug("Transaction status after get = {0} ", new Integer (utx.getStatus())); 248 return utx; 249 } 250 251 255 public void cleanTransaction() throws Exception { 256 UserTransaction utx = TransactionHelper.getInternalUserTransaction(); 257 try { 258 if (transactionIsActive()) { 259 utx.rollback(); 260 } 261 } catch (Exception e) { 262 throw new Exception ("Cannot clean the transaction. The test cannot be started", e); 263 } 264 } 265 266 271 protected boolean transactionIsActive() throws Exception { 272 UserTransaction utx = TransactionHelper.getInternalUserTransaction(); 273 boolean bolResult = false; 274 if (utx != null) { 275 if (utx.getStatus() == Status.STATUS_ACTIVE) { 276 bolResult = true; 277 } 278 } 279 return bolResult; 280 } 281 282 } 283 | Popular Tags |