1 9 package org.jboss.cache.pojo.rollback; 10 11 import junit.framework.TestCase; 12 import org.jboss.cache.pojo.PojoCache; 13 import org.jboss.cache.pojo.PojoCacheFactory; 14 import org.jboss.cache.transaction.DummyTransactionManager; 15 16 import javax.transaction.TransactionManager ; 17 import java.util.ArrayList ; 18 import java.util.HashMap ; 19 import java.util.List ; 20 import java.util.Map ; 21 22 25 public class PojoCollectionRollbackTest extends TestCase 26 { 27 private static final String ID = "id"; 28 private static final String CONTAINER_FQN = "/objsIndex"; 29 TransactionManager tx_mgr; 30 31 public PojoCollectionRollbackTest(String s) 32 { 33 super(s); 34 } 35 36 private PojoCache cache_; 37 38 protected void setUp() throws Exception 39 { 40 super.setUp(); 41 } 42 43 private void startTest() throws Exception 44 { 45 String configFile = "META-INF/local-service.xml"; 46 boolean toStart = false; 47 cache_ = PojoCacheFactory.createCache(configFile, toStart); 48 cache_.start(); 49 tx_mgr = DummyTransactionManager.getInstance(); 50 } 51 52 protected void tearDown() throws Exception 53 { 54 super.tearDown(); 55 cache_.stop(); 56 } 57 58 public void testNestedMapAndIndexWithModifyRollback() throws Exception 59 { 60 System.out.println("testNestedMapAndIndexWithModifyRollback"); 61 startTest(); 62 63 Map obj1 = new HashMap (); 65 obj1.put(ID, "1"); 66 cache_.attach("/objs/1", obj1); 67 obj1 = (Map ) cache_.find("/objs/1"); 68 69 Map indexMap = null; 72 final String KEY = "KEY"; 73 Object beforeModify; 74 Object afterRollback; 75 76 indexMap = new HashMap (); 77 cache_.attach(CONTAINER_FQN, indexMap); 78 indexMap = (Map ) cache_.find(CONTAINER_FQN); 79 indexMap.put(KEY, obj1); 80 81 beforeModify = indexMap.get(KEY); 82 Object idBeforeModify = ((Map ) beforeModify).get(ID); 83 84 System.out.println("beforeModify: " + beforeModify + ", data object id: " + ((Map ) beforeModify).get(ID)); 85 86 tx_mgr.begin(); 89 obj1.put(ID, "newID"); 90 indexMap.remove(KEY); 91 tx_mgr.rollback(); 92 93 indexMap = (Map ) cache_.find(CONTAINER_FQN); 94 afterRollback = indexMap.get(KEY); 95 System.out.println("afterRollback: " + afterRollback + ", data object id: " + ((Map ) afterRollback).get(ID)); 96 97 assertEquals(beforeModify, afterRollback); 99 assertTrue(beforeModify == afterRollback); 100 assertEquals(1, indexMap.size()); 101 102 assertEquals("1", obj1.get(ID)); 103 Object idAfterRollback = ((Map ) afterRollback).get(ID); 104 System.out.println("idBeforeModify: " + idBeforeModify + " idAfterRollback: " + idAfterRollback); 105 assertEquals(idBeforeModify, idAfterRollback); 106 } 107 108 public void testNestedMapWithModifyRollback() throws Exception 109 { 110 System.out.println("testNestedMapWithModifyRollback"); 111 startTest(); 112 113 Map obj1 = new HashMap (); 115 obj1.put(ID, "1"); 116 cache_.attach("/objs/1", obj1); 117 obj1 = (Map ) cache_.find("/objs/1"); 118 119 Map obj2 = new HashMap (); 120 obj2.put(ID, "2"); 121 cache_.attach("/objs/2", obj2); 122 obj2 = (Map ) cache_.find("/objs/2"); 123 124 Map indexMap = null; 127 final String KEY = "KEY"; 128 Object beforeModify; 129 Object afterRollback; 130 131 indexMap = new HashMap (); 132 cache_.attach(CONTAINER_FQN, indexMap); 133 indexMap = (Map ) cache_.find(CONTAINER_FQN); 134 indexMap.put(KEY, obj1); 135 beforeModify = indexMap.get(KEY); 136 137 System.out.println("beforeModify: " + beforeModify + ", data object id: " + ((Map ) beforeModify).get(ID)); 138 139 tx_mgr.begin(); 142 Object removedByModify = indexMap.put(KEY, obj2); 143 System.out.println("removedByModify: " + removedByModify + ", data object id: " + ((Map ) removedByModify).get(ID)); 144 assertEquals(removedByModify, beforeModify); 145 tx_mgr.rollback(); 146 147 indexMap = (Map ) cache_.find(CONTAINER_FQN); 148 afterRollback = indexMap.get(KEY); 149 System.out.println("afterRollback: " + afterRollback + ", data object id: " + ((Map ) afterRollback).get(ID)); 150 151 assertEquals(beforeModify, afterRollback); 153 assertEquals(1, indexMap.size()); 154 indexMap.put(KEY, obj2); 156 assertEquals(obj2, indexMap.get(KEY)); 157 } 158 159 public void testNestedMapWithRemoveRollback() throws Exception 160 { 161 System.out.println("testNestedMapWithRemoveRollback"); 162 startTest(); 163 164 Map obj1 = new HashMap (); 166 obj1.put(ID, "1"); 167 cache_.attach("/objs/1", obj1); 168 obj1 = (Map ) cache_.find("/objs/1"); 169 170 Map obj2 = new HashMap (); 171 obj2.put(ID, "2"); 172 cache_.attach("/objs/2", obj2); 173 obj2 = (Map ) cache_.find("/objs/2"); 174 175 Map indexMap = new HashMap (); 177 cache_.attach(CONTAINER_FQN, indexMap); 178 indexMap = (Map ) cache_.find(CONTAINER_FQN); 179 180 final String KEY = "KEY"; 182 indexMap.put(KEY, obj1); 183 184 Object beforeModify = indexMap.get(KEY); 185 System.out.println("beforeModify: " + beforeModify + ", data object id: " + ((Map ) beforeModify).get(ID)); 186 187 tx_mgr.begin(); 190 Object removedByRemove = indexMap.remove(KEY); 191 System.out.println("removedByRemove: " + removedByRemove + ", data object id: " + ((Map ) removedByRemove).get(ID)); 192 assertEquals(beforeModify, removedByRemove); 193 tx_mgr.rollback(); 194 195 Object afterRollback = indexMap.get(KEY); 196 System.out.println("afterRollback: " + afterRollback + ", data object id: " + ((Map ) afterRollback).get(ID)); 197 198 assertEquals(beforeModify, afterRollback); 200 assertEquals(1, indexMap.size()); 201 202 indexMap.put(KEY, obj2); 204 assertEquals(obj2, indexMap.get(KEY)); 205 } 206 207 public void testNestedListWithModifyAddRollback() throws Exception 208 { 209 System.out.println("testNestedListWithModifyAddRollback"); 210 startTest(); 211 212 Map obj1 = new HashMap (); 214 obj1.put(ID, "1"); 215 cache_.attach("/objs/1", obj1); 216 obj1 = (Map ) cache_.find("/objs/1"); 217 218 Map obj2 = new HashMap (); 219 obj2.put(ID, "2"); 220 cache_.attach("/objs/2", obj2); 221 obj2 = (Map ) cache_.find("/objs/2"); 222 assertFalse(obj1.equals(obj2)); 223 224 List indexList = new ArrayList (); 226 cache_.attach(CONTAINER_FQN, indexList); 227 indexList = (List ) cache_.find(CONTAINER_FQN); 228 229 indexList.add(obj1); 231 232 Object beforeModify = indexList.get(0); 233 System.out.println("beforeModify: " + beforeModify + ", data object id: " + ((Map ) beforeModify).get(ID)); 234 int objIndex1, objIndex2; 235 236 tx_mgr.begin(); 239 indexList.add(obj2); 240 objIndex1 = indexList.indexOf(obj1); 241 objIndex2 = indexList.indexOf(obj2); 242 tx_mgr.rollback(); 243 244 assertFalse(obj1.equals(obj2)); 246 assertEquals(0, objIndex1); 247 assertEquals(1, objIndex2); 248 249 Object afterRollback = indexList.get(0); 250 System.out.println("afterRollback: " + afterRollback + ", data object id: " + ((Map ) afterRollback).get(ID)); 251 252 assertEquals(beforeModify, afterRollback); 254 assertEquals(1, indexList.size()); 255 256 indexList.set(0, obj2); 258 assertEquals(obj2, indexList.get(0)); 259 } 260 261 public void testNestedListWithModifySetRollback() throws Exception 262 { 263 System.out.println("testNestedListWithModifySetRollback"); 264 startTest(); 265 266 Map obj1 = new HashMap (); 268 obj1.put(ID, "1"); 269 cache_.attach("/objs/1", obj1); 270 obj1 = (Map ) cache_.find("/objs/1"); 271 272 Map obj2 = new HashMap (); 273 obj2.put(ID, "2"); 274 cache_.attach("/objs/2", obj2); 275 obj2 = (Map ) cache_.find("/objs/2"); 276 277 List indexList = new ArrayList (); 279 cache_.attach(CONTAINER_FQN, indexList); 280 indexList = (List ) cache_.find(CONTAINER_FQN); 281 282 indexList.add(obj1); 284 285 Object beforeModify = indexList.get(0); 286 System.out.println("beforeModify: " + beforeModify + ", data object id: " + ((Map ) beforeModify).get(ID)); 287 int objIndex; 288 289 tx_mgr.begin(); 292 Object removedBySet = indexList.set(0, obj2); 293 System.out.println("removedBySet: " + removedBySet + ", data object id: " + ((Map ) removedBySet).get(ID)); 294 assertEquals(beforeModify, removedBySet); 295 objIndex = indexList.indexOf(obj2); 296 tx_mgr.rollback(); 297 298 assertEquals(0, objIndex); 300 301 Object afterRollback = indexList.get(0); 302 System.out.println("afterRollback: " + afterRollback + ", data object id: " + ((Map ) afterRollback).get(ID)); 303 304 assertEquals(beforeModify, afterRollback); 306 assertEquals(1, indexList.size()); 307 308 indexList.set(0, obj2); 310 assertEquals(obj2, indexList.get(0)); 311 } 312 313 public void testNestedListWithRemoveRollback() throws Exception 314 { 315 System.out.println("testNestedListWithRemoveRollback"); 316 startTest(); 317 318 Map obj1 = new HashMap (); 320 obj1.put(ID, "1"); 321 cache_.attach("/objs/1", obj1); 322 obj1 = (Map ) cache_.find("/objs/1"); 323 324 Map obj2 = new HashMap (); 325 obj2.put(ID, "2"); 326 cache_.attach("/objs/2", obj2); 327 obj2 = (Map ) cache_.find("/objs/2"); 328 329 List indexList = new ArrayList (); 331 cache_.attach(CONTAINER_FQN, indexList); 332 indexList = (List ) cache_.find(CONTAINER_FQN); 333 334 indexList.add(obj1); 336 337 Object beforeModify = indexList.get(0); 338 System.out.println("beforeModify: " + beforeModify + ", data object id: " + ((Map ) beforeModify).get(ID)); 339 int objIndex; 340 341 tx_mgr.begin(); 344 indexList.remove(obj1); 345 objIndex = indexList.indexOf(obj1); 346 tx_mgr.rollback(); 347 348 assertEquals(-1, objIndex); 350 351 Object afterRollback = indexList.get(0); 352 System.out.println("afterRollback: " + afterRollback + ", data object id: " + ((Map ) afterRollback).get(ID)); 353 354 assertEquals(beforeModify, afterRollback); 356 assertEquals(1, indexList.size()); 357 358 indexList.set(0, obj2); 360 assertEquals(obj2, indexList.get(0)); 361 } 362 363 364 } 365 | Popular Tags |