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.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; |
25 |
| |
26 |
| |
27 |
| |
28 |
| |
29 |
| |
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 |
4
| public LocalUndoTest(String name)
|
39 |
| { |
40 |
4
| super(name);
|
41 |
| } |
42 |
| |
43 |
4
| protected void setUp() throws Exception
|
44 |
| { |
45 |
4
| super.setUp();
|
46 |
4
| log_.info("setUp() ....");
|
47 |
4
| String configFile = "META-INF/local-service.xml";
|
48 |
4
| boolean toStart = false;
|
49 |
4
| cache_ = PojoCacheFactory.createCache(configFile, toStart);
|
50 |
4
| cache_.start();
|
51 |
4
| tx_mgr = DummyTransactionManager.getInstance();
|
52 |
| |
53 |
| } |
54 |
| |
55 |
4
| protected void tearDown() throws Exception
|
56 |
| { |
57 |
4
| super.tearDown();
|
58 |
4
| cache_.stop();
|
59 |
| } |
60 |
| |
61 |
| |
62 |
| |
63 |
4
| private void setTxRollback(boolean isTrue)
|
64 |
| { |
65 |
4
| PojoFailedTxMockupInterceptor.TX_ROLLBACK = isTrue;
|
66 |
| } |
67 |
| |
68 |
2
| public void testSimpleTxWithRollback1() throws Exception
|
69 |
| { |
70 |
2
| log_.info("testSimpleTxWithRollback1() ....");
|
71 |
2
| Person test = new Person();
|
72 |
2
| test.setName("Ben");
|
73 |
2
| test.setAge(10);
|
74 |
| |
75 |
2
| setTxRollback(true);
|
76 |
2
| cache_.attach("/a", test);
|
77 |
2
| assertFalse("Should not have cache interceptor ", hasCacheInterceptor(test));
|
78 |
| } |
79 |
| |
80 |
4
| private boolean hasCacheInterceptor(Object pojo)
|
81 |
| { |
82 |
4
| Interceptor[] interceptors = ((Advised)pojo)._getInstanceAdvisor().getInterceptors();
|
83 |
4
| for(int i=0; i < interceptors.length; i++)
|
84 |
| { |
85 |
2
| if(interceptors[i] instanceof CacheFieldInterceptor)
|
86 |
2
| return true;
|
87 |
| } |
88 |
2
| return false;
|
89 |
| } |
90 |
| |
91 |
2
| public void testSimpleTxWithRollback2() throws Exception
|
92 |
| { |
93 |
2
| log_.info("testSimpleTxWithRollback1() ....");
|
94 |
2
| Person test = new Person();
|
95 |
2
| test.setName("Ben");
|
96 |
2
| test.setAge(10);
|
97 |
2
| cache_.attach("/a", test);
|
98 |
| |
99 |
2
| setTxRollback(true);
|
100 |
2
| cache_.detach("/a");
|
101 |
| |
102 |
2
| assertTrue("Should still have cache interceptor ", hasCacheInterceptor(test));
|
103 |
| } |
104 |
| |
105 |
2
| public static Test suite() throws Exception
|
106 |
| { |
107 |
2
| return new TestSuite(LocalUndoTest.class);
|
108 |
| } |
109 |
| |
110 |
| |
111 |
0
| public static void main(String[] args) throws Exception
|
112 |
| { |
113 |
0
| junit.textui.TestRunner.run(LocalUndoTest.suite());
|
114 |
| } |
115 |
| |
116 |
| } |