KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * JBoss, Home of Professional Open Source.
3  * Copyright 2006, Red Hat Middleware LLC, and individual contributors
4  * as indicated by the @author tags. See the copyright.txt file in the
5  * distribution for a full listing of individual contributors.
6  *
7  * This is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU Lesser General Public License as
9  * published by the Free Software Foundation; either version 2.1 of
10  * the License, or (at your option) any later version.
11  *
12  * This software is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this software; if not, write to the Free
19  * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20  * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21  */

22
23 package org.jboss.cache.pojo.optimistic;
24
25 import junit.framework.Test;
26 import junit.framework.TestSuite;
27 import org.apache.commons.logging.Log;
28 import org.apache.commons.logging.LogFactory;
29 import org.jboss.cache.Fqn;
30 import org.jboss.cache.pojo.PojoCache;
31 import org.jboss.cache.pojo.TestingUtil;
32 import org.jboss.cache.pojo.test.Address;
33 import org.jboss.cache.pojo.test.Person;
34 import org.jboss.cache.transaction.DummyTransactionManager;
35
36 import javax.naming.Context JavaDoc;
37 import javax.naming.InitialContext JavaDoc;
38 import javax.naming.NamingException JavaDoc;
39 import javax.transaction.NotSupportedException JavaDoc;
40 import javax.transaction.RollbackException JavaDoc;
41 import javax.transaction.SystemException JavaDoc;
42 import javax.transaction.Transaction JavaDoc;
43 import javax.transaction.UserTransaction JavaDoc;
44 import java.util.ArrayList JavaDoc;
45 import java.util.List JavaDoc;
46 import java.util.Properties JavaDoc;
47
48 /**
49  */

50 public class LocalTxTest extends AbstractOptimisticTestCase
51 {
52    Log log = LogFactory.getLog(LocalTxTest.class);
53    PojoCache cache;
54    final String JavaDoc FACTORY = "org.jboss.cache.transaction.DummyContextFactory";
55    DummyTransactionManager tx_mgr;
56    Throwable JavaDoc t1_ex, t2_ex;
57    long start = 0;
58
59
60    public LocalTxTest(String JavaDoc name)
61    {
62       super(name);
63    }
64
65    protected void setUp() throws Exception JavaDoc
66    {
67       super.setUp();
68       log.info("setUp() ....");
69
70       cache = createCache();
71 // cache = createPessimisticCache();
72

73       System.setProperty(Context.INITIAL_CONTEXT_FACTORY, FACTORY);
74
75       tx_mgr = DummyTransactionManager.getInstance();
76       t1_ex = t2_ex = null;
77    }
78
79    protected void tearDown()
80    {
81       super.tearDown();
82       cache.stop();
83
84       DummyTransactionManager.destroy();
85    }
86
87 // public void testDummy() {}
88

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