KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > test > session > TestSimpleSession


1 package test.session;
2
3 import junit.framework.TestCase;
4 import org.apache.axis.EngineConfiguration;
5 import org.apache.axis.MessageContext;
6 import org.apache.axis.client.Call;
7 import org.apache.axis.client.Service;
8 import org.apache.axis.configuration.DefaultEngineConfigurationFactory;
9 import org.apache.axis.configuration.SimpleProvider;
10 import org.apache.axis.configuration.XMLStringProvider;
11 import org.apache.axis.deployment.wsdd.WSDDConstants;
12 import org.apache.axis.handlers.SimpleSessionHandler;
13 import org.apache.axis.handlers.soap.SOAPService;
14 import org.apache.axis.providers.java.RPCProvider;
15 import org.apache.axis.server.AxisServer;
16 import org.apache.axis.session.Session;
17 import org.apache.axis.session.SimpleSession;
18 import org.apache.axis.transport.local.LocalTransport;
19
20 import javax.xml.rpc.ServiceException JavaDoc;
21 import javax.xml.rpc.server.ServiceLifecycle JavaDoc;
22
23 /**
24  * Test the SimpleSession implementation (using SOAP headers for session
25  * maintenance)
26  *
27  * @author Glen Daniels (gdaniels@apache.org)
28  */

29 public class TestSimpleSession extends TestCase implements ServiceLifecycle JavaDoc {
30     static final String JavaDoc clientWSDD =
31             "<deployment xmlns=\"http://xml.apache.org/axis/wsdd/\" " +
32                   "xmlns:java=\"" + WSDDConstants.URI_WSDD_JAVA + "\">\n" +
33             " <handler type=\"java:org.apache.axis.handlers.SimpleSessionHandler\" " +
34                       "name=\"SimpleSessionHandler\"/>\n" +
35             " <service name=\"sessionTest\">\n" +
36             " <requestFlow><handler type=\"SimpleSessionHandler\"/></requestFlow>\n" +
37             " <responseFlow><handler type=\"SimpleSessionHandler\"/></responseFlow>\n" +
38             " </service>\n" +
39             " <transport name=\"local\" " +
40                 "pivot=\"java:org.apache.axis.transport.local.LocalSender\"/>\n" +
41             "</deployment>";
42     static XMLStringProvider clientProvider = new XMLStringProvider(clientWSDD);
43
44     /**
45      * Default constructor for use as service
46      */

47     public TestSimpleSession()
48     {
49         super("serviceTest");
50     }
51
52     public TestSimpleSession(String JavaDoc name)
53     {
54         super(name);
55     }
56     
57     public void testSessionAPI() {
58         SimpleSession session = new SimpleSession();
59         Object JavaDoc val = new Float JavaDoc(5.6666);
60         session.set("test", val);
61         
62         assertEquals("\"test\" equals \"" + session.get("test") + "\", not \"" + val + "\" as expected",
63                      val, session.get("test"));
64         
65         session.remove("test");
66         
67         assertNull("Did not remove \"test\" from the session successfully", session.get("test"));
68     }
69
70     /**
71      * Actually test the session functionality using SOAP headers.
72      *
73      * Deploy a simple RPC service which returns a session-based call
74      * counter. Check it out using local transport. To do this we need to
75      * make sure the SimpleSessionHandler is deployed on the request and
76      * response chains of both the client and the server.
77      *
78      */

79     public void testSessionService() throws Exception JavaDoc
80     {
81         // Set up the server side
82
SimpleSessionHandler sessionHandler = new SimpleSessionHandler();
83         // Set a 3-second reap period, and a 3-second timeout
84
sessionHandler.setReapPeriodicity(3);
85         sessionHandler.setDefaultSessionTimeout(3);
86
87         SOAPService service = new SOAPService(sessionHandler,
88                                               new RPCProvider(),
89                                               sessionHandler);
90
91         service.setName("sessionTestService");
92         service.setOption("scope", "session");
93         service.setOption("className", "test.session.TestSimpleSession");
94         service.setOption("allowedMethods", "counter");
95
96         EngineConfiguration defaultConfig =
97             (new DefaultEngineConfigurationFactory()).getServerEngineConfig();
98         SimpleProvider config = new SimpleProvider(defaultConfig);
99         config.deployService("sessionTest", service);
100
101         AxisServer server = new AxisServer(config);
102
103         // Set up the client side (using the WSDD above)
104
Service svc = new Service(clientProvider);
105         Call call = (Call)svc.createCall();
106         svc.setMaintainSession(true);
107         call.setTransport(new LocalTransport(server));
108
109         // Try it - first invocation should return 1.
110
Integer JavaDoc count = (Integer JavaDoc)call.invoke("sessionTest", "counter", null);
111         assertNotNull("count was null!", count);
112         assertEquals("count was wrong", 1, count.intValue());
113
114         // We should have init()ed a single service object
115
assertEquals("Wrong # of calls to init()!", 1, initCalls);
116
117         // Next invocation should return 2, assuming the session-based
118
// counter is working.
119
count = (Integer JavaDoc)call.invoke("sessionTest", "counter", null);
120         assertEquals("count was wrong", 2, count.intValue());
121
122         // We should still have 1
123
assertEquals("Wrong # of calls to init()!", 1, initCalls);
124
125         // Now start fresh and confirm a new session
126
Service svc2 = new Service(clientProvider);
127         Call call2 = (Call)svc2.createCall();
128         svc2.setMaintainSession(true);
129         call2.setTransport(new LocalTransport(server));
130
131         // New session should cause us to return 1 again.
132
count = (Integer JavaDoc)call2.invoke("sessionTest", "counter", null);
133         assertNotNull("count was null on third call!", count);
134         assertEquals("New session count was incorrect", 1,
135                      count.intValue());
136
137         // We should have init()ed 2 service objects now
138
assertEquals("Wrong # of calls to init()!", 2, initCalls);
139         // And no destroy()s yet...
140
assertEquals("Shouldn't have called destroy() yet!", 0, destroyCalls);
141
142         // Wait around a few seconds to let the first session time out
143
Thread.sleep(4000);
144
145         // And now we should get a new session, therefore going back to 1
146
count = (Integer JavaDoc)call.invoke("sessionTest", "counter", null);
147         assertEquals("count after timeout was incorrect", 1, count.intValue());
148
149         // Check init/destroy counts
150
assertEquals("Wrong # of calls to init()!", 3, initCalls);
151         assertEquals("Wrong # of calls to destroy()!", 2, destroyCalls);
152     }
153
154     /**
155      * This is our service method for testing session data. Simply
156      * increments a session-scoped counter.
157      */

158     public Integer JavaDoc counter() throws Exception JavaDoc
159     {
160         Session session = MessageContext.getCurrentContext().getSession();
161         if (session == null)
162             throw new Exception JavaDoc("No session in MessageContext!");
163         Integer JavaDoc count = (Integer JavaDoc)session.get("counter");
164         if (count == null) {
165             count = new Integer JavaDoc(0);
166         }
167         count = new Integer JavaDoc(count.intValue() + 1);
168         session.set("counter", count);
169         return count;
170     }
171
172     public static void main(String JavaDoc args[]) throws Exception JavaDoc
173     {
174         TestSimpleSession test = new TestSimpleSession("test");
175         test.testSessionAPI();
176         test.testSessionService();
177     }
178
179     private static int initCalls = 0;
180     private static int destroyCalls = 0;
181
182     /**
183      * After a service endpoint object (an instance of a service
184      * endpoint class) is instantiated, the JAX-RPC runtime system
185      * invokes the init method.The service endpoint class uses the
186      * init method to initialize its configuration and setup access
187      * to any external resources.
188      * @param context Initialization context for a JAX-RPC service
189      endpoint; Carries javax.servlet.ServletContext
190      for the servlet based JAX-RPC endpoints
191      * @throws ServiceException If any error in initialization of the
192      service endpoint; or if any illegal context has
193      been provided in the init method
194      */

195     public void init(Object JavaDoc context) throws ServiceException JavaDoc {
196         initCalls++;
197     }
198
199     /**
200      * JAX-RPC runtime system ends the lifecycle of a service endpoint
201      * object by invoking the destroy method. The service endpoint
202      * releases its resourcesin the implementation of the destroy
203      * method.
204      */

205     public void destroy() {
206         destroyCalls++;
207     }
208 }
209
Popular Tags