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:
023: package org.jboss.ejb3.test.clusteredsession.unit;
024:
025: import java.rmi.dgc.VMID;
026: import java.util.Properties;
027:
028: import javax.management.ObjectName;
029: import javax.naming.Context;
030: import javax.naming.InitialContext;
031:
032: import org.jboss.ejb3.test.clusteredsession.VMTester;
033: import org.jboss.jmx.adaptor.rmi.RMIAdaptor;
034: import org.jboss.test.JBossClusteredTestCase;
035:
036: /**
037: * @author Brian Stansberry
038: */
039: public abstract class InvokeLocalTestBase extends
040: JBossClusteredTestCase {
041: public static final String TESTER_JNDI_NAME = "NonClusteredStatelessRemote";
042: private static final String PROPERTIES_SERVICE = "jboss:type=Service,name=SystemProperties";
043: private static final String PARTITION_NAME_PROPERTY = "clusteredsession.islocal.partition";
044:
045: public InvokeLocalTestBase(String name) {
046: super (name);
047: }
048:
049: @Override
050: protected void setUp() throws Exception {
051: super .setUp();
052:
053: RMIAdaptor[] adaptors = getAdaptors();
054: setPartitionName(adaptors[0], "DefaultPartition");
055: setPartitionName(adaptors[1], getPartitionName());
056: }
057:
058: @Override
059: protected void tearDown() throws Exception {
060: super .tearDown();
061:
062: RMIAdaptor[] adaptors = getAdaptors();
063: clearPartitionName(adaptors[0]);
064: clearPartitionName(adaptors[1]);
065: }
066:
067: protected void stayLocalTest(String jndiName, boolean expectLocal)
068: throws Exception {
069: String[] jndiURLs = getNamingURLs();
070:
071: Properties env = new Properties();
072: env.setProperty(Context.PROVIDER_URL, jndiURLs[0]);
073: env.setProperty("jnp.disableDiscovery", "true");
074: InitialContext ctx = new InitialContext(env);
075: VMTester tester = (VMTester) ctx.lookup(TESTER_JNDI_NAME);
076:
077: VMID local = tester.getVMID();
078: assertNotNull("Got the local VMID", local);
079:
080: Properties env1 = new Properties();
081: env1.setProperty(Context.PROVIDER_URL, jndiURLs[1]);
082: env1.setProperty("jnp.disableDiscovery", "true");
083: ctx = new InitialContext(env1);
084: VMTester remote = (VMTester) ctx.lookup(jndiName);
085:
086: // This call instantiates the SFSB if needed
087: VMID remoteID = remote.getVMID();
088: assertNotNull("Got the remote VMID", remoteID);
089:
090: VMID passThroughID = tester.getVMIDFromRemote(remote);
091: assertNotNull("Got the remote VMID", passThroughID);
092:
093: if (expectLocal)
094: assertEquals("Call stayed local", local, passThroughID);
095: else
096: assertFalse("Call went remote", local.equals(passThroughID));
097:
098: passThroughID = tester.getVMIDFromRemoteLookup(jndiURLs[1],
099: jndiName);
100: assertNotNull("Got the remote VMID", passThroughID);
101:
102: if (expectLocal)
103: assertEquals("Call stayed local", local, passThroughID);
104: else
105: assertFalse("Call went remote", local.equals(passThroughID));
106: }
107:
108: protected abstract String getPartitionName();
109:
110: private void setPartitionName(RMIAdaptor adaptor,
111: String partitionName) throws Exception {
112: Object[] args = { PARTITION_NAME_PROPERTY, partitionName };
113: String[] sig = { String.class.getName(), String.class.getName() };
114: adaptor.invoke(new ObjectName(PROPERTIES_SERVICE), "set", args,
115: sig);
116: }
117:
118: private void clearPartitionName(RMIAdaptor adaptor)
119: throws Exception {
120: setPartitionName(adaptor, "DefaultPartition");
121: Object[] args = { PARTITION_NAME_PROPERTY };
122: String[] sig = { String.class.getName() };
123: adaptor.invoke(new ObjectName(PROPERTIES_SERVICE), "remove",
124: args, sig);
125: }
126: }
|