2
* JBoss, Home of Professional Open Source.
3
* Copyright 2009, Red Hat Middleware LLC, and individual contributors
4
* as indicated by the @author tags. See the copyright.txt file in the
5
* distribution for a full listing of individual contributors.
7
* This is free software; you can redistribute it and/or modify it
8
* under the terms of the GNU Lesser General Public License as
9
* published by the Free Software Foundation; either version 2.1 of
10
* the License, or (at your option) any later version.
12
* This software is distributed in the hope that it will be useful,
13
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15
* Lesser General Public License for more details.
17
* You should have received a copy of the GNU Lesser General Public
18
* License along with this software; if not, write to the Free
19
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
23
package org.jboss.test.remoting.lease;
25
import java.io.IOException;
26
import java.io.OutputStream;
27
import java.net.InetAddress;
28
import java.net.Socket;
29
import java.net.UnknownHostException;
30
import java.util.HashMap;
33
import javax.management.MBeanServer;
34
import javax.net.SocketFactory;
36
import junit.framework.TestCase;
38
import org.apache.log4j.ConsoleAppender;
39
import org.apache.log4j.Level;
40
import org.apache.log4j.Logger;
41
import org.apache.log4j.PatternLayout;
42
import org.jboss.remoting.CannotConnectException;
43
import org.jboss.remoting.Client;
44
import org.jboss.remoting.ConnectionListener;
45
import org.jboss.remoting.InvocationRequest;
46
import org.jboss.remoting.InvokerLocator;
47
import org.jboss.remoting.Remoting;
48
import org.jboss.remoting.ServerInvocationHandler;
49
import org.jboss.remoting.ServerInvoker;
50
import org.jboss.remoting.callback.InvokerCallbackHandler;
51
import org.jboss.remoting.transport.Connector;
52
import org.jboss.remoting.transport.PortUtil;
56
* Unit test for JBREM-1154.
58
* @author <a href="ron.sigal@jboss.com">Ron Sigal</a>
61
* Copyright Sep 11, 2009
64
public class LeaseCreationFailureTestCase extends TestCase
66
private static Logger log = Logger.getLogger(LeaseCreationFailureTestCase.class);
68
private static boolean firstTime = true;
70
protected String host;
72
protected String locatorURI;
73
protected InvokerLocator serverLocator;
74
protected Connector connector;
75
protected TestInvocationHandler invocationHandler;
78
public void setUp() throws Exception
83
Logger.getLogger("org.jboss.remoting").setLevel(Level.INFO);
84
Logger.getLogger("org.jboss.test.remoting").setLevel(Level.INFO);
85
String pattern = "[%d{ABSOLUTE}] [%t] %5p (%F:%L) - %m%n";
86
PatternLayout layout = new PatternLayout(pattern);
87
ConsoleAppender consoleAppender = new ConsoleAppender(layout);
88
Logger.getRootLogger().addAppender(consoleAppender);
93
public void tearDown()
98
public void testLeaseCreationFailure() throws Throwable
100
log.info("entering " + getName());
106
InvokerLocator clientLocator = new InvokerLocator(locatorURI);
107
HashMap clientConfig = new HashMap();
108
clientConfig.put(InvokerLocator.FORCE_REMOTE, "true");
109
clientConfig.put(Remoting.SOCKET_FACTORY_NAME, TestSocketFactory.class.getName());
110
clientConfig.put(Client.ENABLE_LEASE, "true");
111
addExtraClientConfig(clientConfig);
112
Client client = new Client(clientLocator, clientConfig);
114
// Verify that Client.connect() throws Exception if LeasePinger.addClient() fails.
118
fail("expected exception");
120
catch (CannotConnectException e)
122
log.info("got exception", e);
123
assertEquals("got wrong exception", "Error setting up client lease upon performing connect.", e.getMessage());
127
log.info("got wrong exception", t);
128
fail("got wrong exception");
130
assertFalse(client.isConnected());
133
log.info(getName() + " PASSES");
137
protected String getTransport()
143
protected void addExtraClientConfig(Map config) {}
144
protected void addExtraServerConfig(Map config) {}
147
protected void setupServer() throws Exception
149
host = InetAddress.getLocalHost().getHostAddress();
150
port = PortUtil.findFreePort(host);
151
locatorURI = getTransport() + "://" + host + ":" + port;
152
String metadata = System.getProperty("remoting.metadata");
153
if (metadata != null)
155
locatorURI += "/?" + metadata;
157
serverLocator = new InvokerLocator(locatorURI);
158
log.info("Starting remoting server with locator uri of: " + locatorURI);
159
HashMap config = new HashMap();
160
config.put(InvokerLocator.FORCE_REMOTE, "true");
161
config.put("clientLeasePeriod", "1000");
162
addExtraServerConfig(config);
163
connector = new Connector(serverLocator, config);
165
invocationHandler = new TestInvocationHandler();
166
connector.addInvocationHandler("test", invocationHandler);
167
connector.addConnectionListener(new TestConnectionListener());
172
protected void shutdownServer() throws Exception
174
if (connector != null)
179
static class TestInvocationHandler implements ServerInvocationHandler
181
public void addListener(InvokerCallbackHandler callbackHandler) {}
182
public Object invoke(final InvocationRequest invocation) throws Throwable
184
return invocation.getParameter();
186
public void removeListener(InvokerCallbackHandler callbackHandler) {}
187
public void setMBeanServer(MBeanServer server) {}
188
public void setInvoker(ServerInvoker invoker) {}
192
static class TestConnectionListener implements ConnectionListener
194
public void handleConnectionException(Throwable throwable, Client client)
200
public static class TestSocketFactory extends SocketFactory
202
int initialSuccesses = 2;
204
public TestSocketFactory()
207
public TestSocketFactory(int initialSuccesses)
209
this.initialSuccesses = initialSuccesses;
211
public Socket createSocket()
213
Socket s = new TestSocket(initialSuccesses);
214
log.info("returning " + s);
217
public Socket createSocket(String arg0, int arg1) throws IOException, UnknownHostException
219
Socket s = new TestSocket(arg0, arg1, initialSuccesses);
220
log.info("returning " + s);
224
public Socket createSocket(InetAddress arg0, int arg1) throws IOException
226
Socket s = new TestSocket(arg0, arg1, initialSuccesses);
227
log.info("returning " + s);
231
public Socket createSocket(String arg0, int arg1, InetAddress arg2, int arg3) throws IOException, UnknownHostException
233
Socket s = new TestSocket(arg0, arg1, arg2, arg3, initialSuccesses);
234
log.info("returning " + s);
238
public Socket createSocket(InetAddress arg0, int arg1, InetAddress arg2, int arg3) throws IOException
240
Socket s = new TestSocket(arg0, arg1, arg2, arg3, initialSuccesses);
241
log.info("returning " + s);
247
static class TestSocket extends Socket
249
int initialSuccesses;
251
public TestSocket(int initialWrites)
253
this.initialSuccesses = initialWrites;
255
public TestSocket(String host, int port, int initialWrites) throws UnknownHostException, IOException
258
this.initialSuccesses = initialWrites;
260
public TestSocket(InetAddress address, int port, int initialWrites) throws IOException
262
super(address, port);
263
this.initialSuccesses = initialWrites;
265
public TestSocket(String host, int port, InetAddress localAddr, int localPort, int initialWrites) throws IOException
267
super(host, port, localAddr, localPort);
268
this.initialSuccesses = initialWrites;
270
public TestSocket(InetAddress address, int port, InetAddress localAddr, int localPort, int initialWrites) throws IOException
272
super(address, port, localAddr, localPort);
273
this.initialSuccesses = initialWrites;
275
public OutputStream getOutputStream() throws IOException
277
return new TestOutputStream(super.getOutputStream(), initialSuccesses);
279
public String toString()
281
return "TestSocket[" + getLocalPort() + "->" + getPort() + "]";
286
static class TestOutputStream extends OutputStream
291
boolean doThrow = true;
292
public static int counter;
294
public TestOutputStream(OutputStream os, int initialWrites)
297
this.initialWrites = initialWrites;
299
public void close()throws IOException
303
log.info(this + " closed");
305
public void write(int b) throws IOException
307
System.out.print("b: " + b);
310
log.info("TestOutputStream closed, cannot write");
311
throw new IOException("closed");
313
if (doThrow && ++counter > initialWrites)
315
log.info("throwing exception");
316
throw new IOException("");
320
public void write(byte b[], int off, int len) throws IOException
322
System.out.print("b: ");
323
for (int i = 0; i < len; i++)
325
System.out.print(b[i] + " ");
327
System.out.println("");
330
log.info("TestOutputStream closed, cannot write");
331
throw new IOException("closed");
333
log.info("TestOutputStream: counter = " + ++counter + ", initialWrites = " + initialWrites);
334
if (counter > initialWrites)
336
log.info("throwing exception");
337
throw new IOException("");
341
log.info("TestOutputStream closed, cannot write");
342
throw new IOException("closed");
346
log.info(this + " calling write()");
348
os.write(b, off, len);
351
log.info(this + " back from write()");
353
catch (IOException e)
355
log.info("exception: ", e);
b'\\ No newline at end of file'