KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > cache > replicated > SyncCacheListenerTest


1 /*
2  *
3  * JBoss, the OpenSource J2EE webOS
4  *
5  * Distributable under LGPL license.
6  * See terms of license at gnu.org.
7  */

8 package org.jboss.cache.replicated;
9
10 import junit.framework.Test;
11 import junit.framework.TestCase;
12 import junit.framework.TestSuite;
13 import org.apache.commons.logging.Log;
14 import org.apache.commons.logging.LogFactory;
15 import org.jboss.cache.AbstractCacheListener;
16 import org.jboss.cache.CacheException;
17 import org.jboss.cache.CacheImpl;
18 import org.jboss.cache.Fqn;
19 import org.jboss.cache.config.Configuration;
20 import org.jboss.cache.lock.IsolationLevel;
21 import org.jboss.cache.transaction.DummyTransactionManager;
22
23 import javax.naming.Context JavaDoc;
24 import javax.transaction.NotSupportedException JavaDoc;
25 import javax.transaction.SystemException JavaDoc;
26 import javax.transaction.Transaction JavaDoc;
27 import java.util.HashMap JavaDoc;
28 import java.util.Map JavaDoc;
29 import java.util.Set JavaDoc;
30
31 /**
32  * Test out the TreeCacheListener
33  *
34  * @version $Revision: 1.13 $
35  */

36 public class SyncCacheListenerTest extends TestCase
37 {
38    private CacheImpl cache1, cache2;
39    private final static Log log_ = LogFactory.getLog(SyncCacheListenerTest.class);
40    private String JavaDoc old_factory = null;
41    private final String JavaDoc FACTORY = "org.jboss.cache.transaction.DummyContextFactory";
42
43    public SyncCacheListenerTest(String JavaDoc name)
44    {
45       super(name);
46    }
47
48    protected void setUp() throws Exception JavaDoc
49    {
50       System.out.println("*** starting setUp()");
51       super.setUp();
52       old_factory = System.getProperty(Context.INITIAL_CONTEXT_FACTORY);
53       System.setProperty(Context.INITIAL_CONTEXT_FACTORY, FACTORY);
54
55       initCaches();
56       System.out.println("*** finished setUp()");
57    }
58
59    protected void tearDown() throws Exception JavaDoc
60    {
61       System.out.println("*** starting tearDown()");
62       super.tearDown();
63       DummyTransactionManager.destroy();
64       destroyCaches();
65       if (old_factory != null)
66       {
67          System.setProperty(Context.INITIAL_CONTEXT_FACTORY, old_factory);
68          old_factory = null;
69       }
70       System.out.println("*** finished tearDown()");
71    }
72
73    private Transaction JavaDoc beginTransaction() throws SystemException JavaDoc, NotSupportedException JavaDoc
74    {
75       DummyTransactionManager mgr = DummyTransactionManager.getInstance();
76       mgr.begin();
77       return mgr.getTransaction();
78    }
79
80    private void initCaches() throws Exception JavaDoc
81    {
82       cache1 = new CacheImpl();
83       cache2 = new CacheImpl();
84       cache1.getConfiguration().setCacheMode(Configuration.CacheMode.REPL_SYNC);
85       cache2.getConfiguration().setCacheMode(Configuration.CacheMode.REPL_SYNC);
86       cache1.getConfiguration().setIsolationLevel(IsolationLevel.SERIALIZABLE);
87       cache2.getConfiguration().setIsolationLevel(IsolationLevel.SERIALIZABLE);
88
89       cache1.getConfiguration().setTransactionManagerLookupClass("org.jboss.cache.DummyTransactionManagerLookup");
90       cache2.getConfiguration().setTransactionManagerLookupClass("org.jboss.cache.DummyTransactionManagerLookup");
91       /*
92       cache1.setTransactionManagerLookupClass("org.jboss.cache.JBossTransactionManagerLookup");
93       cache2.setTransactionManagerLookupClass("org.jboss.cache.JBossTransactionManagerLookup");
94 */

95       cache1.getConfiguration().setLockAcquisitionTimeout(5000);
96       cache2.getConfiguration().setLockAcquisitionTimeout(5000);
97       cache1.start();
98       cache2.start();
99    }
100
101    private void destroyCaches()
102    {
103       if (cache1 != null)
104       {
105          cache1.stop();
106       }
107       if (cache2 != null)
108       {
109          cache2.stop();
110       }
111    }
112
113    public void testSyncTxRepl() throws Exception JavaDoc
114    {
115       Integer JavaDoc age;
116       Transaction JavaDoc tx;
117
118       tx = beginTransaction();
119       Listener lis = new Listener();
120       cache1.getNotifier().addCacheListener(lis);
121       lis.put("/a/b/c", "age", 38);
122       cache1.getTransactionManager().suspend();
123       assertNull("age on cache2 must be null as the TX has not yet been committed", cache2.get("/a/b/c", "age"));
124       cache1.getTransactionManager().resume(tx);
125       tx.commit();
126
127       // value on cache2 must be 38
128
age = (Integer JavaDoc) cache2.get("/a/b/c", "age");
129       assertNotNull("\"age\" obtained from cache2 must be non-null ", age);
130       assertTrue("\"age\" must be 38", age == 38);
131    }
132
133    public void testRemoteCacheListener() throws Exception JavaDoc
134    {
135       Integer JavaDoc age;
136       RemoteListener lis = new RemoteListener();
137       cache2.getNotifier().addCacheListener(lis);
138       cache1.put("/a/b/c", "age", 38);
139
140       // value on cache2 must be 38
141
age = (Integer JavaDoc) cache2.get("/a/b/c", "age");
142       assertNotNull("\"age\" obtained from cache2 must be non-null ", age);
143       assertTrue("\"age\" must be 38", age == 38);
144       cache1.remove("/a/b/c", "age");
145    }
146
147
148    public void testSyncRepl() throws Exception JavaDoc
149    {
150       Integer JavaDoc age;
151       Listener lis = new Listener();
152       cache1.getNotifier().addCacheListener(lis);
153       lis.put("/a/b/c", "age", 38);
154
155       // value on cache2 must be 38
156
age = (Integer JavaDoc) cache2.get("/a/b/c", "age");
157       assertNotNull("\"age\" obtained from cache2 must be non-null ", age);
158       assertTrue("\"age\" must be 38", age == 38);
159    }
160
161    public void testSyncTxReplMap() throws Exception JavaDoc
162    {
163       Integer JavaDoc age;
164       Transaction JavaDoc tx;
165
166       tx = beginTransaction();
167       Listener lis = new Listener();
168       cache1.getNotifier().addCacheListener(lis);
169       Map JavaDoc map = new HashMap JavaDoc();
170       map.put("age", 38);
171       map.put("name", "Ben");
172       lis.put("/a/b/c", map);
173       cache1.getTransactionManager().suspend();
174       assertNull("age on cache2 must be null as the TX has not yet been committed", cache2.get("/a/b/c", "age"));
175       cache1.getTransactionManager().resume(tx);
176       tx.commit();
177
178       // value on cache2 must be 38
179
age = (Integer JavaDoc) cache2.get("/a/b/c", "age");
180       assertNotNull("\"age\" obtained from cache2 must be non-null ", age);
181       assertTrue("\"age\" must be 38", age == 38);
182    }
183
184
185    public void testSyncReplMap() throws Exception JavaDoc
186    {
187       Integer JavaDoc age;
188
189       Listener lis = new Listener();
190       cache1.getNotifier().addCacheListener(lis);
191       Map JavaDoc map = new HashMap JavaDoc();
192       map.put("age", 38);
193       map.put("name", "Ben");
194       lis.put("/a/b/c", map);
195
196       // value on cache2 must be 38
197
age = (Integer JavaDoc) cache2.get("/a/b/c", "age");
198       assertNotNull("\"age\" obtained from cache2 must be non-null ", age);
199       assertTrue("\"age\" must be 38", age == 38);
200    }
201
202
203    class Listener extends AbstractCacheListener
204    {
205       Object JavaDoc key_ = null;
206
207       public void put(String JavaDoc fqn, Object JavaDoc key, Object JavaDoc val)
208       {
209          key_ = key;
210          cache1.put(fqn, key, val);
211       }
212
213       public void put(String JavaDoc fqn, Map JavaDoc map)
214       {
215          if (map.size() == 0) fail("put(): map size can't be 0");
216          Set JavaDoc set = map.keySet();
217          key_ = set.iterator().next(); // take anyone
218
cache1.put(fqn, map);
219       }
220
221
222       public void nodeModified(Fqn fqn, boolean pre, boolean isLocal, ModificationType modType, Map JavaDoc data)
223       {
224          if (!pre)
225          {
226             log_.debug("nodeModified visited with fqn: " + fqn);
227             try
228             {
229                // test out if we can get the read lock since there is a write lock going as well.
230
cache1.get(fqn, key_);
231             }
232             catch (CacheException e)
233             {
234                e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
235
fail("nodeModified: test failed with exception: " + e);
236             }
237          }
238       }
239
240    }
241
242    class RemoteListener extends AbstractCacheListener
243    {
244
245       public void nodeRemoved(Fqn fqn, boolean isPre, boolean isLocal, Map JavaDoc data)
246       {
247          System.out.println("nodeRemove visited with fqn: " + fqn + ", isPre: " + isPre + ", isLocal " + isLocal);
248          log_.debug("nodeRemove visited with fqn: " + fqn + ", isPre: " + isPre + ", isLocal " + isLocal);
249          assertFalse("node was removed on remote cache so isLocal should be false", isLocal);
250       }
251
252       public void nodeModified(Fqn fqn, boolean isPre, boolean isLocal, ModificationType modType, Map JavaDoc data)
253       {
254          System.out.println("nodeModify visited with fqn: " + fqn + ", isPre: " + isPre + ", isLocal " + isLocal);
255          log_.debug("nodeModify visited with fqn: " + fqn + ", isPre: " + isPre + ", isLocal " + isLocal);
256          assertFalse("node was modified on remote cache so isLocal should be false", isLocal);
257       }
258
259    }
260
261    public static Test suite()
262    {
263       return new TestSuite(SyncCacheListenerTest.class);
264    }
265
266
267 }
268
Popular Tags