KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > cache > statetransfer > StateTransfer200Test


1 /*
2  * JBoss, the OpenSource J2EE webOS
3  *
4  * Distributable under LGPL license.
5  * See terms of license at gnu.org.
6  */

7
8 package org.jboss.cache.statetransfer;
9
10 import org.jboss.cache.CacheSPI;
11 import org.jboss.cache.Fqn;
12 import org.jboss.cache.Node;
13 import org.jboss.cache.buddyreplication.BuddyManager;
14 import org.jboss.cache.config.BuddyReplicationConfig;
15 import org.jboss.cache.factories.XmlConfigurationParser;
16 import org.jboss.cache.misc.TestingUtil;
17 import org.w3c.dom.Document JavaDoc;
18 import org.w3c.dom.Element JavaDoc;
19
20 import javax.xml.parsers.DocumentBuilder JavaDoc;
21 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
22
23 /**
24  * Tests that state transfer works properly if the version is 2.0.0.GA.
25  *
26  * @author <a HREF="mailto://brian.stansberry@jboss.com">Brian Stansberry</a>
27  * @version $Revision: 1.13 $
28  */

29 public class StateTransfer200Test extends VersionedTestBase
30 {
31
32    protected String JavaDoc getReplicationVersion()
33    {
34       return "2.0.0.GA";
35    }
36
37    public void testBuddyBackupExclusion() throws Exception JavaDoc
38    {
39       CacheSPI cache1 = createCache("cache1", false, false, false, false, false);
40
41
42       cache1.getConfiguration().setBuddyReplicationConfig(getBuddyConfig());
43
44       cache1.start();
45
46       Fqn backup = new Fqn(BuddyManager.BUDDY_BACKUP_SUBTREE_FQN, "test");
47       cache1.put(backup, "name", JOE);
48       cache1.put(A_B, "age", TWENTY);
49
50       CacheSPI cache2 = createCache("cache2", false, false, false);
51
52       // Pause to give caches time to see each other
53
TestingUtil.blockUntilViewsReceived(new CacheSPI[]{cache1, cache2}, 60000);
54
55       assertNull("_buddy_backup_ not transferred", cache2.get(backup, "test"));
56       assertEquals("Correct age for /a/b", TWENTY, cache2.get(A_B, "age"));
57    }
58
59    public void testBuddyIntegration() throws Exception JavaDoc
60    {
61       CacheSPI cache1 = createCache("cache1", false, false, false, false, false);
62
63       cache1.getConfiguration().setBuddyReplicationConfig(getBuddyConfig());
64
65       cache1.start();
66
67       CacheSPI cache2 = createCache("cache2", false, false, false);
68
69       // Pause to give caches time to see each other
70
TestingUtil.blockUntilViewsReceived(new CacheSPI[]{cache1, cache2}, 60000);
71
72       Fqn backup = Fqn.fromString(BuddyManager.BUDDY_BACKUP_SUBTREE);
73       backup = new Fqn(backup, "a");
74       cache1.getInvocationContext().getOptionOverrides().setCacheModeLocal(true);
75       cache1.put(backup, null);
76
77       Node target = cache1.peek(backup);
78
79       assertNotNull("Data Node should not be null", target);
80
81       Fqn abc = Fqn.fromString("/a/b/c");
82       cache2.getInvocationContext().getOptionOverrides().setCacheModeLocal(true);
83       cache2.put(abc, "name", JOE);
84       cache2.getInvocationContext().getOptionOverrides().setCacheModeLocal(true);
85       cache2.put(A_D, "name", JANE);
86
87       Object JavaDoc[] sources = cache1.getMembers().toArray();
88       ClassLoader JavaDoc cl = Thread.currentThread().getContextClassLoader();
89       Fqn a = Fqn.fromString("/a");
90       cache1.getStateTransferManager().loadState(a, target, sources, cl);
91
92       Fqn test = new Fqn(BuddyManager.BUDDY_BACKUP_SUBTREE_FQN, abc);
93       assertEquals("/a/b/c state integrated in backup region", JOE, cache1.get(test, "name"));
94
95       test = new Fqn(BuddyManager.BUDDY_BACKUP_SUBTREE_FQN, A_D);
96       assertEquals("/a/d state integrated in backup region", JANE, cache1.get(test, "name"));
97    }
98
99    public void testCacheLoaderFailure() throws Exception JavaDoc
100    {
101       CacheSPI cache1 = createCache("cache1", false, false, "org.jboss.cache.statetransfer.CorruptedFileCacheLoader", false, true);
102
103       cache1.put(A_B, "name", JOE);
104       cache1.put(A_B, "age", TWENTY);
105       cache1.put(A_C, "name", BOB);
106       cache1.put(A_C, "age", FORTY);
107
108       CacheSPI cache2 = null;
109       try
110       {
111          cache2 = createCache("cache2", false, false, true, false, false);
112          cache2.create();
113          cache2.start();
114          fail("Should have caused an exception");
115       }
116       catch (Exception JavaDoc e)
117       {
118          assertNotNull(e);
119       }
120
121       //when persistent transfer fails as in this case state recipient cacheloader should be wiped clean
122
assertFalse("/a/b is not in cache loader ", cache2.getCacheLoaderManager().getCacheLoader().exists(A_B));
123    }
124
125
126    private BuddyReplicationConfig getBuddyConfig() throws Exception JavaDoc
127    {
128       // TODO just build the object and skip the legacy XML step
129
DocumentBuilderFactory JavaDoc dbf = DocumentBuilderFactory.newInstance();
130       DocumentBuilder JavaDoc db = dbf.newDocumentBuilder();
131       Document JavaDoc doc = db.newDocument();
132       Element JavaDoc config = doc.createElement("config");
133       doc.appendChild(config);
134       Element JavaDoc enabled = doc.createElement("buddyReplicationEnabled");
135       enabled.appendChild(doc.createTextNode("true"));
136       config.appendChild(enabled);
137       Element JavaDoc pool = doc.createElement("buddyPoolName");
138       pool.appendChild(doc.createTextNode("TEST"));
139       config.appendChild(pool);
140       return XmlConfigurationParser.parseBuddyReplicationConfig(config);
141    }
142 }
143
Popular Tags