001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.test.cluster.partition.test;
023:
024: import java.util.Properties;
025: import java.util.Set;
026:
027: import javax.management.ObjectName;
028: import javax.naming.Context;
029: import javax.naming.InitialContext;
030:
031: import junit.framework.Test;
032:
033: import org.jboss.jmx.adaptor.rmi.RMIAdaptor;
034: import org.jboss.test.JBossClusteredTestCase;
035: import org.jboss.test.testbean.interfaces.StatefulSessionHome;
036: import org.jboss.test.testbeancluster.interfaces.NodeAnswer;
037: import org.jboss.test.testbeancluster.interfaces.StatefulSession;
038:
039: /**
040: * A PartitionRestartUnitTestCase.
041: *
042: * @author <a href="mailto://brian.stansberry@jboss.com">Brian Stansberry</a>
043: * @version $Revision$
044: */
045: public class PartitionRestartUnitTestCase extends
046: JBossClusteredTestCase {
047: /**
048: * Create a new PartitionRestartUnitTestCase.
049: *
050: * @param name
051: */
052: public PartitionRestartUnitTestCase(String name) {
053: super (name);
054: }
055:
056: public static Test suite() throws Exception {
057: Test t1 = JBossClusteredTestCase.getDeploySetup(
058: PartitionRestartUnitTestCase.class,
059: "testbeancluster.jar");
060: return t1;
061: }
062:
063: public void testPartitionRestart() throws Exception {
064: getLog().debug("testStatefulBeanFailover");
065:
066: RMIAdaptor[] adaptors = this .getAdaptors();
067:
068: Set channelMBean = getChannelMBeans(adaptors[0]);
069: assertNotNull("Channel mbean registered", channelMBean);
070: assertEquals("Only one channel mbean", 1, channelMBean.size());
071:
072: Set protocolMBeans = getProtocolMBeans(adaptors[0]);
073: assertNotNull("Protocol mbeans registered", channelMBean);
074: assertTrue("Multiple protocol mbeans",
075: protocolMBeans.size() > 1);
076:
077: String[] urls = getNamingURLs();
078: Properties env = new Properties();
079: env.setProperty(Context.INITIAL_CONTEXT_FACTORY,
080: "org.jnp.interfaces.NamingContextFactory");
081: env.setProperty(Context.PROVIDER_URL, urls[0]);
082: Context ctx = new InitialContext(env);
083: getLog().debug("OK");
084:
085: getLog().debug("");
086: getLog()
087: .debug("Looking up the home nextgen.StatefulSession...");
088: StatefulSessionHome statefulSessionHome = (StatefulSessionHome) ctx
089: .lookup("nextgen_StatefulSession");
090: if (statefulSessionHome != null)
091: getLog().debug("ok");
092: getLog().debug("Calling create on StatefulSessionHome...");
093: StatefulSession statefulSession = (StatefulSession) statefulSessionHome
094: .create("Bupple-Dupple");
095: assertTrue("statefulSessionHome.create() != null",
096: statefulSession != null);
097: getLog().debug("ok");
098:
099: NodeAnswer node1 = statefulSession.getNodeState();
100: getLog().debug(node1);
101:
102: // Now we switch to the other node, simulating a failure on node 1
103: //
104: System.setProperty("JBossCluster-DoFail", "once");
105: NodeAnswer node2 = statefulSession.getNodeState();
106: getLog().debug(node2);
107:
108: assertTrue("Failover has occured", !node1.nodeId
109: .equals(node2.nodeId));
110:
111: assertTrue("Value is identical on replicated node",
112: "Bupple-Dupple".equals(node1.answer)
113: && node1.answer.equals(node2.answer));
114:
115: // Stop and restart the ClusterPartition on node1
116: restartPartition(adaptors[0]);
117:
118: // Let the cluster stabilize
119: sleep(2000);
120:
121: // we change our name to see if it replicates to node 1
122: //
123: statefulSession.setName("Changed");
124:
125: // now we travel back to node 1
126: System.setProperty("JBossCluster-DoFail", "once");
127: node1 = statefulSession.getNodeState();
128: getLog().debug(node1);
129:
130: assertTrue("Failover has occured", !node1.nodeId
131: .equals(node2.nodeId));
132:
133: assertTrue("Value is identical on replicated node", "Changed"
134: .equals(node1.answer));
135:
136: statefulSession.remove();
137:
138: Set channelMBean2 = getChannelMBeans(adaptors[0]);
139: assertTrue("Channel mbean unchanged", channelMBean
140: .equals(channelMBean2));
141:
142: Set protocolMBeans2 = getProtocolMBeans(adaptors[0]);
143: assertTrue("Protocol mbeans unchanged", protocolMBeans
144: .equals(protocolMBeans2));
145:
146: getLog().debug("ok");
147: }
148:
149: protected void restartPartition(RMIAdaptor adaptor)
150: throws Exception {
151: ObjectName partition = new ObjectName(
152: "jboss:service=DefaultPartition");
153:
154: Object[] params = new Object[0];
155: String[] types = new String[0];
156: adaptor.invoke(partition, "stop", params, types);
157:
158: sleep(2000);
159:
160: adaptor.invoke(partition, "start", params, types);
161:
162: sleep(2000);
163: }
164:
165: protected Set getChannelMBeans(RMIAdaptor adaptor) throws Exception {
166: ObjectName on = new ObjectName(
167: "jboss.jgroups:type=channel,cluster=DefaultPartition");
168: return adaptor.queryNames(on, null);
169: }
170:
171: protected Set getProtocolMBeans(RMIAdaptor adaptor)
172: throws Exception {
173: ObjectName on = new ObjectName(
174: "jboss.jgroups:*,type=protocol,cluster=DefaultPartition");
175: return adaptor.queryNames(on, null);
176: }
177:
178: }
|