Author: ron.sigal(a)jboss.com
Date: 2010-09-07 15:55:26 -0400 (Tue, 07 Sep 2010)
New Revision: 6092
Added:
remoting3/trunk/jboss-remoting/src/test/java/org/jboss/remoting3/test/LocalCallbackTestCase.java
Log:
JBREM-1228: New unit test.
Added:
remoting3/trunk/jboss-remoting/src/test/java/org/jboss/remoting3/test/LocalCallbackTestCase.java
===================================================================
---
remoting3/trunk/jboss-remoting/src/test/java/org/jboss/remoting3/test/LocalCallbackTestCase.java
(rev 0)
+++
remoting3/trunk/jboss-remoting/src/test/java/org/jboss/remoting3/test/LocalCallbackTestCase.java 2010-09-07
19:55:26 UTC (rev 6092)
@@ -0,0 +1,213 @@
+/*
+ * 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.IOException;
+import java.net.URI;
+import java.net.URISyntaxException;
+
+import org.jboss.remoting3.Client;
+import org.jboss.remoting3.ClientConnector;
+import org.jboss.remoting3.Connection;
+import org.jboss.remoting3.Endpoint;
+import org.jboss.remoting3.RemoteExecutionException;
+import org.jboss.remoting3.RequestContext;
+import org.jboss.remoting3.RequestListener;
+import org.jboss.xnio.IoFuture;
+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 3, 2010
+ */
+//@SuppressWarnings("unused")
+@Test(suiteName = "LocalCallback")
+public class LocalCallbackTestCase extends RemotingLocalTestBase {
+
+ protected static final String SERVICE_TYPE = "testservice";
+ protected static final String INSTANCE_NAME= "testinstance";
+ protected static final String CALLBACK_REQUEST = "callbackRequest";
+ protected static final String CALLBACK = "callback";
+ protected static final String OK = "ok";
+
+ private static final Logger log = Logger.getLogger(LocalCallbackTestCase.class);
+
+ private static int counter;
+
+ @BeforeMethod
+ public void setUp() {
+ }
+
+ @AfterMethod
+ public void tearDown() throws IOException {
+ }
+
+ @Test
+ public void testCallbackByRequestContext() throws Exception {
+ enter();
+ ServerPackage sp = null;
+ Connection connection = null;
+ Client<Object, Object> client = null;
+
+ try {
+ int id = counter++;
+
+ // Set up service.
+ TestRequestListener testRequestListener = new TestRequestListener();
+ sp = setupServer(null, id, testRequestListener, SERVICE_TYPE, INSTANCE_NAME);
+
+ // Set up connection and client.
+ connection = setupConnection(sp);
+ client = setupClient(connection, SERVICE_TYPE, INSTANCE_NAME);
+
+ // Test callback.
+ assertEquals(OK, client.invoke(CALLBACK_REQUEST));
+ assertTrue(testRequestListener.receivedCallbackRequest);
+ assertTrue(testRequestListener.receivedCallback);
+ assertFalse(testRequestListener.receivedClientConnector);
+ assertTrue(testRequestListener.sentCallback);
+
+ log.debug(getName() + " PASSES");
+ } finally {
+ if (client != null) {
+ client.close();
+ }
+ if (connection != null) {
+ connection.close();
+ }
+ if (sp != null) {
+ sp.close();
+ }
+ exit();
+ }
+ }
+
+ @Test
+ public void testCallbackByClientConnector() throws Exception {
+ enter();
+ ServerPackage sp = null;
+ Connection connection = null;
+ Client<Object, Object> client = null;
+
+ try {
+ int id = counter++;
+
+ // Set up service.
+ TestRequestListener testRequestListener = new TestRequestListener();
+ sp = setupServer(null, id, testRequestListener, SERVICE_TYPE, INSTANCE_NAME);
+
+ // Set up connection and client.
+ connection = setupConnection(sp);
+ client = setupClient(connection, SERVICE_TYPE, INSTANCE_NAME);
+
+ // Test callback.
+ TestRequestListener callbackRequestListener = new TestRequestListener();
+ ClientConnector<Object, Object> clientConnector =
connection.createClientConnector(callbackRequestListener, Object.class, Object.class);
+ assertEquals(OK, client.invoke(clientConnector));
+ assertFalse(testRequestListener.receivedCallbackRequest);
+ assertFalse(testRequestListener.receivedCallback);
+ assertTrue(testRequestListener.receivedClientConnector);
+ assertTrue(testRequestListener.sentCallback);
+ assertFalse(callbackRequestListener.receivedCallbackRequest);
+ assertTrue(callbackRequestListener.receivedCallback);
+ assertFalse(callbackRequestListener.receivedClientConnector);
+ assertFalse(callbackRequestListener.sentCallback);
+
+ log.debug(getName() + " PASSES");
+ } finally {
+ if (client != null) {
+ client.close();
+ }
+ if (connection != null) {
+ connection.close();
+ }
+ if (sp != null) {
+ sp.close();
+ }
+ exit();
+ }
+ }
+
+ protected Connection setupConnection(ServerPackage sp) throws IOException,
URISyntaxException {
+ Endpoint endpoint = sp.endpoint;
+ URI uri = new URI(getRemotingScheme() + "://localhost");
+ Connection connection = getFutureResult(endpoint.connect(uri,
getConnectionOptionMap(), "user", null, "password".toCharArray()),
"unable to connect to " + uri);
+ return connection;
+ }
+
+ protected 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), "unable to open client to " + serviceType + ":" +
instanceName);
+ return client;
+ }
+
+ static public class TestRequestListener implements RequestListener<Object,
Object> {
+ public boolean receivedCallbackRequest;
+ public boolean receivedCallback;
+ public boolean receivedClientConnector;
+ public boolean sentCallback;
+
+ public TestRequestListener() {
+ new Exception(this + ": TestRequestListener()").printStackTrace();
+ }
+
+ @SuppressWarnings("unchecked")
+ public void handleRequest(final RequestContext<Object> requestContext, final
Object request) throws RemoteExecutionException {
+ try {
+ log.info(this + " got request %s: ", request);
+ if (request.equals(CALLBACK_REQUEST)) {
+ receivedCallbackRequest = true;
+ Connection connection = requestContext.getContext().getConnection();
+ IoFuture<? extends Client<Object, Object>> future =
connection.openClient(SERVICE_TYPE, INSTANCE_NAME, Object.class, Object.class);
+ Client<Object, Object> client = getFutureResult(future, 5000,
"Unable to create Client");
+ sentCallback = OK.equals(client.invoke(CALLBACK));
+ requestContext.sendReply(OK);
+ log.info(this + " sent callback using RequestContext: " +
sentCallback);
+ } else if (request.equals(CALLBACK)) {
+ receivedCallback = true;
+ requestContext.sendReply(OK);
+ } else if (request instanceof ClientConnector<?, ?>) {
+ receivedClientConnector = true;
+ ClientConnector<Object, Object> clientConnector = (ClientConnector)
request;
+ Client<Object, Object> callbackClient =
getFutureResult(clientConnector.getFutureClient(), 5000, "unable to get callback
client");
+ sentCallback = OK.equals(callbackClient.invoke(CALLBACK));
+ requestContext.sendReply(OK);
+ log.info(this + " sent callback using ClientConnector: " +
sentCallback);
+ } else {
+ throw new RuntimeException("unexpected request: " + request);
+ }
+ } catch (IOException e) {
+ log.error(e, "reply");
+ throw new RemoteExecutionException(e);
+ }
+ }
+ }
+}