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

« back to all changes in this revision

Viewing changes to tests/org/jboss/test/remoting/transport/local/LocalInvokerTestCase.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.local;
24
 
 
25
 
import junit.framework.Test;
26
 
import junit.framework.TestCase;
27
 
import junit.framework.TestSuite;
28
 
import org.apache.log4j.Level;
29
 
import org.jboss.logging.Logger;
30
 
import org.jboss.remoting.Client;
31
 
import org.jboss.remoting.InvokerLocator;
32
 
import org.jboss.remoting.invocation.NameBasedInvocation;
33
 
import org.jboss.remoting.transport.Connector;
34
 
import org.jboss.test.remoting.TestUtil;
35
 
import org.w3c.dom.Document;
36
 
 
37
 
import javax.xml.parsers.DocumentBuilderFactory;
38
 
import java.io.ByteArrayInputStream;
39
 
import java.rmi.server.UID;
40
 
 
41
 
/**
42
 
 * Test for the Local client invoker to verify will call directly on
43
 
 * local server invoker.
44
 
 *
45
 
 * @author <a href="mailto:telrod@e2technologies.net">Tom Elrod</a>
46
 
 */
47
 
public class LocalInvokerTestCase extends TestCase
48
 
{
49
 
   private String sessionId = new UID().toString();
50
 
   private Client client;
51
 
   private String transport = "socket";
52
 
   private int port = -1;
53
 
   private InvokerLocator locator;
54
 
   private static final String NAME = "LocalInvokerTest.class";
55
 
   private static final Logger log = Logger.getLogger(LocalInvokerTestCase.class);
56
 
 
57
 
   public LocalInvokerTestCase()
58
 
   {
59
 
      super(NAME);
60
 
   }
61
 
 
62
 
   public LocalInvokerTestCase(String name)
63
 
   {
64
 
      super(name);
65
 
   }
66
 
 
67
 
   public LocalInvokerTestCase(String transport, int port)
68
 
   {
69
 
      super(NAME);
70
 
      this.transport = transport;
71
 
      this.port = port;
72
 
   }
73
 
 
74
 
   public void init()
75
 
   {
76
 
      try
77
 
      {
78
 
         client = new Client(locator, "mock");
79
 
         client.connect();
80
 
      }
81
 
      catch(Exception e)
82
 
      {
83
 
         log.error(e.getMessage(), e);
84
 
      }
85
 
   }
86
 
 
87
 
   /**
88
 
    * Create and initalize local server
89
 
    *
90
 
    * @param port
91
 
    * @return
92
 
    * @throws Exception
93
 
    */
94
 
   private InvokerLocator initServer(int port) throws Exception
95
 
   {
96
 
      if(port < 0)
97
 
      {
98
 
         port = TestUtil.getRandomPort();
99
 
      }
100
 
      log.debug("port = " + port);
101
 
 
102
 
//      InvokerRegistry.registerInvoker("mock", MockClientInvoker.class, MockServerInvoker.class);
103
 
      Connector connector = new Connector();
104
 
      InvokerLocator locator = new InvokerLocator(transport + "://localhost:" + port);
105
 
      StringBuffer buf = new StringBuffer();
106
 
      buf.append("<?xml version=\"1.0\"?>\n");
107
 
      buf.append("<handlers>\n");
108
 
      buf.append("  <handler subsystem=\"mock\">org.jboss.test.remoting.transport.mock.MockServerInvocationHandler</handler>\n");
109
 
      buf.append("</handlers>\n");
110
 
      Document xml = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new ByteArrayInputStream(buf.toString().getBytes()));
111
 
      connector.setInvokerLocator(locator.getLocatorURI());
112
 
      connector.setConfiguration(xml.getDocumentElement());
113
 
      //connector.create();
114
 
      connector.start();
115
 
      this.port = port;
116
 
      this.locator = connector.getLocator();
117
 
      return locator;
118
 
   }
119
 
 
120
 
 
121
 
   public void testLocalInvoke() throws Throwable
122
 
   {
123
 
      try
124
 
      {
125
 
         log.debug("running testLocalInvoke()");
126
 
 
127
 
         InvokerLocator locator = initServer(-1);
128
 
         init();
129
 
 
130
 
//            MockInvokerCallbackHandler handler = new MockInvokerCallbackHandler(sessionId);
131
 
 
132
 
         log.debug("client.getInvoker()" + client.getInvoker());
133
 
 
134
 
         // simple invoke, should return bar
135
 
         Object ret = makeInvocation("foo", "bar");
136
 
         assertTrue("Result of testLocalInvoke() invocation of foo.", "bar".equals(ret));
137
 
         /*
138
 
         client.addListener(handler, locator);
139
 
         // invoke which should cause callback
140
 
         ret = makeInvocation("test", "test");
141
 
         // allow time for callback
142
 
         Thread.sleep(3000);
143
 
         log.debug("done sleeping.");
144
 
         boolean callbackPerformed = handler.isCallbackReceived();
145
 
         log.debug("callbackPerformed after adding listener is " + callbackPerformed);
146
 
         assertTrue("Result of runPushCallbackTest() failed since did not get callback.",
147
 
                    callbackPerformed);
148
 
         handler.isCallbackReceived(false);
149
 
         // Can now call direct on client
150
 
         //ret = makeInvocation("removeListener", null);
151
 
         client.removeListener(handler);
152
 
         // shouldn't get callback now since removed listener
153
 
         ret = makeInvocation("test", "test");
154
 
         // allow time for callback
155
 
         Thread.sleep(2000);
156
 
         log.debug("done sleeping.");
157
 
         callbackPerformed = handler.isCallbackReceived();
158
 
         log.debug("callbackPerformed after removing listener is " + callbackPerformed);
159
 
         assertTrue("Result of runPushCallbackTest() failed since did get callback " +
160
 
                    "but have been removed as listener.",
161
 
                    !callbackPerformed);
162
 
     */
163
 
      }
164
 
      finally
165
 
      {
166
 
         if(client != null)
167
 
         {
168
 
            client.disconnect();
169
 
         }
170
 
      }
171
 
   }
172
 
 
173
 
   /**
174
 
    * Tests simple invocation and pull callbacks.  Meaning will add a listener and
175
 
    * will then have to get the callbacks from the server.
176
 
    * @throws Throwable
177
 
    */
178
 
   /*
179
 
   public void testPullCallback() throws Throwable
180
 
   {
181
 
       try
182
 
       {
183
 
           log.debug("running testPullCallback()");
184
 
 
185
 
           init();
186
 
           // should be null by default, since don't have connector started, but setting anyway
187
 
           //client.setClientLocator(null);
188
 
 
189
 
           MockInvokerCallbackHandler handler = new MockInvokerCallbackHandler(sessionId);
190
 
 
191
 
           // simple invoke, should return bar
192
 
           Object ret = makeInvocation("bar", "foo");
193
 
           assertTrue("Result of runPullCallbackTest() invocation of bar.", "foo".equals(ret));
194
 
           client.addListener(handler);
195
 
           // invoke which should cause callback on server side
196
 
           ret = makeInvocation("test", "test");
197
 
           // allow time for callback
198
 
           Thread.sleep(2000);
199
 
           ret = client.getCallbacks();
200
 
           log.debug("getCallbacks returned " + ret);
201
 
           log.debug("should have something.");
202
 
           assertTrue("Result of runPullCallbackTest() getCallbacks() after add listener.",
203
 
                      ret != null);
204
 
           // can now call directly on client
205
 
           //ret = makeInvocation("removeListener", null);
206
 
           client.removeListener(handler);
207
 
           ret = makeInvocation("getCallbacks", null);
208
 
           log.debug("getCallbacks returned " + ret);
209
 
           log.debug("should have been empty.");
210
 
           assertTrue("Result of runPullCallbackTest() getCallbacks() after remove listener.",
211
 
                      ret == null);
212
 
       }
213
 
       finally
214
 
       {
215
 
           if(client != null)
216
 
           {
217
 
               client.disconnect();
218
 
               client = null;
219
 
           }
220
 
       }
221
 
   }
222
 
   */
223
 
 
224
 
   /**
225
 
    * Tests complex invocation to get object containing array of complex objects.
226
 
    *
227
 
    * @throws Throwable
228
 
    */
229
 
   /*
230
 
   public void testArrayReturn() throws Throwable
231
 
   {
232
 
       try
233
 
       {
234
 
           init();
235
 
 
236
 
           // simple invoke, should return bar
237
 
           Object ret = makeInvocation("testComplexReturn", null);
238
 
           ComplexReturn complexRet = (ComplexReturn)ret;
239
 
           MockTest[] mockTests = complexRet.getMockTests();
240
 
           assertTrue("ComplexReturn's array should contain 2 items",
241
 
                      2 == mockTests.length);
242
 
           for(int x = 0; x < mockTests.length; x++)
243
 
           {
244
 
               System.err.println(mockTests[x]);
245
 
               MockTest test = mockTests[x];
246
 
               assertNotNull("MockTest should not be null", test);
247
 
           }
248
 
 
249
 
//            assertTrue("Result of runPullCallbackTest() invocation of bar.",
250
 
//                       "foo".equals(ret));
251
 
       }
252
 
       finally
253
 
       {
254
 
           if(client != null)
255
 
           {
256
 
               client.disconnect();
257
 
               client = null;
258
 
           }
259
 
       }
260
 
   }
261
 
   */
262
 
   private Object makeInvocation(String method, String param) throws Throwable
263
 
   {
264
 
      Object ret = client.invoke(new NameBasedInvocation(method,
265
 
                                                         new Object[]{param},
266
 
                                                         new String[]{String.class.getName()}),
267
 
                                 null);
268
 
 
269
 
      return ret;
270
 
   }
271
 
 
272
 
   public static Test suite()
273
 
   {
274
 
      return new TestSuite(LocalInvokerTestCase.class);
275
 
   }
276
 
 
277
 
 
278
 
   public static void main(String[] args)
279
 
   {
280
 
      org.apache.log4j.BasicConfigurator.configure();
281
 
      org.apache.log4j.Category.getRoot().setLevel(Level.DEBUG);
282
 
 
283
 
      LocalInvokerTestCase client = null;
284
 
      if(args.length == 2)
285
 
      {
286
 
         String transport = args[0];
287
 
         int port = Integer.parseInt(args[1]);
288
 
         client = new LocalInvokerTestCase(transport, port);
289
 
      }
290
 
      else
291
 
      {
292
 
         client = new LocalInvokerTestCase(LocalInvokerTestCase.class.getName());
293
 
         System.out.println("Using default transport " +
294
 
                            "and default port." +
295
 
                            "\nCan enter transport and port.");
296
 
      }
297
 
 
298
 
      try
299
 
      {
300
 
         //regular class run
301
 
         //client.testLocalInvoke();
302
 
         //client.runInvokers();
303
 
//            MultipleTestRunner runner = new MultipleTestRunner();
304
 
//            runner.doRun(client, true);
305
 
         junit.textui.TestRunner.run(suite());
306
 
 
307
 
      }
308
 
      catch(Throwable e)
309
 
      {
310
 
         e.printStackTrace();
311
 
         System.exit(1);
312
 
      }
313
 
      System.exit(0);
314
 
   }
315
 
}