Author: ron.sigal(a)jboss.com
Date: 2010-09-15 18:09:27 -0400 (Wed, 15 Sep 2010)
New Revision: 6103
Added:
remoting3/trunk/jboss-remoting/src/test/java/org/jboss/remoting3/test/RemoteClassTableTestCase.java
Log:
JBREM-1228: New unit test.
Added:
remoting3/trunk/jboss-remoting/src/test/java/org/jboss/remoting3/test/RemoteClassTableTestCase.java
===================================================================
---
remoting3/trunk/jboss-remoting/src/test/java/org/jboss/remoting3/test/RemoteClassTableTestCase.java
(rev 0)
+++
remoting3/trunk/jboss-remoting/src/test/java/org/jboss/remoting3/test/RemoteClassTableTestCase.java 2010-09-15
22:09:27 UTC (rev 6103)
@@ -0,0 +1,283 @@
+/*
+ * 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.assertFalse;
+import static org.testng.Assert.assertTrue;
+
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.util.Date;
+import java.util.Random;
+
+import org.jboss.marshalling.ByteInput;
+import org.jboss.marshalling.ByteOutput;
+import org.jboss.marshalling.Marshaller;
+import org.jboss.marshalling.MarshallerFactory;
+import org.jboss.marshalling.Marshalling;
+import org.jboss.marshalling.MarshallingConfiguration;
+import org.jboss.marshalling.Unmarshaller;
+import org.jboss.marshalling.river.RiverMarshallerFactory;
+import org.jboss.remoting3.Client;
+import org.jboss.remoting3.Connection;
+import org.jboss.remoting3.Registration;
+import org.jboss.remoting3.RemoteClassTableWrapper;
+import org.jboss.remoting3.RemoteExecutionException;
+import org.jboss.remoting3.RequestContext;
+import org.jboss.remoting3.RequestListener;
+import org.jboss.remoting3.Endpoint.ServiceBuilder;
+import org.jboss.remoting3.service.classtable.ClassLookupRequest;
+import org.jboss.remoting3.service.classtable.ClassLookupResponse;
+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 7, 2010
+ */
+@Test(suiteName = "RemoteClassTable")
+public class RemoteClassTableTestCase extends RemotingTestBase {
+
+ private static final String RCT_INSTANCE_NAME = "rct_instancename";
+ private static final String RCT_SERVICE_TYPE = "rct_servicetype";
+ private static final Logger log = Logger.getLogger(RemoteClassTableTestCase.class);
+ private static int counter = Math.abs(new Random(new Date().getTime()).nextInt());
+
+ @BeforeMethod
+ public void setUp() {
+ }
+
+ @AfterMethod
+ public void tearDown() throws IOException {
+ }
+
+ @Test
+ public void testRemoteClassTableStandalone() throws Exception {
+ enter();
+
+ int idSender = counter++;
+ int idReceiver = counter++;
+ ServerPackage spSender = null;
+ ServerPackage spReceiver = null;
+ Registration reg = null;
+
+ try {
+
+ RequestListener<Object, Object> dummyRequestListener = new
RequestListener<Object, Object>() {
+ public void handleRequest(RequestContext<Object> context, Object
request) throws RemoteExecutionException { /* unused */ }
+ };
+
+ // Set up RemoteClassTable service for sender.
+ spSender = setupServer(null, idSender, dummyRequestListener, "dummy",
"dummy");
+ RemoteClassTableWrapper remoteClassTableWrapperSender = new
RemoteClassTableWrapper();
+ ServiceBuilder<ClassLookupRequest, ClassLookupResponse> sb =
spSender.endpoint.serviceBuilder(ClassLookupRequest.class, ClassLookupResponse.class);
+ sb.setInstanceName(RCT_INSTANCE_NAME).setServiceType(RCT_SERVICE_TYPE);
+ sb.setClientListener(remoteClassTableWrapperSender.getClientListener());
+ reg = sb.register();
+
+ // Set up RemoteClassTable for receiver.
+ spReceiver = setupServer(null, idReceiver, dummyRequestListener,
"dummy", "dummy");
+ Connection connection = setupConnection(spReceiver, spSender);
+ Client<ClassLookupRequest, ClassLookupResponse> resolver = null;
+ resolver = getFutureResult(connection.openClient(RCT_SERVICE_TYPE,
RCT_INSTANCE_NAME, ClassLookupRequest.class, ClassLookupResponse.class), "unable to
create RCT client");
+ log.info("resolver: " + resolver);
+ RemoteClassTableWrapper remoteClassTableWrapperReceiver = new
RemoteClassTableWrapper(resolver);
+
+ // Create muarshaller.
+ MarshallingConfiguration configSender = new MarshallingConfiguration();
+ configSender.setClassTable(remoteClassTableWrapperSender);
+ MarshallerFactory marshallerFactory = new RiverMarshallerFactory();
+ Marshaller marshaller = marshallerFactory.createMarshaller(configSender);
+
+ // Create unmarshaller
+ MarshallingConfiguration configReceiver = new MarshallingConfiguration();
+ configReceiver.setClassTable(remoteClassTableWrapperReceiver);
+ Unmarshaller unmarshaller =
marshallerFactory.createUnmarshaller(configReceiver);
+
+ // Marshal object.
+ ByteArrayOutputStream baos = new ByteArrayOutputStream(10240);
+ ByteOutput byteOutput = Marshalling.createByteOutput(baos);
+ marshaller.start(byteOutput);
+ InvocationTestObject testObject = new InvocationTestObject();
+ marshaller.writeObject(testObject);
+ marshaller.finish();
+
+ // Unmarshal object.
+ byte[] bytes = baos.toByteArray();
+ ByteInput byteInput = Marshalling.createByteInput(new
ByteArrayInputStream(bytes));
+ unmarshaller.start(byteInput);
+ Object result = unmarshaller.readObject();
+ unmarshaller.finish();
+
+ log.info("remoteClassTableWrapperSender.classesRead: " +
remoteClassTableWrapperSender.classesRead);
+ log.info("remoteClassTableWrapperSender.classesWritten: " +
remoteClassTableWrapperSender.classesWritten);
+ log.info("remoteClassTableWrapperReceiver.classesRead: " +
remoteClassTableWrapperReceiver.classesRead);
+ log.info("remoteClassTableWrapperReceiver.classesWritten: " +
remoteClassTableWrapperReceiver.classesWritten);
+ log.info("result: " + result);
+
+ assertEquals(testObject, result);
+ assertEquals(0, remoteClassTableWrapperSender.classesRead);
+ assertEquals(1, remoteClassTableWrapperSender.classesWritten);
+ assertEquals(1, remoteClassTableWrapperSender.remoteResolutions);
+ assertEquals(1, remoteClassTableWrapperReceiver.classesRead);
+ assertEquals(0, remoteClassTableWrapperReceiver.classesWritten);
+ assertEquals(0, remoteClassTableWrapperReceiver.remoteResolutions);
+ assertFalse(remoteClassTableWrapperSender.writerMatches);
+ assertFalse(remoteClassTableWrapperReceiver.writerMatches);
+
+ // Do it again.
+
+ // Create muarshaller.
+ marshaller = marshallerFactory.createMarshaller(configSender);
+
+ // Create unmarshaller
+ unmarshaller = marshallerFactory.createUnmarshaller(configReceiver);
+
+ // Marshal object.
+ baos = new ByteArrayOutputStream(10240);
+ byteOutput = Marshalling.createByteOutput(baos);
+ marshaller.start(byteOutput);
+ testObject = new InvocationTestObject();
+ marshaller.writeObject(testObject);
+ marshaller.finish();
+
+ // Unmarshal object.
+ bytes = baos.toByteArray();
+ byteInput = Marshalling.createByteInput(new ByteArrayInputStream(bytes));
+ unmarshaller.start(byteInput);
+ result = unmarshaller.readObject();
+ unmarshaller.finish();
+
+ log.info("remoteClassTableWrapperSender.classesRead: " +
remoteClassTableWrapperSender.classesRead);
+ log.info("remoteClassTableWrapperSender.classesWritten: " +
remoteClassTableWrapperSender.classesWritten);
+ log.info("remoteClassTableWrapperReceiver.classesRead: " +
remoteClassTableWrapperReceiver.classesRead);
+ log.info("remoteClassTableWrapperReceiver.classesWritten: " +
remoteClassTableWrapperReceiver.classesWritten);
+ log.info("result: " + result);
+
+ assertEquals(0, remoteClassTableWrapperSender.classesRead);
+ assertEquals(2, remoteClassTableWrapperSender.classesWritten);
+ assertEquals(1, remoteClassTableWrapperSender.remoteResolutions);
+ assertEquals(2, remoteClassTableWrapperReceiver.classesRead);
+ assertEquals(0, remoteClassTableWrapperReceiver.classesWritten);
+ assertEquals(0, remoteClassTableWrapperReceiver.remoteResolutions);
+ assertTrue(remoteClassTableWrapperSender.writerMatches);
+ assertFalse(remoteClassTableWrapperReceiver.writerMatches);
+ assertEquals(testObject, result);
+
+ log.info(getName() + " PASSES");
+ } finally {
+ if (reg != null) {
+ reg.close();
+ }
+ if (spReceiver != null) {
+ spReceiver.close();
+ }
+ if (spSender != null) {
+ spSender.close();
+ }
+ exit();
+ }
+ }
+
+// @Test
+// public void testRemoteClassTableAsService() throws Exception {
+// enter();
+//
+// int id0 = counter++;
+// int id1 = counter++;
+// ServerPackage sp0 = null;
+// ServerPackage sp1 = null;
+//
+// Registration reg0 = null;
+// Registration reg1 = null;
+//
+// try {
+//
+// // Set up "client" endpoint.
+// TestRequestListener testRequestListener0 = new TestRequestListener();
+// sp0 = setupServer(null, id0, testRequestListener0, "test0",
"test0");
+//
+// // Set up "server" endpoint.
+// TestRequestListener testRequestListener1 = new TestRequestListener();
+// sp1 = setupServer(null, id1, testRequestListener1, "test1",
"test1");
+//
+// // Set up RemoteClassTable service.
+// RemoteClassTableWrapper remoteClassTableWrapper1 = new
RemoteClassTableWrapper();
+// ServiceBuilder<ClassLookupRequest, ClassLookupResponse> sb =
sp1.endpoint.serviceBuilder(ClassLookupRequest.class, ClassLookupResponse.class);
+// sb.setInstanceName(RCT_INSTANCE_NAME).setServiceType(RCT_SERVICE_TYPE);
+// sb.setClientListener(remoteClassTableWrapper1.getClientListener());
+// reg1 = sb.register();
+//
+// // Set up RemoteClassTable client.
+// Connection connection = setupConnection(sp0, sp1);
+// Client<ClassLookupRequest, ClassLookupResponse> resolver = null;
+// resolver = getFutureResult(connection.openClient(RCT_SERVICE_TYPE,
RCT_INSTANCE_NAME, ClassLookupRequest.class, ClassLookupResponse.class), "unable to
create RCT client");
+// RemoteClassTableWrapper remoteClassTableWrapper0 = new
RemoteClassTableWrapper(resolver);
+// reg0 = sp0.endpoint.addProtocolService(ProtocolServiceType.CLASS_TABLE,
"remoteclasstable", remoteClassTableWrapper0);
+//
+// // Do invocation.
+// Client<InvocationTestObject, InvocationTestObject> client =
getFutureResult(connection.openClient("test1", "test1",
InvocationTestObject.class, InvocationTestObject.class), "unable to create
client");
+// InvocationTestObject invocationObject = new InvocationTestObject();
+// assertEquals(invocationObject, client.invoke(invocationObject));
+// assertEquals(invocationObject, client.invoke(invocationObject));
+//
+// log.info("remoteClassTableWrapper0.classesRead: " +
remoteClassTableWrapper0.classesRead);
+// log.info("remoteClassTableWrapper0.classesWritten: " +
remoteClassTableWrapper0.classesWritten);
+// log.info("remoteClassTableWrapper1.classesRead: " +
remoteClassTableWrapper1.classesRead);
+// log.info("remoteClassTableWrapper1.classesWritten: " +
remoteClassTableWrapper1.classesWritten);
+//
+// log.info(getName() + " PASSES");
+// } finally {
+// if (reg0 != null) {
+// reg0.close();
+// }
+// if (reg1 != null) {
+// reg1.close();
+// }
+// if (sp0 != null) {
+// sp0.close();
+// }
+// if (sp1 != null) {
+// sp1.close();
+// }
+// exit();
+// }
+// }
+
+ static class TestRequestListener implements RequestListener<Object, Object> {
+
+ public void handleRequest(RequestContext<Object> context, Object request)
throws RemoteExecutionException {
+ try {
+ context.sendReply(request);
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ }
+ }
+}