Author: ron.sigal(a)jboss.com
Date: 2010-09-07 15:27:15 -0400 (Tue, 07 Sep 2010)
New Revision: 6084
Added:
remoting3/trunk/jboss-remoting/src/test/java/org/jboss/remoting3/test/RemotingLocalTestBase.java
remoting3/trunk/jboss-remoting/src/test/java/org/jboss/remoting3/test/RemotingRootTestBase.java
Modified:
remoting3/trunk/jboss-remoting/src/test/java/org/jboss/remoting3/test/RemotingTestBase.java
Log:
JBREM-1228: Broke RemotingTestBase into RemotingRootTestBase, RemotingLocalTestBase, and
RemotingTestBase.
Added:
remoting3/trunk/jboss-remoting/src/test/java/org/jboss/remoting3/test/RemotingLocalTestBase.java
===================================================================
---
remoting3/trunk/jboss-remoting/src/test/java/org/jboss/remoting3/test/RemotingLocalTestBase.java
(rev 0)
+++
remoting3/trunk/jboss-remoting/src/test/java/org/jboss/remoting3/test/RemotingLocalTestBase.java 2010-09-07
19:27:15 UTC (rev 6084)
@@ -0,0 +1,170 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2010, JBoss Inc., and individual contributors as indicated
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site:
http://www.fsf.org.
+ */
+
+package org.jboss.remoting3.test;
+
+
+import static org.testng.Assert.assertEquals;
+import static org.testng.Assert.assertNotNull;
+import static org.testng.Assert.assertNotSame;
+import static org.testng.Assert.assertSame;
+import static org.testng.Assert.assertFalse;
+import static org.testng.Assert.assertTrue;
+
+import java.io.IOException;
+import java.util.concurrent.ArrayBlockingQueue;
+import java.util.concurrent.ThreadPoolExecutor;
+import java.util.concurrent.TimeUnit;
+
+import org.jboss.marshalling.river.RiverProviderDescriptor;
+import org.jboss.remoting3.ClientContext;
+import org.jboss.remoting3.ClientListener;
+import org.jboss.remoting3.CloseHandler;
+import org.jboss.remoting3.Endpoint;
+import org.jboss.remoting3.Registration;
+import org.jboss.remoting3.Remoting;
+import org.jboss.remoting3.RequestListener;
+import org.jboss.remoting3.Endpoint.ServiceBuilder;
+import org.jboss.remoting3.spi.ProtocolServiceType;
+import
org.jboss.remoting3.test.ClientServerRemoteConfigurationTestCase.TestRequestListener;
+import org.jboss.xnio.AcceptingServer;
+import org.jboss.xnio.IoFuture;
+import org.jboss.xnio.OptionMap;
+import org.jboss.xnio.Xnio;
+import org.jboss.xnio.IoFuture.Status;
+import org.jboss.xnio.log.Logger;
+
+/**
+ *
+ * @author <a href="ron.sigal(a)jboss.com">Ron Sigal</a>
+ * @version $Revision: 1.1 $
+ * <p>
+ * Copyright Jun 23, 2010
+ */
+@SuppressWarnings("unused")
+public class RemotingLocalTestBase extends RemotingRootTestBase {
+ private static final Logger log = Logger.getLogger(RemotingLocalTestBase.class);
+
+ protected ServerPackage setupServer(ServerPackage sp, int id,
RequestListener<Object, Object> requestListener, String serviceType, String
instanceName) throws IOException {
+
+ if (sp == null) {
+ sp = new ServerPackage();
+ }
+
+ // Create and configure endpoint.
+ if (sp.endpoint == null) {
+ if (sp.executor == null) {
+ sp.executor = new ThreadPoolExecutor(8, 64, 30, TimeUnit.SECONDS, new
ArrayBlockingQueue<Runnable>(64));
+ }
+ sp.endpoint = Remoting.createEndpoint("endpoint" + id, sp.executor,
OptionMap.EMPTY);
+ final Registration reg =
sp.endpoint.addProtocolService(ProtocolServiceType.MARSHALLER_PROVIDER_DESCRIPTOR,
"river", new RiverProviderDescriptor());
+ sp.endpoint.addCloseHandler(new CloseHandler<Endpoint>() {
+ public void handleClose(final Endpoint closed) {
+ reg.close();
+ }});
+ addConnectionProvider(sp, id);
+ }
+
+ // Register service with endpoint.
+ sp.requestListener = requestListener;
+ if (sp.registration == null) {
+ ServiceBuilder<Object, Object> sb =
sp.endpoint.serviceBuilder(Object.class, Object.class);
+ sb.setInstanceName(instanceName).setServiceType(serviceType);
+ sb.setClientListener(new TestClientListener(sp.requestListener));
+ sp.registration = sb.register();
+ }
+ return sp;
+ }
+
+ protected void addConnectionProvider(ServerPackage sp, int id) throws IOException {
+ }
+
+ public static class ServerPackage {
+ public ThreadPoolExecutor executor;
+ public Endpoint endpoint;
+ public Xnio xnio;
+ public AcceptingServer<?, ?, ?> tcpServer;
+ public RequestListener<Object, Object> requestListener;
+ public Registration registration;
+ public int port;
+
+ public ServerPackage(ThreadPoolExecutor executor, Endpoint endpoint, Xnio xnio,
AcceptingServer<?, ?, ?> tcpServer, RequestListener<Object, Object>
requestListener, Registration registration, int port) {
+ this.executor = executor;
+ this.endpoint = endpoint;
+ this.xnio = xnio;
+ this.tcpServer = tcpServer;
+ this.requestListener = requestListener;
+ this.registration = registration;
+ this.port = port;
+ }
+
+ public ServerPackage() {
+ }
+
+ public void close() {
+ if (executor != null) {
+ executor.shutdown();
+ }
+ if (endpoint != null) {
+ try {
+ endpoint.close();
+ } catch (IOException e) {
+ log.error("unable to close " + endpoint);
+ }
+ }
+ if (xnio != null) {
+ try {
+ xnio.close();
+ } catch (IOException e) {
+ log.error("unable to close " + xnio);
+ }
+ }
+ if (tcpServer != null) {
+ try {
+ tcpServer.close();
+ } catch (IOException e) {
+ log.error("unable to close " + tcpServer);
+ }
+ }
+ if (registration != null) {
+ registration.close();
+ }
+ }
+ }
+
+ static class TestClientListener implements ClientListener<Object, Object> {
+ private RequestListener<Object, Object> requestListener;
+
+ TestClientListener(RequestListener<Object, Object> requestListener) {
+ this.requestListener = requestListener;
+ }
+
+ public RequestListener<Object, Object> handleClientOpen(final ClientContext
clientContext, final OptionMap optionMap) {
+ clientContext.addCloseHandler(new CloseHandler<ClientContext>() {
+ public void handleClose(final ClientContext closed) {
+ log.debug("Client closed");
+ }
+ });
+ return requestListener;
+ }
+ }
+}
Added:
remoting3/trunk/jboss-remoting/src/test/java/org/jboss/remoting3/test/RemotingRootTestBase.java
===================================================================
---
remoting3/trunk/jboss-remoting/src/test/java/org/jboss/remoting3/test/RemotingRootTestBase.java
(rev 0)
+++
remoting3/trunk/jboss-remoting/src/test/java/org/jboss/remoting3/test/RemotingRootTestBase.java 2010-09-07
19:27:15 UTC (rev 6084)
@@ -0,0 +1,102 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2010, JBoss Inc., and individual contributors as indicated
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site:
http://www.fsf.org.
+ */
+
+package org.jboss.remoting3.test;
+
+
+import static org.testng.Assert.assertEquals;
+import static org.testng.Assert.assertNotNull;
+import static org.testng.Assert.assertNotSame;
+import static org.testng.Assert.assertSame;
+import static org.testng.Assert.assertFalse;
+import static org.testng.Assert.assertTrue;
+
+import java.io.IOException;
+import java.util.concurrent.ArrayBlockingQueue;
+import java.util.concurrent.ThreadPoolExecutor;
+import java.util.concurrent.TimeUnit;
+
+import org.jboss.marshalling.river.RiverProviderDescriptor;
+import org.jboss.remoting3.ClientContext;
+import org.jboss.remoting3.ClientListener;
+import org.jboss.remoting3.CloseHandler;
+import org.jboss.remoting3.Endpoint;
+import org.jboss.remoting3.Registration;
+import org.jboss.remoting3.Remoting;
+import org.jboss.remoting3.RequestListener;
+import org.jboss.remoting3.Endpoint.ServiceBuilder;
+import org.jboss.remoting3.spi.ProtocolServiceType;
+import
org.jboss.remoting3.test.ClientServerRemoteConfigurationTestCase.TestRequestListener;
+import org.jboss.xnio.AcceptingServer;
+import org.jboss.xnio.IoFuture;
+import org.jboss.xnio.OptionMap;
+import org.jboss.xnio.Xnio;
+import org.jboss.xnio.IoFuture.Status;
+import org.jboss.xnio.log.Logger;
+
+/**
+ *
+ * @author <a href="ron.sigal(a)jboss.com">Ron Sigal</a>
+ * @version $Revision: 1.1 $
+ * <p>
+ * Copyright Jun 23, 2010
+ */
+@SuppressWarnings("unused")
+public class RemotingRootTestBase {
+ private static final Logger log = Logger.getLogger(RemotingRootTestBase.class);
+
+ protected String getName() {
+ final StackTraceElement e = new Throwable().getStackTrace()[1];
+ return e.getMethodName();
+ }
+
+ protected static void enter() {
+ final StackTraceElement e = new Throwable().getStackTrace()[1];
+ log.info("Entering: %s#%s", e.getClassName(), e.getMethodName());
+ }
+
+ protected static void exit() {
+ final StackTraceElement e = new Throwable().getStackTrace()[1];
+ log.info("Exiting: %s#%s", e.getClassName(), e.getMethodName());
+
log.info("-------------------------------------------------------------");
+ }
+
+ protected static <T> T getFutureResult(IoFuture<T> future, String
errorMessage) throws IOException {
+ return getFutureResult(future, 5000, errorMessage);
+ }
+
+ protected static <T> T getFutureResult(IoFuture<T> future, int timeout,
String errorMessage) throws IOException {
+ Status status = null;
+ switch (status = future.await(timeout, TimeUnit.MILLISECONDS)) {
+ case DONE: {
+ return future.get();
+ }
+ case FAILED: {
+ log.error(errorMessage);
+ throw future.getException();
+ }
+ default: {
+ throw new RuntimeException("unexpected future state: " + status);
+ }
+ }
+ }
+}
Modified:
remoting3/trunk/jboss-remoting/src/test/java/org/jboss/remoting3/test/RemotingTestBase.java
===================================================================
---
remoting3/trunk/jboss-remoting/src/test/java/org/jboss/remoting3/test/RemotingTestBase.java 2010-09-07
17:38:21 UTC (rev 6083)
+++
remoting3/trunk/jboss-remoting/src/test/java/org/jboss/remoting3/test/RemotingTestBase.java 2010-09-07
19:27:15 UTC (rev 6084)
@@ -22,11 +22,49 @@
package org.jboss.remoting3.test;
+
+import static org.testng.Assert.assertEquals;
+import static org.testng.Assert.assertNotNull;
+import static org.testng.Assert.assertNotSame;
+import static org.testng.Assert.assertSame;
+import static org.testng.Assert.assertFalse;
+import static org.testng.Assert.assertTrue;
+
import java.io.IOException;
+import java.net.InetSocketAddress;
+import java.net.URI;
+import java.net.URISyntaxException;
+import java.util.Properties;
+import java.util.concurrent.ArrayBlockingQueue;
+import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
+import org.jboss.marshalling.river.RiverProviderDescriptor;
+import org.jboss.remoting3.CloseHandler;
+import org.jboss.remoting3.Connection;
+import org.jboss.remoting3.Endpoint;
+import org.jboss.remoting3.Registration;
+import org.jboss.remoting3.Remoting;
+import org.jboss.remoting3.RequestListener;
+import org.jboss.remoting3.Endpoint.ServiceBuilder;
+import org.jboss.remoting3.remote.RemoteProtocolDescriptor;
+import org.jboss.remoting3.security.SimpleServerAuthenticationProvider;
+import org.jboss.remoting3.spi.ConnectionProviderFactory;
+import org.jboss.remoting3.spi.NetworkServerProvider;
+import org.jboss.remoting3.spi.ProtocolServiceType;
+import org.jboss.remoting3.spi.RemotingServiceDescriptor;
+import
org.jboss.remoting3.test.ClientServerRemoteConfigurationTestCase.TestRequestListener;
+import org.jboss.remoting3.test.RemotingLocalTestBase.ServerPackage;
+import org.jboss.xnio.AcceptingServer;
+import org.jboss.xnio.ChannelListener;
import org.jboss.xnio.IoFuture;
+import org.jboss.xnio.OptionMap;
+import org.jboss.xnio.Options;
+import org.jboss.xnio.TcpServer;
+import org.jboss.xnio.Xnio;
import org.jboss.xnio.IoFuture.Status;
+import org.jboss.xnio.channels.BoundChannel;
+import org.jboss.xnio.channels.ConnectedStreamChannel;
import org.jboss.xnio.log.Logger;
/**
@@ -36,42 +74,65 @@
* <p>
* Copyright Jun 23, 2010
*/
-public class RemotingTestBase {
+@SuppressWarnings("unused")
+public class RemotingTestBase extends RemotingLocalTestBase {
private static final Logger log = Logger.getLogger(RemotingTestBase.class);
- protected String getName() {
- final StackTraceElement e = new Throwable().getStackTrace()[1];
- return e.getMethodName();
+ protected ServerPackage setupServer(ServerPackage sp, int id,
RequestListener<Object, Object> requestListener, String serviceType, String
instanceName) throws IOException {
+
+ ServerPackage sp2 = super.setupServer(sp, id, requestListener, serviceType,
instanceName);
+
+// Set up XNIO layer.
+ OptionMap serverOptions = OptionMap.builder().setSequence(Options.SASL_MECHANISMS,
"EXTERNAL", "DIGEST-MD5").getMap();
+ SimpleServerAuthenticationProvider authenticationProvider = new
SimpleServerAuthenticationProvider();
+ authenticationProvider.addUser("user", sp2.endpoint.getName(),
"password".toCharArray());
+ NetworkServerProvider provider =
sp2.endpoint.getConnectionProviderInterface(getRemotingScheme(),
NetworkServerProvider.class);
+ ChannelListener<ConnectedStreamChannel<InetSocketAddress>> listener =
provider.getServerListener(serverOptions, authenticationProvider);
+ if (sp2.xnio == null) {
+ sp2.xnio = Xnio.getInstance(String.valueOf(id));
+ }
+ if (sp2.tcpServer == null) {
+ createTcpServer(sp2, listener);
+ }
+
+ return sp2;
}
- protected static void enter() {
- final StackTraceElement e = new Throwable().getStackTrace()[1];
- log.info("Entering: %s#%s", e.getClassName(), e.getMethodName());
+ protected void addConnectionProvider(ServerPackage sp, int id) throws IOException {
+ Properties props = new Properties();
+ props.put("remote.xnio.provider", String.valueOf(id));
+ final Registration reg = sp.endpoint.addConnectionProvider(getRemotingScheme(),
getProtocolDescriptor().getService(props));
+ sp.endpoint.addCloseHandler(new CloseHandler<Endpoint>() {
+ public void handleClose(final Endpoint closed) {
+ reg.close();
+ }});
}
-
- protected static void exit() {
- final StackTraceElement e = new Throwable().getStackTrace()[1];
- log.info("Exiting: %s#%s", e.getClassName(), e.getMethodName());
-
log.info("-------------------------------------------------------------");
+
+ protected String getRemotingScheme() {
+ return "remote";
}
- protected static <T> T getFutureResult(IoFuture<T> future, String
errorMessage) throws IOException {
- return getFutureResult(future, 5000, errorMessage);
+ protected RemotingServiceDescriptor<ConnectionProviderFactory>
getProtocolDescriptor() {
+ return new RemoteProtocolDescriptor();
}
- protected static <T> T getFutureResult(IoFuture<T> future, int timeout,
String errorMessage) throws IOException {
- Status status = null;
- switch (status = future.await(timeout, TimeUnit.MILLISECONDS)) {
- case DONE: {
- return future.get();
- }
- case FAILED: {
- log.error(errorMessage);
- throw future.getException();
- }
- default: {
- throw new RuntimeException("unexpected future state: " + status);
- }
- }
+ protected OptionMap getConnectionOptionMap() {
+ return OptionMap.EMPTY;
}
+
+ protected void createTcpServer(ServerPackage sp,
ChannelListener<ConnectedStreamChannel<InetSocketAddress>> listener) throws
IOException {
+ log.debug(this + " creating TcpServer");
+ TcpServer tcpServer = sp.xnio.createTcpServer(listener, OptionMap.EMPTY);
+ IoFuture<? extends BoundChannel<InetSocketAddress>> future =
tcpServer.bind(new InetSocketAddress("localhost", 0));
+ getFutureResult(future, "unable to bind " + sp.tcpServer);
+ sp.tcpServer = tcpServer;
+ sp.port = tcpServer.getChannels().iterator().next().getLocalAddress().getPort();
+ }
+
+ protected Connection setupConnection(ServerPackage localServerPackage, ServerPackage
remoteServerPackage) throws IOException, URISyntaxException {
+ Endpoint endpoint = localServerPackage.endpoint;
+ URI uri = new URI(getRemotingScheme() + "://localhost:" +
remoteServerPackage.port);
+ Connection connection = getFutureResult(endpoint.connect(uri,
getConnectionOptionMap(), "user", null, "password".toCharArray()),
"unable to connect to " + uri);
+ return connection;
+ }
}