1 |
| |
2 |
| |
3 |
| |
4 |
| |
5 |
| |
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.test.Person; |
16 |
| import org.jboss.cache.pojo.interceptors.dynamic.CacheFieldInterceptor; |
17 |
| import org.jboss.cache.pojo.PojoCache; |
18 |
| import org.jboss.cache.pojo.PojoCacheFactory; |
19 |
| import org.jboss.cache.transaction.DummyTransactionManager; |
20 |
| import org.jboss.aop.Advised; |
21 |
| import org.jboss.aop.advice.Interceptor; |
22 |
| |
23 |
| import javax.transaction.TransactionManager; |
24 |
| |
25 |
| |
26 |
| |
27 |
| |
28 |
| |
29 |
| |
30 |
| |
31 |
| public class LocalTxUndoTest extends TestCase |
32 |
| { |
33 |
| Log log_ = LogFactory.getLog(LocalTxUndoTest.class); |
34 |
| PojoCache cache_; |
35 |
| TransactionManager tx_mgr; |
36 |
| |
37 |
6
| public LocalTxUndoTest(String name)
|
38 |
| { |
39 |
6
| super(name);
|
40 |
| } |
41 |
| |
42 |
6
| protected void setUp() throws Exception
|
43 |
| { |
44 |
6
| super.setUp();
|
45 |
6
| log_.info("setUp() ....");
|
46 |
6
| String configFile = "META-INF/local-service.xml";
|
47 |
6
| boolean toStart = false;
|
48 |
6
| cache_ = PojoCacheFactory.createCache(configFile, toStart);
|
49 |
6
| cache_.start();
|
50 |
6
| tx_mgr = DummyTransactionManager.getInstance();
|
51 |
| |
52 |
| } |
53 |
| |
54 |
6
| protected void tearDown() throws Exception
|
55 |
| { |
56 |
6
| super.tearDown();
|
57 |
6
| cache_.stop();
|
58 |
| } |
59 |
| |
60 |
| |
61 |
| |
62 |
2
| public void testSimpleTxWithRollback1() throws Exception
|
63 |
| { |
64 |
2
| log_.info("testSimpleTxWithRollback1() ....");
|
65 |
2
| Person test = new Person();
|
66 |
2
| test.setName("Ben");
|
67 |
2
| test.setAge(10);
|
68 |
| |
69 |
2
| tx_mgr.begin();
|
70 |
2
| cache_.attach("/a", test);
|
71 |
2
| tx_mgr.getTransaction().rollback();
|
72 |
2
| assertFalse("Should not have cache interceptor ", hasCacheInterceptor(test));
|
73 |
| |
74 |
2
| cache_.attach("/a", test);
|
75 |
| } |
76 |
| |
77 |
6
| private boolean hasCacheInterceptor(Object pojo)
|
78 |
| { |
79 |
6
| Interceptor[] interceptors = ((Advised)pojo)._getInstanceAdvisor().getInterceptors();
|
80 |
6
| for(int i=0; i < interceptors.length; i++)
|
81 |
| { |
82 |
2
| if(interceptors[i] instanceof CacheFieldInterceptor)
|
83 |
2
| return true;
|
84 |
| } |
85 |
4
| return false;
|
86 |
| } |
87 |
| |
88 |
2
| public void testSimpleTxWithRollback2() throws Exception
|
89 |
| { |
90 |
2
| log_.info("testSimpleTxWithRollback2() ....");
|
91 |
2
| Person test = new Person();
|
92 |
2
| test.setName("Ben");
|
93 |
2
| test.setAge(10);
|
94 |
2
| cache_.attach("/a", test);
|
95 |
| |
96 |
2
| tx_mgr.begin();
|
97 |
2
| cache_.detach("/a");
|
98 |
2
| tx_mgr.getTransaction().rollback();
|
99 |
| |
100 |
2
| assertTrue("Should still have cache interceptor ", hasCacheInterceptor(test));
|
101 |
2
| cache_.detach("/a");
|
102 |
| } |
103 |
| |
104 |
2
| public void testSimpleTxWithRollback3() throws Exception
|
105 |
| { |
106 |
2
| log_.info("testSimpleTxWithRollback3() ....");
|
107 |
2
| Person test = new Person();
|
108 |
2
| test.setName("Ben");
|
109 |
2
| test.setAge(10);
|
110 |
2
| tx_mgr.begin();
|
111 |
2
| cache_.attach("/a", test);
|
112 |
2
| cache_.detach("/a");
|
113 |
2
| tx_mgr.getTransaction().rollback();
|
114 |
| |
115 |
2
| assertFalse("Should not have cache interceptor ", hasCacheInterceptor(test));
|
116 |
| } |
117 |
| |
118 |
2
| public static Test suite() throws Exception
|
119 |
| { |
120 |
2
| return new TestSuite(LocalTxUndoTest.class);
|
121 |
| } |
122 |
| |
123 |
| |
124 |
0
| public static void main(String[] args) throws Exception
|
125 |
| { |
126 |
0
| junit.textui.TestRunner.run(LocalTxUndoTest.suite());
|
127 |
| } |
128 |
| |
129 |
| } |