KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > cache > options > cachemodelocal > CacheModeLocalTestBase


1 /*
2  * JBoss, Home of Professional Open Source
3  *
4  * Distributable under LGPL license.
5  * See terms of license at gnu.org.
6  */

7 package org.jboss.cache.options.cachemodelocal;
8
9 import junit.framework.Assert;
10 import junit.framework.TestCase;
11 import org.jboss.cache.CacheImpl;
12 import org.jboss.cache.Fqn;
13 import org.jboss.cache.config.Configuration;
14
15 import javax.transaction.SystemException JavaDoc;
16 import javax.transaction.TransactionManager JavaDoc;
17 import java.util.HashMap JavaDoc;
18 import java.util.Map JavaDoc;
19
20 /**
21  * Tests the cache mode local override in various scenarios. To be subclassed to test REPL_SYNC, REPL_ASYNC, INVALIDATION_SYNC, INVALIDATION_ASYNC for Opt and Pess locking.
22  * <p/>
23  * Option.setCacheModeLocal() only applies to put() and remove() methods.
24  *
25  * @author <a HREF="mailto:manik@jboss.org">Manik Surtani (manik@jboss.org)</a>
26  */

27 public abstract class CacheModeLocalTestBase extends TestCase
28 {
29    // to be subclassed.
30
protected Configuration.CacheMode cacheMode;
31    protected String JavaDoc nodeLockingScheme;
32    /**
33     * set this to true if the implementing class plans to use an invalidating cache mode *
34     */

35    protected boolean isInvalidation;
36
37    private CacheImpl cache1;
38    private CacheImpl cache2;
39
40    private Fqn fqn = Fqn.fromString("/a");
41    private String JavaDoc key = "key";
42
43    protected void setUp() throws Exception JavaDoc
44    {
45       // force a tear down if the test runner didn't run one before (happens in IDEA)
46
if (cache1 != null || cache2 != null) tearDown();
47
48       cache1 = new CacheImpl();
49       cache1.getConfiguration().setClusterName("test");
50       cache1.getConfiguration().setInitialStateRetrievalTimeout(1000);
51       cache1.getConfiguration().setTransactionManagerLookupClass("org.jboss.cache.DummyTransactionManagerLookup");
52       cache1.getConfiguration().setNodeLockingScheme(nodeLockingScheme);
53       cache1.getConfiguration().setCacheMode(cacheMode);
54       cache1.start();
55
56       cache2 = new CacheImpl();
57       cache2.getConfiguration().setClusterName("test");
58       cache2.getConfiguration().setInitialStateRetrievalTimeout(1000);
59       cache2.getConfiguration().setTransactionManagerLookupClass("org.jboss.cache.DummyTransactionManagerLookup");
60       cache2.getConfiguration().setNodeLockingScheme(nodeLockingScheme);
61       cache2.getConfiguration().setCacheMode(cacheMode);
62       cache2.start();
63    }
64
65    protected void tearDown()
66    {
67       if (cache1 != null)
68       {
69          cache1.stop();
70          flushTxs(cache1.getTransactionManager());
71          cache1 = null;
72       }
73
74       if (cache2 != null)
75       {
76          cache2.stop();
77          flushTxs(cache2.getTransactionManager());
78          cache2 = null;
79       }
80    }
81
82    private void flushTxs(TransactionManager JavaDoc mgr)
83    {
84       if (mgr != null)
85       {
86          try
87          {
88             if (mgr.getTransaction() != null)
89             {
90                mgr.rollback();
91             }
92          }
93          catch (SystemException JavaDoc e)
94          {
95             // do nothing
96
}
97       }
98    }
99
100    public void testPutKeyValue() throws Exception JavaDoc
101    {
102       cache1.getInvocationContext().getOptionOverrides().setCacheModeLocal(true);
103       cache1.put(fqn, key, "value");
104       delay();
105       // cache1 should still have this
106
Assert.assertEquals("value", cache1.get(fqn, key));
107
108       // cache 2 should not
109
Assert.assertNull("Should be null", cache2.get(fqn, key));
110
111       // now try again with passing the default options
112
cache1.getInvocationContext().getOptionOverrides().reset();
113       cache1.put(fqn, key, "value");
114       delay();
115       // cache1 should still have this
116
Assert.assertEquals("value", cache1.get(fqn, key));
117
118       // cache 2 should as well
119
if (!isInvalidation)
120          Assert.assertEquals("value", cache2.get(fqn, key));
121       else
122          Assert.assertNull("should be invalidated", cache2.get(fqn, key));
123
124       // now cache2
125
cache2.getInvocationContext().getOptionOverrides().setCacheModeLocal(true);
126       cache2.put(fqn, key, "value2");
127       delay();
128       Assert.assertEquals("value2", cache2.get(fqn, key));
129       Assert.assertEquals("value", cache1.get(fqn, key));
130
131       cache2.getInvocationContext().getOptionOverrides().reset();
132       cache2.put(fqn, key, "value2");
133       delay();
134       Assert.assertEquals("value2", cache2.get(fqn, key));
135       if (!isInvalidation)
136          Assert.assertEquals("value2", cache1.get(fqn, key));
137       else
138          Assert.assertNull("should be invalidated", cache1.get(fqn, key));
139    }
140
141    public void testPutData() throws Exception JavaDoc
142    {
143       Map JavaDoc map = new HashMap JavaDoc();
144       map.put(key, "value");
145
146       cache1.getInvocationContext().getOptionOverrides().setCacheModeLocal(true);
147       cache1.put(fqn, map);
148       delay();
149       // cache1 should still have this
150
Assert.assertEquals("value", cache1.get(fqn, key));
151       // cache 2 should not
152
Assert.assertNull("Should be null", cache2.get(fqn, key));
153
154       // now try again with passing the default options
155
cache1.getInvocationContext().getOptionOverrides().setCacheModeLocal(false);
156       cache1.put(fqn, map);
157       delay();
158       // cache1 should still have this
159
Assert.assertEquals("value", cache1.get(fqn, key));
160       // cache 2 should as well
161
if (!isInvalidation)
162          Assert.assertEquals("value", cache2.get(fqn, key));
163       else
164          Assert.assertNull("should be invalidated", cache2.get(fqn, key));
165
166       // now cache2
167
map.put(key, "value2");
168       cache2.getInvocationContext().getOptionOverrides().setCacheModeLocal(true);
169       cache2.put(fqn, map);
170       delay();
171       Assert.assertEquals("value2", cache2.get(fqn, key));
172       Assert.assertEquals("value", cache1.get(fqn, key));
173
174       cache2.getInvocationContext().getOptionOverrides().reset();
175       cache2.put(fqn, key, "value2");
176       delay();
177       Assert.assertEquals("value2", cache2.get(fqn, key));
178       if (!isInvalidation)
179          Assert.assertEquals("value2", cache1.get(fqn, key));
180       else
181          Assert.assertNull("should be invalidated", cache1.get(fqn, key));
182    }
183
184    public void testRemoveNode() throws Exception JavaDoc
185    {
186       // put some stuff in the cache first
187
// make sure we cleanup thread local vars.
188
cache1.getInvocationContext().setOptionOverrides(null);
189       cache1.put(fqn, key, "value");
190       delay();
191       Assert.assertEquals("value", cache1.get(fqn, key));
192       if (isInvalidation)
193          Assert.assertNull("Should be null", cache2.get(fqn, key));
194       else
195          Assert.assertEquals("value", cache2.get(fqn, key));
196
197       cache1.getInvocationContext().getOptionOverrides().setCacheModeLocal(true);
198       cache1.removeNode(fqn);
199       delay();
200
201       // should be removed in cache1
202
Assert.assertNull("should be null", cache1.get(fqn, key));
203       // Not in cache2
204
if (isInvalidation)
205          Assert.assertNull("Should be null", cache2.get(fqn, key));
206       else
207          Assert.assertEquals("value", cache2.get(fqn, key));
208
209       // replace cache entries
210
cache1.put(fqn, key, "value");
211       delay();
212       Assert.assertEquals("value", cache1.get(fqn, key));
213       if (isInvalidation)
214          Assert.assertNull("Should be null", cache2.get(fqn, key));
215       else
216          Assert.assertEquals("value", cache2.get(fqn, key));
217
218       // now try again with passing the default options
219
cache1.getInvocationContext().getOptionOverrides().setCacheModeLocal(false);
220       cache1.remove(fqn);
221       delay();
222
223       // both should be null
224
Assert.assertNull("should be null", cache1.get(fqn, key));
225       Assert.assertNull("should be null", cache2.get(fqn, key));
226    }
227
228    public void testRemoveKey() throws Exception JavaDoc
229    {
230       // put some stuff in the cache first
231
cache1.getInvocationContext().setOptionOverrides(null);
232       cache1.put(fqn, key, "value");
233       delay();
234       Assert.assertEquals("value", cache1.get(fqn, key));
235       if (isInvalidation)
236          Assert.assertNull("Should be null", cache2.get(fqn, key));
237       else
238          Assert.assertEquals("value", cache2.get(fqn, key));
239
240       cache1.getInvocationContext().getOptionOverrides().setCacheModeLocal(true);
241       cache1.remove(fqn, key);
242       delay();
243
244       // should be removed in cache1
245
Assert.assertNull("should be null", cache1.get(fqn, key));
246       // Not in cache2
247
if (isInvalidation)
248          Assert.assertNull("Should be null", cache2.get(fqn, key));
249       else
250          Assert.assertEquals("value", cache2.get(fqn, key));
251
252       // replace cache entries
253
cache1.put(fqn, key, "value");
254       delay();
255       Assert.assertEquals("value", cache1.get(fqn, key));
256       if (isInvalidation)
257          Assert.assertNull("Should be null", cache2.get(fqn, key));
258       else
259          Assert.assertEquals("value", cache2.get(fqn, key));
260
261       // now try again with passing the default options
262
cache1.getInvocationContext().getOptionOverrides().reset();
263       cache1.remove(fqn, key);
264       delay();
265
266       // both should be null
267
Assert.assertNull("should be null", cache1.get(fqn, key));
268       Assert.assertNull("should be null", cache2.get(fqn, key));
269    }
270
271    public void testTransactionalBehaviour() throws Exception JavaDoc
272    {
273       TransactionManager JavaDoc mgr = cache1.getTransactionManager();
274       mgr.begin();
275       cache1.getInvocationContext().getOptionOverrides().reset();
276       cache1.put(fqn, key, "value1");
277       cache1.getInvocationContext().getOptionOverrides().setCacheModeLocal(true);
278       cache1.put(fqn, key, "value2");
279       mgr.commit();
280       delay();
281       // cache1 should still have this
282
Assert.assertEquals("value2", cache1.get(fqn, key));
283
284       if (!isInvalidation)
285          Assert.assertEquals("value1", cache2.get(fqn, key));
286       else
287          assertNull(cache2.get(fqn, key));
288
289       // now try again with passing the default options
290
mgr.begin();
291       cache1.getInvocationContext().getOptionOverrides().setCacheModeLocal(true);
292       cache1.put(fqn, key, "value3");
293       cache1.getInvocationContext().getOptionOverrides().reset();
294       cache1.put(fqn, key, "value");
295       mgr.commit();
296       delay();
297       // cache1 should still have this
298
Assert.assertEquals("value", cache1.get(fqn, key));
299
300       // cache 2 should as well
301
if (!isInvalidation)
302          Assert.assertEquals("value", cache2.get(fqn, key));
303       else
304          Assert.assertNull("should be invalidated", cache2.get(fqn, key));
305
306       // now cache2
307
mgr = cache2.getTransactionManager();
308       mgr.begin();
309       cache2.getInvocationContext().getOptionOverrides().setCacheModeLocal(false);
310       cache2.put(fqn, key, "value3");
311       cache2.getInvocationContext().getOptionOverrides().setCacheModeLocal(true);
312       cache2.put(fqn, key, "value2");
313       mgr.commit();
314       delay();
315
316       Assert.assertEquals("value2", cache2.get(fqn, key));
317
318       if (!isInvalidation)
319          assertEquals("value3", cache1.get(fqn, key));
320       else
321          assertNull(cache1.get(fqn, key));
322
323       mgr.begin();
324       cache2.getInvocationContext().getOptionOverrides().setCacheModeLocal(true);
325       cache2.put(fqn, key, "value2");
326       cache2.getInvocationContext().getOptionOverrides().reset();
327       cache2.put(fqn, key, "value2");
328       mgr.commit();
329       delay();
330       Assert.assertEquals("value2", cache2.get(fqn, key));
331       if (!isInvalidation)
332          Assert.assertEquals("value2", cache1.get(fqn, key));
333       else
334          Assert.assertNull("should be invalidated", cache1.get(fqn, key));
335
336    }
337
338    protected abstract void delay();
339
340 }
Popular Tags