KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > remoting > oneway > OnewayInvokerClientTest


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.oneway;
8
9 import org.apache.log4j.Level;
10 import org.jboss.dtf.MultipleTestRunner;
11 import org.jboss.logging.Logger;
12 import org.jboss.remoting.AbstractInvokerTest;
13 import org.jboss.remoting.Client;
14 import org.jboss.remoting.InvokerLocator;
15 import org.jboss.remoting.invocation.NameBasedInvocation;
16
17 import java.rmi.server.UID JavaDoc;
18
19 /**
20  * This is the actual concrete test for the invoker client to test oneway calls (client and server based).
21  *
22  * @author <a HREF="mailto:telrod@e2technologies.net">Tom Elrod</a>
23  */

24 public class OnewayInvokerClientTest extends AbstractInvokerTest
25 {
26    private static final Logger log = Logger.getLogger(OnewayInvokerClientTest.class);
27
28    private String JavaDoc sessionId = new UID JavaDoc().toString();
29
30    private Client client;
31
32    private static final String JavaDoc NAME = "OnewayInvokerClientTest.class";
33
34    public OnewayInvokerClientTest(String JavaDoc name)
35    {
36       super(NAME);
37    }
38
39    public OnewayInvokerClientTest(int numberOfInstances)
40    {
41       super(NAME, numberOfInstances);
42    }
43
44    public OnewayInvokerClientTest(String JavaDoc transport, int port)
45    {
46       super(NAME, transport, port);
47    }
48
49    public OnewayInvokerClientTest(String JavaDoc transport, int port, int numberOfInstances)
50    {
51       super(NAME, transport, port, numberOfInstances);
52    }
53
54    public void init()
55    {
56       try
57       {
58          InvokerLocator locator = new InvokerLocator(transport + "://localhost:" + port);
59          client = new Client(locator, "test");
60          client.connect();
61       }
62       catch(Exception JavaDoc e)
63       {
64          log.error(e.getMessage(), e);
65       }
66    }
67
68    public void runInvokers() throws Throwable JavaDoc
69    {
70       startup(getNumberOfInstances());
71       try
72       {
73          log.debug("\n\n*****************************" +
74                    "Calling testOnewayServerInvocation" +
75                    "*******************************\n");
76          testOnewayServerInvocation();
77          log.debug("\n\n*****************************" +
78                    "Calling testOnewayClientInvocation" +
79                    "*******************************\n");
80          testOnewayClientInvocation();
81       }
82       finally
83       {
84          shutdown();
85       }
86    }
87
88    /**
89     * Test simple oneway client invocation
90     *
91     * @throws Throwable
92     */

93    public void testOnewayServerInvocation() throws Throwable JavaDoc
94    {
95       try
96       {
97          log.debug("running testOnewayClientCallback()");
98
99          sessionId = new UID JavaDoc().toString();
100          init();
101
102          sessionId = client.getSessionId();
103
104          log.debug("client.getInvoker().getLocator()" + client.getInvoker().getLocator());
105
106          // simple invoke
107
String JavaDoc param = "bar";
108          makeServerOnewayInvocation("saveInvocationParameter", param);
109          Thread.currentThread().sleep(1000);
110          Object JavaDoc obj = makeInvocation("getLastInvocationParameter", null);
111
112          checkAssertion(param, obj);
113       }
114       finally
115       {
116          if(client != null)
117          {
118             client.disconnect();
119          }
120       }
121    }
122
123    protected void checkAssertion(String JavaDoc param, Object JavaDoc obj)
124    {
125       assertEquals(param, obj);
126    }
127
128    protected void makeServerOnewayInvocation(String JavaDoc method, String JavaDoc param) throws Throwable JavaDoc
129    {
130       client.invokeOneway(new NameBasedInvocation(method,
131                                                   new Object JavaDoc[]{param},
132                                                   new String JavaDoc[]{String JavaDoc.class.getName()}),
133                           null,
134                           false);
135
136    }
137
138    /**
139     * Test simple oneway client invocation
140     *
141     * @throws Throwable
142     */

143    public void testOnewayClientInvocation() throws Throwable JavaDoc
144    {
145       try
146       {
147          log.debug("running testOnewayClientCallback()");
148
149          sessionId = new UID JavaDoc().toString();
150          init();
151
152          sessionId = client.getSessionId();
153
154          log.debug("client.getInvoker().getLocator()" + client.getInvoker().getLocator());
155
156
157          // simple invoke
158
String JavaDoc param = "bar";
159          makeClientOnewayInvocation("saveInvocationParameter", param);
160          Thread.currentThread().sleep(1000);
161          Object JavaDoc obj = makeInvocation("getLastInvocationParameter", null);
162
163          checkAssertion(param, obj);
164
165       }
166       finally
167       {
168          if(client != null)
169          {
170             client.disconnect();
171          }
172       }
173    }
174
175    /**
176     * Test simple oneway client invocation
177     *
178     * @throws Throwable
179     */

180    public void testClientInvocation() throws Throwable JavaDoc
181    {
182       try
183       {
184          log.debug("running testInvocation()");
185
186          sessionId = new UID JavaDoc().toString();
187          init();
188
189          sessionId = client.getSessionId();
190
191          log.debug("client.getInvoker().getLocator()" + client.getInvoker().getLocator());
192
193
194          // simple invoke
195
String JavaDoc param = "bar";
196          Object JavaDoc resp = makeClientInvocation("saveInvocationParameter", param);
197
198          Object JavaDoc obj = makeInvocation("getLastInvocationParameter", null);
199          Thread.currentThread().sleep(1000);
200          checkAssertion(param, obj);
201
202       }
203       finally
204       {
205          if(client != null)
206          {
207             client.disconnect();
208          }
209       }
210    }
211
212    protected void makeClientOnewayInvocation(String JavaDoc method, String JavaDoc param) throws Throwable JavaDoc
213    {
214       client.invokeOneway(new NameBasedInvocation(method,
215                                                   new Object JavaDoc[]{param},
216                                                   new String JavaDoc[]{String JavaDoc.class.getName()}),
217                           null,
218                           true);
219
220    }
221
222    protected Object JavaDoc makeClientInvocation(String JavaDoc method, String JavaDoc param) throws Throwable JavaDoc
223    {
224       Object JavaDoc ret = client.invoke(new NameBasedInvocation(method,
225                                                          new Object JavaDoc[]{param},
226                                                          new String JavaDoc[]{String JavaDoc.class.getName()}),
227                                  null);
228
229       return ret;
230    }
231
232
233    protected Object JavaDoc makeInvocation(String JavaDoc method, String JavaDoc param) throws Throwable JavaDoc
234    {
235       Object JavaDoc ret = client.invoke(new NameBasedInvocation(method,
236                                                          new Object JavaDoc[]{param},
237                                                          new String JavaDoc[]{String JavaDoc.class.getName()}),
238                                  null);
239
240       return ret;
241    }
242
243
244    public static void main(String JavaDoc[] args)
245    {
246
247       org.apache.log4j.BasicConfigurator.configure();
248       org.apache.log4j.Category.getRoot().setLevel(Level.INFO);
249       org.apache.log4j.Category.getInstance("org.jgroups").setLevel(Level.INFO);
250       org.apache.log4j.Category.getInstance("org.jboss.remoting").setLevel(Level.DEBUG);
251       org.apache.log4j.Category.getInstance("test").setLevel(Level.DEBUG);
252
253       OnewayInvokerClientTest client = null;
254       if(args.length == 1)
255       {
256          int instances = Integer.parseInt(args[0]);
257          client = new OnewayInvokerClientTest(instances);
258       }
259       else if(args.length == 2)
260       {
261          String JavaDoc transport = args[0];
262          int port = Integer.parseInt(args[1]);
263          client = new OnewayInvokerClientTest(transport, port);
264       }
265       else if(args.length == 3)
266       {
267          String JavaDoc transport = args[0];
268          int port = Integer.parseInt(args[1]);
269          int instances = Integer.parseInt(args[2]);
270          client = new OnewayInvokerClientTest(transport, port, instances);
271       }
272       else
273       {
274          client = new OnewayInvokerClientTest(OnewayInvokerClientTest.class.getName());
275          System.out.println("Using default transport (" + client.getTransport() +
276                             ") and default port (" + client.getPort() + ") and " +
277                             "default number of instances (" + client.getNumberOfInstances() + ")" +
278                             "\nCan enter transport, port, and instances via command line.");
279       }
280
281       try
282       {
283          //regular class run
284
//client.runInvokers();
285
MultipleTestRunner runner = new MultipleTestRunner();
286          runner.doRun(client, true);
287       }
288       catch(Throwable JavaDoc e)
289       {
290          e.printStackTrace();
291          System.exit(1);
292       }
293       System.exit(0);
294    }
295 }
296
Popular Tags