1 25 package org.objectweb.easybeans.tests.transaction.containermanaged.stateful; 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.transaction.UserTransaction ; 33 34 import org.objectweb.easybeans.log.JLog; 35 import org.objectweb.easybeans.log.JLogFactory; 36 import org.objectweb.easybeans.tests.common.ejbs.stateful.containermanaged.transaction.ItfSessionSync; 37 import org.objectweb.easybeans.tests.common.ejbs.stateful.containermanaged.transaction.SFSBSessionSync; 38 import org.objectweb.easybeans.tests.common.helper.EJBHelper; 39 import org.objectweb.easybeans.tests.transaction.containermanaged.base.TestContainerTransactionBase; 40 import org.testng.annotations.BeforeClass; 41 import org.testng.annotations.BeforeMethod; 42 import org.testng.annotations.Test; 43 44 57 public class TestSessionSynchronization extends TestContainerTransactionBase { 58 59 62 private ItfSessionSync sfsbSessionSync; 63 64 67 private static JLog logger = JLogFactory.getLog(TestSessionSynchronization.class); 68 69 73 @BeforeMethod 74 public void createBean() throws Exception { 75 sfsbSessionSync = EJBHelper.getBeanRemoteInstance(SFSBSessionSync.class, ItfSessionSync.class); 76 sfsbSessionSync.startup(DATABASE_1, DATABASE_2); 77 } 78 79 83 @BeforeClass 84 @Override 85 public void setup() throws Exception { 86 super.setup(); 87 } 88 89 97 private void verifyCallbacks() throws Exception { 98 try { 101 verifyTable(DATABASE_1, ItfSessionSync.TABLE); 102 fail("The beforeCompletion method was not called " 103 + " or the afterBegin method and the method called were not in the same transaction."); 104 } catch (SQLException e) { 105 logger.debug("The method threw an expected exception {0}", e); 106 } 107 108 try { 111 verifyTable(DATABASE_2, ItfSessionSync.TABLE); 112 fail("The beforeCompletion method was not called."); 113 } catch (SQLException e) { 114 logger.debug("The method threw an expected exception {0}", e); 115 } 116 assertTrue(sfsbSessionSync.isRolledback()); 119 } 120 121 127 private void verifyTablesWithouCallbacks() throws Exception { 128 try { 131 verifyTable(DATABASE_1, ItfSessionSync.TABLE); 132 fail("The method afterbegin was called, but it is not correct in this case."); 133 } catch (SQLException e) { 134 logger.debug("The method threw an expected exception {0}", e); 135 } 136 try { 139 verifyTable(DATABASE_2, ItfSessionSync.TABLE); 140 logger.debug("The table was inserted correctly."); 141 } catch (SQLException e) { 142 fail("The container rolled back the transaction, but there is not transaction associated."); 143 } 144 } 145 146 156 @Test(dependsOnMethods = {"testSessionSyncWithUserTransAndSupports"}) 157 public void testSessionSyncWithRequired() throws Exception { 158 sfsbSessionSync.insertTableRequired(); 159 verifyCallbacks(); 160 } 161 162 172 @Test 173 public void testSessionSyncWithRequiresNew() throws Exception { 174 sfsbSessionSync.insertTableRequiredNew(); 175 verifyCallbacks(); 176 } 177 178 188 @Test 189 public void testSessionSyncWithMandatory() throws Exception { 190 UserTransaction utx = getUserTransaction(); 191 utx.begin(); 192 sfsbSessionSync.insertTableMandatory(); 193 try{ 194 utx.commit(); 195 }catch(Exception e){ 196 logger.debug("The method threw an expected exception {e}", e); 197 } 198 verifyCallbacks(); 199 } 200 201 210 @Test 211 public void testSessionSyncWithUserTransAndSupports() throws Exception { 212 UserTransaction utx = getUserTransaction(); 213 utx.begin(); 214 sfsbSessionSync.insertTableSupports(); 215 try{ 216 utx.commit(); 217 }catch(Exception e){ 218 logger.debug("The method threw an expected exception {e}", e); 219 } 220 verifyCallbacks(); 221 } 222 223 230 @Test 231 public void testSessionSyncWithOnlySupports() throws Exception { 232 sfsbSessionSync.insertTableSupports(); 233 verifyTablesWithouCallbacks(); 234 } 235 236 243 @Test 244 public void testSessionSyncWithNotSupported() throws Exception { 245 sfsbSessionSync.insertTableNotSupported(); 246 verifyTablesWithouCallbacks(); 247 } 248 249 256 @Test 257 public void testSessionSyncWithNever() throws Exception { 258 sfsbSessionSync.insertTableNever(); 259 verifyTablesWithouCallbacks(); 260 } 261 262 265 @BeforeMethod 266 @Override 267 public void deleteTable() { 268 deleteTable(DATABASE_1, ItfSessionSync.TABLE); 269 deleteTable(DATABASE_2, ItfSessionSync.TABLE); 270 } 271 272 276 @BeforeMethod 277 @Override 278 public void cleanTransaction() throws Exception { 279 super.cleanTransaction(); 280 } 281 282 } 283 | Popular Tags |