KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > cache > tests > aop > ReplicatedTxAopTest


1 package org.jboss.cache.tests.aop;
2
3 import junit.framework.Test;
4 import junit.framework.TestCase;
5 import junit.framework.TestSuite;
6 import org.apache.commons.logging.Log;
7 import org.apache.commons.logging.LogFactory;
8 import org.jboss.cache.transaction.DummyTransactionManager;
9
10 import javax.naming.Context JavaDoc;
11 import javax.naming.InitialContext JavaDoc;
12 import javax.naming.NamingException JavaDoc;
13 import javax.transaction.*;
14 import java.util.Properties JavaDoc;
15
16 //import org.jboss.test.JBossTestCase;
17

18
19 /**
20  * LocalTestCase.java that uses standalone aop package.
21  * <p/>
22  * <p/>
23  * Created: Mon May 05 17:30:11 2003
24  */

25
26 public class ReplicatedTxAopTest extends TestCase
27 {
28 // Category log = getLog();
29
Log log=LogFactory.getLog(ReplicatedTxAopTest.class);
30    TreeCacheAopTester tester, tester1;
31    final String JavaDoc FACTORY = "org.jboss.cache.transaction.DummyContextFactory";
32    DummyTransactionManager tx_mgr;
33    Throwable JavaDoc t1_ex, t2_ex;
34    long start=0;
35
36
37    public ReplicatedTxAopTest(String JavaDoc name)
38    {
39       super(name);
40    }
41
42    protected void setUp() throws Exception JavaDoc
43    {
44       super.setUp();
45       log.info("setUp() ....");
46       String JavaDoc configFile = "META-INF/replSync-service.xml";
47       tester = new TreeCacheAopTester(configFile);
48       tester1 = new TreeCacheAopTester(configFile);
49
50       System.setProperty(Context.INITIAL_CONTEXT_FACTORY, FACTORY);
51
52       tx_mgr = DummyTransactionManager.getInstance();
53       t1_ex = t2_ex = null;
54    }
55
56    protected void tearDown() throws Exception JavaDoc
57    {
58       super.tearDown();
59       tester.stop();
60       tester1.stop();
61       tester = null;
62       tester1 = null;
63
64       DummyTransactionManager.destroy();
65    }
66
67 // public void testDummy() {}
68

69    public void testSetup()
70    {
71       log.info("testSetup() ....");
72       try {
73          tester.testSetup();
74          tester1.testSetup();
75       } catch (Exception JavaDoc ex) {
76          ex.printStackTrace();
77          fail("testSetup(): fails. " + ex.toString());
78       }
79    }
80
81    UserTransaction getTransaction() throws SystemException, NotSupportedException, NamingException JavaDoc
82    {
83       Properties JavaDoc prop = new Properties JavaDoc();
84       prop.put(Context.INITIAL_CONTEXT_FACTORY,
85           "org.jboss.cache.transaction.DummyContextFactory");
86       return (UserTransaction)new InitialContext JavaDoc(prop).lookup("UserTransaction");
87    }
88
89    public void testSimple() throws Exception JavaDoc
90    {
91       log.info("testSimple() ....");
92       UserTransaction tx = getTransaction();
93       tx.begin();
94       tester.createPerson("/person/test1", "Harald Gliebe", 32);
95       tx.commit();
96       tx.begin();
97       tester.setName("/person/test1", "Benoit");
98       tx.commit();
99       assertEquals((Object JavaDoc) "Benoit", (Object JavaDoc) tester.getName("/person/test1"));
100       assertEquals((Object JavaDoc) "Benoit", (Object JavaDoc) tester1.getName("/person/test1"));
101       tx.begin();
102       tester.setAge("/person/test1", 61);
103       tx.commit();
104       assertEquals(61, tester.getAge("/person/test1"));
105       assertEquals(61, tester1.getAge("/person/test1"));
106    }
107
108    public void testModification() throws Exception JavaDoc
109    {
110       UserTransaction tx = getTransaction();
111       tx.begin();
112       tester.createPerson("/person/test2", "Harald", 32);
113       tester.setName("/person/test2", "Harald Gliebe");
114       tx.commit();
115       tx.begin();
116       tester1.setName("/person/test2", "Benoit");
117       tx.commit();
118       assertEquals((Object JavaDoc) tester.getName("/person/test2"), (Object JavaDoc) "Benoit");
119       assertEquals((Object JavaDoc) tester1.getName("/person/test2"), (Object JavaDoc) "Benoit");
120       tx.begin();
121       tester1.setName("/person/test2", "Harald");
122       tx.rollback();
123       assertEquals((Object JavaDoc) tester.getName("/person/test2"), (Object JavaDoc) "Benoit");
124       assertEquals((Object JavaDoc) tester1.getName("/person/test2"), (Object JavaDoc) "Benoit");
125       tester.removePerson("/person/test2");
126    }
127
128
129
130    public void testConcurrentPuts() throws Exception JavaDoc
131    {
132       tester.setSyncCommitPhase(true);
133
134       Thread JavaDoc t1 = new Thread JavaDoc()
135       {
136          Transaction tx;
137
138          public void run()
139          {
140             try {
141                tester.createPerson("/person/test6", "p6", 50);
142                UserTransaction tx = getTransaction();
143                tx.begin();
144                tester.addLanguage("/person/test6", "German");
145                _pause(4000);
146                tx.commit();
147             }
148             catch(RollbackException rollback) {
149                ;
150             }
151             catch (Exception JavaDoc ex) {
152                t1_ex = ex;
153             }
154          }
155       };
156
157       Thread JavaDoc t2 = new Thread JavaDoc()
158       {
159          Transaction tx;
160
161          public void run()
162          {
163             try {
164                _pause(1000); // give Thread1 time to createPerson
165
UserTransaction tx = getTransaction();
166                tx.begin();
167                tester.addLanguage("/person/test6", "English");
168                tx.commit();
169             }
170             catch(RollbackException rollback) {
171                ;
172             }
173             catch (Exception JavaDoc ex) {
174                t2_ex = ex;
175             }
176          }
177       };
178
179       t1.start();
180       t2.start();
181
182       t1.join();
183       t2.join();
184
185       // t2 should rollback due to timeout while t2 should succeed
186
if(t2_ex != null)
187          fail("Thread1 failed: " + t2_ex);
188       if(t1_ex != null)
189          fail("Thread2 failed: " + t1_ex);
190
191       int size = tester.getLanguagesSize("/person/test6");
192       assertEquals("number of languages should be 2, but is " + size + " (" +
193                    tester.getLanguages("/person/test6") + ")",
194                    2, size);
195       System.out.println("tester: " + tester.getLanguages("/person/test6"));
196       size = tester1.getLanguagesSize("/person/test6");
197       assertEquals("number of languages should be 2, but is " + size + " (" +
198                    tester.getLanguages("/person/test6") + ")",
199                    2, size);
200       System.out.println("tester1: " + tester1.getLanguages("/person/test6"));
201    }
202
203    static void _pause(long millis)
204    {
205       try {
206          Thread.sleep(millis);
207       } catch (Exception JavaDoc ex) {
208       }
209    }
210
211    void log(String JavaDoc s) {
212       long now;
213       if(start == 0)
214          start=System.currentTimeMillis();
215       now=System.currentTimeMillis();
216
217       System.out.println("[" + Thread.currentThread().getName() + "] [" + (now - start) + "] " + s);
218     }
219
220
221    public static Test suite() throws Exception JavaDoc
222    {
223       return new TestSuite(ReplicatedTxAopTest.class);
224    }
225
226
227    public static void main(String JavaDoc[] args) throws Exception JavaDoc
228    {
229       junit.textui.TestRunner.run(suite());
230    }
231
232 }
233
234
Popular Tags