From jboss-remoting-commits at lists.jboss.org Fri Sep 24 11:59:42 2010 Content-Type: multipart/mixed; boundary="===============9145244895941948119==" MIME-Version: 1.0 From: jboss-remoting-commits at lists.jboss.org To: jboss-remoting-commits at lists.jboss.org Subject: [jboss-remoting-commits] JBoss Remoting SVN: r6114 - in remoting3/trunk/jboss-remoting/src/test/java/org/jboss/remoting3/test: remote and 1 other directory. Date: Fri, 24 Sep 2010 11:59:42 -0400 Message-ID: <201009241559.o8OFxgt1004662@svn01.web.mwc.hst.phx2.redhat.com> --===============9145244895941948119== Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable 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/re= mote/ remoting3/trunk/jboss-remoting/src/test/java/org/jboss/remoting3/test/re= mote/RemoteMessageHandlerTestCase.java Log: JBREM-1228: Moved a couple of methods from RemotingTestBase to RemotingLoca= lTestBase. Added: remoting3/trunk/jboss-remoting/src/test/java/org/jboss/remoting3/tes= t/remote/RemoteMessageHandlerTestCase.java =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D --- remoting3/trunk/jboss-remoting/src/test/java/org/jboss/remoting3/test/r= emote/RemoteMessageHandlerTestCase.java (rev 0) +++ remoting3/trunk/jboss-remoting/src/test/java/org/jboss/remoting3/test/r= emote/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 Ron Sigal + * @version $Revision: 1.1 $ + *

+ * Copyright September 16, 2010 + */ +(a)Test(suiteName =3D "RemoteMessageHandler") +public class RemoteMessageHandlerTestCase extends RemotingTestBase { + + private static final Option BAD_OPTION =3D Option.simple(Remo= teMessageHandlerTestCase.class, "BAD_OPTION", BadClass.class); + private static final String SERVICE_TYPE =3D "servicetyype"; + private static final String INSTANCE_NAME =3D "instancename"; + private static final String WRITE_EXCEPTION_MSG =3D "can't write this"; + = + private static final Logger log =3D Logger.getLogger(RemoteMessageHandl= erTestCase.class); + private static int counter =3D new Random(new Date().getTime()).nextInt= (); + = + private boolean useBadOption; + = + @BeforeMethod + public void setUp() { + useBadOption =3D false; + } + + @AfterMethod + public void tearDown() throws IOException { + } + + @Test + public void testServiceError() throws Exception { + enter(); + = + ServerPackage sp0 =3D null; + ServerPackage sp1 =3D null; + Connection connection =3D null; + Client client =3D null; + = + try { + useBadOption =3D true; + int id0 =3D counter++; + int id1 =3D counter++; + + // Set up services. + sp0 =3D setupServer(null, id0, new TestRequestListener(), SERVICE= _TYPE, INSTANCE_NAME); + sp1 =3D setupServer(null, id1, new TestRequestListener(), SERVICE= _TYPE, INSTANCE_NAME); + + // Set up connection and client. + connection =3D setupConnection(sp0, sp1); + try { + client =3D 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(\"Remot= e 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 mes= sage \"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 op= en service\""); + } + = + log.info(getName() + " PASSES"); + } finally { + if (client !=3D null) { + client.close(); + } + if (connection !=3D null) { + connection.close(); + } + if (sp0 !=3D null) { + sp0.close(); + } + if (sp1 !=3D null) { + sp1.close(); + } + exit(); + } + } + = + @Test + public void testRequestAbort() throws Exception { + enter(); + = + ServerPackage sp0 =3D null; + ServerPackage sp1 =3D null; + Connection connection =3D null; + Client client =3D null; + = + try { + int id0 =3D counter++; + int id1 =3D counter++; + + // Set up services. + sp0 =3D setupServer(null, id0, new TestRequestListener(), SERVICE= _TYPE, INSTANCE_NAME); + sp1 =3D setupServer(null, id1, new TestRequestListener(), SERVICE= _TYPE, INSTANCE_NAME); + + // Set up connection and client. + connection =3D setupConnection(sp0, sp1); + client =3D 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 !=3D null) { + client.close(); + } + if (connection !=3D null) { + connection.close(); + } + if (sp0 !=3D null) { + sp0.close(); + } + if (sp1 !=3D null) { + sp1.close(); + } + exit(); + } + } + = + @Test + public void testLongMessage() throws Exception { + enter(); + = + ServerPackage sp0 =3D null; + ServerPackage sp1 =3D null; + Connection connection =3D null; + Client client =3D null; + = + try { + int id0 =3D counter++; + int id1 =3D counter++; + + // Set up services. + sp0 =3D setupServer(null, id0, new TestRequestListener(), SERVICE= _TYPE, INSTANCE_NAME); + sp1 =3D setupServer(null, id1, new TestRequestListener(), SERVICE= _TYPE, INSTANCE_NAME); + + // Set up connection and client. + connection =3D setupConnection(sp0, sp1); + client =3D setupClient(connection, SERVICE_TYPE, INSTANCE_NAME); + = + // Send long message. + log.info(this + " sending long message"); +// byte[] bytes =3D new byte[1024 * 8 - 5]; + byte[] bytes =3D new byte[1024 * 1024 * 16]; + for (int i =3D 0; i < bytes.length; i++) { + bytes[i] =3D 0x01; + } + client.invoke(bytes); + log.info(this + " sent long message"); + = + log.info(getName() + " PASSES"); + } finally { + if (client !=3D null) { + client.close(); + } + if (connection !=3D null) { + connection.close(); + } + if (sp0 !=3D null) { + sp0.close(); + } + if (sp1 !=3D null) { + sp1.close(); + } + exit(); + } + } + = +// @Test +// public void testReplyExceptionAbort() throws Exception { +// enter(); +// = +// ServerPackage sp0 =3D null; +// ServerPackage sp1 =3D null; +// Connection connection =3D null; +// Client client =3D null; +// = +// try { +// int id0 =3D counter++; +// int id1 =3D counter++; +// +// // Set up services. +// sp0 =3D setupServer(null, id0, new TestRequestListener(), SERVI= CE_TYPE, INSTANCE_NAME); +// sp1 =3D setupServer(null, id1, new TestRequestListener(), SERVI= CE_TYPE, INSTANCE_NAME); +// +// // Set up connection and client. +// connection =3D setupConnection(sp0, sp1); +// client =3D setupClient(connection, SERVICE_TYPE, INSTANCE_NAME); +// +// client.invoke(new TestException("testReplyExceptionAbort")); +// = +// log.info(getName() + " PASSES"); +// } finally { +// if (client !=3D null) { +// client.close(); +// } +// if (connection !=3D null) { +// connection.close(); +// } +// if (sp0 !=3D null) { +// sp0.close(); +// } +// if (sp1 !=3D null) { +// sp1.close(); +// } +// exit(); +// } +// } + = + public Connection setupConnection(ServerPackage localServerPackage, Ser= verPackage 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 setupClient(Connection connection, String= serviceType, String instanceName) throws IOException, URISyntaxException { + Client client =3D getFutureResult(connection.openCli= ent(serviceType, instanceName, Object.class, Object.class, getClientOptionM= ap()), 1000000, "unable to open client to " + serviceType + ":" + instanceN= ame); + return client; + } + = + static class TestRequestListener implements RequestListener { + public void handleRequest(RequestContext context, Object req= uest) 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 =3D -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 =3D 7626170116737760955L; + = + private void writeObject(ObjectOutputStream stream) throws IOExcepti= on { + throw new TestException(WRITE_EXCEPTION_MSG); + } + } + = + static class TestException extends IOException { + private static final long serialVersionUID =3D -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 IOExcepti= on { + log.info(this + ".writeObject()"); + if (counter > 0) { + throw new TestException(WRITE_EXCEPTION_MSG); = + } + stream.defaultWriteObject(); + } + } +} --===============9145244895941948119==--