1 22 package org.jboss.test.tm.test; 23 24 import javax.transaction.Transaction ; 25 import javax.transaction.TransactionManager ; 26 27 import org.jboss.test.JBossTestCase; 28 import org.jboss.tm.TransactionLocal; 29 import org.jboss.tm.TransactionManagerLocator; 30 31 public class TransactionLocalUnitTestCase extends JBossTestCase 32 { 33 protected TransactionManager tm = TransactionManagerLocator.getInstance().locate(); 34 35 public void testSimpleSetGet() throws Exception 36 { 37 TransactionLocal local = new TransactionLocal(tm); 38 tm.begin(); 39 try 40 { 41 local.set("Simple"); 42 assertEquals("Simple", local.get()); 43 } 44 finally 45 { 46 tm.commit(); 47 } 48 } 49 50 public void testSimpleSetNull() throws Exception 51 { 52 TransactionLocal local = new TransactionLocal(tm); 53 tm.begin(); 54 try 55 { 56 local.set(null); 57 } 58 finally 59 { 60 tm.commit(); 61 } 62 } 63 64 public void testSimpleGetWithNoInitial() throws Exception 65 { 66 TransactionLocal local = new TransactionLocal(tm); 67 tm.begin(); 68 try 69 { 70 assertEquals(null, local.get()); 71 } 72 finally 73 { 74 tm.commit(); 75 } 76 } 77 78 public void testSimpleGetWithInitial() throws Exception 79 { 80 TransactionLocal local = new TransactionLocal(tm) 81 { 82 protected Object initialValue() 83 { 84 return "Initial"; 85 } 86 }; 87 tm.begin(); 88 try 89 { 90 assertEquals("Initial", local.get()); 91 } 92 finally 93 { 94 tm.commit(); 95 } 96 } 97 98 public void testGetNoTx() throws Exception 99 { 100 TransactionLocal local = new TransactionLocal(tm); 101 assertEquals(null, local.get(null)); 102 } 103 104 public void testGetNoTxInitial() throws Exception 105 { 106 TransactionLocal local = new TransactionLocal(tm) 107 { 108 protected Object initialValue() 109 { 110 return "Initial"; 111 } 112 }; 113 assertEquals("Initial", local.get()); 114 } 115 116 public void testSetNoTx() throws Exception 117 { 118 TransactionLocal local = new TransactionLocal(tm); 119 try 120 { 121 local.set("Something"); 122 fail("Should not be here"); 123 } 124 catch (IllegalStateException expected) 125 { 126 } 127 } 128 129 public void testSimpleSetGetExplicit() throws Exception 130 { 131 TransactionLocal local = new TransactionLocal(tm); 132 tm.begin(); 133 Transaction tx = tm.suspend(); 134 try 135 { 136 local.set(tx, "Simple"); 137 assertEquals("Simple", local.get(tx)); 138 } 139 finally 140 { 141 tm.resume(tx); 142 tm.commit(); 143 } 144 } 145 146 public void testSimplePutNullExplicit() throws Exception 147 { 148 TransactionLocal local = new TransactionLocal(tm); 149 tm.begin(); 150 Transaction tx = tm.suspend(); 151 try 152 { 153 local.set(tx, null); 154 } 155 finally 156 { 157 tm.resume(tx); 158 tm.commit(); 159 } 160 } 161 162 public void testSimpleGetWithNoInitialExplicit() throws Exception 163 { 164 TransactionLocal local = new TransactionLocal(tm); 165 tm.begin(); 166 Transaction tx = tm.suspend(); 167 try 168 { 169 assertEquals(null, local.get(tx)); 170 } 171 finally 172 { 173 tm.resume(tx); 174 tm.commit(); 175 } 176 } 177 178 public void testSimpleGetWithInitialExplicit() throws Exception 179 { 180 TransactionLocal local = new TransactionLocal(tm) 181 { 182 protected Object initialValue() 183 { 184 return "Initial"; 185 } 186 }; 187 tm.begin(); 188 Transaction tx = tm.suspend(); 189 try 190 { 191 assertEquals("Initial", local.get(tx)); 192 } 193 finally 194 { 195 tm.resume(tx); 196 tm.commit(); 197 } 198 } 199 200 public void testGetNoTxExplicit() throws Exception 201 { 202 TransactionLocal local = new TransactionLocal(tm); 203 assertEquals(null, local.get(null)); 204 } 205 206 public void testGetNoTxInitialExplicit() throws Exception 207 { 208 TransactionLocal local = new TransactionLocal(tm) 209 { 210 protected Object initialValue() 211 { 212 return "Initial"; 213 } 214 }; 215 assertEquals("Initial", local.get(null)); 216 } 217 218 public void testSetNoTxExplicit() throws Exception 219 { 220 TransactionLocal local = new TransactionLocal(tm); 221 try 222 { 223 local.set(null, "Something"); 224 fail("Should not be here"); 225 } 226 catch (IllegalStateException expected) 227 { 228 } 229 } 230 231 public void testGetAfterCommit() throws Exception 232 { 233 TransactionLocal local = new TransactionLocal(tm); 234 tm.begin(); 235 try 236 { 237 local.set("Something"); 238 } 239 finally 240 { 241 tm.commit(); 242 } 243 assertEquals(null, local.get()); 244 } 245 246 public void testGetInitialAfterCommit() throws Exception 247 { 248 TransactionLocal local = new TransactionLocal(tm) 249 { 250 protected Object initialValue() 251 { 252 return "Initial"; 253 } 254 }; 255 tm.begin(); 256 try 257 { 258 local.set("Something"); 259 assertEquals("Something", local.get()); 260 } 261 finally 262 { 263 tm.commit(); 264 } 265 assertEquals("Initial", local.get()); 266 } 267 268 public void testGetMarkedRolledBack() throws Exception 269 { 270 TransactionLocal local = new TransactionLocal(tm); 271 tm.begin(); 272 tm.setRollbackOnly(); 273 try 274 { 275 assertEquals(null, local.get()); 276 } 277 finally 278 { 279 tm.rollback(); 280 } 281 } 282 283 public void testGetInitialMarkedRolledBack() throws Exception 284 { 285 TransactionLocal local = new TransactionLocal(tm) 286 { 287 protected Object initialValue() 288 { 289 return "Initial"; 290 } 291 }; 292 tm.begin(); 293 tm.setRollbackOnly(); 294 try 295 { 296 assertEquals("Initial", local.get()); 297 } 298 finally 299 { 300 tm.rollback(); 301 } 302 } 303 304 public void testSetMarkedRolledBack() throws Exception 305 { 306 TransactionLocal local = new TransactionLocal(tm); 307 tm.begin(); 308 tm.setRollbackOnly(); 309 try 310 { 311 local.set("Something"); 312 assertEquals("Something", local.get()); 313 } 314 finally 315 { 316 tm.rollback(); 317 } 318 } 319 320 public void testGetAfterComplete() throws Exception 321 { 322 TransactionLocal local = new TransactionLocal(tm); 323 tm.begin(); 324 Transaction tx = tm.getTransaction(); 325 try 326 { 327 local.set("Something"); 328 } 329 finally 330 { 331 tx.commit(); 332 } 333 assertEquals(null, local.get()); 334 tm.suspend(); 335 } 336 337 public void testGetInitialAfterComplete() throws Exception 338 { 339 TransactionLocal local = new TransactionLocal(tm) 340 { 341 protected Object initialValue() 342 { 343 return "Initial"; 344 } 345 }; 346 tm.begin(); 347 Transaction tx = tm.getTransaction(); 348 try 349 { 350 local.set("Something"); 351 assertEquals("Something", local.get()); 352 } 353 finally 354 { 355 tx.commit(); 356 } 357 assertEquals("Initial", local.get()); 358 tm.suspend(); 359 } 360 361 public void testSuspendResume() throws Exception 362 { 363 TransactionLocal local = new TransactionLocal(tm); 364 tm.begin(); 365 Transaction tx1 = tm.getTransaction(); 366 try 367 { 368 local.set("Something"); 369 assertEquals("Something", local.get()); 370 tm.suspend(); 371 tm.begin(); 372 try 373 { 374 Transaction tx2 = tm.getTransaction(); 375 assertEquals(null, local.get()); 376 assertEquals("Something", local.get(tx1)); 377 tm.suspend(); 378 tm.resume(tx1); 379 assertEquals("Something", local.get()); 380 assertEquals(null, local.get(tx2)); 381 tm.suspend(); 382 tm.resume(tx2); 383 } 384 finally 385 { 386 tm.commit(); 387 } 388 } 389 finally 390 { 391 tm.resume(tx1); 392 tm.commit(); 393 } 394 } 395 396 public TransactionLocalUnitTestCase(String name) 397 { 398 super(name); 399 } 400 } 401 | Popular Tags |