Author: ron.sigal(a)jboss.com
Date: 2010-09-07 15:49:06 -0400 (Tue, 07 Sep 2010)
New Revision: 6091
Added:
remoting3/trunk/jboss-remoting/src/test/java/org/jboss/remoting3/test/ClientConnectorMisuseTestCase.java
Log:
JBREM-1228: New unit test.
Added:
remoting3/trunk/jboss-remoting/src/test/java/org/jboss/remoting3/test/ClientConnectorMisuseTestCase.java
===================================================================
---
remoting3/trunk/jboss-remoting/src/test/java/org/jboss/remoting3/test/ClientConnectorMisuseTestCase.java
(rev 0)
+++
remoting3/trunk/jboss-remoting/src/test/java/org/jboss/remoting3/test/ClientConnectorMisuseTestCase.java 2010-09-07
19:49:06 UTC (rev 6091)
@@ -0,0 +1,160 @@
+/*
+ * 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 java.io.IOException;
+import java.net.URI;
+import java.net.URISyntaxException;
+import java.util.ServiceLoader;
+
+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.Remoting;
+import org.jboss.remoting3.RequestContext;
+import org.jboss.remoting3.RequestListener;
+import org.jboss.remoting3.spi.RemotingServiceDescriptor;
+import org.jboss.xnio.log.Logger;
+import org.testng.Assert;
+import org.testng.annotations.AfterMethod;
+import org.testng.annotations.BeforeMethod;
+import org.testng.annotations.Test;
+
+/**
+ * Tests an attempt to use a ClientConnector locally.
+ *
+ * @author <a href="ron.sigal(a)jboss.com">Ron Sigal</a>
+ * @version $Revision: 1.1 $
+ * <p>
+ * Copyright August 27, 2010
+ */
+@Test(suiteName = "ClientConnectorMisuse")
+public class ClientConnectorMisuseTestCase extends RemotingTestBase {
+
+ private static final Logger log =
Logger.getLogger(ClientConnectorMisuseTestCase.class);
+
+ private static int counter;
+
+ @BeforeMethod
+ public void setUp() {
+ ServiceLoader.load(RemotingServiceDescriptor.class);
+ }
+
+ @AfterMethod
+ public void tearDown() throws IOException {
+ }
+
+ /**
+ * Distinct Executors, Endpoints, Xnio's, TcpServers, RequestListeners.
+ * One instance of one service in each Endpoint.
+ * Two Connections per TcpServer, two Clients per Connection.
+ */
+ @Test
+ public void testClientConnectorMisuse() throws Exception {
+ enter();
+ ServerPackage sp = null;
+ Connection connection = null;
+ Client<Object, Object> client = null;
+
+ try {
+ int id = counter++;
+
+ // Set up services.
+ TestRequestListener testRequestListener = new TestRequestListener(id);
+ sp = setupServer(null, id, testRequestListener, "test",
"test");
+
+ // Set up connection and client.
+ connection = setupConnection(sp.port);
+ client = setupClient(connection, "test", "test");
+
+ // Test connection.
+ assertEquals(id, client.invoke("dummy"), "Should be
equal");
+
+ // Get ClientConnector and attempt to use it locally.
+ ClientConnector<Object, Object> clientConnector = null;
+ clientConnector = connection.createClientConnector(new
RequestListener<Object, Object>() {
+ public void handleRequest(RequestContext<Object> context, Object
request) throws RemoteExecutionException { }
+ }, Object.class, Object.class);
+
+ try {
+ Client<Object, Object> client2 =
getFutureResult(clientConnector.getFutureClient(), "unable to get client");
+ client2.invoke(new Object());
+ Assert.fail("expected SecurityException");
+ } catch (SecurityException e) {
+ log.info("received expected SecurityException");
+ }
+
+ log.info(getName() + " PASSES");
+ } finally {
+ if (client != null) {
+ client.close();
+ }
+ if (connection != null) {
+ connection.close();
+ }
+ if (sp != null) {
+ sp.close();
+ }
+ exit();
+ }
+ }
+
+ protected Connection setupConnection(int port) throws IOException, URISyntaxException
{
+ Endpoint endpoint = Remoting.getConfiguredEndpoint();
+ URI uri = new URI(getRemotingScheme() + "://localhost:" + port);
+ 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 class TestRequestListener implements RequestListener<Object, Object> {
+ public int counter = 0;
+ private int answer;
+
+ public TestRequestListener(int answer) {
+ this.answer = answer;
+ }
+
+ public void handleRequest(RequestContext<Object> context, Object request)
throws RemoteExecutionException {
+ try {
+ counter++;
+ context.sendReply(answer);
+ } catch (IOException e) {
+ try {
+ context.sendFailure("error returning response", e);
+ } catch (IOException e1) {
+ e.printStackTrace();
+ throw new RemoteExecutionException("error returning exception",
e1);
+ }
+ }
+ }
+ }
+}