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.test.Person; |
19 |
| import org.jboss.cache.pojo.test.Address; |
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 |
| import java.util.ArrayList; |
26 |
| |
27 |
| |
28 |
| |
29 |
| |
30 |
| |
31 |
| |
32 |
| |
33 |
| public class InMemoryTxUndoTest extends TestCase |
34 |
| { |
35 |
| Log log_ = LogFactory.getLog(InMemoryTxUndoTest.class); |
36 |
| PojoCache cache_; |
37 |
| TransactionManager tx_mgr; |
38 |
| |
39 |
10
| public InMemoryTxUndoTest(String name)
|
40 |
| { |
41 |
10
| super(name);
|
42 |
| } |
43 |
| |
44 |
10
| protected void setUp() throws Exception
|
45 |
| { |
46 |
10
| super.setUp();
|
47 |
10
| log_.info("setUp() ....");
|
48 |
10
| String configFile = "META-INF/local-service.xml";
|
49 |
10
| boolean toStart = false;
|
50 |
10
| cache_ = PojoCacheFactory.createCache(configFile, toStart);
|
51 |
10
| cache_.start();
|
52 |
10
| tx_mgr = DummyTransactionManager.getInstance();
|
53 |
| |
54 |
| } |
55 |
| |
56 |
10
| protected void tearDown() throws Exception
|
57 |
| { |
58 |
10
| super.tearDown();
|
59 |
10
| cache_.stop();
|
60 |
| } |
61 |
| |
62 |
| |
63 |
2
| private boolean hasCacheInterceptor(Object pojo)
|
64 |
| { |
65 |
2
| Interceptor[] interceptors = ((Advised)pojo)._getInstanceAdvisor().getInterceptors();
|
66 |
2
| for(int i=0; i < interceptors.length; i++)
|
67 |
| { |
68 |
0
| if(interceptors[i] instanceof CacheFieldInterceptor)
|
69 |
0
| return true;
|
70 |
| } |
71 |
2
| return false;
|
72 |
| } |
73 |
| |
74 |
2
| public void testSimpleTxWithRollback1() throws Exception
|
75 |
| { |
76 |
2
| log_.info("testSimpleTxWithRollback1() ....");
|
77 |
2
| Person test = new Person();
|
78 |
2
| test.setName("Ben");
|
79 |
2
| test.setAge(10);
|
80 |
2
| tx_mgr.begin();
|
81 |
2
| cache_.attach("/a", test);
|
82 |
2
| test.setAge(20);
|
83 |
2
| tx_mgr.getTransaction().rollback();
|
84 |
| |
85 |
2
| assertFalse("Should not have cache interceptor ", hasCacheInterceptor(test));
|
86 |
2
| assertEquals("Should still be ", 10, test.getAge());
|
87 |
| } |
88 |
| |
89 |
2
| public void testSimpleTxWithRollback2() throws Exception
|
90 |
| { |
91 |
2
| log_.info("testSimpleTxWithRollback2() ....");
|
92 |
2
| Person test = new Person();
|
93 |
2
| test.setName("Ben");
|
94 |
2
| test.setAge(10);
|
95 |
2
| cache_.attach("/a", test);
|
96 |
| |
97 |
2
| tx_mgr.begin();
|
98 |
2
| test.setAge(20);
|
99 |
2
| tx_mgr.getTransaction().rollback();
|
100 |
2
| assertEquals("Should still be ", 10, test.getAge());
|
101 |
| } |
102 |
| |
103 |
2
| public void testSimpleTxWithRollback3() throws Exception
|
104 |
| { |
105 |
2
| log_.info("testSimpleTxWithRollback3() ....");
|
106 |
2
| Person test = new Person();
|
107 |
2
| test.setName("Ben");
|
108 |
2
| test.setAge(10);
|
109 |
2
| cache_.attach("/a", test);
|
110 |
| |
111 |
2
| Person test1 = new Person();
|
112 |
2
| test1.setName("Irin");
|
113 |
2
| test1.setAge(30);
|
114 |
2
| Address addr = new Address();
|
115 |
2
| addr.setCity("Taipei");
|
116 |
2
| test1.setAddress(addr);
|
117 |
2
| tx_mgr.begin();
|
118 |
2
| cache_.attach("/a", test1);
|
119 |
2
| tx_mgr.getTransaction().rollback();
|
120 |
| |
121 |
2
| assertEquals("Should still be ", 10, test.getAge());
|
122 |
2
| assertNull("Address should be ", test.getAddress());
|
123 |
| } |
124 |
| |
125 |
2
| public void testSimpleTxWithRollback4() throws Exception
|
126 |
| { |
127 |
2
| log_.info("testSimpleTxWithRollback4() ....");
|
128 |
2
| Person test = new Person();
|
129 |
2
| test.setName("Ben");
|
130 |
2
| test.setAge(10);
|
131 |
2
| cache_.attach("/a", test);
|
132 |
2
| Address addr = new Address();
|
133 |
2
| addr.setCity("Taipei");
|
134 |
2
| test.setAddress(addr);
|
135 |
| |
136 |
2
| tx_mgr.begin();
|
137 |
2
| addr.setCity("Tainan");
|
138 |
2
| tx_mgr.getTransaction().rollback();
|
139 |
| |
140 |
2
| assertEquals("Should still be ", "Taipei", test.getAddress().getCity());
|
141 |
| } |
142 |
| |
143 |
2
| public void testSimpleTxWithRollback5() throws Exception
|
144 |
| { |
145 |
2
| log_.info("testSimpleTxWithRollback5() ....");
|
146 |
2
| Person test = new Person();
|
147 |
2
| test.setName("Ben");
|
148 |
2
| test.setAge(10);
|
149 |
2
| cache_.attach("/a", test);
|
150 |
| |
151 |
2
| ArrayList lang = new ArrayList();
|
152 |
2
| lang.add("English");
|
153 |
2
| test.setLanguages(lang);
|
154 |
2
| tx_mgr.begin();
|
155 |
2
| lang.add("French");
|
156 |
2
| tx_mgr.getTransaction().rollback();
|
157 |
| |
158 |
2
| assertEquals("Should still be ", 1, test.getLanguages().size());
|
159 |
| } |
160 |
| |
161 |
2
| public static Test suite() throws Exception
|
162 |
| { |
163 |
2
| return new TestSuite(InMemoryTxUndoTest.class);
|
164 |
| } |
165 |
| |
166 |
| |
167 |
0
| public static void main(String[] args) throws Exception
|
168 |
| { |
169 |
0
| junit.textui.TestRunner.run(InMemoryTxUndoTest.suite());
|
170 |
| } |
171 |
| |
172 |
| } |