KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > cache > buddyreplication > BuddyReplicationWithCacheLoaderTest


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.buddyreplication;
8
9 import org.jboss.cache.CacheImpl;
10 import org.jboss.cache.Fqn;
11 import org.jboss.cache.loader.CacheLoader;
12 import org.jboss.cache.misc.TestingUtil;
13
14 /**
15  * Tests use of the data gravitator alongside other cache loaders as well as data gravitator options such as removeOnFind.
16  *
17  * @author <a HREF="mailto:manik@jboss.org">Manik Surtani (manik@jboss.org)</a>
18  */

19 public class BuddyReplicationWithCacheLoaderTest extends BuddyReplicationTestsBase
20 {
21
22    protected Fqn fqn = Fqn.fromString("/test");
23    protected String JavaDoc key = "key";
24    protected String JavaDoc value = "value";
25    protected boolean passivation = false;
26
27    private CacheLoader[] getLoaders(CacheImpl[] caches)
28    {
29       CacheLoader[] retVal = new CacheLoader[caches.length];
30
31       for (int i = 0; i < retVal.length; i++)
32       {
33          retVal[i] = caches[i].getCacheLoaderManager().getCacheLoader();
34       }
35
36       return retVal;
37    }
38
39    public void testWithDataGravitationDefault() throws Exception JavaDoc
40    {
41
42       CacheImpl[] caches = null;
43       try
44       {
45          // create 3 caches
46
caches = createCachesWithCacheLoader(3, true, true, passivation);
47
48          TestingUtil.sleepThread(1000);
49
50          CacheLoader[] loaders = getLoaders(caches);
51
52          // cleanup
53
for (int i = 0; i < 3; i++) loaders[i].remove(Fqn.ROOT);
54
55          for (int i = 0; i < 3; i++) System.out.println(i + ": " + loaders[i].get(fqn));
56
57          // put stuff in cache0
58
caches[0].put(fqn, key, value);
59
60          // make sure there are no locks.
61
assertNoLocks(caches);
62          for (int i = 0; i < 3; i++) System.out.println(i + ": " + loaders[i].get(fqn));
63
64          dumpCacheContents(caches);
65
66          // request data from cache2
67
assertEquals(value, caches[2].get(fqn, key));
68          assertNoLocks(caches);
69
70          for (int i = 0; i < 3; i++) System.out.println(i + ": " + loaders[i].get(fqn));
71
72          dumpCacheContents(caches);
73
74          // test that data does not exist in cache0
75
assertTrue("should not exist in cache0", !caches[0].exists(fqn));
76
77          // test that data does not exist in cache1
78
assertTrue("should not exist in cache1", !caches[1].exists(fqn));
79
80          // test that data does exist in cache2
81
assertTrue("should exist in cache2", caches[2].exists(fqn));
82
83          // test that data does not exist in loader0
84
assertTrue("should not exist in loader0", !loaders[0].exists(fqn));
85
86          // test that data does not exist in loader1
87
assertTrue("should not exist in loader1", !loaders[1].exists(fqn));
88
89          // test that data does exist in loader2
90
assertTrue("should exist in loader2", passivation ? !loaders[2].exists(fqn) : loaders[2].exists(fqn));
91
92          Fqn b1 = new Fqn(new Fqn(BuddyManager.BUDDY_BACKUP_SUBTREE_FQN, BuddyManager.getGroupNameFromAddress(caches[0].getLocalAddress())), fqn.peekElements());
93          Fqn b2 = new Fqn(new Fqn(BuddyManager.BUDDY_BACKUP_SUBTREE_FQN, BuddyManager.getGroupNameFromAddress(caches[2].getLocalAddress())), fqn.peekElements());
94
95          // test that bkup does exist in cache0
96
assertTrue("should not exist in cache0", !caches[0].exists(b1));
97          assertTrue("should exist in cache0", caches[0].exists(b2));
98
99          // test that bkup does not exist in cache1
100
assertTrue("should not exist in cache1", !caches[1].exists(b1));
101          assertTrue("should not exist in cache1", !caches[1].exists(b2));
102
103          // test that bkup does not exist in cache2
104
assertTrue("should not exist in cache2", !caches[2].exists(b1));
105          assertTrue("should not exist in cache2", !caches[2].exists(b2));
106
107          // test that bkup does exist in loader0
108
assertTrue("should not exist in loader0", !loaders[0].exists(b1));
109          assertTrue("should exist in loader0", passivation ? !loaders[0].exists(b2) : loaders[0].exists(b2));
110
111          // test that bkup does not exist in loader1
112
assertTrue("should not exist in loaders1", !loaders[1].exists(b1));
113          assertTrue("should not exist in loaders1", !loaders[1].exists(b2));
114
115          // test that bkup does not exist in loader2
116
assertTrue("should not exist in loaders2", !loaders[2].exists(b1));
117          assertTrue("should not exist in loaders2", !loaders[2].exists(b2));
118
119
120       }
121       finally
122       {
123          cleanup(caches);
124       }
125
126    }
127
128    public void testWithDataGravitationEviction() throws Exception JavaDoc
129    {
130       CacheImpl[] caches = null;
131       try
132       {
133          // create 3 caches
134
caches = createCachesWithCacheLoader(3, true, false, passivation);
135          CacheLoader[] loaders = getLoaders(caches);
136
137          // put stuff in cache0
138
caches[0].put(fqn, key, value);
139
140          // request data from cache2
141
assertEquals(value, caches[2].get(fqn, key));
142
143          // test that data does not exist in cache0
144
assertTrue("should not exist in cache0", !caches[0].exists(fqn));
145
146          // test that data does not exist in cache1
147
assertTrue("should not exist in cache1", !caches[1].exists(fqn));
148
149          // test that data does exist in cache2
150
assertTrue("should exist in cache2", caches[2].exists(fqn));
151
152          // test that data does exist in loader0
153
assertTrue("should exist in loader0", loaders[0].exists(fqn));
154
155          // test that data does not exist in loader1
156
assertTrue("should not exist in loader1", !loaders[1].exists(fqn));
157
158          // test that data does exist in loader2
159
assertTrue("should exist in loader2", passivation ? !loaders[2].exists(fqn) : loaders[2].exists(fqn));
160
161          Fqn b1 = new Fqn(new Fqn(BuddyManager.BUDDY_BACKUP_SUBTREE_FQN, BuddyManager.getGroupNameFromAddress(caches[0].getLocalAddress())), fqn.peekElements());
162          Fqn b2 = new Fqn(new Fqn(BuddyManager.BUDDY_BACKUP_SUBTREE_FQN, BuddyManager.getGroupNameFromAddress(caches[2].getLocalAddress())), fqn.peekElements());
163
164          // test that bkup does exist in cache0
165
assertTrue("should not exist in cache0", !caches[0].exists(b1));
166          assertTrue("should exist in cache0", caches[0].exists(b2));
167
168          // test that bkup does not exist in cache1
169
assertTrue("should not exist in cache1", !caches[1].exists(b1));
170          assertTrue("should not exist in cache1", !caches[1].exists(b2));
171
172          // test that bkup does not exist in cache2
173
assertTrue("should not exist in cache2", !caches[2].exists(b1));
174          assertTrue("should not exist in cache2", !caches[2].exists(b2));
175
176          // test that bkup does exist in loader0
177
assertTrue("should not exist in loader0", !loaders[0].exists(b1));
178          assertTrue("should exist in loader0", passivation ? !loaders[0].exists(b2) : loaders[0].exists(b2));
179
180          // test that bkup does not exist in loader1
181
assertTrue("should exist in loaders1", loaders[1].exists(b1));
182          assertTrue("should not exist in loaders1", !loaders[1].exists(b2));
183
184          // test that bkup does not exist in loader2
185
assertTrue("should not exist in loaders2", !loaders[2].exists(b1));
186          assertTrue("should not exist in loaders2", !loaders[2].exists(b2));
187       }
188       finally
189       {
190          cleanup(caches);
191       }
192    }
193
194 }
195
Popular Tags