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

« back to all changes in this revision

Viewing changes to tests/org/jboss/test/remoting/callback/timeout/CallbackTimeoutTestParent.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 2005, 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.callback.timeout;
23
 
 
24
 
import java.lang.reflect.Field;
25
 
import java.net.InetAddress;
26
 
import java.util.HashMap;
27
 
import java.util.HashSet;
28
 
import java.util.Iterator;
29
 
import java.util.Map;
30
 
import java.util.Set;
31
 
 
32
 
import javax.management.MBeanServer;
33
 
 
34
 
import junit.framework.TestCase;
35
 
 
36
 
import org.apache.log4j.ConsoleAppender;
37
 
import org.apache.log4j.Level;
38
 
import org.apache.log4j.Logger;
39
 
import org.apache.log4j.PatternLayout;
40
 
import org.jboss.remoting.Client;
41
 
import org.jboss.remoting.InvocationRequest;
42
 
import org.jboss.remoting.InvokerLocator;
43
 
import org.jboss.remoting.ServerInvocationHandler;
44
 
import org.jboss.remoting.ServerInvoker;
45
 
import org.jboss.remoting.callback.Callback;
46
 
import org.jboss.remoting.callback.HandleCallbackException;
47
 
import org.jboss.remoting.callback.InvokerCallbackHandler;
48
 
import org.jboss.remoting.callback.ServerInvokerCallbackHandler;
49
 
import org.jboss.remoting.transport.Connector;
50
 
import org.jboss.remoting.transport.PortUtil;
51
 
 
52
 
 
53
 
 
54
 
/**
55
 
 * Unit test for JBREM-799.
56
 
 * 
57
 
 * @author <a href="ron.sigal@jboss.com">Ron Sigal</a>
58
 
 * @version $Revision: 1.1.2.2 $
59
 
 * <p>
60
 
 * Copyright Jan 25, 2008
61
 
 * </p>
62
 
 */
63
 
abstract public class CallbackTimeoutTestParent extends TestCase
64
 
{
65
 
   public static int port;
66
 
   private static Logger log = Logger.getLogger(CallbackTimeoutTestParent.class);
67
 
   private static final String CALLBACK_TEST = "callbackTest";
68
 
 
69
 
   private static boolean firstTime = true;
70
 
 
71
 
   // remoting server connector
72
 
   private Connector connector;
73
 
   private InvokerLocator serverLocator;
74
 
   private TestInvocationHandler invocationHandler;
75
 
 
76
 
 
77
 
   /**
78
 
    * Sets up target remoting server.
79
 
    */
80
 
   public void setUp() throws Exception
81
 
   {
82
 
      if (firstTime)
83
 
      {
84
 
         firstTime = false;
85
 
         Logger.getLogger("org.jboss.remoting").setLevel(Level.INFO);
86
 
         Logger.getLogger("org.jboss.test.remoting").setLevel(Level.INFO);
87
 
         String pattern = "[%d{ABSOLUTE}] [%t] %5p (%F:%L) - %m%n";
88
 
         PatternLayout layout = new PatternLayout(pattern);
89
 
         ConsoleAppender consoleAppender = new ConsoleAppender(layout);
90
 
         Logger.getRootLogger().addAppender(consoleAppender);  
91
 
      }
92
 
   }
93
 
 
94
 
 
95
 
   public void tearDown()
96
 
   {
97
 
   }
98
 
 
99
 
 
100
 
   /**
101
 
    * Verifies that "timeout" value is used in absence of "callbackTimeout" value.
102
 
    */
103
 
   public void testNoCallbackTimeout() throws Throwable
104
 
   {
105
 
      log.info("entering " + getName());
106
 
 
107
 
      // Start server.
108
 
      String host = InetAddress.getLocalHost().getHostAddress();
109
 
      port = PortUtil.findFreePort(host);
110
 
      String locatorURI = getTransport() + "://" + host + ":" + port; 
111
 
      serverLocator = new InvokerLocator(locatorURI);
112
 
      log.info("Starting remoting server with locator uri of: " + locatorURI);
113
 
      HashMap config = new HashMap();
114
 
      config.put(InvokerLocator.FORCE_REMOTE, "true");
115
 
      config.put(ServerInvoker.TIMEOUT, "3000");
116
 
      addExtraServerConfig(config);
117
 
      connector = new Connector(serverLocator, config);
118
 
      connector.create();
119
 
      invocationHandler = new TestInvocationHandler();
120
 
      connector.addInvocationHandler("sample", invocationHandler);
121
 
      connector.start();
122
 
 
123
 
      // Create client.
124
 
      HashMap clientConfig = new HashMap();
125
 
      clientConfig.put(InvokerLocator.FORCE_REMOTE, "true");
126
 
      addExtraClientConfig(clientConfig);
127
 
      final Client client = new Client(serverLocator, clientConfig);
128
 
      client.connect();
129
 
      assertEquals("abc", client.invoke("abc"));
130
 
      log.info("client is connected");
131
 
 
132
 
      // Add callback handler.
133
 
      TestInvokerCallbackHandler testCallbackHandler = new TestInvokerCallbackHandler();
134
 
      client.addListener(testCallbackHandler, new HashMap(), null, true);
135
 
      client.invoke(CALLBACK_TEST);
136
 
      assertEquals(1, testCallbackHandler.counter);
137
 
      log.info("callback handler is installed");
138
 
      
139
 
      // Verify that "timeout" is used correctly.
140
 
      ServerInvoker serverInvoker = connector.getServerInvoker();
141
 
      Map configuration = serverInvoker.getConfiguration();
142
 
      assertEquals("3000", configuration.get(ServerInvoker.TIMEOUT));
143
 
 
144
 
      // Verify that "timeout" is used in the absence of "callbackTimeout".
145
 
      Field field = ServerInvoker.class.getDeclaredField("callbackHandlers");
146
 
      field.setAccessible(true);
147
 
      Map callbackHandlers = (Map) field.get(serverInvoker);
148
 
      assertEquals(1, callbackHandlers.size());
149
 
      Object o = callbackHandlers.values().iterator().next();
150
 
      ServerInvokerCallbackHandler callbackHandler = (ServerInvokerCallbackHandler) o;
151
 
      field = ServerInvokerCallbackHandler.class.getDeclaredField("callBackClient");
152
 
      field.setAccessible(true);
153
 
      Client callBackClient = (Client) field.get(callbackHandler);
154
 
      configuration = callBackClient.getConfiguration();
155
 
      assertEquals("3000", configuration.get(ServerInvoker.TIMEOUT));
156
 
 
157
 
      client.removeListener(testCallbackHandler);
158
 
      client.disconnect();
159
 
      connector.stop();
160
 
   }
161
 
 
162
 
 
163
 
   /**
164
 
    * Verifies that "callbackTimeout" value overrides "timeout" value.
165
 
    * Values are specified in configuration map.
166
 
    */
167
 
   public void testDistinctCallbackTimeoutConfigMap() throws Throwable
168
 
   {
169
 
      log.info("entering " + getName());
170
 
 
171
 
      // Start server.
172
 
      String host = InetAddress.getLocalHost().getHostAddress();
173
 
      port = PortUtil.findFreePort(host);
174
 
      String locatorURI = getTransport() + "://" + host + ":" + port; 
175
 
      serverLocator = new InvokerLocator(locatorURI);
176
 
      log.info("Starting remoting server with locator uri of: " + locatorURI);
177
 
      HashMap config = new HashMap();
178
 
      config.put(InvokerLocator.FORCE_REMOTE, "true");
179
 
      config.put(ServerInvoker.TIMEOUT, "3000");
180
 
      config.put(ServerInvokerCallbackHandler.CALLBACK_TIMEOUT, "7000");
181
 
      addExtraServerConfig(config);
182
 
      connector = new Connector(serverLocator, config);
183
 
      connector.create();
184
 
      invocationHandler = new TestInvocationHandler();
185
 
      connector.addInvocationHandler("sample", invocationHandler);
186
 
      connector.start();
187
 
 
188
 
      // Create client.
189
 
      HashMap clientConfig = new HashMap();
190
 
      clientConfig.put(InvokerLocator.FORCE_REMOTE, "true");
191
 
      addExtraClientConfig(clientConfig);
192
 
      final Client client = new Client(serverLocator, clientConfig);
193
 
      client.connect();
194
 
      assertEquals("abc", client.invoke("abc"));
195
 
      log.info("client is connected");
196
 
 
197
 
      // Add callback handler.
198
 
      TestInvokerCallbackHandler testCallbackHandler = new TestInvokerCallbackHandler();
199
 
      client.addListener(testCallbackHandler, new HashMap(), null, true);
200
 
      client.invoke(CALLBACK_TEST);
201
 
      assertEquals(1, testCallbackHandler.counter);
202
 
      log.info("callback handler is installed");
203
 
 
204
 
      // Verify that "timeout" is used correctly.
205
 
      ServerInvoker serverInvoker = connector.getServerInvoker();
206
 
      Map configuration = serverInvoker.getConfiguration();
207
 
      assertEquals("3000", configuration.get(ServerInvoker.TIMEOUT));
208
 
 
209
 
      // Verify that "callbackTimeout" is used correctly.
210
 
      Field field = ServerInvoker.class.getDeclaredField("callbackHandlers");
211
 
      field.setAccessible(true);
212
 
      Map callbackHandlers = (Map) field.get(serverInvoker);
213
 
      assertEquals(1, callbackHandlers.size());
214
 
      Object o = callbackHandlers.values().iterator().next();
215
 
      ServerInvokerCallbackHandler callbackHandler = (ServerInvokerCallbackHandler) o;
216
 
      field = ServerInvokerCallbackHandler.class.getDeclaredField("callBackClient");
217
 
      field.setAccessible(true);
218
 
      Client callBackClient = (Client) field.get(callbackHandler);
219
 
      configuration = callBackClient.getConfiguration();
220
 
      assertEquals("7000", configuration.get(ServerInvoker.TIMEOUT));
221
 
 
222
 
      client.removeListener(testCallbackHandler);
223
 
      client.disconnect();
224
 
      connector.stop();
225
 
   }
226
 
 
227
 
 
228
 
   /**
229
 
    * Verifies that "callbackTimeout" value overrides "timeout" value.
230
 
    * Values are specified in InvokerLocator.
231
 
    */
232
 
   public void testDistinctCallbackTimeoutLocator() throws Throwable
233
 
   {
234
 
      log.info("entering " + getName());
235
 
 
236
 
      // Start server.
237
 
      String host = InetAddress.getLocalHost().getHostAddress();
238
 
      port = PortUtil.findFreePort(host);
239
 
      String locatorURI = getTransport() + "://" + host + ":" + port; 
240
 
      locatorURI += "/?timeout=3000&callbackTimeout=7000";
241
 
      serverLocator = new InvokerLocator(locatorURI);
242
 
      log.info("Starting remoting server with locator uri of: " + locatorURI);
243
 
      HashMap config = new HashMap();
244
 
      config.put(InvokerLocator.FORCE_REMOTE, "true");
245
 
      addExtraServerConfig(config);
246
 
      connector = new Connector(serverLocator, config);
247
 
      connector.create();
248
 
      invocationHandler = new TestInvocationHandler();
249
 
      connector.addInvocationHandler("sample", invocationHandler);
250
 
      connector.start();
251
 
 
252
 
      // Create client.
253
 
      HashMap clientConfig = new HashMap();
254
 
      clientConfig.put(InvokerLocator.FORCE_REMOTE, "true");
255
 
      addExtraClientConfig(clientConfig);
256
 
      final Client client = new Client(serverLocator, clientConfig);
257
 
      client.connect();
258
 
      assertEquals("abc", client.invoke("abc"));
259
 
      log.info("client is connected");
260
 
 
261
 
      // Add callback handler.
262
 
      TestInvokerCallbackHandler testCallbackHandler = new TestInvokerCallbackHandler();
263
 
      client.addListener(testCallbackHandler, new HashMap(), null, true);
264
 
      client.invoke(CALLBACK_TEST);
265
 
      assertEquals(1, testCallbackHandler.counter);
266
 
      log.info("callback handler is installed");
267
 
 
268
 
      // Verify that "timeout" is used correctly.
269
 
      ServerInvoker serverInvoker = connector.getServerInvoker();
270
 
      Map configuration = serverInvoker.getConfiguration();
271
 
      assertEquals("3000", configuration.get(ServerInvoker.TIMEOUT));
272
 
 
273
 
      // Verify that "callbackTimeout" is used correctly.
274
 
      Field field = ServerInvoker.class.getDeclaredField("callbackHandlers");
275
 
      field.setAccessible(true);
276
 
      Map callbackHandlers = (Map) field.get(serverInvoker);
277
 
      assertEquals(1, callbackHandlers.size());
278
 
      Object o = callbackHandlers.values().iterator().next();
279
 
      ServerInvokerCallbackHandler callbackHandler = (ServerInvokerCallbackHandler) o;
280
 
      field = ServerInvokerCallbackHandler.class.getDeclaredField("callBackClient");
281
 
      field.setAccessible(true);
282
 
      Client callBackClient = (Client) field.get(callbackHandler);
283
 
      configuration = callBackClient.getConfiguration();
284
 
      assertEquals("7000", configuration.get(ServerInvoker.TIMEOUT));
285
 
 
286
 
      client.removeListener(testCallbackHandler);
287
 
      client.disconnect();
288
 
      connector.stop();
289
 
   }
290
 
 
291
 
 
292
 
   /**
293
 
    * Verifies that "timeout" value is used in absense of "callbackTimeout".
294
 
    * "timeout" value is specified in Client.addListener() metadata map.
295
 
    */
296
 
   public void testNoCallbackTimeoutMetadata() throws Throwable
297
 
   {
298
 
      log.info("entering " + getName());
299
 
 
300
 
      // Start server.
301
 
      String host = InetAddress.getLocalHost().getHostAddress();
302
 
      port = PortUtil.findFreePort(host);
303
 
      String locatorURI = getTransport() + "://" + host + ":" + port; 
304
 
      serverLocator = new InvokerLocator(locatorURI);
305
 
      log.info("Starting remoting server with locator uri of: " + locatorURI);
306
 
      HashMap config = new HashMap();
307
 
      config.put(InvokerLocator.FORCE_REMOTE, "true");
308
 
      addExtraServerConfig(config);
309
 
      connector = new Connector(serverLocator, config);
310
 
      connector.create();
311
 
      invocationHandler = new TestInvocationHandler();
312
 
      connector.addInvocationHandler("sample", invocationHandler);
313
 
      connector.start();
314
 
 
315
 
      // Create client.
316
 
      HashMap clientConfig = new HashMap();
317
 
      clientConfig.put(InvokerLocator.FORCE_REMOTE, "true");
318
 
      addExtraClientConfig(clientConfig);
319
 
      final Client client = new Client(serverLocator, clientConfig);
320
 
      client.connect();
321
 
      assertEquals("abc", client.invoke("abc"));
322
 
      log.info("client is connected");
323
 
 
324
 
      // Add callback handler.
325
 
      TestInvokerCallbackHandler testCallbackHandler = new TestInvokerCallbackHandler();
326
 
      Map metadata = new HashMap();
327
 
      metadata.put(ServerInvoker.TIMEOUT, "3000");
328
 
      client.addListener(testCallbackHandler, metadata, null, true);
329
 
      client.invoke(CALLBACK_TEST);
330
 
      assertEquals(1, testCallbackHandler.counter);
331
 
      log.info("callback handler is installed");
332
 
 
333
 
      // Verify that "timeout" is used in the absence of "callbackTimeout".
334
 
      ServerInvoker serverInvoker = connector.getServerInvoker();
335
 
      Field field = ServerInvoker.class.getDeclaredField("callbackHandlers");
336
 
      field.setAccessible(true);
337
 
      Map callbackHandlers = (Map) field.get(serverInvoker);
338
 
      assertEquals(1, callbackHandlers.size());
339
 
      Object o = callbackHandlers.values().iterator().next();
340
 
      ServerInvokerCallbackHandler callbackHandler = (ServerInvokerCallbackHandler) o;
341
 
      field = ServerInvokerCallbackHandler.class.getDeclaredField("callBackClient");
342
 
      field.setAccessible(true);
343
 
      Client callBackClient = (Client) field.get(callbackHandler);
344
 
      Map configuration = callBackClient.getConfiguration();
345
 
      assertEquals("3000", configuration.get(ServerInvoker.TIMEOUT));
346
 
 
347
 
      client.removeListener(testCallbackHandler);
348
 
      client.disconnect();
349
 
      connector.stop();
350
 
   }
351
 
 
352
 
 
353
 
   /**
354
 
    * Verifies that "callbackTimeout" value overrides "timeout" value.
355
 
    * "callbackTimeout" is specified in Client.addListener() metadata map.
356
 
    */
357
 
   public void testDistinctCallbackTimeoutMetadata() throws Throwable
358
 
   {
359
 
      log.info("entering " + getName());
360
 
 
361
 
      // Start server.
362
 
      String host = InetAddress.getLocalHost().getHostAddress();
363
 
      port = PortUtil.findFreePort(host);
364
 
      String locatorURI = getTransport() + "://" + host + ":" + port; 
365
 
      serverLocator = new InvokerLocator(locatorURI);
366
 
      log.info("Starting remoting server with locator uri of: " + locatorURI);
367
 
      HashMap config = new HashMap();
368
 
      config.put(InvokerLocator.FORCE_REMOTE, "true");
369
 
      addExtraServerConfig(config);
370
 
      connector = new Connector(serverLocator, config);
371
 
      connector.create();
372
 
      invocationHandler = new TestInvocationHandler();
373
 
      connector.addInvocationHandler("sample", invocationHandler);
374
 
      connector.start();
375
 
 
376
 
      // Create client.
377
 
      HashMap clientConfig = new HashMap();
378
 
      clientConfig.put(InvokerLocator.FORCE_REMOTE, "true");
379
 
      addExtraClientConfig(clientConfig);
380
 
      final Client client = new Client(serverLocator, clientConfig);
381
 
      client.connect();
382
 
      assertEquals("abc", client.invoke("abc"));
383
 
      log.info("client is connected");
384
 
 
385
 
      // Add callback handler.
386
 
      TestInvokerCallbackHandler testCallbackHandler = new TestInvokerCallbackHandler();
387
 
      Map metadata = new HashMap();
388
 
      metadata.put(ServerInvokerCallbackHandler.CALLBACK_TIMEOUT, "7000");
389
 
      client.addListener(testCallbackHandler, metadata, null, true);
390
 
      client.invoke(CALLBACK_TEST);
391
 
      assertEquals(1, testCallbackHandler.counter);
392
 
      log.info("callback handler is installed");
393
 
 
394
 
      // Verify that "callbackTimeout" is used correctly.
395
 
      ServerInvoker serverInvoker = connector.getServerInvoker();
396
 
      Field field = ServerInvoker.class.getDeclaredField("callbackHandlers");
397
 
      field.setAccessible(true);
398
 
      Map callbackHandlers = (Map) field.get(serverInvoker);
399
 
      assertEquals(1, callbackHandlers.size());
400
 
      Object o = callbackHandlers.values().iterator().next();
401
 
      ServerInvokerCallbackHandler callbackHandler = (ServerInvokerCallbackHandler) o;
402
 
      field = ServerInvokerCallbackHandler.class.getDeclaredField("callBackClient");
403
 
      field.setAccessible(true);
404
 
      Client callBackClient = (Client) field.get(callbackHandler);
405
 
      Map configuration = callBackClient.getConfiguration();
406
 
      assertEquals("7000", configuration.get(ServerInvoker.TIMEOUT));
407
 
 
408
 
      client.removeListener(testCallbackHandler);
409
 
      client.disconnect();
410
 
      connector.stop();
411
 
   }
412
 
 
413
 
 
414
 
   /**
415
 
    * Verifies that "callbackTimeout" specified in Client.addListener() metadata
416
 
    * map overrides values in server InvokerLocator and server configuration map.
417
 
    */
418
 
   public void testMetadataOverridesLocatorAndConfigMap() throws Throwable
419
 
   {
420
 
      log.info("entering " + getName());
421
 
 
422
 
      // Start server.
423
 
      String host = InetAddress.getLocalHost().getHostAddress();
424
 
      port = PortUtil.findFreePort(host);
425
 
      String locatorURI = getTransport() + "://" + host + ":" + port; 
426
 
      locatorURI += "/?callbackTimeout=3000";
427
 
      serverLocator = new InvokerLocator(locatorURI);
428
 
      log.info("Starting remoting server with locator uri of: " + locatorURI);
429
 
      HashMap config = new HashMap();
430
 
      config.put(InvokerLocator.FORCE_REMOTE, "true");
431
 
      config.put(ServerInvokerCallbackHandler.CALLBACK_TIMEOUT, "7000");
432
 
      addExtraServerConfig(config);
433
 
      connector = new Connector(serverLocator, config);
434
 
      connector.create();
435
 
      invocationHandler = new TestInvocationHandler();
436
 
      connector.addInvocationHandler("sample", invocationHandler);
437
 
      connector.start();
438
 
 
439
 
      // Create client.
440
 
      HashMap clientConfig = new HashMap();
441
 
      clientConfig.put(InvokerLocator.FORCE_REMOTE, "true");
442
 
      addExtraClientConfig(clientConfig);
443
 
      final Client client = new Client(serverLocator, clientConfig);
444
 
      client.connect();
445
 
      assertEquals("abc", client.invoke("abc"));
446
 
      log.info("client is connected");
447
 
 
448
 
      // Add callback handler.
449
 
      TestInvokerCallbackHandler testCallbackHandler = new TestInvokerCallbackHandler();
450
 
      Map metadata = new HashMap();
451
 
      metadata.put(ServerInvokerCallbackHandler.CALLBACK_TIMEOUT, "11000");
452
 
      client.addListener(testCallbackHandler, metadata, null, true);
453
 
      client.invoke(CALLBACK_TEST);
454
 
      assertEquals(1, testCallbackHandler.counter);
455
 
      log.info("callback handler is installed");
456
 
 
457
 
      // Verify that "callbackTimeout" from Client.addListener() metadata
458
 
      // has precedence.
459
 
      ServerInvoker serverInvoker = connector.getServerInvoker();
460
 
      Field field = ServerInvoker.class.getDeclaredField("callbackHandlers");
461
 
      field.setAccessible(true);
462
 
      Map callbackHandlers = (Map) field.get(serverInvoker);
463
 
      assertEquals(1, callbackHandlers.size());
464
 
      Object o = callbackHandlers.values().iterator().next();
465
 
      ServerInvokerCallbackHandler callbackHandler = (ServerInvokerCallbackHandler) o;
466
 
      field = ServerInvokerCallbackHandler.class.getDeclaredField("callBackClient");
467
 
      field.setAccessible(true);
468
 
      Client callBackClient = (Client) field.get(callbackHandler);
469
 
      Map configuration = callBackClient.getConfiguration();
470
 
      assertEquals("11000", configuration.get(ServerInvoker.TIMEOUT));
471
 
 
472
 
      client.removeListener(testCallbackHandler);
473
 
      client.disconnect();
474
 
      connector.stop();
475
 
   }
476
 
   
477
 
   
478
 
   /**
479
 
    * Verifies that "callbackTimeout" specified in server InvokerLocator
480
 
    * overrides value specified in server configuration map.
481
 
    */
482
 
   public void testLocatorOverridesConfigMap() throws Throwable
483
 
   {
484
 
      log.info("entering " + getName());
485
 
 
486
 
      // Start server.
487
 
      String host = InetAddress.getLocalHost().getHostAddress();
488
 
      port = PortUtil.findFreePort(host);
489
 
      String locatorURI = getTransport() + "://" + host + ":" + port; 
490
 
      locatorURI += "/?callbackTimeout=3000";
491
 
      serverLocator = new InvokerLocator(locatorURI);
492
 
      log.info("Starting remoting server with locator uri of: " + locatorURI);
493
 
      HashMap config = new HashMap();
494
 
      config.put(InvokerLocator.FORCE_REMOTE, "true");
495
 
      config.put(ServerInvokerCallbackHandler.CALLBACK_TIMEOUT, "7000");
496
 
      addExtraServerConfig(config);
497
 
      connector = new Connector(serverLocator, config);
498
 
      connector.create();
499
 
      invocationHandler = new TestInvocationHandler();
500
 
      connector.addInvocationHandler("sample", invocationHandler);
501
 
      connector.start();
502
 
 
503
 
      // Create client.
504
 
      HashMap clientConfig = new HashMap();
505
 
      clientConfig.put(InvokerLocator.FORCE_REMOTE, "true");
506
 
      addExtraClientConfig(clientConfig);
507
 
      final Client client = new Client(serverLocator, clientConfig);
508
 
      client.connect();
509
 
      assertEquals("abc", client.invoke("abc"));
510
 
      log.info("client is connected");
511
 
 
512
 
      // Add callback handler.
513
 
      TestInvokerCallbackHandler testCallbackHandler = new TestInvokerCallbackHandler();
514
 
      client.addListener(testCallbackHandler, new HashMap(), null, true);
515
 
      client.invoke(CALLBACK_TEST);
516
 
      assertEquals(1, testCallbackHandler.counter);
517
 
      log.info("callback handler is installed");
518
 
 
519
 
      // Verify that "callbackTimeout" from InvokerLocator overrides value
520
 
      // in configuration map.
521
 
      ServerInvoker serverInvoker = connector.getServerInvoker();
522
 
      Field field = ServerInvoker.class.getDeclaredField("callbackHandlers");
523
 
      field.setAccessible(true);
524
 
      Map callbackHandlers = (Map) field.get(serverInvoker);
525
 
      assertEquals(1, callbackHandlers.size());
526
 
      Object o = callbackHandlers.values().iterator().next();
527
 
      ServerInvokerCallbackHandler callbackHandler = (ServerInvokerCallbackHandler) o;
528
 
      field = ServerInvokerCallbackHandler.class.getDeclaredField("callBackClient");
529
 
      field.setAccessible(true);
530
 
      Client callBackClient = (Client) field.get(callbackHandler);
531
 
      Map configuration = callBackClient.getConfiguration();
532
 
      assertEquals("3000", configuration.get(ServerInvoker.TIMEOUT));
533
 
 
534
 
      client.removeListener(testCallbackHandler);
535
 
      client.disconnect();
536
 
      connector.stop();
537
 
   }
538
 
   
539
 
   
540
 
   abstract protected String getTransport();
541
 
   
542
 
   
543
 
   protected void addExtraClientConfig(Map config) {}
544
 
   protected void addExtraServerConfig(Map config) {}
545
 
   
546
 
 
547
 
   static class TestInvocationHandler implements ServerInvocationHandler
548
 
   {
549
 
      public Set listeners = new HashSet();
550
 
      
551
 
      public void addListener(InvokerCallbackHandler callbackHandler)
552
 
      {
553
 
         listeners.add(callbackHandler);
554
 
      }
555
 
      public Object invoke(final InvocationRequest invocation) throws Throwable
556
 
      {
557
 
         if (CALLBACK_TEST.equals(invocation.getParameter()))
558
 
         {
559
 
            Iterator it = listeners.iterator();
560
 
            while (it.hasNext())
561
 
            {
562
 
               InvokerCallbackHandler handler = (InvokerCallbackHandler) it.next();
563
 
               handler.handleCallback(new Callback("test"));
564
 
            }
565
 
         }
566
 
         return invocation.getParameter();
567
 
      }
568
 
      public void removeListener(InvokerCallbackHandler callbackHandler) {}
569
 
      public void setMBeanServer(MBeanServer server) {}
570
 
      public void setInvoker(ServerInvoker invoker) {}
571
 
   }
572
 
   
573
 
   
574
 
   static class TestInvokerCallbackHandler implements InvokerCallbackHandler
575
 
   {
576
 
      public int counter;
577
 
      
578
 
      public void handleCallback(Callback callback) throws HandleCallbackException
579
 
      {
580
 
         counter++;
581
 
      }
582
 
   }
583
 
}
 
 
b'\\ No newline at end of file'