1 22 package org.jboss.test.tm.resource; 23 24 import java.io.Serializable ; 25 import java.util.HashMap ; 26 27 import javax.naming.InitialContext ; 28 import javax.transaction.Status ; 29 import javax.transaction.Transaction ; 30 import javax.transaction.TransactionManager ; 31 32 import org.jboss.logging.Logger; 33 34 39 public class Operation 40 implements Serializable 41 { 42 static final long serialVersionUID = 6263843332702708629L; 43 public static final int BEGIN = -1; 44 public static final int COMMIT = -2; 45 public static final int ROLLBACK = -3; 46 public static final int SUSPEND = -4; 47 public static final int RESUME = -5; 48 public static final int SETROLLBACK = -6; 49 public static final int STATUS = -7; 50 public static final int STATE = 0; 51 public static final int CREATE = 1; 52 public static final int ENLIST = 2; 53 public static final int DIFFRM = 3; 54 public static final int SETSTATUS = 4; 55 public static final int CREATE_LOCAL = 5; 56 public static final int FAIL_LOCAL = 6; 57 58 static HashMap resources = new HashMap (); 59 static HashMap transactions = new HashMap (); 60 61 static Logger log; 62 63 static TransactionManager tm = null; 64 65 Integer id; 66 int op; 67 int status; 68 Throwable throwable; 69 70 public Operation(int op, int id) 71 { 72 this(op, id, 0); 73 } 74 75 public Operation(int op, int id, int status) 76 { 77 this(op, id, status, null); 78 } 79 80 public Operation(int op, int id, int status, Throwable throwable) 81 { 82 this.id = new Integer (id); 83 this.op = op; 84 this.status = status; 85 this.throwable = throwable; 86 } 87 88 public void perform() throws Exception 89 { 90 Throwable caught = null; 91 try 92 { 93 switch (op) 94 { 95 case BEGIN: 96 begin(); 97 break; 98 case COMMIT: 99 commit(); 100 break; 101 case ROLLBACK: 102 rollback(); 103 break; 104 case SUSPEND: 105 suspend(); 106 break; 107 case RESUME: 108 resume(); 109 break; 110 case SETROLLBACK: 111 setRollbackOnly(); 112 break; 113 case STATUS: 114 checkStatus(); 115 break; 116 case STATE: 117 checkState(); 118 break; 119 case CREATE: 120 create(); 121 break; 122 case CREATE_LOCAL: 123 createLocal(); 124 break; 125 case FAIL_LOCAL: 126 failLocal(); 127 break; 128 case ENLIST: 129 enlist(); 130 break; 131 case DIFFRM: 132 differentRM(); 133 break; 134 case SETSTATUS: 135 setStatus(); 136 break; 137 default: 138 throw new IllegalArgumentException ("Invalid operation " + op); 139 } 140 } 141 catch (Throwable t) 142 { 143 caught = t; 144 } 145 if (throwable != null && caught == null) 146 throw new Exception ("Expected throwable " + throwable); 147 if (throwable != null && (throwable.getClass().isAssignableFrom(caught.getClass())) == false) 148 { 149 caught.printStackTrace(); 150 throw new Exception ("Expected throwable " + throwable + " was " + caught); 151 } 152 if (throwable == null && caught != null) 153 { 154 caught.printStackTrace(); 155 throw new Exception ("Unexpected throwable " + caught); 156 } 157 } 158 159 public void begin() throws Exception 160 { 161 log.info("BEGIN " + id); 162 getTM().begin(); 163 Transaction tx = getTM().getTransaction(); 164 transactions.put(id, tx); 165 log.info("BEGUN " + tx); 166 } 167 168 public void commit() throws Exception 169 { 170 log.info("COMMIT " + id); 171 assertTx(id); 172 getTM().commit(); 173 int status = getTM().getStatus(); 174 if (Status.STATUS_NO_TRANSACTION != status) 175 throw new Exception ("Expected no thread association after commit status=" + status); 176 log.info("COMMITTED " + id); 177 } 178 179 public void rollback() throws Exception 180 { 181 log.info("ROLLBACK " + id); 182 assertTx(id); 183 getTM().rollback(); 184 int status = getTM().getStatus(); 185 if (Status.STATUS_NO_TRANSACTION != status) 186 throw new Exception ("Expected no thread association after rollback status=" + status); 187 log.info("ROLLEDBACK " + id); 188 } 189 190 public void suspend() throws Exception 191 { 192 log.info("SUSPEND " + id); 193 assertTx(id); 194 getTM().suspend(); 195 log.info("SUSPENDED " + id); 196 } 197 198 public void resume() throws Exception 199 { 200 log.info("RESUME " + id); 201 getTM().resume(getTx(id)); 202 assertTx(id); 203 log.info("RESUMED " + id); 204 } 205 206 public void setRollbackOnly() throws Exception 207 { 208 log.info("SETROLLBACK " + id); 209 getTx(id).setRollbackOnly(); 210 log.info("SETTEDROLLBACK " + id); 211 } 212 213 public void checkStatus() throws Exception 214 { 215 log.info("CHECKSTATUS " + id); 216 int actualStatus = getTx(id).getStatus(); 217 log.info("CHECKINGSTATUS " + id + " Expected " + status + " was " + actualStatus); 218 if (actualStatus != status) 219 throw new Exception ("Transaction " + id + " Expected status " + status + " was " + actualStatus); 220 } 221 222 public void checkState() throws Exception 223 { 224 log.info("CHECKSTATE " + id); 225 int actualStatus = getRes(id).getStatus(); 226 log.info("CHECKINGSTATE " + id + " Expected " + status + " was " + actualStatus); 227 if (actualStatus != status) 228 throw new Exception ("Resource " + id + " Expected state " + status + " was " + actualStatus); 229 } 230 231 public void create() throws Exception 232 { 233 log.info("CREATE " + id); 234 Resource res = new Resource(id); 235 resources.put(id, res); 236 log.info("CREATED " + res); 237 } 238 239 public void createLocal() throws Exception 240 { 241 log.info("CREATE_LOCAL " + id); 242 Resource res = new LocalResource(id); 243 resources.put(id, res); 244 log.info("CREATED_LOCAL " + res); 245 } 246 247 public void enlist() throws Exception 248 { 249 log.info("ENLIST " + id); 250 Transaction tx = getTM().getTransaction(); 251 if (tx.enlistResource(getRes(id)) == false) 252 throw new Exception ("Unable to enlist resource"); 253 log.info("ENLISTED " + id + " " + tx); 254 } 255 256 public void differentRM() throws Exception 257 { 258 log.info("DIFFRM " + id); 259 getRes(id).newResourceManager(); 260 } 261 262 public void failLocal() throws Exception 263 { 264 log.info("FAIL_LOCAL " + id); 265 LocalResource resource = (LocalResource) getRes(id); 266 resource.failLocal(); 267 } 268 269 public void setStatus() throws Exception 270 { 271 log.info("SETSTATUS " + id + " " + status); 272 getRes(id).setStatus(status); 273 log.info("SETTEDSTATUS " + id + " " + status); 274 } 275 276 public static void start(Logger log) 277 throws Exception 278 { 279 Operation.log = log; 280 if (getTM().getTransaction() != null) 281 throw new IllegalStateException ("Invalid thread association " + getTM().getTransaction()); 282 reset(); 283 } 284 285 public static void end() 286 { 287 reset(); 288 } 289 290 public static void reset() 291 { 292 resources.clear(); 293 transactions.clear(); 294 } 295 296 public static void tidyUp() 297 { 298 try 299 { 300 if (getTM().getStatus() != Status.STATUS_NO_TRANSACTION) 301 { 302 log.warn("TIDYING UP AFTER BROKEN TEST!"); 303 getTM().rollback(); 304 } 305 } 306 catch (Exception ignored) 307 { 308 } 309 } 310 311 public Resource getRes(Integer id) 312 { 313 Resource res = (Resource) resources.get(id); 314 if (res == null) 315 throw new IllegalStateException ("No resource: " + id); 316 return res; 317 } 318 public Transaction getTx(Integer id) 319 { 320 Transaction tx = (Transaction ) transactions.get(id); 321 if (tx == null) 322 throw new IllegalStateException ("No transaction: " + id); 323 return tx; 324 } 325 326 public void assertTx(Integer id) 327 throws Exception 328 { 329 Transaction tx = getTx(id); 330 Transaction current = getTM().getTransaction(); 331 log.info("Asserting tx " + tx + " current " + current); 332 if (tx.equals(current) == false) 333 throw new IllegalStateException ("Expected tx " + tx + " was " + current); 334 } 335 336 public static TransactionManager getTM() 337 throws Exception 338 { 339 if (tm == null) 340 tm = (TransactionManager ) new InitialContext ().lookup("java:/TransactionManager"); 341 return tm; 342 } 343 } 344 | Popular Tags |