KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > cache > pojo > NewReplicatedTxTest


1 /*
2  * JBoss, Home of Professional Open Source
3  *
4  * Distributable under LGPL license.
5  * See terms of license at gnu.org.
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.pojo.test.Address;
16 import org.jboss.cache.pojo.test.Person;
17 import org.jboss.cache.transaction.DummyTransactionManager;
18
19 import javax.naming.Context JavaDoc;
20 import javax.naming.InitialContext JavaDoc;
21 import javax.naming.NamingException JavaDoc;
22 import javax.transaction.NotSupportedException JavaDoc;
23 import javax.transaction.RollbackException JavaDoc;
24 import javax.transaction.SystemException JavaDoc;
25 import javax.transaction.Transaction JavaDoc;
26 import javax.transaction.UserTransaction JavaDoc;
27 import java.util.ArrayList JavaDoc;
28 import java.util.List JavaDoc;
29 import java.util.Properties JavaDoc;
30
31 /**
32  * PojoCache replicated test cases with tx.
33  *
34  * @author Ben Wang
35  */

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