1 22 package org.jboss.test.cts.interfaces; 23 24 25 import javax.ejb.*; 26 27 import javax.transaction.Status ; 28 import javax.transaction.UserTransaction ; 29 import javax.transaction.RollbackException ; 30 31 import org.jboss.test.cts.keys.AccountPK; 32 33 43 public class UserTransactionTester 44 { 45 static org.jboss.logging.Logger log = 46 org.jboss.logging.Logger.getLogger(UserTransactionTester.class); 47 48 51 private CtsBmpHome home; 52 53 56 private UserTransaction ut; 57 58 61 private CtsBmp bean1; 62 63 66 private CtsBmp bean2; 67 68 71 public UserTransactionTester(CtsBmpHome home, 72 UserTransaction userTransaction) 73 { 74 this.home = home; 75 this.ut = userTransaction; 76 } 77 78 81 public boolean runAllTests() 82 { 83 if (!testBeginRollback()) 85 return false; 86 if (!testBeginCommit()) 87 return false; 88 if (!testBeginSetrollbackonlyRollback()) 89 return false; 90 if (!testBeginSetrollbackonlyCommit()) 91 return false; 92 93 try { 95 bean1 = home.create(new AccountPK("UT_TestBean1"), "Ole1"); 96 } catch (Exception ex) { 97 log.debug("failed", ex); 98 return false; 99 } 100 101 if (!testSingleRollback()) 103 return false; 104 if (!testSingleCommit()) 105 return false; 106 if (!testSingleSetrollbackonlyCommit()) 107 return false; 108 109 try { 111 ut.begin(); 112 bean2 = home.create(new AccountPK("UT_TestBean2"), "Ole2"); 113 ut.rollback(); 114 115 boolean gotException = false; 117 try { 118 bean2.setPersonsName("Ole"); 119 } catch (Exception e) { 120 log.info("IGNORE PREVIOUS NoSuchEntityException - it is intentional"); 121 gotException = true; 122 } 123 if (!gotException) 124 throw new RuntimeException ("Rollback didn't rollback create."); 125 } catch (Exception ex) { 126 log.debug("failed", ex); 127 return false; 128 } 129 130 try { 132 bean2 = home.create(new AccountPK("UT_TestBean2"), "Ole2"); 133 } catch (Exception ex) { 134 log.debug("failed", ex); 135 return false; 136 } 137 138 return true; 139 } 140 141 145 148 private boolean testBeginRollback() 149 { 150 try { 151 if (ut.getStatus() != Status.STATUS_NO_TRANSACTION) 152 throw new RuntimeException ("No tx should be active."); 153 154 ut.begin(); 155 if (ut.getStatus() != Status.STATUS_ACTIVE) 156 throw new RuntimeException ("New tx not active."); 157 ut.rollback(); 158 159 if (ut.getStatus() != Status.STATUS_NO_TRANSACTION) 160 throw new RuntimeException ("No tx should be active."); 161 } catch (Exception ex) { 162 log.debug("failed", ex); 163 return false; 164 } 165 return true; 166 } 167 168 171 private boolean testBeginCommit() 172 { 173 try { 174 if (ut.getStatus() != Status.STATUS_NO_TRANSACTION) 175 throw new RuntimeException ("No tx should be active."); 176 177 ut.begin(); 178 if (ut.getStatus() != Status.STATUS_ACTIVE) 179 throw new RuntimeException ("New tx not active."); 180 ut.commit(); 181 182 if (ut.getStatus() != Status.STATUS_NO_TRANSACTION) 183 throw new RuntimeException ("No tx should be active."); 184 } catch (Exception ex) { 185 log.debug("failed", ex); 186 return false; 187 } 188 return true; 189 } 190 191 194 private boolean testBeginSetrollbackonlyRollback() 195 { 196 try { 197 if (ut.getStatus() != Status.STATUS_NO_TRANSACTION) 198 throw new RuntimeException ("No tx should be active."); 199 200 ut.begin(); 201 if (ut.getStatus() != Status.STATUS_ACTIVE) 202 throw new RuntimeException ("New tx not active."); 203 ut.setRollbackOnly(); 204 if (ut.getStatus() != Status.STATUS_MARKED_ROLLBACK) 205 throw new RuntimeException ("Tx not marked for rollback."); 206 ut.rollback(); 207 208 if (ut.getStatus() != Status.STATUS_NO_TRANSACTION) 209 throw new RuntimeException ("No tx should be active."); 210 } catch (Exception ex) { 211 log.debug("failed", ex); 212 return false; 213 } 214 return true; 215 } 216 217 220 private boolean testBeginSetrollbackonlyCommit() 221 { 222 try { 223 if (ut.getStatus() != Status.STATUS_NO_TRANSACTION) 224 throw new RuntimeException ("No tx should be active."); 225 226 ut.begin(); 227 if (ut.getStatus() != Status.STATUS_ACTIVE) 228 throw new RuntimeException ("New tx not active."); 229 ut.setRollbackOnly(); 230 if (ut.getStatus() != Status.STATUS_MARKED_ROLLBACK) 231 throw new RuntimeException ("Tx not marked for rollback."); 232 233 boolean gotException = false; 234 try { 235 ut.commit(); 236 } catch (RollbackException rbe) { 237 gotException = true; 238 } 239 if (!gotException) 240 throw new RuntimeException ("Didn't get expected RollbackException."); 241 242 if (ut.getStatus() != Status.STATUS_NO_TRANSACTION) 243 throw new RuntimeException ("No tx should be active."); 244 } catch (Exception ex) { 245 log.debug("failed", ex); 246 return false; 247 } 248 return true; 249 } 250 251 252 256 259 private boolean testSingleRollback() 260 { 261 try { 262 if (ut.getStatus() != Status.STATUS_NO_TRANSACTION) 263 throw new RuntimeException ("No tx should be active."); 264 265 bean1.setPersonsName("Ole"); 266 if (!bean1.getPersonsName().equals("Ole")) 267 throw new RuntimeException ("Unable to set property."); 268 269 ut.begin(); 270 if (ut.getStatus() != Status.STATUS_ACTIVE) 271 throw new RuntimeException ("New tx not active."); 272 if (!bean1.getPersonsName().equals("Ole")) 273 throw new RuntimeException ("Property changes after begin."); 274 275 bean1.setPersonsName("Peter"); 276 if (!bean1.getPersonsName().equals("Peter")) 277 throw new RuntimeException ("Unable to set property."); 278 279 ut.rollback(); 280 if (!bean1.getPersonsName().equals("Ole")) 281 throw new RuntimeException ("Rollback doesn't work."); 282 283 284 if (ut.getStatus() != Status.STATUS_NO_TRANSACTION) 285 throw new RuntimeException ("No tx should be active."); 286 } catch (Exception ex) { 287 log.debug("failed", ex); 288 return false; 289 } 290 return true; 291 } 292 293 296 private boolean testSingleCommit() 297 { 298 try { 299 if (ut.getStatus() != Status.STATUS_NO_TRANSACTION) 300 throw new RuntimeException ("No tx should be active."); 301 302 bean1.setPersonsName("Ole"); 303 if (!bean1.getPersonsName().equals("Ole")) 304 throw new RuntimeException ("Unable to set property."); 305 306 ut.begin(); 307 if (ut.getStatus() != Status.STATUS_ACTIVE) 308 throw new RuntimeException ("New tx not active."); 309 if (!bean1.getPersonsName().equals("Ole")) 310 throw new RuntimeException ("Property changes after begin."); 311 312 bean1.setPersonsName("Peter"); 313 if (!bean1.getPersonsName().equals("Peter")) 314 throw new RuntimeException ("Unable to set property."); 315 316 ut.commit(); 317 if (!bean1.getPersonsName().equals("Peter")) 318 throw new RuntimeException ("Property not set after commit."); 319 320 321 if (ut.getStatus() != Status.STATUS_NO_TRANSACTION) 322 throw new RuntimeException ("No tx should be active."); 323 } catch (Exception ex) { 324 log.debug("failed", ex); 325 return false; 326 } 327 return true; 328 } 329 330 333 private boolean testSingleSetrollbackonlyCommit() 334 { 335 try { 336 if (ut.getStatus() != Status.STATUS_NO_TRANSACTION) 337 throw new RuntimeException ("No tx should be active."); 338 339 bean1.setPersonsName("Ole"); 340 if (!bean1.getPersonsName().equals("Ole")) 341 throw new RuntimeException ("Unable to set property."); 342 343 ut.begin(); 344 if (ut.getStatus() != Status.STATUS_ACTIVE) 345 throw new RuntimeException ("New tx not active."); 346 if (!bean1.getPersonsName().equals("Ole")) 347 throw new RuntimeException ("Property changes after begin."); 348 349 bean1.setPersonsName("Peter"); 350 if (!bean1.getPersonsName().equals("Peter")) 351 throw new RuntimeException ("Unable to set property."); 352 353 ut.setRollbackOnly(); 354 if (ut.getStatus() != Status.STATUS_MARKED_ROLLBACK) 355 throw new RuntimeException ("Tx not marked for rollback."); 356 357 boolean gotException = false; 358 try { 359 ut.commit(); 360 } catch (RollbackException rbe) { 361 gotException = true; 362 } 363 if (!gotException) 364 throw new RuntimeException ("Didn't get expected RollbackException."); 365 366 if (!bean1.getPersonsName().equals("Ole")) 367 throw new RuntimeException ("Didn't roll back."); 368 369 if (ut.getStatus() != Status.STATUS_NO_TRANSACTION) 370 throw new RuntimeException ("No tx should be active."); 371 } catch (Exception ex) { 372 log.debug("failed", ex); 373 return false; 374 } 375 return true; 376 } 377 } 378 | Popular Tags |