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

« back to all changes in this revision

Viewing changes to tests/org/jboss/test/remoting/transport/InvokerClientTest.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 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
 
 
23
 
package org.jboss.test.remoting.transport;
24
 
 
25
 
import junit.framework.TestCase;
26
 
import org.apache.log4j.Level;
27
 
import org.jboss.logging.Logger;
28
 
import org.jboss.remoting.Client;
29
 
import org.jboss.remoting.InvokerLocator;
30
 
import org.jboss.remoting.ServerInvocationHandler;
31
 
import org.jboss.remoting.invocation.NameBasedInvocation;
32
 
import org.jboss.remoting.transport.Connector;
33
 
import org.jboss.test.remoting.ComplexReturn;
34
 
import org.jboss.test.remoting.TestUtil;
35
 
import org.jboss.test.remoting.performance.synchronous.PerformanceTestCase;
36
 
import org.jboss.test.remoting.transport.mock.MockInvokerCallbackHandler;
37
 
import org.jboss.test.remoting.transport.mock.MockServerInvocationHandler;
38
 
import org.jboss.test.remoting.transport.mock.MockTest;
39
 
 
40
 
import java.net.BindException;
41
 
import java.rmi.server.UID;
42
 
 
43
 
 
44
 
/**
45
 
 * This is the actual concrete test for the invoker client.  Uses socket transport by default.
46
 
 *
47
 
 * @author <a href="mailto:telrod@e2technologies.net">Tom Elrod</a>
48
 
 */
49
 
public abstract class InvokerClientTest extends TestCase
50
 
{
51
 
   private String sessionId = new UID().toString();
52
 
   private Client client;
53
 
   protected Connector connector;
54
 
   protected InvokerLocator locator;
55
 
 
56
 
   protected int port = 9091; //default port
57
 
   protected int callbackPort = -1;
58
 
   protected String metadata = null;
59
 
 
60
 
   public static final Logger log = Logger.getLogger(InvokerClientTest.class);
61
 
 
62
 
   public abstract String getTransport();
63
 
 
64
 
   public int getCallbackPort()
65
 
   {
66
 
      return callbackPort;
67
 
   }
68
 
 
69
 
   public void init()
70
 
   {
71
 
      try
72
 
      {
73
 
         String host = System.getProperty("jrunit.bind_addr", "localhost");
74
 
         String locatorURI = getTransport() + "://" + host+ ":" + port;
75
 
         if(metadata != null)
76
 
         {
77
 
            locatorURI = locatorURI + "/?" + metadata;
78
 
         }
79
 
         log.info("connecting to: " + locatorURI);
80
 
         InvokerLocator locator = new InvokerLocator(locatorURI);
81
 
 
82
 
         client = new Client(locator, "mock");
83
 
         client.connect();
84
 
      }
85
 
      catch(Exception e)
86
 
      {
87
 
         log.error(e.getMessage(), e);
88
 
      }
89
 
   }
90
 
 
91
 
   /**
92
 
    * This will be used to create callback server
93
 
    *
94
 
    * @param port
95
 
    * @return
96
 
    * @throws Exception
97
 
    */
98
 
   protected InvokerLocator initServer(int port) throws Exception
99
 
   {
100
 
      if(port < 0)
101
 
      {
102
 
         port = TestUtil.getRandomPort();
103
 
      }
104
 
      log.debug("port = " + port);
105
 
 
106
 
//      InvokerRegistry.registerInvoker("mock", MockClientInvoker.class, MockServerInvoker.class);
107
 
      connector = new Connector();
108
 
 
109
 
      String locatorURI = getTransport() + "://localhost:" + port;
110
 
      if(metadata != null)
111
 
      {
112
 
         locatorURI = locatorURI + "/?" + metadata;
113
 
      }
114
 
      InvokerLocator locator = new InvokerLocator(locatorURI);
115
 
 
116
 
      connector.setInvokerLocator(locator.getLocatorURI());
117
 
      connector.create();
118
 
      connector.addInvocationHandler(getSubsystem(), getServerInvocationHandler());
119
 
      connector.start();
120
 
 
121
 
      return locator;
122
 
   }
123
 
 
124
 
   protected String getSubsystem()
125
 
   {
126
 
      return "mock";
127
 
   }
128
 
 
129
 
   protected ServerInvocationHandler getServerInvocationHandler()
130
 
   {
131
 
      return new MockServerInvocationHandler();
132
 
   }
133
 
 
134
 
 
135
 
   public void setUp() throws Exception
136
 
   {
137
 
      org.apache.log4j.BasicConfigurator.configure();
138
 
      org.apache.log4j.Category.getRoot().setLevel(Level.INFO);
139
 
 
140
 
      String newMetadata = System.getProperty(PerformanceTestCase.REMOTING_METADATA);
141
 
      if(newMetadata != null && newMetadata.length() > 0)
142
 
      {
143
 
         metadata = newMetadata;
144
 
         log.info("Using metadata: " + metadata);
145
 
      }
146
 
 
147
 
      newMetadata = System.getProperty(PerformanceTestCase.REMOTING_METADATA_CALLBACK);
148
 
      if(newMetadata != null && newMetadata.length() > 0)
149
 
      {
150
 
         if(metadata == null)
151
 
         {
152
 
            metadata = newMetadata;
153
 
         }
154
 
         else
155
 
         {
156
 
            metadata += newMetadata;
157
 
         }
158
 
 
159
 
         log.info("Using metadata: " + metadata);
160
 
      }
161
 
 
162
 
      // this is a retry hack because in some cases, can get duplicate callback server ports
163
 
      // when trying to find a free one.
164
 
      int retryLimit = 3;
165
 
      for(int x = 0; x < retryLimit; x++)
166
 
      {
167
 
         try
168
 
         {
169
 
            locator = initServer(getCallbackPort());
170
 
         }
171
 
         catch(BindException e)
172
 
         {
173
 
            if(x + 1 == retryLimit)
174
 
            {
175
 
               throw e;
176
 
            }
177
 
            else
178
 
            {
179
 
               continue;
180
 
            }
181
 
         }
182
 
         break;
183
 
      }
184
 
 
185
 
      init();
186
 
 
187
 
   }
188
 
 
189
 
   public void tearDown() throws Exception
190
 
   {
191
 
      if(connector != null)
192
 
      {
193
 
         connector.stop();
194
 
         connector.destroy();
195
 
         connector = null;
196
 
      }
197
 
      locator = null;
198
 
      if(client != null)
199
 
      {
200
 
         client.disconnect();
201
 
         client = null;
202
 
      }
203
 
   }
204
 
 
205
 
   /**
206
 
    * Test simple invocation and adding of listener with push callback (meaning server
207
 
    * will send callback message when it gets it) to a local callback server
208
 
    *
209
 
    * @throws Throwable
210
 
    */
211
 
   public void testLocalPushCallback() throws Throwable
212
 
   {
213
 
      log.debug("running testLocalPushCallback()");
214
 
 
215
 
      sessionId = new UID().toString();
216
 
 
217
 
      sessionId = client.getSessionId();
218
 
      MockInvokerCallbackHandler handler = new MockInvokerCallbackHandler(sessionId);
219
 
 
220
 
      log.debug("client.getInvoker().getLocator()" + client.getInvoker().getLocator());
221
 
 
222
 
      // simple invoke, should return bar
223
 
      Object ret = makeInvocation("foo", "bar");
224
 
      assertTrue("Result of testLocalPushCallback() invocation of foo.", "bar".equals(ret));
225
 
      if("bar".equals(ret))
226
 
      {
227
 
         log.debug("PASS");
228
 
      }
229
 
      else
230
 
      {
231
 
         log.debug("FAILED - testLocalPushCallback1");
232
 
      }
233
 
 
234
 
      client.addListener(handler, locator);
235
 
      // invoke which should cause callback
236
 
      ret = makeInvocation("test", "test");
237
 
      // allow time for callback
238
 
      Thread.sleep(3000);
239
 
      log.debug("done sleeping.");
240
 
      int callbacksPerformed = handler.isCallbackReceived();
241
 
      log.debug("callbacksPerformed after adding listener is " + callbacksPerformed);
242
 
      assertTrue("Result of testLocalPushCallback() failed since did not get callback.",
243
 
                 (callbacksPerformed == 1));
244
 
      if(callbacksPerformed == 1)
245
 
      {
246
 
         log.debug("PASS");
247
 
      }
248
 
      else
249
 
      {
250
 
         log.debug("FAILED - testLocalPushCallback2");
251
 
      }
252
 
      // Can now call direct on client
253
 
      client.removeListener(handler);
254
 
      // shouldn't get callback now since removed listener
255
 
      ret = makeInvocation("test", "test");
256
 
      // allow time for callback
257
 
      Thread.sleep(2000);
258
 
      log.debug("done sleeping.");
259
 
      callbacksPerformed = handler.isCallbackReceived();
260
 
      log.debug("callbackPerformed after removing listener is " + callbacksPerformed);
261
 
      assertTrue("Result of testLocalPushCallback() failed since did get callback " +
262
 
                 "but have been removed as listener.",
263
 
                 (callbacksPerformed == 1));
264
 
      if(callbacksPerformed == 1)
265
 
      {
266
 
         log.debug("PASS");
267
 
      }
268
 
      else
269
 
      {
270
 
         log.debug("FAILED - testLocalPushCallback3");
271
 
      }
272
 
 
273
 
   }
274
 
 
275
 
   /**
276
 
    * Test simple invocation and adding of listener with push callback (meaning server
277
 
    * will send callback message when it gets it) to a remote callback server
278
 
    *
279
 
    * @throws Throwable
280
 
    */
281
 
   public void testRemotePushCallback() throws Throwable
282
 
   {
283
 
      log.debug("running testRemotePushCallback()");
284
 
 
285
 
      sessionId = new UID().toString();
286
 
      //InvokerLocator locator = client.getInvoker().getLocator();
287
 
      sessionId = client.getSessionId();
288
 
      MockInvokerCallbackHandler handler = new MockInvokerCallbackHandler(sessionId);
289
 
 
290
 
      log.debug("client.getInvoker().getLocator()" + client.getInvoker().getLocator());
291
 
 
292
 
      // simple invoke, should return bar
293
 
      Object ret = makeInvocation("foo", "bar");
294
 
      assertTrue("Result of testRemotePushCallback() invocation of foo.", "bar".equals(ret));
295
 
      if("bar".equals(ret))
296
 
      {
297
 
         log.debug("PASS");
298
 
      }
299
 
      else
300
 
      {
301
 
         log.debug("FAILED - testRemotePushCallback1");
302
 
      }
303
 
 
304
 
      client.addListener(handler, locator);
305
 
      // invoke which should cause callback
306
 
      ret = makeInvocation("test", "test");
307
 
      // allow time for callback
308
 
      Thread.sleep(3000);
309
 
      log.debug("done sleeping.");
310
 
      // TODO: No way to currently check the remote callback handler
311
 
      // to see if it got callback -TME
312
 
      /*
313
 
      int callbacksPerformed = handler.isCallbackReceived();
314
 
      log.debug("callbacksPerformed after adding listener is " + callbacksPerformed);
315
 
      assertTrue("Result of testRemotePushCallback() failed since did not get callback.",
316
 
                 (callbacksPerformed == 1));
317
 
      */
318
 
      // Can now call direct on client
319
 
      client.removeListener(handler);
320
 
      // shouldn't get callback now since removed listener
321
 
      ret = makeInvocation("test", "test");
322
 
      // allow time for callback
323
 
      Thread.sleep(2000);
324
 
      log.debug("done sleeping.");
325
 
      /*
326
 
      callbacksPerformed = handler.isCallbackReceived();
327
 
      log.debug("callbackPerformed after removing listener is " + callbacksPerformed);
328
 
      assertTrue("Result of testRemotePushCallback() failed since did get callback " +
329
 
                 "but have been removed as listener.",
330
 
                 (callbacksPerformed == 1));
331
 
      */
332
 
   }
333
 
 
334
 
   /**
335
 
    * Tests simple invocation and pull callbacks.  Meaning will add a listener and
336
 
    * will then have to get the callbacks from the server.
337
 
    *
338
 
    * @throws Throwable
339
 
    */
340
 
   public void testPullCallback() throws Throwable
341
 
   {
342
 
      log.debug("running testPullCallback()");
343
 
 
344
 
      // should be null by default, since don't have connector started, but setting anyway
345
 
      //client.setClientLocator(null);
346
 
 
347
 
      MockInvokerCallbackHandler handler = new MockInvokerCallbackHandler(sessionId);
348
 
 
349
 
      // simple invoke, should return bar
350
 
      Object ret = makeInvocation("bar", "foo");
351
 
      assertTrue("Result of runPullCallbackTest() invocation of bar.", "foo".equals(ret));
352
 
      if("foo".equals(ret))
353
 
      {
354
 
         log.debug("PASS");
355
 
      }
356
 
      else
357
 
      {
358
 
         log.debug("FAILED - testPullCallback1");
359
 
      }
360
 
 
361
 
      client.addListener(handler);
362
 
      // invoke which should cause callback on server side
363
 
      ret = makeInvocation("test", "test");
364
 
      // allow time for callback
365
 
      Thread.sleep(5000);
366
 
      ret = client.getCallbacks(handler);
367
 
      log.debug("getCallbacks returned " + ret);
368
 
      log.debug("should have something.");
369
 
      assertTrue("Result of runPullCallbackTest() getCallbacks() after add listener.",
370
 
                 ret != null);
371
 
      if(ret != null)
372
 
      {
373
 
         log.debug("PASS");
374
 
      }
375
 
      else
376
 
      {
377
 
         log.debug("FAILED - testPullCallback2");
378
 
      }
379
 
 
380
 
      // can now call directly on client
381
 
      //ret = makeInvocation("removeListener", null);
382
 
      client.removeListener(handler);
383
 
      ret = makeInvocation("getCallbacks", null);
384
 
      log.debug("getCallbacks returned " + ret);
385
 
      log.debug("should have been empty.");
386
 
      assertTrue("Result of runPullCallbackTest() getCallbacks() after remove listener.",
387
 
                 ret == null);
388
 
      if(ret == null)
389
 
      {
390
 
         log.debug("PASS");
391
 
      }
392
 
      else
393
 
      {
394
 
         log.debug("FAILED - testPullCallback3");
395
 
      }
396
 
 
397
 
   }
398
 
 
399
 
   /**
400
 
    * Tests complex invocation to get object containing array of complex objects.
401
 
    *
402
 
    * @throws Throwable
403
 
    */
404
 
   public void testArrayReturn() throws Throwable
405
 
   {
406
 
      // simple invoke, should return bar
407
 
      Object ret = makeInvocation("testComplexReturn", null);
408
 
      ComplexReturn complexRet = (ComplexReturn) ret;
409
 
      MockTest[] mockTests = complexRet.getMockTests();
410
 
      assertTrue("ComplexReturn's array should contain 2 items",
411
 
                 2 == mockTests.length);
412
 
      if(2 == mockTests.length)
413
 
      {
414
 
         log.debug("PASS");
415
 
      }
416
 
      else
417
 
      {
418
 
         log.debug("FAILED - testArrayReturn1");
419
 
      }
420
 
 
421
 
      for(int x = 0; x < mockTests.length; x++)
422
 
      {
423
 
         System.err.println(mockTests[x]);
424
 
         MockTest test = mockTests[x];
425
 
         assertNotNull("MockTest should not be null", test);
426
 
         if(test != null)
427
 
         {
428
 
            log.debug("PASS");
429
 
         }
430
 
         else
431
 
         {
432
 
            log.debug("FAILED - testArrayReturn2");
433
 
         }
434
 
 
435
 
      }
436
 
 
437
 
//            assertTrue("Result of runPullCallbackTest() invocation of bar.",
438
 
//                       "foo".equals(ret));
439
 
   }
440
 
 
441
 
   public void testThrownException() throws Throwable
442
 
   {
443
 
      try
444
 
      {
445
 
         makeInvocation("testException", null);
446
 
         assertTrue("Did not get exception thrown as expected.", false);
447
 
      }
448
 
      catch(Exception throwable)
449
 
      {
450
 
         assertTrue("Got exception thrown as expected.", true);
451
 
      }
452
 
   }
453
 
 
454
 
   protected Client getClient()
455
 
   {
456
 
      return client;
457
 
   }
458
 
 
459
 
   protected Object makeInvocation(String method, String param) throws Throwable
460
 
   {
461
 
      return client.invoke(new NameBasedInvocation(method,
462
 
                                                   new Object[]{param},
463
 
                                                   new String[]{String.class.getName()}),
464
 
                           null);
465
 
   }
466
 
}