KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.jboss.cache.mgmt;
2
3 import junit.framework.Test;
4 import junit.framework.TestCase;
5 import junit.framework.TestSuite;
6 import org.jboss.cache.CacheImpl;
7 import org.jboss.cache.Fqn;
8 import org.jboss.cache.config.Configuration;
9 import org.jboss.cache.factories.XmlConfigurationParser;
10 import org.jboss.cache.interceptors.InvalidationInterceptor;
11
12 import java.util.HashMap JavaDoc;
13 import java.util.List JavaDoc;
14
15 /**
16  * Simple functional tests for InvalidationInterceptor statistics
17  *
18  * @author Jerry Gauthier
19  * @version $Id: InvalidationTest.java,v 1.10 2006/12/30 19:48:46 msurtani Exp $
20  */

21 public class InvalidationTest extends TestCase
22 {
23    private static final String JavaDoc CLUSTER_NAME = "InvalidationTestCluster";
24    private static final String JavaDoc CAPITAL = "capital";
25    private static final String JavaDoc CURRENCY = "currency";
26    private static final String JavaDoc POPULATION = "population";
27    private static final String JavaDoc AREA = "area";
28
29    CacheImpl cache1 = null;
30    CacheImpl cache2 = null;
31
32    protected void setUp() throws Exception JavaDoc
33    {
34       super.setUp();
35       cache1 = createCache(CLUSTER_NAME);
36       cache2 = createCache(CLUSTER_NAME);
37    }
38
39    protected void tearDown() throws Exception JavaDoc
40    {
41       super.tearDown();
42       if (cache1 != null)
43       {
44          cache1.stop();
45          cache1.destroy();
46          cache1 = null;
47       }
48       if (cache2 != null)
49       {
50          cache2.stop();
51          cache2.destroy();
52          cache2 = null;
53       }
54    }
55
56    public void testInvalidationMgmt() throws Exception JavaDoc
57    {
58       assertNotNull("Cache1 is null.", cache1);
59       assertNotNull("Cache2 is null.", cache2);
60
61       // populate cache1 with test data
62
loadCache1(cache1);
63
64       // confirm that data is in cache1 and not in cache2
65
Fqn key = Fqn.fromString("Europe/Austria");
66       assertNotNull("Cache1 retrieval error: expected to retrieve " + CAPITAL + " for " + key, cache1.get(key, CAPITAL));
67       assertNull("Cache2 retrieval error: did not expect to retrieve " + CAPITAL + " for " + key, cache2.get(key, CAPITAL));
68       key = Fqn.fromString("Europe/Albania");
69       assertNotNull("Cache1 retrieval error: expected to retrieve " + CAPITAL + " for " + key, cache1.get(key, CAPITAL));
70       assertNull("Cache2 retrieval error: did not expect to retrieve " + CAPITAL + " for " + key, cache2.get(key, CAPITAL));
71
72       // populate cache2 with test data - this will invalidate Austria
73
loadCache2(cache2);
74
75       // confirm that Austria is now in cache2 and not in cache1
76
key = Fqn.fromString("Europe/Austria");
77       assertNull("Cache1 retrieval error: did not expect to retrieve " + CAPITAL + " for " + key, cache1.get(key, CAPITAL));
78       assertNotNull("Cache2 retrieval error: expected to retrieve " + CAPITAL + " for " + key, cache2.get(key, CAPITAL));
79
80       // confirm that Albania is still in cache1
81
key = Fqn.fromString("Europe/Albania");
82       assertNotNull("Cache1 retrieval error after unrelated eviction: expected to retrieve " + CAPITAL + " for " + key, cache1.get(key, CAPITAL));
83
84       // Note: because these tests are normally executed without a server, the interceptor
85
// MBeans are usually not available for use in the tests. Consequently it's necessary
86
// to obtain a reference to the interceptor and work with it directly.
87
InvalidationInterceptor mgmt1 = getInvalidationInterceptor(cache1);
88       assertNotNull("Cache1 InvalidationInterceptor not found.", mgmt1);
89
90       InvalidationInterceptor mgmt2 = getInvalidationInterceptor(cache2);
91       assertNotNull("Cache2 InvalidationInterceptor not found.", mgmt2);
92
93       assertTrue("Cache1 not configured to use MBeans", cache1.getConfiguration().getExposeManagementStatistics());
94       assertTrue("Cache2 not configured to use MBeans", cache2.getConfiguration().getExposeManagementStatistics());
95       assertTrue("InvalidationInterceptor on Cache1 not set up to use statistics!", mgmt1.getStatisticsEnabled());
96       assertTrue("InvalidationInterceptor on Cache2 not set up to use statistics!", mgmt2.getStatisticsEnabled());
97
98       // verify basic statistics for entries loaded into cache
99
assertEquals("Cache1 Invalidations count error: ", new Long JavaDoc(6), new Long JavaDoc(mgmt1.getInvalidations()));
100       assertEquals("Cache2 Invalidations count error: ", new Long JavaDoc(10), new Long JavaDoc(mgmt2.getInvalidations()));
101
102       // reset statistics
103
mgmt1.resetStatistics();
104       mgmt2.resetStatistics();
105
106       // check the statistics again
107
assertEquals("Cache1 Invalidations count error after reset: ", new Long JavaDoc(0), new Long JavaDoc(mgmt1.getInvalidations()));
108       assertEquals("Cache2 Invalidations count error after reset: ", new Long JavaDoc(0), new Long JavaDoc(mgmt2.getInvalidations()));
109
110    }
111
112    private void loadCache1(CacheImpl cache) throws Exception JavaDoc
113    {
114       cache.put("Europe", new HashMap JavaDoc());
115       cache.put("Europe/Austria", new HashMap JavaDoc());
116       cache.put("Europe/Austria", CAPITAL, "Vienna");
117       cache.put("Europe/Austria", CURRENCY, "Euro");
118       cache.put("Europe/Austria", POPULATION, 8184691);
119
120       HashMap JavaDoc albania = new HashMap JavaDoc(4);
121       albania.put(CAPITAL, "Tirana");
122       albania.put(CURRENCY, "Lek");
123       albania.put(POPULATION, 3563112);
124       albania.put(AREA, 28748);
125       cache.put("Europe/Albania", albania);
126
127    }
128
129    private void loadCache2(CacheImpl cache) throws Exception JavaDoc
130    {
131       cache.put("Europe", new HashMap JavaDoc());
132       cache.put("Europe/Austria", new HashMap JavaDoc());
133       cache.put("Europe/Austria", CAPITAL, "Vienna");
134       cache.put("Europe/Austria", CURRENCY, "Euro");
135       cache.put("Europe/Austria", POPULATION, 8184691);
136
137       cache.put("Europe/Romania", new HashMap JavaDoc());
138       cache.put("Europe/Romania", CAPITAL, "Bucharest");
139       cache.put("Europe/Romania", CURRENCY, "Leu");
140       cache.put("Europe/Romania", POPULATION, 22329977);
141       cache.put("Europe/Romania", AREA, 237500);
142
143    }
144
145    private CacheImpl createCache(String JavaDoc clusterName) throws Exception JavaDoc
146    {
147       CacheImpl cache = new CacheImpl();
148       XmlConfigurationParser parser = new XmlConfigurationParser();
149       Configuration c = parser.parseFile("META-INF/invalidationSync-service.xml");
150       cache.setConfiguration(c);
151       c.setCacheMode(Configuration.CacheMode.INVALIDATION_SYNC);
152       c.setExposeManagementStatistics(true);
153       c.setClusterName(clusterName);
154       cache.create();
155       cache.start();
156       return cache;
157    }
158
159    private InvalidationInterceptor getInvalidationInterceptor(CacheImpl cache)
160    {
161       List JavaDoc interceptors = cache.getInterceptors();
162       if (interceptors.isEmpty())
163          return null;
164
165       for (int i = 0; i < interceptors.size(); i++)
166       {
167          Object JavaDoc o = interceptors.get(i);
168          if (o instanceof InvalidationInterceptor)
169             return (InvalidationInterceptor) o;
170       }
171       return null;
172    }
173
174    public static Test suite()
175    {
176       return new TestSuite(InvalidationTest.class);
177    }
178
179 }
180
Popular Tags