1 28 29 package org.apache.commons.transaction.memory; 30 31 import junit.framework.*; 32 33 import java.util.Collection ; 34 import java.util.HashMap ; 35 import java.util.Iterator ; 36 import java.util.Map ; 37 import java.util.logging.*; 38 39 import org.apache.commons.transaction.util.Jdk14Logger; 40 import org.apache.commons.transaction.util.LoggerFacade; 41 import org.apache.commons.transaction.util.RendezvousBarrier; 42 43 48 public class MapWrapperTest extends TestCase { 49 50 private static final Logger logger = Logger.getLogger(MapWrapperTest.class.getName()); 51 private static final LoggerFacade sLogger = new Jdk14Logger(logger); 52 53 protected static final long BARRIER_TIMEOUT = 20000; 54 55 protected static void report(String should, String is) { 57 if (!should.equals(is)) { 58 fail("\nWrong output:\n'" + is + "'\nShould be:\n'" + should + "'\n"); 59 } 60 } 61 62 protected static void checkCollection(Collection col, Object [] values) { 63 int cnt = 0; 64 int trueCnt = 0; 65 66 for (Iterator it = col.iterator(); it.hasNext();) { 67 cnt++; 68 Object value1 = it.next(); 69 for (int i = 0; i < values.length; i++) { 70 Object value2 = values[i]; 71 if (value2.equals(value1)) 72 trueCnt++; 73 } 74 } 75 assertEquals(cnt, values.length); 76 assertEquals(trueCnt, values.length); 77 } 78 79 public static Test suite() { 80 TestSuite suite = new TestSuite(MapWrapperTest.class); 81 return suite; 82 } 83 84 public static void main(java.lang.String [] args) { 85 junit.textui.TestRunner.run(suite()); 86 } 87 88 public MapWrapperTest(String testName) { 89 super(testName); 90 } 91 92 protected TransactionalMapWrapper getNewWrapper(Map map) { 93 return new TransactionalMapWrapper(map); 94 } 95 96 public void testBasic() throws Throwable { 97 98 logger.info("Checking basic transaction features"); 99 100 final Map map1 = new HashMap (); 101 102 final TransactionalMapWrapper txMap1 = getNewWrapper(map1); 103 104 assertTrue(txMap1.isEmpty()); 105 106 txMap1.put("key1", "value1"); 108 report("value1", (String ) map1.get("key1")); 109 assertFalse(txMap1.isEmpty()); 110 111 txMap1.startTransaction(); 113 assertFalse(txMap1.isEmpty()); 114 txMap1.put("key1", "value2"); 115 report("value1", (String ) map1.get("key1")); 116 report("value2", (String ) txMap1.get("key1")); 117 txMap1.commitTransaction(); 118 report("value2", (String ) map1.get("key1")); 119 report("value2", (String ) txMap1.get("key1")); 120 121 txMap1.startTransaction(); 123 txMap1.put("key1", "value3"); 124 txMap1.rollbackTransaction(); 125 report("value2", (String ) map1.get("key1")); 126 report("value2", (String ) txMap1.get("key1")); 127 } 128 129 public void testComplex() throws Throwable { 130 131 logger.info("Checking advanced and complex transaction features"); 132 133 final Map map1 = new HashMap (); 134 135 final TransactionalMapWrapper txMap1 = getNewWrapper(map1); 136 137 txMap1.put("key1", "value1"); 139 txMap1.put("key2", "value2"); 140 141 logger.info("Checking if global values are present"); 143 144 assertTrue(txMap1.containsValue("value1")); 145 assertTrue(txMap1.containsValue("value2")); 146 assertFalse(txMap1.containsValue("novalue")); 147 148 logger.info("Checking if global keys are present"); 150 assertTrue(txMap1.containsKey("key1")); 151 assertTrue(txMap1.containsKey("key2")); 152 assertFalse(txMap1.containsKey("nokey")); 153 154 txMap1.startTransaction(); 156 txMap1.put("key3", "value3"); 157 txMap1.put("key4", "value4"); 158 159 logger.info("Checking if values inside transactions are present"); 161 assertTrue(txMap1.containsValue("value1")); 162 assertTrue(txMap1.containsValue("value2")); 163 assertTrue(txMap1.containsValue("value3")); 164 assertTrue(txMap1.containsValue("value4")); 165 assertFalse(txMap1.containsValue("novalue")); 166 167 logger.info("Checking if keys inside transactions are present"); 169 assertTrue(txMap1.containsKey("key1")); 170 assertTrue(txMap1.containsKey("key2")); 171 assertTrue(txMap1.containsKey("key3")); 172 assertTrue(txMap1.containsKey("key4")); 173 assertFalse(txMap1.containsKey("nokey")); 174 175 logger.info("Checking remove inside transactions"); 177 txMap1.remove("key1"); 178 assertFalse(txMap1.containsKey("key1")); 179 assertFalse(txMap1.containsValue("value1")); 180 assertNull(txMap1.get("key1")); 181 assertEquals(3, txMap1.size()); 182 183 txMap1.remove("key3"); 185 assertFalse(txMap1.containsKey("key3")); 186 assertFalse(txMap1.containsValue("value3")); 187 assertNull(txMap1.get("key3")); 188 assertEquals(2, txMap1.size()); 189 190 logger.info("Checking remove and propagation after commit"); 191 txMap1.commitTransaction(); 192 193 txMap1.remove("key1"); 194 assertFalse(txMap1.containsKey("key1")); 195 assertFalse(txMap1.containsValue("value1")); 196 assertNull(txMap1.get("key1")); 197 assertFalse(txMap1.containsKey("key3")); 198 assertFalse(txMap1.containsValue("value3")); 199 assertNull(txMap1.get("key3")); 200 assertEquals(2, txMap1.size()); 201 } 202 203 public void testSets() throws Throwable { 204 205 logger.info("Checking set opertaions"); 206 207 final Map map1 = new HashMap (); 208 209 final TransactionalMapWrapper txMap1 = getNewWrapper(map1); 210 211 txMap1.put("key1", "value1"); 213 txMap1.put("key2", "value200"); 214 215 txMap1.startTransaction(); 217 txMap1.put("key2", "value2"); txMap1.put("key3", "value3"); 219 txMap1.put("key4", "value4"); 220 221 boolean key1P, key2P, key3P, key4P; 223 key1P = key2P = key3P = key4P = false; 224 int cnt = 0; 225 for (Iterator it = txMap1.entrySet().iterator(); it.hasNext();) { 226 cnt++; 227 Map.Entry entry = (Map.Entry ) it.next(); 228 if (entry.getKey().equals("key1") && entry.getValue().equals("value1")) 229 key1P = true; 230 else if (entry.getKey().equals("key2") && entry.getValue().equals("value2")) 231 key2P = true; 232 else if (entry.getKey().equals("key3") && entry.getValue().equals("value3")) 233 key3P = true; 234 else if (entry.getKey().equals("key4") && entry.getValue().equals("value4")) 235 key4P = true; 236 } 237 assertEquals(cnt, 4); 238 assertTrue(key1P && key2P && key3P && key4P); 239 240 checkCollection(txMap1.values(), new String [] { "value1", "value2", "value3", "value4" }); 241 checkCollection(txMap1.keySet(), new String [] { "key1", "key2", "key3", "key4" }); 242 243 txMap1.commitTransaction(); 244 245 key1P = key2P = key3P = key4P = false; 247 cnt = 0; 248 for (Iterator it = txMap1.entrySet().iterator(); it.hasNext();) { 249 cnt++; 250 Map.Entry entry = (Map.Entry ) it.next(); 251 if (entry.getKey().equals("key1") && entry.getValue().equals("value1")) 252 key1P = true; 253 else if (entry.getKey().equals("key2") && entry.getValue().equals("value2")) 254 key2P = true; 255 else if (entry.getKey().equals("key3") && entry.getValue().equals("value3")) 256 key3P = true; 257 else if (entry.getKey().equals("key4") && entry.getValue().equals("value4")) 258 key4P = true; 259 } 260 assertEquals(cnt, 4); 261 assertTrue(key1P && key2P && key3P && key4P); 262 263 checkCollection(txMap1.values(), new String [] { "value1", "value2", "value3", "value4" }); 264 checkCollection(txMap1.keySet(), new String [] { "key1", "key2", "key3", "key4" }); 265 266 268 txMap1.startTransaction(); 269 270 txMap1.put("key5", "value5"); 272 txMap1.put("key4", "value400"); 274 txMap1.remove("key1"); 276 277 assertEquals(txMap1.size(), 4); 278 279 txMap1.clear(); 280 assertEquals(txMap1.size(), 0); 281 assertEquals(map1.size(), 4); 282 283 txMap1.put("key5", "value5"); 285 txMap1.remove("key1"); 287 288 assertEquals(txMap1.size(), 1); 290 assertEquals(map1.size(), 4); 291 assertNull(txMap1.get("key4")); 292 assertNotNull(txMap1.get("key5")); 293 294 txMap1.commitTransaction(); 295 296 assertEquals(txMap1.size(), 1); 298 assertEquals(map1.size(), 1); 299 assertNull(txMap1.get("key4")); 300 assertNotNull(txMap1.get("key5")); 301 assertNull(map1.get("key4")); 302 assertNotNull(map1.get("key5")); 303 } 304 305 public void testMulti() throws Throwable { 306 logger.info("Checking concurrent transaction features"); 307 308 final Map map1 = new HashMap (); 309 310 final TransactionalMapWrapper txMap1 = getNewWrapper(map1); 311 312 final RendezvousBarrier beforeCommitBarrier = 313 new RendezvousBarrier("Before Commit", 2, BARRIER_TIMEOUT, sLogger); 314 315 final RendezvousBarrier afterCommitBarrier = new RendezvousBarrier("After Commit", 2, BARRIER_TIMEOUT, sLogger); 316 317 Thread thread1 = new Thread (new Runnable () { 318 public void run() { 319 txMap1.startTransaction(); 320 try { 321 beforeCommitBarrier.meet(); 322 txMap1.put("key1", "value2"); 323 txMap1.commitTransaction(); 324 afterCommitBarrier.call(); 325 } catch (InterruptedException e) { 326 logger.log(Level.WARNING, "Thread interrupted", e); 327 afterCommitBarrier.reset(); 328 beforeCommitBarrier.reset(); 329 } 330 } 331 }, "Thread1"); 332 333 txMap1.put("key1", "value1"); 334 335 txMap1.startTransaction(); 336 thread1.start(); 337 338 report("value1", (String ) txMap1.get("key1")); 339 beforeCommitBarrier.call(); 340 afterCommitBarrier.meet(); 341 report("value2", (String ) txMap1.get("key1")); 343 344 txMap1.put("key1", "value3"); 346 report("value3", (String ) txMap1.get("key1")); 347 348 txMap1.rollbackTransaction(); 350 report("value2", (String ) txMap1.get("key1")); 351 } 352 353 public void testTxControl() throws Throwable { 354 logger.info("Checking advanced transaction control (heavily used in JCA implementation)"); 355 356 final Map map1 = new HashMap (); 357 358 final TransactionalMapWrapper txMap1 = getNewWrapper(map1); 359 360 assertEquals(txMap1.getTransactionState(), TransactionalMapWrapper.STATUS_NO_TRANSACTION); 361 txMap1.startTransaction(); 362 assertEquals(txMap1.getTransactionState(), TransactionalMapWrapper.STATUS_ACTIVE); 363 364 assertTrue(txMap1.isReadOnly()); 365 txMap1.put("key", "value"); 366 assertFalse(txMap1.isReadOnly()); 367 368 assertFalse(txMap1.isTransactionMarkedForRollback()); 369 txMap1.markTransactionForRollback(); 370 assertTrue(txMap1.isTransactionMarkedForRollback()); 371 372 boolean failed = false; 373 try { 374 txMap1.commitTransaction(); 375 } catch (IllegalStateException ise) { 376 failed = true; 377 } 378 assertTrue(failed); 379 txMap1.rollbackTransaction(); 380 assertEquals(txMap1.getTransactionState(), TransactionalMapWrapper.STATUS_NO_TRANSACTION); 381 382 txMap1.startTransaction(); 383 final TransactionalMapWrapper.TxContext ctx = txMap1.suspendTransaction(); 384 final RendezvousBarrier afterSuspendBarrier = 385 new RendezvousBarrier("After Suspend", 2, BARRIER_TIMEOUT, sLogger); 386 387 new Thread (new Runnable () { 388 public void run() { 389 txMap1.resumeTransaction(ctx); 390 txMap1.put("key2", "value2"); 391 txMap1.suspendTransaction(); 392 afterSuspendBarrier.call(); 393 } 394 }).start(); 395 396 afterSuspendBarrier.meet(); 397 txMap1.resumeTransaction(ctx); 398 399 assertEquals(txMap1.size(), 1); 400 txMap1.put("key3", "value3"); 401 assertEquals(txMap1.size(), 2); 402 assertEquals(map1.size(), 0); 403 404 txMap1.commitTransaction(); 405 assertEquals(txMap1.size(), 2); 406 assertEquals(map1.size(), 2); 407 } 408 409 } 410 | Popular Tags |