KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > remoting > transport > local > LocalInvokerTestCase


1 /*
2  * JBoss, the OpenSource J2EE webOS
3  *
4  * Distributable under LGPL license.
5  * See terms of license at gnu.org.
6  */

7 package org.jboss.remoting.transport.local;
8
9 import junit.framework.Test;
10 import junit.framework.TestCase;
11 import junit.framework.TestSuite;
12 import org.apache.log4j.Level;
13 import org.jboss.logging.Logger;
14 import org.jboss.remoting.Client;
15 import org.jboss.remoting.InvokerLocator;
16 import org.jboss.remoting.InvokerRegistry;
17 import org.jboss.remoting.invocation.NameBasedInvocation;
18 import org.jboss.remoting.transport.Connector;
19 import org.jboss.remoting.transport.mock.MockClientInvoker;
20 import org.jboss.remoting.transport.mock.MockServerInvoker;
21 import org.w3c.dom.Document JavaDoc;
22
23 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
24 import java.io.ByteArrayInputStream JavaDoc;
25 import java.rmi.server.UID JavaDoc;
26 import java.util.Random JavaDoc;
27
28 /**
29  * Test for the Local client invoker to verify will call directly on
30  * local server invoker.
31  *
32  * @author <a HREF="mailto:telrod@e2technologies.net">Tom Elrod</a>
33  */

34 public class LocalInvokerTestCase extends TestCase
35 {
36    private String JavaDoc sessionId = new UID JavaDoc().toString();
37    private Client client;
38    private String JavaDoc transport = "socket";
39    private int port = -1;
40    private InvokerLocator locator;
41    private static final String JavaDoc NAME = "LocalInvokerTest.class";
42    private static final Logger log = Logger.getLogger(LocalInvokerTestCase.class);
43
44    public LocalInvokerTestCase()
45    {
46       super(NAME);
47    }
48
49    public LocalInvokerTestCase(String JavaDoc name)
50    {
51       super(name);
52    }
53
54    public LocalInvokerTestCase(String JavaDoc transport, int port)
55    {
56       super(NAME);
57       this.transport = transport;
58       this.port = port;
59    }
60
61    public void init()
62    {
63       try
64       {
65          client = new Client(locator, "mock");
66          client.connect();
67       }
68       catch(Exception JavaDoc e)
69       {
70          log.error(e.getMessage(), e);
71       }
72    }
73
74    /**
75     * Create and initalize local server
76     *
77     * @param port
78     * @return
79     * @throws Exception
80     */

81    private InvokerLocator initServer(int port) throws Exception JavaDoc
82    {
83       if(port < 0)
84       {
85          port = Math.abs(new Random JavaDoc().nextInt(2000));
86       }
87       log.debug("port = " + port);
88
89       InvokerRegistry.registerInvoker("mock", MockClientInvoker.class, MockServerInvoker.class);
90       Connector connector = new Connector();
91       InvokerLocator locator = new InvokerLocator(transport + "://localhost:" + port);
92       StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
93       buf.append("<?xml version=\"1.0\"?>\n");
94       buf.append("<handlers>\n");
95       buf.append(" <handler subsystem=\"mock\">org.jboss.remoting.transport.mock.MockServerInvocationHandler</handler>\n");
96       buf.append("</handlers>\n");
97       Document JavaDoc xml = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new ByteArrayInputStream JavaDoc(buf.toString().getBytes()));
98       connector.setInvokerLocator(locator.getLocatorURI());
99       connector.setConfiguration(xml.getDocumentElement());
100       //connector.create();
101
connector.start();
102       this.port = port;
103       this.locator = connector.getLocator();
104       return locator;
105    }
106
107
108    public void testLocalInvoke() throws Throwable JavaDoc
109    {
110       try
111       {
112          log.debug("running testLocalInvoke()");
113
114          InvokerLocator locator = initServer(-1);
115          init();
116
117 // MockInvokerCallbackHandler handler = new MockInvokerCallbackHandler(sessionId);
118

119          log.debug("client.getInvoker()" + client.getInvoker());
120
121          // simple invoke, should return bar
122
Object JavaDoc ret = makeInvocation("foo", "bar");
123          assertTrue("Result of testLocalInvoke() invocation of foo.", "bar".equals(ret));
124          /*
125          client.addListener(handler, locator);
126          // invoke which should cause callback
127          ret = makeInvocation("test", "test");
128          // allow time for callback
129          Thread.sleep(3000);
130          log.debug("done sleeping.");
131          boolean callbackPerformed = handler.isCallbackReceived();
132          log.debug("callbackPerformed after adding listener is " + callbackPerformed);
133          assertTrue("Result of runPushCallbackTest() failed since did not get callback.",
134                     callbackPerformed);
135          handler.isCallbackReceived(false);
136          // Can now call direct on client
137          //ret = makeInvocation("removeListener", null);
138          client.removeListener(handler);
139          // shouldn't get callback now since removed listener
140          ret = makeInvocation("test", "test");
141          // allow time for callback
142          Thread.sleep(2000);
143          log.debug("done sleeping.");
144          callbackPerformed = handler.isCallbackReceived();
145          log.debug("callbackPerformed after removing listener is " + callbackPerformed);
146          assertTrue("Result of runPushCallbackTest() failed since did get callback " +
147                     "but have been removed as listener.",
148                     !callbackPerformed);
149      */

150       }
151       finally
152       {
153          if(client != null)
154          {
155             client.disconnect();
156          }
157       }
158    }
159
160    /**
161     * Tests simple invocation and pull callbacks. Meaning will add a listener and
162     * will then have to get the callbacks from the server.
163     * @throws Throwable
164     */

165    /*
166    public void testPullCallback() throws Throwable
167    {
168        try
169        {
170            log.debug("running testPullCallback()");
171
172            init();
173            // should be null by default, since don't have connector started, but setting anyway
174            //client.setClientLocator(null);
175
176            MockInvokerCallbackHandler handler = new MockInvokerCallbackHandler(sessionId);
177
178            // simple invoke, should return bar
179            Object ret = makeInvocation("bar", "foo");
180            assertTrue("Result of runPullCallbackTest() invocation of bar.", "foo".equals(ret));
181            client.addListener(handler);
182            // invoke which should cause callback on server side
183            ret = makeInvocation("test", "test");
184            // allow time for callback
185            Thread.sleep(2000);
186            ret = client.getCallbacks();
187            log.debug("getCallbacks returned " + ret);
188            log.debug("should have something.");
189            assertTrue("Result of runPullCallbackTest() getCallbacks() after add listener.",
190                       ret != null);
191            // can now call directly on client
192            //ret = makeInvocation("removeListener", null);
193            client.removeListener(handler);
194            ret = makeInvocation("getCallbacks", null);
195            log.debug("getCallbacks returned " + ret);
196            log.debug("should have been empty.");
197            assertTrue("Result of runPullCallbackTest() getCallbacks() after remove listener.",
198                       ret == null);
199        }
200        finally
201        {
202            if(client != null)
203            {
204                client.disconnect();
205                client = null;
206            }
207        }
208    }
209    */

210
211    /**
212     * Tests complex invocation to get object containing array of complex objects.
213     *
214     * @throws Throwable
215     */

216    /*
217    public void testArrayReturn() throws Throwable
218    {
219        try
220        {
221            init();
222
223            // simple invoke, should return bar
224            Object ret = makeInvocation("testComplexReturn", null);
225            ComplexReturn complexRet = (ComplexReturn)ret;
226            MockTest[] mockTests = complexRet.getMockTests();
227            assertTrue("ComplexReturn's array should contain 2 items",
228                       2 == mockTests.length);
229            for(int x = 0; x < mockTests.length; x++)
230            {
231                System.err.println(mockTests[x]);
232                MockTest test = mockTests[x];
233                assertNotNull("MockTest should not be null", test);
234            }
235
236 // assertTrue("Result of runPullCallbackTest() invocation of bar.",
237 // "foo".equals(ret));
238        }
239        finally
240        {
241            if(client != null)
242            {
243                client.disconnect();
244                client = null;
245            }
246        }
247    }
248    */

249
250    private Object JavaDoc makeInvocation(String JavaDoc method, String JavaDoc param) throws Throwable JavaDoc
251    {
252       Object JavaDoc ret = client.invoke(new NameBasedInvocation(method,
253                                                          new Object JavaDoc[]{param},
254                                                          new String JavaDoc[]{String JavaDoc.class.getName()}),
255                                  null);
256
257       return ret;
258    }
259
260    public static Test suite()
261    {
262       return new TestSuite(LocalInvokerTestCase.class);
263    }
264
265
266    public static void main(String JavaDoc[] args)
267    {
268       org.apache.log4j.BasicConfigurator.configure();
269       org.apache.log4j.Category.getRoot().setLevel(Level.DEBUG);
270
271       LocalInvokerTestCase client = null;
272       if(args.length == 2)
273       {
274          String JavaDoc transport = args[0];
275          int port = Integer.parseInt(args[1]);
276          client = new LocalInvokerTestCase(transport, port);
277       }
278       else
279       {
280          client = new LocalInvokerTestCase(LocalInvokerTestCase.class.getName());
281          System.out.println("Using default transport " +
282                             "and default port." +
283                             "\nCan enter transport and port.");
284       }
285
286       try
287       {
288          //regular class run
289
//client.testLocalInvoke();
290
//client.runInvokers();
291
// MultipleTestRunner runner = new MultipleTestRunner();
292
// runner.doRun(client, true);
293
junit.textui.TestRunner.run(suite());
294
295       }
296       catch(Throwable JavaDoc e)
297       {
298          e.printStackTrace();
299          System.exit(1);
300       }
301       System.exit(0);
302    }
303 }
304
Popular Tags