KickJava   Java API By Example, From Geeks To Geeks.

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


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

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

60    public void testSimpleTxWithRollback1() throws Exception JavaDoc
61    {
62       log_.info("testSimpleTxWithRollback1() ....");
63       Person test = new Person();
64       test.setName("Ben");
65       test.setAge(10);
66
67       tx_mgr.begin();
68       cache_.attach("/a", test);
69       tx_mgr.getTransaction().rollback();
70       assertFalse("Should not have cache interceptor ", hasCacheInterceptor(test));
71    }
72
73    private boolean hasCacheInterceptor(Object JavaDoc pojo)
74    {
75       Interceptor[] interceptors = null;
76       try {
77          interceptors = ((Advised)pojo)._getInstanceAdvisor().getInterceptors();
78       } catch (Exception JavaDoc ex)
79       {
80          return false;
81       }
82
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       tx_mgr.begin();
100       cache_.detach("/a");
101       tx_mgr.getTransaction().rollback();
102
103       assertTrue("Should still have cache interceptor ", hasCacheInterceptor(test));
104    }
105
106    public void testSimpleTxWithRollback3() throws Exception JavaDoc
107    {
108       log_.info("testSimpleTxWithRollback1() ....");
109       Person test = new Person();
110       test.setName("Ben");
111       test.setAge(10);
112       tx_mgr.begin();
113       cache_.attach("/a", test);
114       cache_.detach("/a");
115       tx_mgr.getTransaction().rollback();
116
117       assertFalse("Should not have cache interceptor ", hasCacheInterceptor(test));
118    }
119
120    public static Test suite() throws Exception JavaDoc
121    {
122       return new TestSuite(TxUndoTest.class);
123    }
124
125
126    public static void main(String JavaDoc[] args) throws Exception JavaDoc
127    {
128       junit.textui.TestRunner.run(TxUndoTest.suite());
129    }
130
131 }
132
Popular Tags