Author: ron.sigal(a)jboss.com
Date: 2010-09-24 11:59:42 -0400 (Fri, 24 Sep 2010)
New Revision: 6114
Added:
remoting3/trunk/jboss-remoting/src/test/java/org/jboss/remoting3/test/remote/
remoting3/trunk/jboss-remoting/src/test/java/org/jboss/remoting3/test/remote/RemoteMessageHandlerTestCase.java
Log:
JBREM-1228: Moved a couple of methods from RemotingTestBase to RemotingLocalTestBase.
Added:
remoting3/trunk/jboss-remoting/src/test/java/org/jboss/remoting3/test/remote/RemoteMessageHandlerTestCase.java
===================================================================
---
remoting3/trunk/jboss-remoting/src/test/java/org/jboss/remoting3/test/remote/RemoteMessageHandlerTestCase.java
(rev 0)
+++
remoting3/trunk/jboss-remoting/src/test/java/org/jboss/remoting3/test/remote/RemoteMessageHandlerTestCase.java 2010-09-24
15:59:42 UTC (rev 6114)
@@ -0,0 +1,346 @@
+/*
+ * 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.remote;
+
+import static org.testng.Assert.fail;
+
+import java.io.IOException;
+import java.io.ObjectInputStream;
+import java.io.ObjectOutputStream;
+import java.io.Serializable;
+import java.net.URISyntaxException;
+import java.util.Date;
+import java.util.Random;
+
+import org.jboss.remoting3.Client;
+import org.jboss.remoting3.Connection;
+import org.jboss.remoting3.RemoteExecutionException;
+import org.jboss.remoting3.RequestContext;
+import org.jboss.remoting3.RequestListener;
+import org.jboss.remoting3.ServiceOpenException;
+import org.jboss.remoting3.test.RemotingTestBase;
+import org.jboss.xnio.Option;
+import org.jboss.xnio.OptionMap;
+import org.jboss.xnio.log.Logger;
+import org.testng.annotations.AfterMethod;
+import org.testng.annotations.BeforeMethod;
+import org.testng.annotations.Test;
+
+/**
+ * @author <a href="ron.sigal(a)jboss.com">Ron Sigal</a>
+ * @version $Revision: 1.1 $
+ * <p>
+ * Copyright September 16, 2010
+ */
+@Test(suiteName = "RemoteMessageHandler")
+public class RemoteMessageHandlerTestCase extends RemotingTestBase {
+
+ private static final Option<BadClass> BAD_OPTION =
Option.simple(RemoteMessageHandlerTestCase.class, "BAD_OPTION",
BadClass.class);
+ private static final String SERVICE_TYPE = "servicetyype";
+ private static final String INSTANCE_NAME = "instancename";
+ private static final String WRITE_EXCEPTION_MSG = "can't write this";
+
+ private static final Logger log =
Logger.getLogger(RemoteMessageHandlerTestCase.class);
+ private static int counter = new Random(new Date().getTime()).nextInt();
+
+ private boolean useBadOption;
+
+ @BeforeMethod
+ public void setUp() {
+ useBadOption = false;
+ }
+
+ @AfterMethod
+ public void tearDown() throws IOException {
+ }
+
+ @Test
+ public void testServiceError() throws Exception {
+ enter();
+
+ ServerPackage sp0 = null;
+ ServerPackage sp1 = null;
+ Connection connection = null;
+ Client<Object, Object> client = null;
+
+ try {
+ useBadOption = true;
+ int id0 = counter++;
+ int id1 = counter++;
+
+ // Set up services.
+ sp0 = setupServer(null, id0, new TestRequestListener(), SERVICE_TYPE,
INSTANCE_NAME);
+ sp1 = setupServer(null, id1, new TestRequestListener(), SERVICE_TYPE,
INSTANCE_NAME);
+
+ // Set up connection and client.
+ connection = setupConnection(sp0, sp1);
+ try {
+ client = setupClient(connection, SERVICE_TYPE, INSTANCE_NAME);
+ } catch (ServiceOpenException e) {
+ if ("Remote side failed to open service".equals(e.getMessage())) {
+ log.info(this + " got expected ServiceOpenException(\"Remote
side failed to open service\"");
+ } else {
+ log.error(this + " got ServiceOpenException but didn't get
expected message \"Remote side failed to open service\": " +
e.getMessage());
+ fail(" got ServiceOpenException but didn't get expected message
\"Remote side failed to open service\": " + e.getMessage());
+ }
+ } catch (Throwable t) {
+ log.error("expected ServiceOpenException(\"Remote side failed to
open service\"");
+ fail("expected ServiceOpenException(\"Remote side failed to open
service\"");
+ }
+
+ log.info(getName() + " PASSES");
+ } finally {
+ if (client != null) {
+ client.close();
+ }
+ if (connection != null) {
+ connection.close();
+ }
+ if (sp0 != null) {
+ sp0.close();
+ }
+ if (sp1 != null) {
+ sp1.close();
+ }
+ exit();
+ }
+ }
+
+ @Test
+ public void testRequestAbort() throws Exception {
+ enter();
+
+ ServerPackage sp0 = null;
+ ServerPackage sp1 = null;
+ Connection connection = null;
+ Client<Object, Object> client = null;
+
+ try {
+ int id0 = counter++;
+ int id1 = counter++;
+
+ // Set up services.
+ sp0 = setupServer(null, id0, new TestRequestListener(), SERVICE_TYPE,
INSTANCE_NAME);
+ sp1 = setupServer(null, id1, new TestRequestListener(), SERVICE_TYPE,
INSTANCE_NAME);
+
+ // Set up connection and client.
+ connection = setupConnection(sp0, sp1);
+ client = setupClient(connection, SERVICE_TYPE, INSTANCE_NAME);
+
+ try {
+ client.invoke(new UnwriteableObject());
+ } catch (TestException e) {
+ if (WRITE_EXCEPTION_MSG.equals(e.getMessage())) {
+ log.info(this + " got expected TestException");
+ } else {
+ fail(" expected TestException message: " +
WRITE_EXCEPTION_MSG);
+ }
+ } catch (Throwable t) {
+ fail(" expected TestException(" + WRITE_EXCEPTION_MSG +
")");
+ }
+
+ log.info(getName() + " PASSES");
+ } finally {
+ if (client != null) {
+ client.close();
+ }
+ if (connection != null) {
+ connection.close();
+ }
+ if (sp0 != null) {
+ sp0.close();
+ }
+ if (sp1 != null) {
+ sp1.close();
+ }
+ exit();
+ }
+ }
+
+ @Test
+ public void testLongMessage() throws Exception {
+ enter();
+
+ ServerPackage sp0 = null;
+ ServerPackage sp1 = null;
+ Connection connection = null;
+ Client<Object, Object> client = null;
+
+ try {
+ int id0 = counter++;
+ int id1 = counter++;
+
+ // Set up services.
+ sp0 = setupServer(null, id0, new TestRequestListener(), SERVICE_TYPE,
INSTANCE_NAME);
+ sp1 = setupServer(null, id1, new TestRequestListener(), SERVICE_TYPE,
INSTANCE_NAME);
+
+ // Set up connection and client.
+ connection = setupConnection(sp0, sp1);
+ client = setupClient(connection, SERVICE_TYPE, INSTANCE_NAME);
+
+ // Send long message.
+ log.info(this + " sending long message");
+// byte[] bytes = new byte[1024 * 8 - 5];
+ byte[] bytes = new byte[1024 * 1024 * 16];
+ for (int i = 0; i < bytes.length; i++) {
+ bytes[i] = 0x01;
+ }
+ client.invoke(bytes);
+ log.info(this + " sent long message");
+
+ log.info(getName() + " PASSES");
+ } finally {
+ if (client != null) {
+ client.close();
+ }
+ if (connection != null) {
+ connection.close();
+ }
+ if (sp0 != null) {
+ sp0.close();
+ }
+ if (sp1 != null) {
+ sp1.close();
+ }
+ exit();
+ }
+ }
+
+// @Test
+// public void testReplyExceptionAbort() throws Exception {
+// enter();
+//
+// ServerPackage sp0 = null;
+// ServerPackage sp1 = null;
+// Connection connection = null;
+// Client<Object, Object> client = null;
+//
+// try {
+// int id0 = counter++;
+// int id1 = counter++;
+//
+// // Set up services.
+// sp0 = setupServer(null, id0, new TestRequestListener(), SERVICE_TYPE,
INSTANCE_NAME);
+// sp1 = setupServer(null, id1, new TestRequestListener(), SERVICE_TYPE,
INSTANCE_NAME);
+//
+// // Set up connection and client.
+// connection = setupConnection(sp0, sp1);
+// client = setupClient(connection, SERVICE_TYPE, INSTANCE_NAME);
+//
+// client.invoke(new TestException("testReplyExceptionAbort"));
+//
+// log.info(getName() + " PASSES");
+// } finally {
+// if (client != null) {
+// client.close();
+// }
+// if (connection != null) {
+// connection.close();
+// }
+// if (sp0 != null) {
+// sp0.close();
+// }
+// if (sp1 != null) {
+// sp1.close();
+// }
+// exit();
+// }
+// }
+
+ public Connection setupConnection(ServerPackage localServerPackage, ServerPackage
remoteServerPackage) throws IOException, URISyntaxException {
+ return super.setupConnection(localServerPackage, remoteServerPackage);
+ }
+
+ protected OptionMap getConnectionOptionMap() {
+ if (useBadOption) {
+ return OptionMap.builder().set(BAD_OPTION, new BadClass()).getMap();
+ } else {
+ return OptionMap.EMPTY;
+ }
+ }
+
+ protected OptionMap getClientOptionMap() {
+ if (useBadOption) {
+ return OptionMap.builder().set(BAD_OPTION, new BadClass()).getMap();
+ } else {
+ return OptionMap.EMPTY;
+ }
+ }
+
+ public Client<Object, Object> setupClient(Connection connection, String
serviceType, String instanceName) throws IOException, URISyntaxException {
+ Client<Object, Object> client =
getFutureResult(connection.openClient(serviceType, instanceName, Object.class,
Object.class, getClientOptionMap()), 1000000, "unable to open client to " +
serviceType + ":" + instanceName);
+ return client;
+ }
+
+ static class TestRequestListener implements RequestListener<Object, Object> {
+ public void handleRequest(RequestContext<Object> context, Object request)
throws RemoteExecutionException {
+ try {
+ context.sendReply(request);
+ } catch (IOException e) {
+ log.error(this + " unable to send reply", e);
+ e.printStackTrace();
+ }
+ }
+ }
+
+ static class BadClass implements Serializable {
+ private static final long serialVersionUID = -2289052108857883020L;
+
+ private void readObject(ObjectInputStream stream) throws IOException,
ClassNotFoundException {
+ log.info(this + ".readObject()");
+ stream.defaultReadObject();
+ throw new RuntimeException("BadClass.readObject()");
+ }
+ }
+
+ static class UnwriteableObject implements Serializable {
+ private static final long serialVersionUID = 7626170116737760955L;
+
+ private void writeObject(ObjectOutputStream stream) throws IOException {
+ throw new TestException(WRITE_EXCEPTION_MSG);
+ }
+ }
+
+ static class TestException extends IOException {
+ private static final long serialVersionUID = -5729262735391226111L;
+ private int counter;
+
+ public TestException(String msg) {
+ super(msg);
+ }
+
+ private void readObject(ObjectInputStream stream) throws IOException,
ClassNotFoundException {
+ log.info(this + ".readObject()");
+ stream.defaultReadObject();
+ counter++;
+ }
+
+ private void writeObject(ObjectOutputStream stream) throws IOException {
+ log.info(this + ".writeObject()");
+ if (counter > 0) {
+ throw new TestException(WRITE_EXCEPTION_MSG);
+ }
+ stream.defaultWriteObject();
+ }
+ }
+}