KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > dynaop > remote > RemoteTest


1 package dynaop.remote;
2
3 import java.net.URL JavaDoc;
4
5 import org.mortbay.http.HttpContext;
6 import org.mortbay.http.HttpServer;
7 import org.mortbay.http.SocketListener;
8 import org.mortbay.jetty.servlet.ServletHandler;
9 import org.mortbay.jetty.servlet.ServletHolder;
10
11 import junit.framework.TestCase;
12
13 /**
14  *
15  *
16  * @author Bob Lee (crazybob@crazybob.org)
17  */

18 public class RemoteTest extends TestCase {
19
20     static final int PORT = 12345;
21     
22     public void testMyService() throws Exception JavaDoc {
23         HttpServer server = new HttpServer();
24         try {
25             // set up server.
26
SocketListener listener = new SocketListener();
27             listener.setPort(PORT);
28             server.addListener(listener);
29
30             // map service servlet.
31
HttpContext context = server.getContext("/");
32             ServletHandler handler = new ServletHandler();
33             ServletHolder holder =
34                 handler.addServlet("MyService", "/myService",
35                     Service.class.getName());
36             holder.setInitParameter("class", MyServiceImpl.class.getName());
37             context.addHandler(handler);
38
39             // start server.
40
server.start();
41
42             // invoke service.
43
MyService stub = (MyService) new Home().create(
44                 new URL JavaDoc("http://localhost:" + PORT + "/myService"),
45                 MyService.class
46             );
47             assertEquals("TEST", stub.toUpperCase("test"));
48             assertEquals(5, stub.add(2, 3));
49             try {
50                 stub.throwException();
51             }
52             catch (MyException e) {}
53         }
54         finally {
55             server.stop();
56         }
57     }
58
59     /*
60     public void testPerformance() throws Exception {
61         HttpServer server = new HttpServer();
62         try {
63             // set up server.
64             SocketListener listener = new SocketListener();
65             listener.setPort(PORT);
66             server.addListener(listener);
67
68             // map service servlet.
69             HttpContext context = server.getContext("/");
70             ServletHandler handler = new ServletHandler();
71             ServletHolder holder =
72                 handler.addServlet("MyService", "/myService",
73                     Service.class.getName());
74             holder.setInitParameter("class", MyServiceImpl.class.getName());
75             context.addHandler(handler);
76
77             // start server.
78             server.start();
79             
80             // invoke service.
81             MyService stub = (MyService) new Home().create(
82                 new URL("http://localhost:" + PORT + "/myService"),
83                 MyService.class
84             );
85
86             stub.add(1, 1);
87             
88             long start = System.currentTimeMillis();
89             for (int i = 0; i < 1000; i++)
90                 stub.add(1, 1);
91             System.out.println("*** " + (System.currentTimeMillis() - start));
92         }
93         finally {
94             server.stop();
95         }
96     }
97     */

98 }
99
Popular Tags