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