KickJava   Java API By Example, From Geeks To Geeks.

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


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.rollback;
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.pojo.PojoCache;
16 import org.jboss.cache.pojo.PojoCacheFactory;
17 import org.jboss.cache.pojo.interceptors.dynamic.CacheFieldInterceptor;
18 import org.jboss.cache.pojo.interceptors.PojoFailedTxMockupInterceptor;
19 import org.jboss.cache.pojo.test.Person;
20 import org.jboss.cache.transaction.DummyTransactionManager;
21 import org.jboss.aop.advice.Interceptor;
22 import org.jboss.aop.Advised;
23
24 import javax.transaction.TransactionManager JavaDoc;
25
26 /**
27  * Additional basic tests
28  *
29  * @author Ben Wang
30  */

31
32 public class LocalUndoTest extends TestCase
33 {
34    Log log_ = LogFactory.getLog(LocalUndoTest.class);
35    PojoCache cache_;
36    TransactionManager tx_mgr;
37
38    public LocalUndoTest(String JavaDoc name)
39    {
40       super(name);
41    }
42
43    protected void setUp() throws Exception JavaDoc
44    {
45       super.setUp();
46       log_.info("setUp() ....");
47       String JavaDoc configFile = "META-INF/local-service.xml";
48       boolean toStart = false;
49       cache_ = PojoCacheFactory.createCache(configFile, toStart);
50       cache_.start();
51       tx_mgr = DummyTransactionManager.getInstance();
52
53    }
54
55    protected void tearDown() throws Exception JavaDoc
56    {
57       super.tearDown();
58       cache_.stop();
59    }
60
61 // public void testDummy() {}
62

63    private void setTxRollback(boolean isTrue)
64    {
65       PojoFailedTxMockupInterceptor.TX_ROLLBACK = isTrue;
66    }
67
68    public void testSimpleTxWithRollback1() throws Exception JavaDoc
69    {
70       log_.info("testSimpleTxWithRollback1() ....");
71       Person test = new Person();
72       test.setName("Ben");
73       test.setAge(10);
74
75       setTxRollback(true);
76       cache_.attach("/a", test);
77       assertFalse("Should not have cache interceptor ", hasCacheInterceptor(test));
78    }
79
80    private boolean hasCacheInterceptor(Object JavaDoc pojo)
81    {
82       Interceptor[] interceptors = ((Advised)pojo)._getInstanceAdvisor().getInterceptors();
83       for(int i=0; i < interceptors.length; i++)
84       {
85          if(interceptors[i] instanceof CacheFieldInterceptor)
86             return true;
87       }
88       return false;
89    }
90
91    public void testSimpleTxWithRollback2() throws Exception JavaDoc
92    {
93       log_.info("testSimpleTxWithRollback1() ....");
94       Person test = new Person();
95       test.setName("Ben");
96       test.setAge(10);
97       cache_.attach("/a", test);
98
99       setTxRollback(true);
100       cache_.detach("/a");
101
102       assertTrue("Should still have cache interceptor ", hasCacheInterceptor(test));
103    }
104
105    public static Test suite() throws Exception JavaDoc
106    {
107       return new TestSuite(LocalUndoTest.class);
108    }
109
110
111    public static void main(String JavaDoc[] args) throws Exception JavaDoc
112    {
113       junit.textui.TestRunner.run(LocalUndoTest.suite());
114    }
115
116 }
117
Popular Tags