1 7 8 package org.jboss.cache.pojo.rollback; 9 10 import junit.framework.TestCase; 11 import junit.framework.Test; 12 import junit.framework.TestSuite; 13 import org.apache.commons.logging.Log; 14 import org.apache.commons.logging.LogFactory; 15 import org.jboss.cache.pojo.PojoCache; 16 import org.jboss.cache.pojo.PojoCacheFactory; 17 import org.jboss.cache.pojo.test.Person; 18 import org.jboss.cache.transaction.DummyTransactionManager; 19 import org.jboss.aop.proxy.ClassProxy; 20 21 import javax.transaction.TransactionManager ; 22 import java.util.HashMap ; 23 import java.util.Map ; 24 25 30 31 public class MapTxUndoTest extends TestCase 32 { 33 Log log_ = LogFactory.getLog(MapTxUndoTest.class); 34 PojoCache cache_; 35 TransactionManager tx_mgr; 36 37 public MapTxUndoTest(String name) 38 { 39 super(name); 40 } 41 42 protected void setUp() throws Exception 43 { 44 super.setUp(); 45 log_.info("setUp() ...."); 46 String configFile = "META-INF/local-service.xml"; 47 boolean toStart = false; 48 cache_ = PojoCacheFactory.createCache(configFile, toStart); 49 cache_.start(); 50 tx_mgr = DummyTransactionManager.getInstance(); 51 52 } 53 54 protected void tearDown() throws Exception 55 { 56 super.tearDown(); 57 cache_.stop(); 58 } 59 60 62 public void testSimple() throws Exception 63 { 64 HashMap map = new HashMap (); 65 map.put("1", "test1"); 66 67 tx_mgr.begin(); 68 cache_.attach("/a", map); 69 tx_mgr.getTransaction().rollback(); 70 assertFalse("Should not have cache interceptor ", isProxy(map)); 71 72 cache_.attach("/a", map); 73 } 74 75 public void testSimpleTxWithRollback1() throws Exception 76 { 77 log_.info("testSimpleTxWithRollback1() ...."); 78 Person test = new Person(); 79 test.setName("Ben"); 80 test.setAge(10); 81 HashMap map = new HashMap (); 82 map.put("1", "English"); 83 test.setHobbies(map); 84 85 tx_mgr.begin(); 86 cache_.attach("/a", test); 87 tx_mgr.getTransaction().rollback(); 88 assertFalse("Should not have cache interceptor ", isProxy(test.getHobbies())); 89 90 cache_.attach("/a", test); 91 } 92 93 private boolean isProxy(Object pojo) 94 { 95 if(pojo instanceof ClassProxy) return true; 96 return false; 97 } 98 99 public void testSimpleTxWithRollback2() throws Exception 100 { 101 log_.info("testSimpleTxWithRollback1() ...."); 102 Person test = new Person(); 103 test.setName("Ben"); 104 test.setAge(10); 105 HashMap map = new HashMap (); 106 map.put("1", "English"); 107 test.setHobbies(map); 108 109 cache_.attach("/a", test); 110 111 tx_mgr.begin(); 112 cache_.detach("/a"); 113 tx_mgr.getTransaction().rollback(); 114 115 assertTrue("Should still have cache interceptor ", isProxy(test.getHobbies())); 116 cache_.detach("/a"); 117 } 118 119 public void testSimpleTxWithRollback3() throws Exception 120 { 121 log_.info("testSimpleTxWithRollback1() ...."); 122 Person test = new Person(); 123 test.setName("Ben"); 124 test.setAge(10); 125 HashMap map = new HashMap (); 126 map.put("1", "English"); 127 test.setHobbies(map); 128 tx_mgr.begin(); 129 cache_.attach("/a", test); 130 cache_.detach("/a"); 131 tx_mgr.getTransaction().rollback(); 132 133 assertFalse("Should not have cache interceptor ", isProxy(test.getHobbies())); 134 } 135 136 140 public void testNestedMapWithRollback() throws Exception 141 { 142 Map obj1 = new HashMap (); 144 obj1.put("id", "1"); 145 cache_.attach("objs/1", obj1); 146 obj1 = (Map ) cache_.find("objs/1"); 147 148 Map obj2 = new HashMap (); 149 obj2.put("id", "2"); 150 cache_.attach("objs/2", obj2); 151 obj2 = (Map ) cache_.find("objs/2"); 152 153 Map indexMap = new HashMap (); 155 cache_.attach("objsIndex", indexMap); 156 indexMap = (Map ) cache_.find("objsIndex"); 157 158 final String KEY = "KEY"; 160 indexMap.put(KEY, obj1); 161 162 Object beforeModify = indexMap.get(KEY); 163 System.out.println("beforeModify: " + beforeModify + ", data object id: " + ((Map )beforeModify).get("id")); 164 165 tx_mgr.begin(); 168 indexMap.put(KEY, obj2); 169 tx_mgr.rollback(); 170 171 Object afterRollback = indexMap.get(KEY); 172 System.out.println("afterRollback: " + afterRollback + ", data object id: " + ((Map )afterRollback).get("id")); 173 174 assertEquals(beforeModify, afterRollback); 176 } 177 178 public void testPlainMapRollback() throws Exception 179 { 180 Map obj1 = new HashMap (); 182 obj1.put("id", "1"); 183 cache_.attach("objs/1", obj1); 184 obj1 = (Map ) cache_.find("objs/1"); 185 186 tx_mgr.begin(); 187 obj1.put("id", "newId"); 188 tx_mgr.rollback(); 189 190 assertEquals("1", obj1.get("id")); 191 } 192 193 public static Test suite() throws Exception 194 { 195 return new TestSuite(MapTxUndoTest.class); 196 } 197 198 199 public static void main(String [] args) throws Exception 200 { 201 junit.textui.TestRunner.run(MapTxUndoTest.suite()); 202 } 203 204 } 205 | Popular Tags |