~ubuntu-branches/ubuntu/raring/libjboss-remoting-java/raring

« back to all changes in this revision

Viewing changes to src/tests/org/jboss/test/remoting/lease/LeaseCreationFailureTestCase.java

  • Committer: Package Import Robot
  • Author(s): Torsten Werner
  • Date: 2011-09-09 14:01:03 UTC
  • mfrom: (1.1.6 upstream)
  • Revision ID: package-import@ubuntu.com-20110909140103-hqokx61534tas9rg
Tags: 2.5.3.SP1-1
* Newer but not newest upstream release. Do not build samples.
* Change debian/watch to upstream's svn repo.
* Add patch to fix compile error caused by tomcat update.
  (Closes: #628303)
* Switch to source format 3.0.
* Switch to debhelper level 7.
* Remove useless Depends.
* Update Standards-Version: 3.9.2.
* Update README.source.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
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.
 
6
 *
 
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.
 
11
 *
 
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.
 
16
 *
 
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.
 
21
 */
 
22
 
 
23
package org.jboss.test.remoting.lease;
 
24
 
 
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;
 
31
import java.util.Map;
 
32
 
 
33
import javax.management.MBeanServer;
 
34
import javax.net.SocketFactory;
 
35
 
 
36
import junit.framework.TestCase;
 
37
 
 
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;
 
53
 
 
54
 
 
55
/**
 
56
 * Unit test for JBREM-1154.
 
57
 * 
 
58
 * @author <a href="ron.sigal@jboss.com">Ron Sigal</a>
 
59
 * @version 
 
60
 * <p>
 
61
 * Copyright Sep 11, 2009
 
62
 * </p>
 
63
 */
 
64
public class LeaseCreationFailureTestCase extends TestCase
 
65
{
 
66
   private static Logger log = Logger.getLogger(LeaseCreationFailureTestCase.class);
 
67
   
 
68
   private static boolean firstTime = true;
 
69
   
 
70
   protected String host;
 
71
   protected int port;
 
72
   protected String locatorURI;
 
73
   protected InvokerLocator serverLocator;
 
74
   protected Connector connector;
 
75
   protected TestInvocationHandler invocationHandler;
 
76
 
 
77
   
 
78
   public void setUp() throws Exception
 
79
   {
 
80
      if (firstTime)
 
81
      {
 
82
         firstTime = false;
 
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);  
 
89
      }
 
90
   }
 
91
 
 
92
   
 
93
   public void tearDown()
 
94
   {
 
95
   }
 
96
   
 
97
   
 
98
   public void testLeaseCreationFailure() throws Throwable
 
99
   {
 
100
      log.info("entering " + getName());
 
101
      
 
102
      // Start server.
 
103
      setupServer();
 
104
      
 
105
      // Create client.
 
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);
 
113
      
 
114
      // Verify that Client.connect() throws Exception if LeasePinger.addClient() fails.
 
115
      try
 
116
      {
 
117
         client.connect();
 
118
         fail("expected exception");
 
119
      }
 
120
      catch (CannotConnectException e)
 
121
      {
 
122
         log.info("got exception", e);
 
123
         assertEquals("got wrong exception", "Error setting up client lease upon performing connect.", e.getMessage());
 
124
      }
 
125
      catch (Throwable t)
 
126
      {
 
127
         log.info("got wrong exception", t);
 
128
         fail("got wrong exception");
 
129
      }
 
130
      assertFalse(client.isConnected());
 
131
      
 
132
      shutdownServer();
 
133
      log.info(getName() + " PASSES");
 
134
   }
 
135
   
 
136
   
 
137
   protected String getTransport()
 
138
   {
 
139
      return "socket";
 
140
   }
 
141
   
 
142
   
 
143
   protected void addExtraClientConfig(Map config) {}
 
144
   protected void addExtraServerConfig(Map config) {}
 
145
   
 
146
 
 
147
   protected void setupServer() throws Exception
 
148
   {
 
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)
 
154
      {
 
155
         locatorURI += "/?" + metadata;
 
156
      }
 
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);
 
164
      connector.create();
 
165
      invocationHandler = new TestInvocationHandler();
 
166
      connector.addInvocationHandler("test", invocationHandler);
 
167
      connector.addConnectionListener(new TestConnectionListener());
 
168
      connector.start();
 
169
   }
 
170
   
 
171
   
 
172
   protected void shutdownServer() throws Exception
 
173
   {
 
174
      if (connector != null)
 
175
         connector.stop();
 
176
   }
 
177
   
 
178
   
 
179
   static class TestInvocationHandler implements ServerInvocationHandler
 
180
   {
 
181
      public void addListener(InvokerCallbackHandler callbackHandler) {}
 
182
      public Object invoke(final InvocationRequest invocation) throws Throwable
 
183
      {
 
184
         return invocation.getParameter();
 
185
      }
 
186
      public void removeListener(InvokerCallbackHandler callbackHandler) {}
 
187
      public void setMBeanServer(MBeanServer server) {}
 
188
      public void setInvoker(ServerInvoker invoker) {}
 
189
   }
 
190
   
 
191
   
 
192
   static class TestConnectionListener implements ConnectionListener
 
193
   {
 
194
      public void handleConnectionException(Throwable throwable, Client client)
 
195
      {
 
196
      }
 
197
   }
 
198
 
 
199
 
 
200
   public static class TestSocketFactory extends SocketFactory
 
201
   {
 
202
      int initialSuccesses = 2;
 
203
 
 
204
      public TestSocketFactory()
 
205
      {
 
206
      }
 
207
      public TestSocketFactory(int initialSuccesses)
 
208
      {
 
209
         this.initialSuccesses = initialSuccesses;
 
210
      }
 
211
      public Socket createSocket()
 
212
      {
 
213
         Socket s = new TestSocket(initialSuccesses);
 
214
         log.info("returning " + s);
 
215
         return s;
 
216
      }
 
217
      public Socket createSocket(String arg0, int arg1) throws IOException, UnknownHostException
 
218
      {
 
219
         Socket s = new TestSocket(arg0, arg1, initialSuccesses);
 
220
         log.info("returning " + s);
 
221
         return s;
 
222
      }
 
223
 
 
224
      public Socket createSocket(InetAddress arg0, int arg1) throws IOException
 
225
      {
 
226
         Socket s = new TestSocket(arg0, arg1, initialSuccesses);
 
227
         log.info("returning " + s);
 
228
         return s;
 
229
      }
 
230
 
 
231
      public Socket createSocket(String arg0, int arg1, InetAddress arg2, int arg3) throws IOException, UnknownHostException
 
232
      {
 
233
         Socket s = new TestSocket(arg0, arg1, arg2, arg3, initialSuccesses);
 
234
         log.info("returning " + s);
 
235
         return s;
 
236
      }
 
237
 
 
238
      public Socket createSocket(InetAddress arg0, int arg1, InetAddress arg2, int arg3) throws IOException
 
239
      {
 
240
         Socket s = new TestSocket(arg0, arg1, arg2, arg3, initialSuccesses);
 
241
         log.info("returning " + s);
 
242
         return s;
 
243
      }
 
244
   }
 
245
   
 
246
   
 
247
   static class TestSocket extends Socket
 
248
   {
 
249
      int initialSuccesses;
 
250
      
 
251
      public TestSocket(int initialWrites)
 
252
      {
 
253
         this.initialSuccesses = initialWrites;
 
254
      }
 
255
      public TestSocket(String host, int port, int initialWrites) throws UnknownHostException, IOException
 
256
      {
 
257
         super(host, port);
 
258
         this.initialSuccesses = initialWrites;
 
259
      }
 
260
      public TestSocket(InetAddress address, int port, int initialWrites) throws IOException
 
261
      {
 
262
         super(address, port);
 
263
         this.initialSuccesses = initialWrites;
 
264
      }
 
265
      public TestSocket(String host, int port, InetAddress localAddr, int localPort, int initialWrites) throws IOException
 
266
      {
 
267
         super(host, port, localAddr, localPort);
 
268
         this.initialSuccesses = initialWrites;
 
269
      }
 
270
      public TestSocket(InetAddress address, int port, InetAddress localAddr, int localPort, int initialWrites) throws IOException
 
271
      {
 
272
         super(address, port, localAddr, localPort);
 
273
         this.initialSuccesses = initialWrites;
 
274
      }
 
275
      public OutputStream getOutputStream() throws IOException
 
276
      {
 
277
         return new TestOutputStream(super.getOutputStream(), initialSuccesses);
 
278
      }
 
279
      public String toString()
 
280
      {
 
281
         return "TestSocket[" + getLocalPort() + "->" + getPort() + "]";
 
282
      }
 
283
   }
 
284
   
 
285
   
 
286
   static class TestOutputStream extends OutputStream
 
287
   {
 
288
      OutputStream os;
 
289
      boolean closed;
 
290
      int initialWrites;
 
291
      boolean doThrow = true;
 
292
      public static int counter;
 
293
      
 
294
      public TestOutputStream(OutputStream os, int initialWrites)
 
295
      {
 
296
         this.os = os;
 
297
         this.initialWrites = initialWrites;
 
298
      }
 
299
      public void close()throws IOException
 
300
      {
 
301
         closed = true;
 
302
         super.close();
 
303
         log.info(this + " closed");
 
304
      }
 
305
      public void write(int b) throws IOException
 
306
      {
 
307
         System.out.print("b: " + b);
 
308
         if (closed)
 
309
         {
 
310
            log.info("TestOutputStream closed, cannot write");
 
311
            throw new IOException("closed");
 
312
         }
 
313
         if (doThrow && ++counter > initialWrites)
 
314
         {
 
315
            log.info("throwing exception");
 
316
            throw new IOException("");
 
317
         }
 
318
         os.write(b);
 
319
      }
 
320
      public void write(byte b[], int off, int len) throws IOException
 
321
      {
 
322
         System.out.print("b: ");
 
323
         for (int i = 0; i < len; i++)
 
324
         {
 
325
            System.out.print(b[i] + " ");
 
326
         }
 
327
         System.out.println("");
 
328
         if (closed)
 
329
         {
 
330
            log.info("TestOutputStream closed, cannot write");
 
331
            throw new IOException("closed");
 
332
         }
 
333
         log.info("TestOutputStream: counter = " + ++counter + ", initialWrites = " + initialWrites);
 
334
         if (counter > initialWrites)
 
335
         {
 
336
            log.info("throwing exception");
 
337
            throw new IOException("");
 
338
         }
 
339
         if (closed)
 
340
         {
 
341
            log.info("TestOutputStream closed, cannot write");
 
342
            throw new IOException("closed");
 
343
         }
 
344
         try
 
345
         {
 
346
            log.info(this + " calling write()");
 
347
            doThrow = false;
 
348
            os.write(b, off, len);
 
349
            os.flush();
 
350
            doThrow = true;
 
351
            log.info(this + " back from write()");
 
352
         }
 
353
         catch (IOException e)
 
354
         {
 
355
            log.info("exception: ", e);
 
356
            throw e;
 
357
         }
 
358
      }
 
359
   }
 
360
}
 
 
b'\\ No newline at end of file'