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

« back to all changes in this revision

Viewing changes to tests/org/jboss/test/remoting/transport/socket/socketexception/SocketCreationExceptionTestCase.java

  • Committer: Package Import Robot
  • Author(s): Torsten Werner
  • Date: 2011-09-09 14:01:03 UTC
  • mto: This revision was merged to the branch mainline in revision 9.
  • Revision ID: package-import@ubuntu.com-20110909140103-o8ucrolqt5g25k57
Tags: upstream-2.5.3.SP1
ImportĀ upstreamĀ versionĀ 2.5.3.SP1

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
* JBoss, Home of Professional Open Source
3
 
* Copyright 2009, JBoss Inc., and individual contributors as indicated
4
 
* by the @authors tag. See the copyright.txt in the distribution for a
5
 
* 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
 
package org.jboss.test.remoting.transport.socket.socketexception;
23
 
 
24
 
import java.io.IOException;
25
 
import java.net.InetAddress;
26
 
import java.net.Socket;
27
 
import java.net.SocketException;
28
 
import java.net.UnknownHostException;
29
 
import java.util.HashMap;
30
 
import java.util.Map;
31
 
 
32
 
import javax.management.MBeanServer;
33
 
import javax.net.ServerSocketFactory;
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.Client;
43
 
import org.jboss.remoting.InvocationRequest;
44
 
import org.jboss.remoting.InvokerLocator;
45
 
import org.jboss.remoting.Remoting;
46
 
import org.jboss.remoting.ServerInvocationHandler;
47
 
import org.jboss.remoting.ServerInvoker;
48
 
import org.jboss.remoting.callback.Callback;
49
 
import org.jboss.remoting.callback.HandleCallbackException;
50
 
import org.jboss.remoting.callback.InvokerCallbackHandler;
51
 
import org.jboss.remoting.transport.Connector;
52
 
import org.jboss.remoting.transport.PortUtil;
53
 
 
54
 
/**
55
 
 * Unit tests for JBREM-1152.
56
 
 * 
57
 
 * @author <a href="ron.sigal@jboss.com">Ron Sigal</a>
58
 
 * @version $Rev$
59
 
 * <p>
60
 
 * Copyright Sep 9, 2009
61
 
 * </p>
62
 
 */
63
 
public class SocketCreationExceptionTestCase extends TestCase
64
 
{
65
 
   protected static String SEND_CALLBACK = "sendCallback";
66
 
   private static Logger log = Logger.getLogger(SocketCreationExceptionTestCase.class);
67
 
   private static boolean firstTime = true;
68
 
   
69
 
   protected String host;
70
 
   protected int port;
71
 
   protected String locatorURI;
72
 
   protected InvokerLocator serverLocator;
73
 
   protected Connector connector;
74
 
   protected TestInvocationHandler invocationHandler;
75
 
 
76
 
   
77
 
   public void setUp() throws Exception
78
 
   {
79
 
      if (firstTime)
80
 
      {
81
 
         firstTime = false;
82
 
         Logger.getLogger("org.jboss.remoting").setLevel(Level.INFO);
83
 
         Logger.getLogger("org.jboss.test.remoting").setLevel(Level.INFO);
84
 
         String pattern = "[%d{ABSOLUTE}] [%t] %5p (%F:%L) - %m%n";
85
 
         PatternLayout layout = new PatternLayout(pattern);
86
 
         ConsoleAppender consoleAppender = new ConsoleAppender(layout);
87
 
         Logger.getRootLogger().addAppender(consoleAppender);  
88
 
      }
89
 
   }
90
 
 
91
 
   
92
 
   public void tearDown()
93
 
   {
94
 
   }
95
 
   
96
 
   
97
 
   public void testInvocationWithSocketException() throws Throwable
98
 
   {
99
 
      log.info("entering " + getName());
100
 
      
101
 
      // Start server.
102
 
      setupServer(null);
103
 
      
104
 
      // Create client.
105
 
      InvokerLocator clientLocator = new InvokerLocator(locatorURI);
106
 
      HashMap clientConfig = new HashMap();
107
 
      clientConfig.put(InvokerLocator.FORCE_REMOTE, "true");
108
 
      clientConfig.put(Remoting.CUSTOM_SOCKET_FACTORY, new TestSocketFactory(new SocketException(getName()), 2));
109
 
      addExtraClientConfig(clientConfig);
110
 
      Client client = new Client(clientLocator, clientConfig);
111
 
      client.connect();
112
 
      log.info("client is connected");
113
 
      
114
 
      // Test connection.
115
 
      assertEquals("abc", client.invoke("abc"));
116
 
      log.info("connection is good");
117
 
      
118
 
      client.disconnect();
119
 
      shutdownServer();
120
 
      log.info(getName() + " PASSES");
121
 
   }
122
 
   
123
 
   
124
 
   public void testInvocationWithIOException() throws Throwable
125
 
   {
126
 
      log.info("entering " + getName());
127
 
      
128
 
      // Start server.
129
 
      setupServer(null);
130
 
      
131
 
      // Create client.
132
 
      InvokerLocator clientLocator = new InvokerLocator(locatorURI);
133
 
      HashMap clientConfig = new HashMap();
134
 
      clientConfig.put(InvokerLocator.FORCE_REMOTE, "true");
135
 
      clientConfig.put(Remoting.CUSTOM_SOCKET_FACTORY, new TestSocketFactory(new IOException("Connection reset"), 2));
136
 
      clientConfig.put("generalizeSocketException", "true");
137
 
      addExtraClientConfig(clientConfig);
138
 
      Client client = new Client(clientLocator, clientConfig);
139
 
      client.connect();
140
 
      log.info("client is connected");
141
 
      
142
 
      // Test connection.
143
 
      assertEquals("abc", client.invoke("abc"));
144
 
      log.info("connection is good");
145
 
      
146
 
      client.disconnect();
147
 
      shutdownServer();
148
 
      log.info(getName() + " PASSES");
149
 
   }
150
 
   
151
 
   
152
 
   protected String getTransport()
153
 
   {
154
 
      return "socket";
155
 
   }
156
 
   
157
 
   
158
 
   protected void addExtraClientConfig(Map config) {}
159
 
   protected void addExtraServerConfig(Map config) {}
160
 
   
161
 
 
162
 
   protected void setupServer(ServerSocketFactory ssf) throws Exception
163
 
   {
164
 
      host = InetAddress.getLocalHost().getHostAddress();
165
 
      port = PortUtil.findFreePort(host);
166
 
      locatorURI = getTransport() + "://" + host + ":" + port;
167
 
      String metadata = System.getProperty("remoting.metadata");
168
 
      if (metadata != null)
169
 
      {
170
 
         locatorURI += "/?" + metadata;
171
 
      }
172
 
      serverLocator = new InvokerLocator(locatorURI);
173
 
      log.info("Starting remoting server with locator uri of: " + locatorURI);
174
 
      HashMap config = new HashMap();
175
 
      config.put(InvokerLocator.FORCE_REMOTE, "true");
176
 
      config.put("timeout", "5000");
177
 
      if (ssf != null)
178
 
      {
179
 
         config.put(Remoting.CUSTOM_SERVER_SOCKET_FACTORY, ssf);
180
 
      }
181
 
      addExtraServerConfig(config);
182
 
      connector = new Connector(serverLocator, config);
183
 
      connector.create();
184
 
      invocationHandler = new TestInvocationHandler();
185
 
      connector.addInvocationHandler("test", invocationHandler);
186
 
      connector.start();
187
 
   }
188
 
   
189
 
   
190
 
   protected void shutdownServer() throws Exception
191
 
   {
192
 
      if (connector != null)
193
 
         connector.stop();
194
 
   }
195
 
   
196
 
   
197
 
   static class TestInvocationHandler implements ServerInvocationHandler
198
 
   {
199
 
      InvokerCallbackHandler callbackHandler;
200
 
      
201
 
      public void addListener(InvokerCallbackHandler callbackHandler)
202
 
      {
203
 
         this.callbackHandler = callbackHandler;
204
 
      }
205
 
      public Object invoke(final InvocationRequest invocation) throws Throwable
206
 
      {
207
 
         Object parameter = invocation.getParameter();
208
 
         if (SEND_CALLBACK.equals(parameter))
209
 
         {
210
 
            try
211
 
            {
212
 
               callbackHandler.handleCallback(new Callback("callback"));
213
 
            }
214
 
            catch (HandleCallbackException e)
215
 
            {
216
 
               log.error("unable to send callback", e);
217
 
            }
218
 
         }
219
 
         return parameter;
220
 
      }
221
 
      public void removeListener(InvokerCallbackHandler callbackHandler) {}
222
 
      public void setMBeanServer(MBeanServer server) {}
223
 
      public void setInvoker(ServerInvoker invoker) {}
224
 
   }
225
 
   
226
 
   
227
 
   public static class TestSocketFactory extends SocketFactory
228
 
   {
229
 
      int counter;
230
 
      int limit; 
231
 
      IOException exception;
232
 
      
233
 
      public TestSocketFactory()
234
 
      {
235
 
      }
236
 
      public TestSocketFactory(IOException exception, int limit)
237
 
      {
238
 
         this.exception = exception;
239
 
         this.limit = limit;
240
 
      }
241
 
      public Socket createSocket() throws IOException
242
 
      {
243
 
         counter++;
244
 
         log.info("counter: " + counter);
245
 
         if (counter <= limit)
246
 
         {
247
 
            log.info("throwing exception");
248
 
            throw exception;
249
 
         }
250
 
         log.info("returning socket");
251
 
         return new Socket();
252
 
      }
253
 
      public Socket createSocket(String arg0, int arg1) throws IOException, UnknownHostException
254
 
      {
255
 
         counter++;
256
 
         log.info("counter: " + counter);
257
 
         if (counter <= limit)
258
 
         {
259
 
            throw exception;
260
 
         }
261
 
         log.info("returning socket");
262
 
         return new Socket(arg0, arg1);
263
 
      }
264
 
      public Socket createSocket(InetAddress arg0, int arg1) throws IOException
265
 
      {
266
 
         counter++;
267
 
         log.info("counter: " + counter);
268
 
         if (counter <= limit)
269
 
         {
270
 
            throw exception;
271
 
         }
272
 
         log.info("returning socket");
273
 
         return new Socket(arg0, arg1);
274
 
      }
275
 
      public Socket createSocket(String arg0, int arg1, InetAddress arg2, int arg3) throws IOException, UnknownHostException
276
 
      {
277
 
         counter++;
278
 
         log.info("counter: " + counter);
279
 
         if (counter <= limit)
280
 
         {
281
 
            throw exception;
282
 
         }
283
 
         log.info("returning socket");
284
 
         return new Socket(arg0, arg1, arg2, arg3);
285
 
      }
286
 
 
287
 
      public Socket createSocket(InetAddress arg0, int arg1, InetAddress arg2, int arg3) throws IOException
288
 
      {
289
 
         counter++;
290
 
         log.info("counter: " + counter);
291
 
         if (counter <= limit)
292
 
         {
293
 
            throw exception;
294
 
         }
295
 
         log.info("returning socket");
296
 
         return new Socket(arg0, arg1, arg2, arg3);
297
 
      }
298
 
   }
299
 
}
 
 
b'\\ No newline at end of file'