1 |
| |
2 |
| |
3 |
| |
4 |
| |
5 |
| |
6 |
| |
7 |
| |
8 |
| |
9 |
| |
10 |
| |
11 |
| |
12 |
| |
13 |
| |
14 |
| |
15 |
| |
16 |
| |
17 |
| |
18 |
| |
19 |
| |
20 |
| |
21 |
| |
22 |
| |
23 |
| package org.jboss.cache.pojo.rollback; |
24 |
| |
25 |
| import junit.framework.TestCase; |
26 |
| import junit.framework.Test; |
27 |
| import junit.framework.TestSuite; |
28 |
| import org.apache.commons.logging.Log; |
29 |
| import org.apache.commons.logging.LogFactory; |
30 |
| import org.jboss.cache.pojo.PojoCache; |
31 |
| import org.jboss.cache.pojo.PojoCacheFactory; |
32 |
| import org.jboss.cache.pojo.InternalConstant; |
33 |
| import org.jboss.cache.pojo.PojoCacheException; |
34 |
| import org.jboss.cache.pojo.test.Person; |
35 |
| import org.jboss.cache.pojo.interceptors.PojoFailedTxMockupInterceptor; |
36 |
| import org.jboss.cache.pojo.interceptors.dynamic.CacheFieldInterceptor; |
37 |
| import org.jboss.cache.transaction.DummyTransactionManager; |
38 |
| import org.jboss.cache.Fqn; |
39 |
| import org.jboss.aop.advice.Interceptor; |
40 |
| import org.jboss.aop.Advised; |
41 |
| |
42 |
| import javax.transaction.TransactionManager; |
43 |
| import java.util.ArrayList; |
44 |
| |
45 |
| |
46 |
| |
47 |
| |
48 |
| |
49 |
| |
50 |
| |
51 |
| public class LocalExceptionUndoTest extends TestCase |
52 |
| { |
53 |
| Log log_ = LogFactory.getLog(LocalExceptionUndoTest.class); |
54 |
| PojoCache cache_; |
55 |
| TransactionManager tx_mgr; |
56 |
| boolean isTrue = false; |
57 |
| |
58 |
4
| public LocalExceptionUndoTest(String name)
|
59 |
| { |
60 |
4
| super(name);
|
61 |
| } |
62 |
| |
63 |
4
| protected void setUp() throws Exception
|
64 |
| { |
65 |
4
| super.setUp();
|
66 |
4
| log_.info("setUp() ....");
|
67 |
4
| String configFile = "META-INF/local-service.xml";
|
68 |
4
| boolean toStart = false;
|
69 |
4
| cache_ = PojoCacheFactory.createCache(configFile, toStart);
|
70 |
4
| cache_.getCache().getConfiguration().setLockAcquisitionTimeout(500);
|
71 |
4
| cache_.getCache().getConfiguration().setLockParentForChildInsertRemove(true);
|
72 |
4
| cache_.start();
|
73 |
4
| tx_mgr = DummyTransactionManager.getInstance();
|
74 |
| } |
75 |
| |
76 |
4
| private void startLockThread() throws Exception
|
77 |
| { |
78 |
| |
79 |
4
| (new LockThread()).start();
|
80 |
4
| Thread.sleep(300);
|
81 |
| } |
82 |
| |
83 |
4
| private void stopLockThread() throws Exception
|
84 |
| { |
85 |
4
| isTrue = true;
|
86 |
4
| Thread.sleep(200);
|
87 |
| } |
88 |
| |
89 |
4
| protected void tearDown() throws Exception
|
90 |
| { |
91 |
4
| super.tearDown();
|
92 |
4
| cache_.stop();
|
93 |
| } |
94 |
| |
95 |
2
| public void testSimpleTxWithRollback1() throws Exception
|
96 |
| { |
97 |
2
| log_.info("testSimpleTxWithRollback1() ....");
|
98 |
2
| Person test = new Person();
|
99 |
2
| test.setName("Ben");
|
100 |
2
| test.setAge(10);
|
101 |
2
| ArrayList list = new ArrayList();
|
102 |
2
| list.add("English");
|
103 |
2
| test.setLanguages(list);
|
104 |
| |
105 |
2
| startLockThread();
|
106 |
| |
107 |
2
| try {
|
108 |
2
| cache_.attach("/a", test);
|
109 |
| } catch (PojoCacheException ex) |
110 |
| { |
111 |
| |
112 |
| } |
113 |
2
| assertFalse("Should not have cache interceptor ", hasCacheInterceptor(test));
|
114 |
2
| assertNull("Should be null", cache_.find("/a"));
|
115 |
| |
116 |
2
| stopLockThread();
|
117 |
2
| cache_.attach("/a", test);
|
118 |
| } |
119 |
| |
120 |
2
| public void testSimpleTxWithRollback2() throws Exception
|
121 |
| { |
122 |
2
| log_.info("testSimpleTxWithRollback1() ....");
|
123 |
2
| Person test = new Person();
|
124 |
2
| test.setName("Ben");
|
125 |
2
| test.setAge(10);
|
126 |
2
| ArrayList list = new ArrayList();
|
127 |
2
| list.add("English");
|
128 |
2
| test.setLanguages(list);
|
129 |
| |
130 |
2
| try {
|
131 |
2
| cache_.attach("/a", test);
|
132 |
| } catch (PojoCacheException ex) |
133 |
| { |
134 |
| |
135 |
| } |
136 |
| |
137 |
2
| startLockThread();
|
138 |
| |
139 |
2
| try {
|
140 |
2
| cache_.detach("/a");
|
141 |
| } catch (PojoCacheException ex) |
142 |
| { |
143 |
| |
144 |
| } |
145 |
| |
146 |
2
| stopLockThread();
|
147 |
2
| assertTrue("Should still have cache interceptor ", hasCacheInterceptor(test));
|
148 |
2
| cache_.detach("/a");
|
149 |
2
| assertNull("Should be null", cache_.find("/a"));
|
150 |
| } |
151 |
| |
152 |
4
| private boolean hasCacheInterceptor(Object pojo)
|
153 |
| { |
154 |
4
| Interceptor[] interceptors = ((Advised)pojo)._getInstanceAdvisor().getInterceptors();
|
155 |
4
| for(int i=0; i < interceptors.length; i++)
|
156 |
| { |
157 |
2
| if(interceptors[i] instanceof CacheFieldInterceptor)
|
158 |
2
| return true;
|
159 |
| } |
160 |
2
| return false;
|
161 |
| } |
162 |
| |
163 |
2
| public static Test suite() throws Exception
|
164 |
| { |
165 |
2
| return new TestSuite(LocalExceptionUndoTest.class);
|
166 |
| } |
167 |
| |
168 |
| |
169 |
0
| public static void main(String[] args) throws Exception
|
170 |
| { |
171 |
0
| junit.textui.TestRunner.run(suite());
|
172 |
| } |
173 |
| |
174 |
| public class LockThread extends Thread |
175 |
| { |
176 |
4
| public void run()
|
177 |
| { |
178 |
4
| try
|
179 |
| { |
180 |
4
| Fqn f = new Fqn(InternalConstant.JBOSS_INTERNAL_STRING);
|
181 |
4
| cache_.getCache().put(new Fqn(f, "123"), "key", "test");
|
182 |
4
| cache_.getCache().put(new Fqn("a"), "key", "test");
|
183 |
4
| tx_mgr.begin();
|
184 |
4
| cache_.getCache().put(new Fqn(f, "124"), "key", "test");
|
185 |
| |
186 |
4
| while (!isTrue)
|
187 |
| { |
188 |
40
| sleep(100);
|
189 |
| } |
190 |
4
| tx_mgr.commit();
|
191 |
| } catch (Exception ex) |
192 |
| { |
193 |
0
| ex.printStackTrace();
|
194 |
| } |
195 |
| |
196 |
| } |
197 |
| } |
198 |
| } |