KickJava   Java API By Example, From Geeks To Geeks.

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


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.test.Person;
18 import org.jboss.cache.transaction.DummyTransactionManager;
19 import org.jboss.aop.proxy.ClassProxy;
20
21 import javax.transaction.TransactionManager JavaDoc;
22 import java.util.HashMap JavaDoc;
23 import java.util.Map JavaDoc;
24
25 /**
26  * Additional basic tests
27  *
28  * @author Ben Wang
29  */

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

62    public void testSimple() throws Exception JavaDoc
63    {
64       HashMap JavaDoc map = new HashMap JavaDoc();
65       map.put("1", "test1");
66
67       tx_mgr.begin();
68       cache_.attach("/a", map);
69       tx_mgr.getTransaction().rollback();
70       assertFalse("Should not have cache interceptor ", isProxy(map));
71
72       cache_.attach("/a", map);
73    }
74
75    public void testSimpleTxWithRollback1() throws Exception JavaDoc
76    {
77       log_.info("testSimpleTxWithRollback1() ....");
78       Person test = new Person();
79       test.setName("Ben");
80       test.setAge(10);
81       HashMap JavaDoc map = new HashMap JavaDoc();
82       map.put("1", "English");
83       test.setHobbies(map);
84
85       tx_mgr.begin();
86       cache_.attach("/a", test);
87       tx_mgr.getTransaction().rollback();
88       assertFalse("Should not have cache interceptor ", isProxy(test.getHobbies()));
89
90       cache_.attach("/a", test);
91    }
92
93    private boolean isProxy(Object JavaDoc pojo)
94    {
95       if(pojo instanceof ClassProxy) return true;
96       return false;
97    }
98
99    public void testSimpleTxWithRollback2() throws Exception JavaDoc
100    {
101       log_.info("testSimpleTxWithRollback1() ....");
102       Person test = new Person();
103       test.setName("Ben");
104       test.setAge(10);
105       HashMap JavaDoc map = new HashMap JavaDoc();
106       map.put("1", "English");
107       test.setHobbies(map);
108
109       cache_.attach("/a", test);
110
111       tx_mgr.begin();
112       cache_.detach("/a");
113       tx_mgr.getTransaction().rollback();
114
115       assertTrue("Should still have cache interceptor ", isProxy(test.getHobbies()));
116       cache_.detach("/a");
117    }
118
119    public void testSimpleTxWithRollback3() throws Exception JavaDoc
120    {
121       log_.info("testSimpleTxWithRollback1() ....");
122       Person test = new Person();
123       test.setName("Ben");
124       test.setAge(10);
125       HashMap JavaDoc map = new HashMap JavaDoc();
126       map.put("1", "English");
127       test.setHobbies(map);
128       tx_mgr.begin();
129       cache_.attach("/a", test);
130       cache_.detach("/a");
131       tx_mgr.getTransaction().rollback();
132
133       assertFalse("Should not have cache interceptor ", isProxy(test.getHobbies()));
134    }
135
136    /**
137     * Contributed by Niztan Niv
138     * @throws Exception
139     */

140    public void testNestedMapWithRollback() throws Exception JavaDoc
141    {
142        // create cached data objects
143
Map JavaDoc obj1 = new HashMap JavaDoc();
144        obj1.put("id", "1");
145        cache_.attach("objs/1", obj1);
146        obj1 = (Map JavaDoc) cache_.find("objs/1");
147
148        Map JavaDoc obj2 = new HashMap JavaDoc();
149        obj2.put("id", "2");
150        cache_.attach("objs/2", obj2);
151        obj2 = (Map JavaDoc) cache_.find("objs/2");
152
153        // create cached collection of data objects
154
Map JavaDoc indexMap = new HashMap JavaDoc();
155        cache_.attach("objsIndex", indexMap);
156        indexMap = (Map JavaDoc) cache_.find("objsIndex");
157
158        // initialize collection by adding a data object
159
final String JavaDoc KEY = "KEY";
160        indexMap.put(KEY, obj1);
161
162        Object JavaDoc beforeModify = indexMap.get(KEY);
163        System.out.println("beforeModify: " + beforeModify + ", data object id: " + ((Map JavaDoc)beforeModify).get("id"));
164
165        // modify the collection by replacing the first data object with the second
166
// and then roll-back the transaction
167
tx_mgr.begin();
168        indexMap.put(KEY, obj2);
169        tx_mgr.rollback();
170
171        Object JavaDoc afterRollback = indexMap.get(KEY);
172        System.out.println("afterRollback: " + afterRollback + ", data object id: " + ((Map JavaDoc)afterRollback).get("id"));
173
174        // check if state of collection was restored
175
assertEquals(beforeModify, afterRollback);
176    }
177
178    public void testPlainMapRollback() throws Exception JavaDoc
179    {
180       // create cached data objects
181
Map JavaDoc obj1 = new HashMap JavaDoc();
182       obj1.put("id", "1");
183       cache_.attach("objs/1", obj1);
184       obj1 = (Map JavaDoc) cache_.find("objs/1");
185
186       tx_mgr.begin();
187       obj1.put("id", "newId");
188       tx_mgr.rollback();
189
190       assertEquals("1", obj1.get("id"));
191    }
192
193    public static Test suite() throws Exception JavaDoc
194    {
195       return new TestSuite(MapTxUndoTest.class);
196    }
197
198
199    public static void main(String JavaDoc[] args) throws Exception JavaDoc
200    {
201       junit.textui.TestRunner.run(MapTxUndoTest.suite());
202    }
203
204 }
205
Popular Tags