KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > cache > marshall > ActiveInactiveTest


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.marshall;
9
10 import junit.framework.TestCase;
11 import org.jboss.cache.CacheImpl;
12 import org.jboss.cache.Fqn;
13 import org.jboss.cache.RegionManager;
14 import org.jboss.cache.Version;
15
16 import java.lang.reflect.Method JavaDoc;
17
18 /**
19  * Tests the "activate/deactivate" functionality of LegacyTreeCacheMarshaller.
20  *
21  * @author <a HREF="mailto://brian.stansberry@jboss.com">Brian Stansberry</a>
22  * @version $Revision$
23  */

24 public class ActiveInactiveTest extends TestCase
25 {
26    RegionManager rman;
27    CacheImpl c;
28
29    @Override JavaDoc
30    protected void setUp() throws Exception JavaDoc
31    {
32       super.setUp();
33       c = new CacheImpl();
34       c.getConfiguration().setUseRegionBasedMarshalling(true);
35       c.getConfiguration().setFetchInMemoryState(false);
36       c.start();
37       rman = c.getRegionManager();
38    }
39
40    @Override JavaDoc
41    protected void tearDown() throws Exception JavaDoc
42    {
43       super.tearDown();
44       c.stop();
45       c = null;
46       rman = null;
47    }
48
49    public void testDefaultActive() throws Exception JavaDoc
50    {
51       rman.setDefaultInactive(false);
52       VersionAwareMarshaller testee = new VersionAwareMarshaller(rman, false, true, Version.getVersionString(Version.getVersionShort()));
53       assertFalse("Root is not active", testee.isInactive("/"));
54
55       rman.deactivate("/a");
56       assertFalse("Root is not active after inactivating subtree",
57               testee.isInactive("/"));
58
59       rman.activate("/a");
60       assertFalse("Root is not active after activating subtree",
61               testee.isInactive("/"));
62
63       rman.activate("/a/b");
64
65       rman.deactivate("/");
66       assertTrue("Root is active", testee.isInactive("/"));
67    }
68
69    public void testDefaultInactive() throws Exception JavaDoc
70    {
71       rman.setDefaultInactive(true);
72       VersionAwareMarshaller testee = new VersionAwareMarshaller(rman, true, true, Version.getVersionString(Version.getVersionShort()));
73       assertTrue("Root is not inactive", testee.isInactive("/"));
74
75       rman.activate("/a");
76       assertTrue("Root is not inactive after activating subtree",
77               testee.isInactive("/"));
78
79       rman.deactivate("/a");
80       assertTrue("Root is not inactive after inactivating subtree",
81               testee.isInactive("/"));
82
83       rman.deactivate("/a/b");
84
85       rman.activate("/");
86       assertFalse("Root is not active", testee.isInactive("/"));
87    }
88
89    public void testActivate() throws Exception JavaDoc
90    {
91       rman.setDefaultInactive(false);
92       VersionAwareMarshaller defaultActive = new VersionAwareMarshaller(rman, false, true, Version.getVersionString(Version.getVersionShort()));
93
94       rman.activate("/a");
95       assertFalse("/a is not active after activating",
96               defaultActive.isInactive("/a"));
97
98       rman.deactivate("/a");
99       rman.activate("/a");
100       assertFalse("/a is not active after reactivating",
101               defaultActive.isInactive("/a"));
102
103       rman.reset();
104       rman.setDefaultInactive(true);
105       VersionAwareMarshaller defaultInactive = new VersionAwareMarshaller(rman, true, true, Version.getVersionString(Version.getVersionShort()));
106
107       rman.activate("/i");
108       assertFalse("/i is not active after activating",
109               defaultInactive.isInactive("/i"));
110       assertFalse("/i/k is not active after activating /i",
111               defaultInactive.isInactive("/i/k"));
112
113       rman.deactivate("/i");
114       rman.activate("/i");
115       assertFalse("/i is not active after reactivating",
116               defaultInactive.isInactive("/i"));
117    }
118
119    public void testInactivate() throws Exception JavaDoc
120    {
121       rman.setDefaultInactive(true);
122       VersionAwareMarshaller defaultInactive = new VersionAwareMarshaller(rman, true, true, Version.getVersionString(Version.getVersionShort()));
123
124       rman.deactivate("/i");
125       assertTrue("/i is not inactive after inactivating",
126               defaultInactive.isInactive("/i"));
127
128       rman.activate("/i");
129       rman.deactivate("/i");
130       assertTrue("/i is not inactive after re-inactivating",
131               defaultInactive.isInactive("/i"));
132
133       rman.reset();
134       rman.setDefaultInactive(false);
135       VersionAwareMarshaller defaultActive = new VersionAwareMarshaller(rman, false, true, Version.getVersionString(Version.getVersionShort()));
136
137       rman.deactivate("/a");
138       assertTrue("/a is not inactive after inactivating",
139               defaultInactive.isInactive("/a"));
140       assertTrue("/a/b is not inactive after inactivating /a",
141               defaultInactive.isInactive("/a/b"));
142
143       rman.activate("/a");
144       rman.deactivate("/a");
145       assertTrue("/a is not inactive after re-inactivating",
146               defaultInactive.isInactive("/a"));
147    }
148
149    public void testObjectFromByteBuffer() throws Exception JavaDoc
150    {
151       MethodCall put = MethodCallFactory.create(MethodDeclarations.putKeyValMethodLocal,
152               null, Fqn.fromString("/a/b"), "name", "Joe", false);
153
154       MethodCall replicate = MethodCallFactory.create(MethodDeclarations.replicateMethod, put);
155
156       rman.setDefaultInactive(true);
157       VersionAwareMarshaller testee = new VersionAwareMarshaller(rman, true, true, Version.getVersionString(Version.getVersionShort()));
158       byte[] callBytes = testee.objectToByteBuffer(replicate);
159
160       try
161       {
162          testee.objectFromByteBuffer(callBytes);
163          fail("Expected to fail since region is inactive");
164       }
165       catch (Exception JavaDoc e)
166       {
167          // expected to fail since region is inactive
168
}
169
170       rman.activate("/a");
171       assertTrue(rman.hasRegion(Fqn.fromString("/a")));
172
173       MethodCall result = (MethodCall) testee.objectFromByteBuffer(callBytes);
174       Method JavaDoc method = result.getMethod();
175       assertEquals("Did not get replicate method when passing" +
176               " call for active node", MethodDeclarations.replicateMethod, method);
177    }
178
179 }
180
Popular Tags