1 |
| |
2 |
| |
3 |
| |
4 |
| |
5 |
| |
6 |
| |
7 |
| |
8 |
| package org.jboss.cache.pojo; |
9 |
| |
10 |
| import junit.framework.Test; |
11 |
| import junit.framework.TestCase; |
12 |
| import junit.framework.TestSuite; |
13 |
| import org.apache.commons.logging.Log; |
14 |
| import org.apache.commons.logging.LogFactory; |
15 |
| import org.jboss.cache.config.Configuration.CacheMode; |
16 |
| import org.jboss.cache.factories.UnitTestCacheFactory; |
17 |
| import org.jboss.cache.pojo.test.Address; |
18 |
| import org.jboss.cache.pojo.test.Person; |
19 |
| import org.jboss.cache.transaction.DummyTransactionManager; |
20 |
| |
21 |
| import javax.naming.Context; |
22 |
| import javax.naming.InitialContext; |
23 |
| import javax.naming.NamingException; |
24 |
| import javax.transaction.NotSupportedException; |
25 |
| import javax.transaction.RollbackException; |
26 |
| import javax.transaction.SystemException; |
27 |
| import javax.transaction.Transaction; |
28 |
| import javax.transaction.UserTransaction; |
29 |
| import java.util.ArrayList; |
30 |
| import java.util.List; |
31 |
| import java.util.Properties; |
32 |
| |
33 |
| |
34 |
| |
35 |
| |
36 |
| |
37 |
| |
38 |
| |
39 |
| public class NewReplicatedTxTest extends TestCase |
40 |
| { |
41 |
| Log log = LogFactory.getLog(NewReplicatedTxTest.class); |
42 |
| PojoCache cache_; |
43 |
| PojoCache cache1_; |
44 |
| final String FACTORY = "org.jboss.cache.transaction.DummyContextFactory"; |
45 |
| DummyTransactionManager tx_mgr; |
46 |
| Throwable t1_ex, t2_ex; |
47 |
| long start = 0; |
48 |
| |
49 |
| |
50 |
10
| public NewReplicatedTxTest(String name)
|
51 |
| { |
52 |
10
| super(name);
|
53 |
| } |
54 |
| |
55 |
10
| protected void setUp() throws Exception
|
56 |
| { |
57 |
10
| super.setUp();
|
58 |
10
| log.info("setUp() ....");
|
59 |
10
| super.setUp();
|
60 |
10
| Properties prop = new Properties();
|
61 |
10
| prop.put(Context.INITIAL_CONTEXT_FACTORY, "org.jboss.cache.transaction.DummyContextFactory");
|
62 |
10
| boolean toStart = false;
|
63 |
10
| cache_ = PojoCacheFactory.createCache(UnitTestCacheFactory.createConfiguration(CacheMode.REPL_SYNC), toStart);
|
64 |
10
| cache1_ = PojoCacheFactory.createCache(UnitTestCacheFactory.createConfiguration(CacheMode.REPL_SYNC), toStart);
|
65 |
10
| cache_.start();
|
66 |
10
| cache1_.start();
|
67 |
| |
68 |
10
| tx_mgr = DummyTransactionManager.getInstance();
|
69 |
10
| t1_ex = t2_ex = null;
|
70 |
| } |
71 |
| |
72 |
10
| protected void tearDown() throws Exception
|
73 |
| { |
74 |
10
| super.tearDown();
|
75 |
10
| cache_.stop();
|
76 |
10
| cache1_.stop();
|
77 |
10
| DummyTransactionManager.destroy();
|
78 |
| } |
79 |
| |
80 |
20
| UserTransaction getTransaction() throws SystemException, NotSupportedException, NamingException
|
81 |
| { |
82 |
20
| Properties prop = new Properties();
|
83 |
20
| prop.put(Context.INITIAL_CONTEXT_FACTORY,
|
84 |
| "org.jboss.cache.transaction.DummyContextFactory"); |
85 |
20
| return (UserTransaction) new InitialContext(prop).lookup("UserTransaction");
|
86 |
| } |
87 |
| |
88 |
2
| public void testSimpleTxWithRollback() throws Exception
|
89 |
| { |
90 |
2
| log.info("testSimpleTxWithRollback() ....");
|
91 |
2
| UserTransaction tx = getTransaction();
|
92 |
| |
93 |
2
| Person joe = new Person();
|
94 |
2
| joe.setName("Joe");
|
95 |
2
| Address add = new Address();
|
96 |
2
| add.setZip(104);
|
97 |
2
| add.setCity("Taipei");
|
98 |
2
| joe.setAddress(add);
|
99 |
| |
100 |
2
| tx.begin();
|
101 |
2
| cache_.attach("/person/joe", joe);
|
102 |
2
| tx.commit();
|
103 |
2
| Person p = (Person) cache1_.find("/person/joe");
|
104 |
2
| assertEquals("Zip should be the same ", joe.getAddress().getZip(), p.getAddress().getZip());
|
105 |
| |
106 |
| |
107 |
2
| Person ben = new Person();
|
108 |
2
| ben.setName("Ben");
|
109 |
2
| add = new Address();
|
110 |
2
| add.setZip(104);
|
111 |
2
| add.setCity("Taipei");
|
112 |
2
| joe.setAddress(add);
|
113 |
2
| tx.begin();
|
114 |
2
| cache_.attach("/person/ben", ben);
|
115 |
2
| tx.rollback();
|
116 |
2
| assertEquals("Zip should be the same ", joe.getAddress().getZip(), p.getAddress().getZip());
|
117 |
| } |
118 |
| |
119 |
2
| public void testCollectionTxWithRollback() throws Exception
|
120 |
| { |
121 |
2
| log.info("testCollectionTxWithRollback() ....");
|
122 |
2
| UserTransaction tx = getTransaction();
|
123 |
| |
124 |
2
| Person joe = new Person();
|
125 |
2
| joe.setName("Joe");
|
126 |
2
| Address add = new Address();
|
127 |
2
| add.setZip(104);
|
128 |
2
| add.setCity("Taipei");
|
129 |
2
| joe.setAddress(add);
|
130 |
2
| ArrayList<String> lang = new ArrayList<String>();
|
131 |
2
| lang.add("English");
|
132 |
2
| lang.add("Taiwanese");
|
133 |
2
| lang.add("Mandirin");
|
134 |
2
| joe.setLanguages(lang);
|
135 |
| |
136 |
2
| tx.begin();
|
137 |
2
| cache_.attach("/person/joe", joe);
|
138 |
2
| tx.commit();
|
139 |
2
| Person p = (Person) cache1_.find("/person/joe");
|
140 |
2
| assertEquals("Zip should be the same ", joe.getAddress().getZip(), p.getAddress().getZip());
|
141 |
| |
142 |
| |
143 |
2
| Person ben = new Person();
|
144 |
2
| ben.setName("Ben");
|
145 |
2
| add = new Address();
|
146 |
2
| add.setZip(104);
|
147 |
2
| add.setCity("Taipei");
|
148 |
2
| ben.setAddress(add);
|
149 |
2
| tx.begin();
|
150 |
2
| cache_.attach("/person/ben", ben);
|
151 |
2
| tx.rollback();
|
152 |
2
| assertEquals("Zip should be the same ", joe.getAddress().getZip(), p.getAddress().getZip());
|
153 |
2
| assertEquals("Langue 1 should be: ", "English", joe.getLanguages().get(0));
|
154 |
| } |
155 |
| |
156 |
| |
157 |
2
| public void testConcurrentPutsWithRollback() throws Exception
|
158 |
| { |
159 |
2
| Thread t1 = new Thread()
|
160 |
| { |
161 |
2
| public void run()
|
162 |
| { |
163 |
2
| Person p = new Person();
|
164 |
2
| p.setName("Ben");
|
165 |
2
| Address add = new Address();
|
166 |
2
| add.setCity("Taipei");
|
167 |
2
| p.setAddress(add);
|
168 |
2
| List<String> lang = new ArrayList<String>();
|
169 |
2
| lang.add("English");
|
170 |
2
| lang.add("Taiwanese");
|
171 |
2
| lang.add("Japanese");
|
172 |
2
| p.setLanguages(lang);
|
173 |
2
| try
|
174 |
| { |
175 |
2
| cache_.attach("/test/ben", p);
|
176 |
| } |
177 |
| catch (Exception ex) |
178 |
| { |
179 |
0
| try
|
180 |
| { |
181 |
0
| throw new RuntimeException("PojoCache is not rolling back properly for Collection yet.");
|
182 |
| |
183 |
| } |
184 |
| catch (Exception ex1) |
185 |
| { |
186 |
0
| t1_ex = ex1;
|
187 |
| } |
188 |
| } |
189 |
| } |
190 |
| }; |
191 |
| |
192 |
2
| Thread t2 = new Thread()
|
193 |
| { |
194 |
| Transaction tx; |
195 |
| |
196 |
2
| public void run()
|
197 |
| { |
198 |
2
| try
|
199 |
| { |
200 |
2
| UserTransaction tx = getTransaction();
|
201 |
2
| tx.begin();
|
202 |
2
| Person p = new Person();
|
203 |
2
| p.setName("Ben");
|
204 |
2
| Address add = new Address();
|
205 |
2
| add.setCity("Taipei");
|
206 |
2
| p.setAddress(add);
|
207 |
2
| cache1_.attach("/test/ben", p);
|
208 |
2
| TestingUtil.sleepThread(1000);
|
209 |
2
| tx.commit();
|
210 |
| } |
211 |
| catch (RollbackException rollback) |
212 |
| { |
213 |
| } |
214 |
| catch (Exception ex) |
215 |
| { |
216 |
0
| t2_ex = ex;
|
217 |
| } |
218 |
| } |
219 |
| }; |
220 |
| |
221 |
2
| t1.start();
|
222 |
2
| t2.start();
|
223 |
| |
224 |
2
| t1.join();
|
225 |
2
| t2.join();
|
226 |
| |
227 |
| |
228 |
2
| if (t1_ex != null)
|
229 |
| { |
230 |
0
| fail("Thread1 failed: " + t1_ex);
|
231 |
| } |
232 |
2
| if (t2_ex != null)
|
233 |
| { |
234 |
0
| fail("Thread2 failed: " + t2_ex);
|
235 |
| } |
236 |
| } |
237 |
| |
238 |
2
| public void testConcurrentTxPutsWithRollback() throws Exception
|
239 |
| { |
240 |
2
| Thread t1 = new Thread()
|
241 |
| { |
242 |
| Transaction tx; |
243 |
| |
244 |
2
| public void run()
|
245 |
| { |
246 |
2
| UserTransaction tx = null;
|
247 |
2
| Person p = new Person();
|
248 |
2
| p.setName("Ben");
|
249 |
2
| Address add = new Address();
|
250 |
2
| add.setCity("Taipei");
|
251 |
2
| p.setAddress(add);
|
252 |
2
| List<String> lang = new ArrayList<String>();
|
253 |
2
| lang.add("English");
|
254 |
2
| lang.add("Taiwanese");
|
255 |
2
| lang.add("Japanese");
|
256 |
2
| p.setLanguages(lang);
|
257 |
2
| try
|
258 |
| { |
259 |
2
| tx = getTransaction();
|
260 |
2
| tx.begin();
|
261 |
2
| cache_.attach("/test/ben", p);
|
262 |
2
| TestingUtil.sleepThread(500);
|
263 |
2
| tx.commit();
|
264 |
| } |
265 |
| catch (RollbackException rollback) |
266 |
| { |
267 |
2
| try
|
268 |
| { |
269 |
2
| tx = getTransaction();
|
270 |
2
| tx.begin();
|
271 |
| |
272 |
2
| cache_.attach("/test/ben", p);
|
273 |
2
| tx.commit();
|
274 |
| } |
275 |
| catch (Exception ex) |
276 |
| { |
277 |
0
| t1_ex = ex;
|
278 |
0
| ex.printStackTrace();
|
279 |
| } |
280 |
| } |
281 |
| catch (Exception ex) |
282 |
| { |
283 |
0
| t1_ex = ex;
|
284 |
0
| ex.printStackTrace();
|
285 |
| } |
286 |
| } |
287 |
| }; |
288 |
| |
289 |
2
| Thread t2 = new Thread()
|
290 |
| { |
291 |
| Transaction tx; |
292 |
| |
293 |
2
| public void run()
|
294 |
| { |
295 |
2
| try
|
296 |
| { |
297 |
2
| UserTransaction tx = getTransaction();
|
298 |
2
| tx.begin();
|
299 |
2
| Person p = new Person();
|
300 |
2
| p.setName("Ben");
|
301 |
2
| Address add = new Address();
|
302 |
2
| add.setCity("Taipei");
|
303 |
2
| p.setAddress(add);
|
304 |
2
| cache1_.attach("/test/ben", p);
|
305 |
2
| TestingUtil.sleepThread(1000);
|
306 |
2
| tx.commit();
|
307 |
| } |
308 |
| catch (RollbackException rollback) |
309 |
| { |
310 |
| } |
311 |
| catch (Exception ex) |
312 |
| { |
313 |
0
| t2_ex = ex;
|
314 |
| } |
315 |
| } |
316 |
| }; |
317 |
| |
318 |
2
| t1.start();
|
319 |
2
| t2.start();
|
320 |
| |
321 |
2
| t1.join();
|
322 |
2
| t2.join();
|
323 |
| |
324 |
| |
325 |
2
| if (t1_ex != null)
|
326 |
| { |
327 |
0
| fail("Thread1 failed: " + t1_ex);
|
328 |
| } |
329 |
2
| if (t2_ex != null)
|
330 |
| { |
331 |
0
| fail("Thread2 failed: " + t2_ex);
|
332 |
| } |
333 |
| } |
334 |
| |
335 |
2
| public void testConcurrentTxPutsWithRollbackField() throws Exception
|
336 |
| { |
337 |
2
| Person p = new Person();
|
338 |
2
| p.setName("Ben");
|
339 |
2
| Address add = new Address();
|
340 |
2
| add.setCity("Taipei");
|
341 |
2
| p.setAddress(add);
|
342 |
2
| List<String> lang = new ArrayList<String>();
|
343 |
2
| lang.add("1");
|
344 |
2
| p.setLanguages(lang);
|
345 |
2
| cache_.attach("/test/ben", p);
|
346 |
| |
347 |
2
| Thread t1 = new Thread()
|
348 |
| { |
349 |
| Transaction tx; |
350 |
| |
351 |
2
| public void run()
|
352 |
| { |
353 |
2
| UserTransaction tx = null;
|
354 |
2
| List<String> lang = null;
|
355 |
2
| try
|
356 |
| { |
357 |
2
| Person ben = (Person) cache_.find("/test/ben");
|
358 |
2
| lang = ben.getLanguages();
|
359 |
| |
360 |
2
| tx = getTransaction();
|
361 |
2
| tx.begin();
|
362 |
2
| lang.add("2");
|
363 |
2
| lang.add("3");
|
364 |
2
| lang.remove(0);
|
365 |
2
| TestingUtil.sleepThread(1000);
|
366 |
2
| tx.commit();
|
367 |
| } |
368 |
| catch (RollbackException rollback) |
369 |
| { |
370 |
2
| try
|
371 |
| { |
372 |
2
| tx = getTransaction();
|
373 |
2
| tx.begin();
|
374 |
2
| lang.add("2");
|
375 |
2
| lang.add("3");
|
376 |
2
| lang.remove(0);
|
377 |
2
| tx.commit();
|
378 |
| } |
379 |
| catch (Exception ex) |
380 |
| { |
381 |
0
| t1_ex = ex;
|
382 |
| } |
383 |
| } |
384 |
| catch (Exception ex) |
385 |
| { |
386 |
0
| t1_ex = ex;
|
387 |
| } |
388 |
| } |
389 |
| }; |
390 |
| |
391 |
2
| Thread t2 = new Thread()
|
392 |
| { |
393 |
| Transaction tx; |
394 |
| |
395 |
2
| public void run()
|
396 |
| { |
397 |
2
| List<String> lang = null;
|
398 |
2
| UserTransaction tx = null;
|
399 |
2
| try
|
400 |
| { |
401 |
2
| Person ben = (Person) cache1_.find("/test/ben");
|
402 |
2
| lang = ben.getLanguages();
|
403 |
| |
404 |
2
| tx = getTransaction();
|
405 |
2
| tx.begin();
|
406 |
2
| lang.add("2");
|
407 |
2
| TestingUtil.sleepThread(1000);
|
408 |
2
| tx.commit();
|
409 |
| } |
410 |
| catch (RollbackException rollback) |
411 |
| { |
412 |
2
| TestingUtil.sleepThread(1000);
|
413 |
2
| try
|
414 |
| { |
415 |
2
| tx = getTransaction();
|
416 |
2
| tx.begin();
|
417 |
2
| lang.add("2");
|
418 |
2
| tx.commit();
|
419 |
| } |
420 |
| catch (Exception e) |
421 |
| { |
422 |
0
| e.printStackTrace();
|
423 |
| } |
424 |
| } |
425 |
| catch (Exception ex) |
426 |
| { |
427 |
0
| t2_ex = ex;
|
428 |
| } |
429 |
| } |
430 |
| }; |
431 |
| |
432 |
2
| t1.start();
|
433 |
2
| t2.start();
|
434 |
| |
435 |
2
| t1.join();
|
436 |
2
| t2.join();
|
437 |
| |
438 |
| |
439 |
2
| if (t1_ex != null)
|
440 |
| { |
441 |
0
| fail("Thread1 failed: " + t1_ex);
|
442 |
| } |
443 |
2
| if (t2_ex != null)
|
444 |
| { |
445 |
0
| fail("Thread2 failed: " + t2_ex);
|
446 |
| } |
447 |
| } |
448 |
| |
449 |
2
| public static Test suite() throws Exception
|
450 |
| { |
451 |
2
| return new TestSuite(NewReplicatedTxTest.class);
|
452 |
| } |
453 |
| |
454 |
| |
455 |
0
| public static void main(String[] args) throws Exception
|
456 |
| { |
457 |
0
| junit.textui.TestRunner.run(NewReplicatedTxTest.suite());
|
458 |
| } |
459 |
| |
460 |
| } |