KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > cache > mgmt > NotificationTest


1 package org.jboss.cache.mgmt;
2
3 import junit.framework.TestCase;
4 import org.jboss.cache.CacheImpl;
5 import org.jboss.cache.Fqn;
6 import org.jboss.cache.config.CacheLoaderConfig;
7 import org.jboss.cache.config.Configuration;
8 import org.jboss.cache.factories.XmlConfigurationParser;
9 import org.jboss.cache.interceptors.CacheMgmtInterceptor;
10 import org.jboss.cache.jmx.CacheJmxWrapper;
11 import org.jboss.cache.jmx.JmxUtil;
12 import org.jboss.cache.loader.CacheLoader;
13 import org.jboss.cache.xml.XmlHelper;
14 import org.w3c.dom.Element JavaDoc;
15
16 import javax.management.MBeanServer JavaDoc;
17 import javax.management.MBeanServerFactory JavaDoc;
18 import javax.management.Notification JavaDoc;
19 import javax.management.NotificationListener JavaDoc;
20 import javax.management.ObjectName JavaDoc;
21 import java.util.HashMap JavaDoc;
22
23 /**
24  * Functional tests for CacheMgmtInterceptor broadcast of cache event notifications
25  *
26  * @author Jerry Gauthier
27  * @version $Id: NotificationTest.java,v 1.17 2007/01/05 14:27:15 msurtani Exp $
28  */

29 public class NotificationTest extends TestCase
30 {
31    private static final String JavaDoc MGMT_SERVICE = ",cache-interceptor=CacheMgmtInterceptor";
32    private static final String JavaDoc CLUSTER_NAME = "NotificationTestCluster";
33
34    private static final String JavaDoc CAPITAL = "capital";
35    private static final String JavaDoc CURRENCY = "currency";
36    private static final String JavaDoc POPULATION = "population";
37    private static final String JavaDoc EUROPE_NODE = "Europe";
38
39    private boolean m_cacheStarted;
40    private boolean m_cacheStopped;
41    private boolean m_nodeCreatedPre;
42    private boolean m_nodeCreatedPost;
43    private boolean m_nodeEvictedPre;
44    private boolean m_nodeEvictedPost;
45    private boolean m_nodeLoadedPre;
46    private boolean m_nodeLoadedPost;
47    private boolean m_nodeRemovedPre;
48    private boolean m_nodeRemovedPost;
49    private boolean m_nodeVisitedPre;
50    private boolean m_nodeVisitedPost;
51    private boolean m_viewChange;
52    private boolean m_nodeActivatedPre;
53    private boolean m_nodeActivatedPost;
54    private boolean m_nodeModifiedPre;
55    private boolean m_nodeModifiedPost;
56    private boolean m_nodePassivatedPre;
57    private boolean m_nodePassivatedPost;
58
59    private MBeanServer JavaDoc m_server;
60
61    CacheImpl cache = null;
62    boolean optimistic = false;
63
64    protected void setUp() throws Exception JavaDoc
65    {
66       super.setUp();
67       m_server = MBeanServerFactory.createMBeanServer();
68       cache = createCache(CLUSTER_NAME);
69       // bind manually for now.
70
ObjectName JavaDoc mgmt = new ObjectName JavaDoc(JmxUtil.PREFIX + cache.getConfiguration().getClusterName());
71       CacheJmxWrapper cacheMBean = new CacheJmxWrapper(cache);
72
73       m_server.registerMBean(cacheMBean, mgmt);
74    }
75
76    protected void tearDown() throws Exception JavaDoc
77    {
78       super.tearDown();
79       if (cache != null)
80       {
81          // stop the cache before the listener is unregistered
82
//cache1.stop();
83
cache.destroy();
84          cache = null;
85          // make sure we stop the mbean server
86
MBeanServerFactory.releaseMBeanServer(m_server);
87       }
88    }
89
90    public void testNotifications() throws Exception JavaDoc
91    {
92       assertNotNull("MBeanServer is null.", m_server);
93       assertNotNull("Cache is null.", cache);
94
95       MyListener listener = new MyListener();
96
97       ObjectName JavaDoc mgmt = new ObjectName JavaDoc(JmxUtil.PREFIX + cache.getConfiguration().getClusterName() + MGMT_SERVICE);
98       m_server.addNotificationListener(mgmt, listener, null, null);
99
100       // start the cache after registering listener - this will trigger CacheStarted
101
// since cache is defined with cluster, thiswill also trigger ViewChange
102
cache.start();
103
104       // add a node - this will trigger NodeCreated, NodeModify(pre/post) and NodeModified
105
HashMap JavaDoc albania = new HashMap JavaDoc(4);
106       albania.put(CAPITAL, "Tirana");
107       albania.put(CURRENCY, "Lek");
108       cache.put("Europe/Albania", albania);
109
110       // modify a node - this will trigger NodeModified and NodeModify(pre/post)
111
cache.put("Europe/Albania", POPULATION, 3563112);
112
113       // retrieve an attribute - this will trigger NodeVisited
114
Fqn key = Fqn.fromString("Europe/Albania");
115       assertNotNull("Retrieval error: expected to retrieve " + CURRENCY + " for " + key, cache.get(key, CURRENCY));
116
117       // evict the node - this will trigger NodePassivate, NodeEvicted and NodeEvict(pre/post)
118
cache.evict(key);
119
120       // retrieve the attribute again - this will trigger NodeVisited and NodeActivate
121
assertNotNull("Retrieval error: expected to retrieve " + CURRENCY + " for " + key, cache.get(key, CURRENCY));
122
123       // remove the node - this will trigger NodeRemoved and NodeRemove(pre/post)
124
cache.remove(key);
125
126       // clean up before stopping the cache
127
CacheLoader cl = cache.getCacheLoader();
128       cl.remove(Fqn.fromString(EUROPE_NODE));
129
130       // stop the cache
131
cache.stop();
132       m_server.removeNotificationListener(mgmt, listener);
133
134       // run the tests
135
assertTrue("Expected CacheStarted notification", m_cacheStarted);
136       assertTrue("Expected CacheStopped notification", m_cacheStopped);
137       assertTrue("Expected NodeCreated notification", m_nodeCreatedPre);
138       assertTrue("Expected NodeCreated notification", m_nodeCreatedPost);
139       assertTrue("Expected NodeEvicted notification", m_nodeEvictedPre);
140       assertTrue("Expected NodeEvicted notification", m_nodeEvictedPost);
141       assertTrue("Expected NodeLoaded notification", m_nodeLoadedPre);
142       assertTrue("Expected NodeLoaded notification", m_nodeLoadedPost);
143       assertTrue("Expected NodeRemoved notification", m_nodeRemovedPre);
144       assertTrue("Expected NodeVisited notification", m_nodeVisitedPre);
145       assertTrue("Expected NodeVisited notification", m_nodeVisitedPost);
146       assertTrue("Expected NodeActivated notification", m_nodeActivatedPre);
147       assertTrue("Expected NodeActivated notification", m_nodeActivatedPost);
148       assertTrue("Expected NodeEvicted notification", m_nodeEvictedPre);
149       assertTrue("Expected NodeEvicted notification", m_nodeEvictedPost);
150       assertTrue("Expected NodeModified notification", m_nodeModifiedPre);
151       assertTrue("Expected NodeModified notification", m_nodeModifiedPost);
152       assertTrue("Expected NodePassivated notification", m_nodePassivatedPre);
153       assertTrue("Expected NodePassivated notification", m_nodePassivatedPost);
154       assertTrue("Expected NodeRemoved notification", m_nodeRemovedPre);
155       assertTrue("Expected NodeRemoved notification", m_nodeRemovedPost);
156       assertTrue("Expected ViewChange notification", m_viewChange);
157    }
158
159    private CacheImpl createCache(String JavaDoc clusterName) throws Exception JavaDoc
160    {
161       CacheImpl cache = new CacheImpl();
162       cache.setConfiguration(new XmlConfigurationParser().parseFile("META-INF/replSync-service.xml"));
163       cache.getConfiguration().setCacheMode(Configuration.CacheMode.REPL_SYNC);
164       cache.getConfiguration().setCacheLoaderConfig(getCacheLoaderConfig("location=" + getTempDir()));
165       cache.getConfiguration().setExposeManagementStatistics(true);
166       cache.getConfiguration().setClusterName(clusterName);
167       if (optimistic)
168       {
169          cache.getConfiguration().setTransactionManagerLookupClass("org.jboss.cache.DummyTransactionManagerLookup");
170          cache.getConfiguration().setNodeLockingScheme("OPTIMISTIC");
171       }
172       cache.create();
173       // start the cache after the listener has been registered
174
//cache.start();
175
return cache;
176    }
177
178    private String JavaDoc getTempDir()
179    {
180       return System.getProperty("java.io.tempdir", "/tmp");
181    }
182
183    private boolean getPre(Object JavaDoc data)
184    {
185       assertNotNull("User data is null, should be Object[]", data);
186       assertTrue("User data is " + data.getClass().getName() + ", should be Object[]", data instanceof Object JavaDoc[]);
187
188       Object JavaDoc[] parms = (Object JavaDoc[]) data;
189       assertTrue("Parameter is " + parms[1].getClass().getName() + ", should be Boolean", parms[1] instanceof Boolean JavaDoc);
190       return (Boolean JavaDoc) parms[1];
191    }
192
193    private CacheLoaderConfig getCacheLoaderConfig(String JavaDoc properties) throws Exception JavaDoc
194    {
195       String JavaDoc xml = "<config>\n" +
196               "<passivation>true</passivation>\n" +
197               "<preload></preload>\n" +
198               "<shared>true</shared>\n" +
199               "<cacheloader>\n" +
200               "<class>org.jboss.cache.loader.FileCacheLoader</class>\n" +
201               "<properties>" + properties + "</properties>\n" +
202               "<async>false</async>\n" +
203               "<fetchPersistentState>false</fetchPersistentState>\n" +
204               "<ignoreModifications>false</ignoreModifications>\n" +
205               "</cacheloader>\n" +
206               "</config>";
207       Element JavaDoc element = XmlHelper.stringToElement(xml);
208       return XmlConfigurationParser.parseCacheLoaderConfig(element);
209    }
210
211    private class MyListener implements NotificationListener JavaDoc
212    {
213       public void handleNotification(Notification JavaDoc notification, Object JavaDoc handback)
214       {
215          String JavaDoc type = notification.getType();
216          Object JavaDoc userData = notification.getUserData();
217
218          if (type.equals(CacheMgmtInterceptor.NOTIF_CACHE_STARTED))
219          {
220             m_cacheStarted = true;
221          }
222          else if (type.equals(CacheMgmtInterceptor.NOTIF_CACHE_STOPPED))
223          {
224             m_cacheStopped = true;
225          }
226          else if (type.equals(CacheMgmtInterceptor.NOTIF_NODE_CREATED))
227          {
228             if (getPre(userData))
229             {
230                m_nodeCreatedPre = true;
231             }
232             else
233             {
234                m_nodeCreatedPost = true;
235             }
236          }
237          else if (type.equals(CacheMgmtInterceptor.NOTIF_NODE_EVICTED))
238          {
239             if (getPre(userData))
240             {
241                m_nodeEvictedPre = true;
242             }
243             else
244             {
245                m_nodeEvictedPost = true;
246             }
247          }
248          else if (type.equals(CacheMgmtInterceptor.NOTIF_NODE_LOADED))
249          {
250             if (getPre(userData))
251             {
252                m_nodeLoadedPre = true;
253             }
254             else
255             {
256                m_nodeLoadedPost = true;
257             }
258          }
259          else if (type.equals(CacheMgmtInterceptor.NOTIF_NODE_REMOVED))
260          {
261             if (getPre(userData))
262             {
263                m_nodeRemovedPre = true;
264             }
265             else
266             {
267                m_nodeRemovedPost = true;
268             }
269          }
270          else if (type.equals(CacheMgmtInterceptor.NOTIF_NODE_VISITED))
271          {
272             if (getPre(userData))
273             {
274                m_nodeVisitedPre = true;
275             }
276             else
277             {
278                m_nodeVisitedPost = true;
279             }
280          }
281          else if (type.equals(CacheMgmtInterceptor.NOTIF_VIEW_CHANGED))
282          {
283             m_viewChange = true;
284          }
285          else if (type.equals(CacheMgmtInterceptor.NOTIF_NODE_ACTIVATED))
286          {
287             if (getPre(userData))
288             {
289                m_nodeActivatedPre = true;
290             }
291             else
292             {
293                m_nodeActivatedPost = true;
294             }
295          }
296          else if (type.equals(CacheMgmtInterceptor.NOTIF_NODE_MODIFIED))
297          {
298             if (getPre(userData))
299             {
300                m_nodeModifiedPre = true;
301             }
302             else
303             {
304                m_nodeModifiedPost = true;
305             }
306          }
307          else if (type.equals(CacheMgmtInterceptor.NOTIF_NODE_PASSIVATED))
308          {
309             if (getPre(userData))
310             {
311                m_nodePassivatedPre = true;
312             }
313             else
314             {
315                m_nodePassivatedPost = true;
316             }
317          }
318       }
319    }
320
321 }
322
Popular Tags