KickJava   Java API By Example, From Geeks To Geeks.

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


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.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.transaction.DummyTransactionManager;
16 import org.jboss.cache.pojo.test.Person;
17 import org.jboss.cache.pojo.test.Address;
18
19 import javax.naming.Context JavaDoc;
20 import javax.naming.NamingException JavaDoc;
21 import javax.naming.InitialContext JavaDoc;
22 import javax.transaction.UserTransaction JavaDoc;
23 import javax.transaction.SystemException JavaDoc;
24 import javax.transaction.NotSupportedException JavaDoc;
25 import javax.transaction.Transaction JavaDoc;
26 import javax.transaction.RollbackException JavaDoc;
27 import java.util.Properties JavaDoc;
28 import java.util.List JavaDoc;
29 import java.util.ArrayList JavaDoc;
30
31 /**
32  */

33
34 public class LocalTxTest extends TestCase
35 {
36    Log log = LogFactory.getLog(LocalTxTest.class);
37    PojoCache cache;
38    final String JavaDoc FACTORY = "org.jboss.cache.transaction.DummyContextFactory";
39    DummyTransactionManager tx_mgr;
40    Throwable JavaDoc t1_ex, t2_ex;
41    long start = 0;
42
43
44    public LocalTxTest(String JavaDoc name)
45    {
46       super(name);
47    }
48
49    protected void setUp() throws Exception JavaDoc
50    {
51       super.setUp();
52       log.info("setUp() ....");
53       String JavaDoc configFile = "META-INF/local-service.xml";
54       boolean toStart = false;
55       cache = PojoCacheFactory.createCache(configFile, toStart);
56       cache.start();
57
58       System.setProperty(Context.INITIAL_CONTEXT_FACTORY, FACTORY);
59
60       tx_mgr = DummyTransactionManager.getInstance();
61       t1_ex = t2_ex = null;
62    }
63
64    protected void tearDown() throws Exception JavaDoc
65    {
66       super.tearDown();
67       cache.stop();
68
69       DummyTransactionManager.destroy();
70    }
71
72 // public void testDummy() {}
73

74    UserTransaction JavaDoc getTransaction() throws SystemException JavaDoc, NotSupportedException JavaDoc, NamingException JavaDoc
75    {
76       Properties JavaDoc prop = new Properties JavaDoc();
77       prop.put(Context.INITIAL_CONTEXT_FACTORY,
78               "org.jboss.cache.transaction.DummyContextFactory");
79       return (UserTransaction JavaDoc) new InitialContext JavaDoc(prop).lookup("UserTransaction");
80    }
81
82    private Person createPerson(String JavaDoc id, String JavaDoc name, int age)
83    {
84       Person p = new Person();
85       p.setName(name);
86       p.setAge(age);
87       cache.attach(id, p);
88       return p;
89    }
90
91    public void testSimple() throws Exception JavaDoc
92    {
93       log.info("testSimple() ....");
94       UserTransaction JavaDoc tx = getTransaction();
95       tx.begin();
96       Person p = createPerson("/person/test1", "Harald Gliebe", 32);
97       tx.commit();
98       tx.begin();
99       p.setName("Benoit");
100       tx.commit();
101       Person p1 = (Person) cache.find("/person/test1");
102       assertEquals("Benoit", p.getName());
103       assertEquals("Benoit", p1.getName());
104       tx.begin();
105       p1.setAge(61);
106       tx.commit();
107       assertEquals(61, p.getAge());
108       assertEquals(61, p1.getAge());
109    }
110
111    public void testModification() throws Exception JavaDoc
112    {
113       UserTransaction JavaDoc tx = getTransaction();
114       tx.begin();
115       Person p = createPerson("/person/test2", "Harald", 32);
116       p.setName("Harald Gliebe");
117       tx.commit();
118       Person p1 = (Person) cache.find("/person/test2");
119       tx.begin();
120       p1.setName("Benoit");
121       tx.commit();
122       assertEquals(p.getName(), "Benoit");
123       assertEquals(p1.getName(), "Benoit");
124       tx.begin();
125       p1.setName("Harald");
126       tx.rollback();
127       assertEquals(p.getName(), "Benoit");
128       assertEquals(p1.getName(), "Benoit");
129    }
130
131    public void testConcurrentSimplePuts() throws Exception JavaDoc
132    {
133       Thread JavaDoc t1 = new Thread JavaDoc("t1")
134       {
135          Transaction JavaDoc tx;
136
137          public void run()
138          {
139             try
140             {
141                Person p = (Person) cache.find("/person/test6");
142                Address addr = new Address();
143                addr.setCity("San Jose");
144                UserTransaction JavaDoc tx = getTransaction();
145                tx.begin();
146                // Note that this will create a write lock on p (on the JBossInternal node)
147
p.setAddress(addr);
148                TestingUtil.sleepThread(17000);
149                tx.commit();
150             }
151             catch (RollbackException JavaDoc rollback)
152             {
153                ;
154             }
155             catch (Exception JavaDoc ex)
156             {
157                t1_ex = ex;
158             }
159          }
160       };
161
162       Thread JavaDoc t2 = new Thread JavaDoc("t2")
163       {
164          Transaction JavaDoc tx;
165
166          public void run()
167          {
168             UserTransaction JavaDoc tx = null;
169             try
170             {
171                TestingUtil.sleepThread(1000); // give Thread1 time to createPerson
172
Person p = (Person) cache.find("/person/test6");
173                Address addr = new Address();
174                addr.setCity("Santa Clara");
175                tx = getTransaction();
176                tx.begin();
177                p.setAddress(addr);
178                tx.commit();
179             }
180             catch (RollbackException JavaDoc rollback)
181             {
182                ;
183             }
184             catch (org.jboss.cache.pojo.PojoCacheException tex)
185             {
186                // This will be a timeout exception. OK.
187
}
188             catch (Exception JavaDoc ex)
189             {
190 // t2_ex = ex;
191
if(tx != null)
192                {
193                   try
194                   {
195                      tx.rollback();
196                   } catch (SystemException JavaDoc e)
197                   {
198                      e.printStackTrace();
199                      t2_ex = e;
200                   }
201                }
202                t2_ex = ex;
203                ex.printStackTrace();
204             }
205          }
206       };
207
208       Person p = createPerson("/person/test6", "p6", 50);
209
210       t1.start();
211       t2.start();
212
213       t1.join();
214       t2.join();
215
216       // t2 should rollback due to timeout while t2 should succeed
217
if (t2_ex != null)
218          fail("Thread1 failed: " + t2_ex);
219       if (t1_ex != null)
220          fail("Thread2 failed: " + t1_ex);
221
222       assertEquals("City ", "San Jose", p.getAddress().getCity());
223    }
224
225    public void testConcurrentPuts() throws Exception JavaDoc
226    {
227       Thread JavaDoc t1 = new Thread JavaDoc("t1")
228       {
229          Transaction JavaDoc tx;
230
231          public void run()
232          {
233             try
234             {
235                List JavaDoc<String JavaDoc> lang = ((Person) cache.find("/person/test6")).getLanguages();
236                UserTransaction JavaDoc tx = getTransaction();
237                tx.begin();
238                lang.add("German");
239                TestingUtil.sleepThread(17000);
240                tx.commit();
241             }
242             catch (RollbackException JavaDoc rollback)
243             {
244                ;
245             }
246             catch (Exception JavaDoc ex)
247             {
248                t1_ex = ex;
249             }
250          }
251       };
252
253       Thread JavaDoc t2 = new Thread JavaDoc("t2")
254       {
255          Transaction JavaDoc tx;
256
257          public void run()
258          {
259             UserTransaction JavaDoc tx = null;
260             try
261             {
262                TestingUtil.sleepThread(1000); // give Thread1 time to createPerson
263
List JavaDoc<String JavaDoc> lang = ((Person) cache.find("/person/test6")).getLanguages();
264                tx = getTransaction();
265                tx.begin();
266                lang.add("English");
267                tx.commit();
268             }
269             catch (RollbackException JavaDoc rollback)
270             {
271                ;
272             }
273             catch (org.jboss.cache.lock.TimeoutException tex)
274             {
275                //
276
}
277             catch (Exception JavaDoc ex)
278             {
279                if(tx != null)
280                {
281                   try
282                   {
283                      tx.rollback();
284                   } catch (SystemException JavaDoc e)
285                   {
286                      e.printStackTrace();
287                      t2_ex = e;
288                   }
289                }
290                t2_ex = ex;
291                ex.printStackTrace();
292             }
293          }
294       };
295
296       Person p = createPerson("/person/test6", "p6", 50);
297       List JavaDoc<String JavaDoc> lang = new ArrayList JavaDoc<String JavaDoc>();
298       lang.add("German");
299       p.setLanguages(lang);
300
301       t1.start();
302       t2.start();
303
304       t1.join();
305       t2.join();
306
307       // t2 should rollback due to timeout while t2 should succeed
308
if (t2_ex != null)
309          fail("Thread1 failed: " + t2_ex);
310       if (t1_ex != null)
311          fail("Thread2 failed: " + t1_ex);
312
313       int size = ((Person) cache.find("/person/test6")).getLanguages().size();
314       assertEquals("number of languages ",
315               2, size);
316    }
317
318    public void testConcurrentPutsNoWait() throws Exception JavaDoc
319    {
320       Thread JavaDoc t1 = new Thread JavaDoc("t1")
321       {
322          UserTransaction JavaDoc tx;
323
324          public void run()
325          {
326             try
327             {
328                for(int i=0; i< 100; i++)
329                {
330                   String JavaDoc id = "/p1/test7";
331                   Person p = createPerson(id, "p6", 50);
332                   cache.detach(id);
333                   p = createPerson(id, "p6", 51);
334                   List JavaDoc<String JavaDoc> lang = new ArrayList JavaDoc<String JavaDoc>();
335                   tx = getTransaction();
336                   tx.begin();
337                   lang.add("German");
338                   lang.add("English");
339                   try {
340                     p.setLanguages(lang);
341                   } catch (PojoCacheException ex)
342                   {
343                      ex.printStackTrace();
344                   }
345                   tx.commit();
346                   TestingUtil.sleepThread(20); // give Thread1 time to createPerson
347
}
348             }
349             catch (RollbackException JavaDoc rollback)
350             {
351                rollback.printStackTrace();
352             }
353             catch (PojoCacheException pe)
354             {
355                pe.printStackTrace();
356             }
357             catch (Exception JavaDoc ex)
358             {
359                t1_ex = ex;
360             }
361          }
362       };
363
364       Thread JavaDoc t2 = new Thread JavaDoc("t2")
365       {
366          UserTransaction JavaDoc tx;
367
368          public void run()
369          {
370             try
371             {
372                for(int i=0; i< 100; i++)
373                {
374                   String JavaDoc id = "/p1/test8";
375                   Person p = createPerson(id, "p6", 50);
376                   cache.detach(id);
377                   p = createPerson(id, "p6", 51);
378                   List JavaDoc<String JavaDoc> lang = new ArrayList JavaDoc<String JavaDoc>();
379                   tx = getTransaction();
380                   tx.begin();
381                   lang.add("German");
382                   lang.add("English");
383                      try {
384                        p.setLanguages(lang);
385                      } catch (PojoCacheException ex)
386                      {
387                         ex.printStackTrace();
388                      }
389                   tx.commit();
390                   TestingUtil.sleepThread(20); // give Thread1 time to createPerson
391
}
392             }
393             catch (RollbackException JavaDoc rollback)
394             {
395                rollback.printStackTrace();
396             }
397             catch (PojoCacheException pe)
398             {
399                pe.printStackTrace();
400             }
401             catch (Exception JavaDoc ex)
402             {
403                t2_ex = ex;
404                ex.printStackTrace();
405             }
406          }
407       };
408
409       Person p1 = createPerson("/p1/test6", "p6", 50);
410       Person p2 = createPerson("/p2/test6", "p6", 50);
411
412       t1.start();
413       t2.start();
414
415       t1.join();
416       t2.join();
417
418       // t2 should rollback due to timeout while t2 should succeed
419
if (t2_ex != null)
420          fail("Thread1 failed: " + t2_ex);
421       if (t1_ex != null)
422          fail("Thread2 failed: " + t1_ex);
423
424 // int size = ((Person) cache.find("/p1/test7")).getLanguages().size();
425
// assertEquals("number of languages ",
426
// 2, size);
427
}
428
429    void log(String JavaDoc s)
430    {
431       long now;
432       if (start == 0)
433          start = System.currentTimeMillis();
434       now = System.currentTimeMillis();
435
436       System.out.println("[" + Thread.currentThread().getName() + "] [" + (now - start) + "] " + s);
437    }
438
439
440    public static Test suite() throws Exception JavaDoc
441    {
442       return new TestSuite(LocalTxTest.class);
443    }
444
445
446    public static void main(String JavaDoc[] args) throws Exception JavaDoc
447    {
448       junit.textui.TestRunner.run(LocalTxTest.suite());
449    }
450
451 }
452
Popular Tags