KickJava   Java API By Example, From Geeks To Geeks.

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


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.config.BuddyReplicationConfig;
12 import org.jboss.cache.factories.XmlConfigurationParser;
13 import org.w3c.dom.Document JavaDoc;
14 import org.w3c.dom.Element JavaDoc;
15
16 import javax.xml.parsers.DocumentBuilder JavaDoc;
17 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
18 import java.util.HashMap JavaDoc;
19 import java.util.Map JavaDoc;
20 import java.util.Set JavaDoc;
21
22 /**
23  * Tests handling of the buddy backup region during region
24  * activation and inactivation
25  *
26  * @author Brian Stansberry
27  */

28 public class BuddyBackupActivationInactivationTest extends BuddyReplicationTestsBase
29 {
30    public static final Fqn A = Fqn.fromString("/a");
31    public static final Fqn A_B = Fqn.fromString("/a/b");
32    public static final String JavaDoc JOE = "JOE";
33
34    protected Map JavaDoc caches;
35    private ClassLoader JavaDoc orig_TCL;
36
37    public void testBuddyBackupActivation() throws Exception JavaDoc
38    {
39       /** TODO: Uncomment once we have FLUSH in place
40        *
41
42        CacheImpl[] caches = new CacheImpl[2];
43        caches[0] = createCache("cache1", true, true, true);
44        caches[1] = createCache("cache2", true, true, true);
45
46        TestingUtil.blockUntilViewsReceived(caches, VIEW_BLOCK_TIMEOUT);
47
48        caches[0].activateRegion("/a");
49
50        caches[0].put(A_B, "name", JOE);
51
52        TestingUtil.sleepThread(getSleepTimeout());
53
54        caches[1].activateRegion("/a");
55
56        Fqn fqn = new Fqn(BuddyManager.BUDDY_BACKUP_SUBTREE_FQN,
57        BuddyManager.getGroupNameFromAddress(caches[0].getLocalAddress()));
58        fqn = new Fqn(fqn, A_B);
59
60        assertEquals("State transferred with activation", JOE, caches[1].get(fqn, "name"));
61        */

62    }
63
64    public void testReplToInactiveRegion() throws Exception JavaDoc
65    {
66       // TODO: Reinstate once we have proper FLUSH working.
67
// This test relies on calls to inactive regions being queued and re-run
68
// when the region is activated. After discussions with Brian, the queueing
69
// was removed, presuming FLUSH will fix this. Need to test with FLUSH.
70
// - Manik Surtani (16 Oct 2006)
71
/*
72       CacheImpl[] caches = new CacheImpl[2];
73       caches[0] = createCache("cache1", true, true, true);
74       caches[1] = createCache("cache2", true, true, true);
75
76       TestingUtil.blockUntilViewsReceived(caches, VIEW_BLOCK_TIMEOUT);
77
78       caches[0].activateRegion("/a");
79
80       // Activate the buddy backup subtree in the recipient so any
81       // repl message doesn't get rejected due to that tree being inactive
82       caches[1].activateRegion(BuddyManager.BUDDY_BACKUP_SUBTREE);
83
84       caches[0].put(A_B, "name", JOE);
85
86       TestingUtil.sleepThread(getSleepTimeout());
87
88       assertNull("No replication to inactive region", caches[1].get(A_B, "name"));
89
90       Fqn fqn = new Fqn(BuddyManager.BUDDY_BACKUP_SUBTREE_FQN,
91               BuddyManager.getGroupNameFromAddress(caches[0].getLocalAddress()));
92       fqn = new Fqn(fqn, A_B);
93
94       assertNull("No replication to inactive backup region", caches[1].get(fqn, "name"));
95       */

96    }
97
98    public void testBuddyBackupInactivation() throws Exception JavaDoc
99    {
100       // TODO: Reinstate once we have proper FLUSH working.
101
// This test relies on calls to inactive regions being queued and re-run
102
// when the region is activated. After discussions with Brian, the queueing
103
// was removed, presuming FLUSH will fix this. Need to test with FLUSH.
104
// - Manik Surtani (16 Oct 2006)
105
/*
106       CacheImpl cache1 = createCache("cache1", true, true, true);
107
108       cache1.activateRegion("/a");
109
110       Fqn fqn = new Fqn(BuddyManager.BUDDY_BACKUP_SUBTREE_FQN, "test");
111       fqn = new Fqn(fqn, A_B);
112       cache1.getInvocationContext().getOptionOverrides().setCacheModeLocal(true);
113       cache1.put(fqn, "name", JOE);
114
115       assertEquals("Put OK", JOE, cache1.get(fqn, "name"));
116
117       cache1.inactivateRegion("/a");
118
119       assertNull("Inactivation cleared region", cache1.get(fqn, "name"));
120 */

121
122    }
123
124    protected CacheImpl createCache(String JavaDoc cacheID,
125                                    boolean sync,
126                                    boolean useMarshalling,
127                                    boolean startCache)
128            throws Exception JavaDoc
129    {
130       if (caches.get(cacheID) != null)
131          throw new IllegalStateException JavaDoc(cacheID + " already created");
132
133       CacheImpl cache = new CacheImpl();
134
135       String JavaDoc configFile = sync ? "META-INF/replSync-service.xml"
136               : "META-INF/replAsync-service.xml";
137       cache.setConfiguration(new XmlConfigurationParser().parseFile(configFile));
138       cache.getConfiguration().setClusterName("Test");
139       if (useMarshalling)
140       {
141          cache.getConfiguration().setUseRegionBasedMarshalling(true);
142          cache.getConfiguration().setInactiveOnStartup(true);
143       }
144       cache.getConfiguration().setBuddyReplicationConfig(getBuddyConfig());
145       // Put the cache in the map before starting, so if it fails in
146
// start it can still be destroyed later
147
caches.put(cacheID, cache);
148
149       if (startCache)
150       {
151          cache.create();
152          cache.start();
153       }
154
155       return cache;
156    }
157
158    protected void setUp() throws Exception JavaDoc
159    {
160       super.setUp();
161
162       caches = new HashMap JavaDoc();
163
164       // Save the TCL in case a test changes it
165
orig_TCL = Thread.currentThread().getContextClassLoader();
166    }
167
168    protected void tearDown() throws Exception JavaDoc
169    {
170       super.tearDown();
171
172       // Restore the TCL in case a test changed it
173
Thread.currentThread().setContextClassLoader(orig_TCL);
174
175       Set JavaDoc keys = caches.keySet();
176       String JavaDoc[] cacheIDs = new String JavaDoc[keys.size()];
177       cacheIDs = (String JavaDoc[]) keys.toArray(cacheIDs);
178       for (int i = 0; i < cacheIDs.length; i++)
179       {
180          stopCache((CacheImpl) caches.get(cacheIDs[i]));
181       }
182    }
183
184    protected void stopCache(CacheImpl cache)
185    {
186       if (cache != null)
187       {
188          try
189          {
190             cache.stop();
191             cache.destroy();
192          }
193          catch (Exception JavaDoc e)
194          {
195             System.out.println("Exception stopping cache " + e.getMessage());
196             e.printStackTrace(System.out);
197          }
198       }
199    }
200
201    private BuddyReplicationConfig getBuddyConfig() throws Exception JavaDoc
202    {
203       // TODO just build the object; skip the legacy XML business
204

205       DocumentBuilderFactory JavaDoc dbf = DocumentBuilderFactory.newInstance();
206       DocumentBuilder JavaDoc db = dbf.newDocumentBuilder();
207       Document JavaDoc doc = db.newDocument();
208       Element JavaDoc config = doc.createElement("config");
209       doc.appendChild(config);
210       Element JavaDoc replEnabled = doc.createElement("buddyReplicationEnabled");
211       replEnabled.appendChild(doc.createTextNode("true"));
212       config.appendChild(replEnabled);
213       Element JavaDoc gravDisabled = doc.createElement("autoDataGravitation");
214       gravDisabled.appendChild(doc.createTextNode("false"));
215       config.appendChild(gravDisabled);
216
217       return XmlConfigurationParser.parseBuddyReplicationConfig(config);
218    }
219 }
220
Popular Tags