KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > cache > pojo > rollback > LocalExceptionUndoTest


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.rollback;
24
25 import junit.framework.TestCase;
26 import junit.framework.Test;
27 import junit.framework.TestSuite;
28 import org.apache.commons.logging.Log;
29 import org.apache.commons.logging.LogFactory;
30 import org.jboss.cache.pojo.PojoCache;
31 import org.jboss.cache.pojo.PojoCacheFactory;
32 import org.jboss.cache.pojo.InternalConstant;
33 import org.jboss.cache.pojo.PojoCacheException;
34 import org.jboss.cache.pojo.test.Person;
35 import org.jboss.cache.pojo.interceptors.PojoFailedTxMockupInterceptor;
36 import org.jboss.cache.pojo.interceptors.dynamic.CacheFieldInterceptor;
37 import org.jboss.cache.transaction.DummyTransactionManager;
38 import org.jboss.cache.Fqn;
39 import org.jboss.aop.advice.Interceptor;
40 import org.jboss.aop.Advised;
41
42 import javax.transaction.TransactionManager JavaDoc;
43 import java.util.ArrayList JavaDoc;
44
45 /**
46  * Additional basic tests
47  *
48  * @author Ben Wang
49  */

50
51 public class LocalExceptionUndoTest extends TestCase
52 {
53    Log log_ = LogFactory.getLog(LocalExceptionUndoTest.class);
54    PojoCache cache_;
55    TransactionManager tx_mgr;
56    boolean isTrue = false;
57
58    public LocalExceptionUndoTest(String JavaDoc name)
59    {
60       super(name);
61    }
62
63    protected void setUp() throws Exception JavaDoc
64    {
65       super.setUp();
66       log_.info("setUp() ....");
67       String JavaDoc configFile = "META-INF/local-service.xml";
68       boolean toStart = false;
69       cache_ = PojoCacheFactory.createCache(configFile, toStart);
70       cache_.getCache().getConfiguration().setLockAcquisitionTimeout(500); // timeout to 500 ms
71
cache_.start();
72       tx_mgr = DummyTransactionManager.getInstance();
73    }
74
75    private void startLockThread() throws Exception JavaDoc
76    {
77       // Lock thread to lock underlying node.
78
(new LockThread()).start();
79       Thread.sleep(300);
80    }
81
82    private void stopLockThread() throws Exception JavaDoc
83    {
84       isTrue = true;
85       Thread.sleep(200);
86    }
87
88    protected void tearDown() throws Exception JavaDoc
89    {
90       super.tearDown();
91       cache_.stop();
92    }
93
94    public void testSimpleTxWithRollback1() throws Exception JavaDoc
95    {
96       log_.info("testSimpleTxWithRollback1() ....");
97       Person test = new Person();
98       test.setName("Ben");
99       test.setAge(10);
100       ArrayList JavaDoc list = new ArrayList JavaDoc();
101       list.add("English");
102       test.setLanguages(list);
103
104       startLockThread();
105
106       try {
107          cache_.attach("/a", test);
108       } catch (PojoCacheException ex)
109       {
110 // ex.printStackTrace();
111
}
112       assertFalse("Should not have cache interceptor ", hasCacheInterceptor(test));
113       assertNull("Should be null", cache_.find("/a"));
114
115       stopLockThread();
116       cache_.attach("/a", test);
117    }
118
119    public void testSimpleTxWithRollback2() throws Exception JavaDoc
120    {
121       log_.info("testSimpleTxWithRollback1() ....");
122       Person test = new Person();
123       test.setName("Ben");
124       test.setAge(10);
125       ArrayList JavaDoc list = new ArrayList JavaDoc();
126       list.add("English");
127       test.setLanguages(list);
128
129       try {
130          cache_.attach("/a", test);
131       } catch (PojoCacheException ex)
132       {
133 // ex.printStackTrace();
134
}
135
136       startLockThread();
137
138       try {
139          cache_.detach("/a");
140       } catch (PojoCacheException ex)
141       {
142 // ex.printStackTrace();
143
}
144
145       stopLockThread();
146       assertTrue("Should still have cache interceptor ", hasCacheInterceptor(test));
147       cache_.detach("/a");
148       assertNull("Should be null", cache_.find("/a"));
149    }
150
151    private boolean hasCacheInterceptor(Object JavaDoc pojo)
152    {
153       Interceptor[] interceptors = ((Advised)pojo)._getInstanceAdvisor().getInterceptors();
154       for(int i=0; i < interceptors.length; i++)
155       {
156          if(interceptors[i] instanceof CacheFieldInterceptor)
157             return true;
158       }
159       return false;
160    }
161
162    public static Test suite() throws Exception JavaDoc
163    {
164       return new TestSuite(LocalExceptionUndoTest.class);
165    }
166
167
168    public static void main(String JavaDoc[] args) throws Exception JavaDoc
169    {
170       junit.textui.TestRunner.run(suite());
171    }
172
173    public class LockThread extends Thread JavaDoc
174    {
175       public void run()
176       {
177          try
178          {
179             Fqn f = new Fqn(InternalConstant.JBOSS_INTERNAL_STRING);
180             cache_.getCache().put(new Fqn(f, "123"), "key", "test");
181             cache_.getCache().put(new Fqn("a"), "key", "test");
182             tx_mgr.begin();
183             cache_.getCache().put(new Fqn(f, "124"), "key", "test");
184
185             while (!isTrue)
186             {
187                sleep(100);
188             }
189             tx_mgr.commit();
190          } catch (Exception JavaDoc ex)
191          {
192             ex.printStackTrace();
193          }
194
195       }
196    }
197 }
198
Popular Tags