KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > cache > optimistic > OptimisticWithCacheLoaderTest


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.optimistic;
8
9 import junit.framework.Assert;
10 import org.jboss.cache.CacheImpl;
11 import org.jboss.cache.loader.CacheLoader;
12 import org.jboss.cache.transaction.DummyTransactionManager;
13
14 import javax.transaction.Transaction JavaDoc;
15
16 /**
17  * Tests optimistic locking with cache loaders
18  *
19  * @author Manik Surtani (<a HREF="mailto:manik@jboss.org">manik@jboss.org</a>)
20  */

21 public class OptimisticWithCacheLoaderTest extends AbstractOptimisticTestCase
22 {
23
24    public OptimisticWithCacheLoaderTest(String JavaDoc s)
25    {
26       super(s);
27    }
28
29
30    public void testLoaderIndependently() throws Exception JavaDoc
31    {
32       CacheImpl cache = createCacheWithLoader();
33       CacheLoader loader = cache.getCacheLoaderManager().getCacheLoader();
34
35       // test the cache loader independently first ...
36
loader.remove(fqn);
37       Assert.assertNull(loader.get(fqn));
38       loader.put(fqn, key, value);
39       Assert.assertEquals(value, loader.get(fqn).get(key));
40       // clean up
41
loader.remove(fqn);
42       Assert.assertNull(loader.get(fqn));
43    }
44
45    public void testCacheLoadOnTree() throws Exception JavaDoc
46    {
47       CacheLoader loader = null;
48       try
49       {
50          CacheImpl cache = createCacheWithLoader();
51          loader = cache.getCacheLoaderManager().getCacheLoader();
52
53          DummyTransactionManager mgr = DummyTransactionManager.getInstance();
54          Transaction JavaDoc tx;
55
56          // make sure the fqn is not in cache
57
assertNull(cache.get(fqn));
58
59          // put something in the loader and make sure all tx's can see it
60
loader.put(fqn, key, value);
61
62          // start the 1st tx
63
mgr.begin();
64          tx = mgr.getTransaction();
65          assertEquals(value, cache.get(fqn, key));
66          mgr.suspend();
67
68          // start a new tx
69
mgr.begin();
70          assertEquals(value, cache.get(fqn, key));
71          mgr.commit();
72
73          mgr.resume(tx);
74          assertEquals(value, cache.get(fqn, key));
75          mgr.commit();
76       }
77       finally
78       {
79          // cleanup
80
if (loader != null) loader.remove(fqn);
81       }
82    }
83
84    public void testCacheStoring() throws Exception JavaDoc
85    {
86       Transaction JavaDoc tx;
87       CacheImpl cache = createCacheWithLoader();
88       CacheLoader loader = cache.getCacheLoaderManager().getCacheLoader();
89
90       // test the cache ...
91
DummyTransactionManager mgr = DummyTransactionManager.getInstance();
92       assertNull(mgr.getTransaction());
93       mgr.begin();
94       cache.put(fqn, key, value);
95       mgr.commit();
96
97       Assert.assertEquals(value, cache.get(fqn, key));
98
99       //now lets see if the state has been persisted in the cache loader
100
Assert.assertEquals(value, loader.get(fqn).get(key));
101
102
103       mgr.begin();
104       cache.remove(fqn);
105       mgr.commit();
106
107       Assert.assertNull(cache.get(fqn, key));
108       //now lets see if the state has been persisted in the cache loader
109
Assert.assertNull(loader.get(fqn));
110
111       mgr.begin();
112       cache.put(fqn, key, value);
113       tx = mgr.getTransaction();
114       mgr.suspend();
115
116       // lets see what we've got halfway within a tx
117
Assert.assertNull(cache.get(fqn, key));
118       Assert.assertNull(loader.get(fqn));
119
120       mgr.resume(tx);
121       mgr.commit();
122
123       // and after committing...
124
Assert.assertEquals(value, cache.get(fqn, key));
125       Assert.assertEquals(value, loader.get(fqn).get(key));
126
127       // clean up loader
128
loader.remove(fqn);
129    }
130
131    public void testCacheLoading() throws Exception JavaDoc
132    {
133       CacheImpl cache = createCacheWithLoader();
134       CacheLoader loader = cache.getCacheLoaderManager().getCacheLoader();
135
136       Assert.assertNull(cache.get(fqn, key));
137
138       // put something in the loader
139
loader.put(fqn, key, value);
140       Assert.assertEquals(value, loader.get(fqn).get(key));
141
142       // test that this can now be accessed by the cache
143
Assert.assertEquals(value, cache.get(fqn, key));
144
145       // clean up loader
146
loader.remove(fqn);
147
148       // what's in the cache now? Should not ne null...
149
Assert.assertNotNull(cache.get(fqn, key));
150    }
151
152    public void testCacheLoadingWithReplication() throws Exception JavaDoc
153    {
154       CacheImpl cache1 = createReplicatedCacheWithLoader(false);
155       CacheLoader loader1 = cache1.getCacheLoader();
156
157       CacheImpl cache2 = createReplicatedCacheWithLoader(false);
158       CacheLoader loader2 = cache2.getCacheLoader();
159
160       // test the cache ...
161
DummyTransactionManager mgr = DummyTransactionManager.getInstance();
162       assertNull(mgr.getTransaction());
163
164       mgr.begin();
165
166       // add something in cache1
167
cache1.put(fqn, key, value);
168       Assert.assertEquals(value, cache1.get(fqn, key));
169
170       // test that loader1, loader2, cache2 doesnt have entry
171
Assert.assertNull(loader1.get(fqn));
172       Assert.assertNull(loader2.get(fqn));
173       Assert.assertNull(cache2.get(fqn, key));
174
175       // commit
176
mgr.commit();
177
178       // test that loader1, loader2, cache2 has entry
179
Assert.assertEquals(value, cache1.get(fqn, key));
180       Assert.assertEquals(value, loader1.get(fqn).get(key));
181       Assert.assertEquals(value, loader2.get(fqn).get(key));
182       Assert.assertEquals(value, cache2.get(fqn, key));
183
184       // cache2 removes entry
185
mgr.begin();
186       cache2.remove(fqn);
187       Assert.assertNull(cache2.get(fqn, key));
188       // test that loader1, loader2 and cache2 have the entry
189
Assert.assertEquals(value, cache1.get(fqn, key));
190       Assert.assertEquals(value, loader1.get(fqn).get(key));
191       Assert.assertEquals(value, loader2.get(fqn).get(key));
192
193       // commit
194
mgr.commit();
195
196       // test that the entry has been removed everywhere.
197
Assert.assertNull(cache1.get(fqn));
198       Assert.assertNull(loader1.get(fqn));
199       Assert.assertNull(loader2.get(fqn));
200       Assert.assertNull(cache2.get(fqn));
201
202    }
203
204    public void testSharedCacheLoadingWithReplication() throws Exception JavaDoc
205    {
206       CacheImpl cache1 = createReplicatedCacheWithLoader(true);
207       CacheLoader loader1 = cache1.getCacheLoader();
208
209       CacheImpl cache2 = createReplicatedCacheWithLoader(true);
210       CacheLoader loader2 = cache2.getCacheLoader();
211
212       // test the cache ...
213
DummyTransactionManager mgr = DummyTransactionManager.getInstance();
214       assertNull(mgr.getTransaction());
215
216       mgr.begin();
217
218       // add something in cache1
219
cache1.put(fqn, key, value);
220       Assert.assertEquals(value, cache1.get(fqn, key));
221
222       // test that loader1, loader2, cache2 doesnt have entry
223
System.out.println("*** " + loader1.get(fqn));
224       Assert.assertNull(loader1.get(fqn));
225       Assert.assertNull(loader2.get(fqn));
226       Assert.assertNull(cache2.get(fqn, key));
227
228       // commit
229
mgr.commit();
230
231       // test that loader1, loader2, cache2 has entry
232
Assert.assertEquals(value, cache1.get(fqn, key));
233       Assert.assertEquals(value, loader1.get(fqn).get(key));
234       Assert.assertEquals(value, loader2.get(fqn).get(key));
235       Assert.assertEquals(value, cache2.get(fqn, key));
236
237       // cache2 removes entry
238
mgr.begin();
239       cache2.remove(fqn);
240       Assert.assertNull(cache2.get(fqn, key));
241       // test that loader1, loader2 and cache2 have the entry
242
Assert.assertEquals(value, cache1.get(fqn, key));
243       Assert.assertEquals(value, loader1.get(fqn).get(key));
244       Assert.assertEquals(value, loader2.get(fqn).get(key));
245
246       // commit
247
mgr.commit();
248
249       // test that the entry has been removed everywhere.
250
Assert.assertNull(cache1.get(fqn));
251       Assert.assertNull(loader1.get(fqn));
252       Assert.assertNull(loader2.get(fqn));
253       Assert.assertNull(cache2.get(fqn));
254
255    }
256 }
257
Popular Tags