FileDocCategorySizeDatePackage
TestSimpleSession.javaAPI DocApache Axis 1.48197Sat Apr 22 18:57:28 BST 2006test.session

TestSimpleSession

public class TestSimpleSession extends TestCase implements javax.xml.rpc.server.ServiceLifecycle
Test the SimpleSession implementation (using SOAP headers for session maintenance)
author
Glen Daniels (gdaniels@apache.org)

Fields Summary
static final String
clientWSDD
static org.apache.axis.configuration.XMLStringProvider
clientProvider
private static int
initCalls
private static int
destroyCalls
Constructors Summary
public TestSimpleSession()
Default constructor for use as service


               
     
    
        super("serviceTest");
    
public TestSimpleSession(String name)

        super(name);
    
Methods Summary
public java.lang.Integercounter()
This is our service method for testing session data. Simply increments a session-scoped counter.

        Session session = MessageContext.getCurrentContext().getSession();
        if (session == null)
            throw new Exception("No session in MessageContext!");
        Integer count = (Integer)session.get("counter");
        if (count == null) {
            count = new Integer(0);
        }
        count = new Integer(count.intValue() + 1);
        session.set("counter", count);
        return count;
    
public voiddestroy()
JAX-RPC runtime system ends the lifecycle of a service endpoint object by invoking the destroy method. The service endpoint releases its resourcesin the implementation of the destroy method.

        destroyCalls++;
    
public voidinit(java.lang.Object context)
After a service endpoint object (an instance of a service endpoint class) is instantiated, the JAX-RPC runtime system invokes the init method.The service endpoint class uses the init method to initialize its configuration and setup access to any external resources.

param
context Initialization context for a JAX-RPC service endpoint; Carries javax.servlet.ServletContext for the servlet based JAX-RPC endpoints
throws
ServiceException If any error in initialization of the service endpoint; or if any illegal context has been provided in the init method


                                                                                                              
          
        initCalls++;
    
public static voidmain(java.lang.String[] args)

        TestSimpleSession test = new TestSimpleSession("test");
        test.testSessionAPI();
        test.testSessionService();
    
public voidtestSessionAPI()

        SimpleSession session = new SimpleSession();
        Object val = new Float(5.6666);
        session.set("test", val);
        
        assertEquals("\"test\" equals \"" + session.get("test") + "\", not \"" + val + "\" as expected",
                     val, session.get("test"));
        
        session.remove("test");
        
        assertNull("Did not remove \"test\" from the session successfully", session.get("test"));
    
public voidtestSessionService()
Actually test the session functionality using SOAP headers. Deploy a simple RPC service which returns a session-based call counter. Check it out using local transport. To do this we need to make sure the SimpleSessionHandler is deployed on the request and response chains of both the client and the server.

        // Set up the server side
        SimpleSessionHandler sessionHandler = new SimpleSessionHandler();
        // Set a 3-second reap period, and a 3-second timeout
        sessionHandler.setReapPeriodicity(3);
        sessionHandler.setDefaultSessionTimeout(3);

        SOAPService service = new SOAPService(sessionHandler,
                                              new RPCProvider(),
                                              sessionHandler);

        service.setName("sessionTestService");
        service.setOption("scope", "session");
        service.setOption("className", "test.session.TestSimpleSession");
        service.setOption("allowedMethods", "counter");

        EngineConfiguration defaultConfig =
            (new DefaultEngineConfigurationFactory()).getServerEngineConfig();
        SimpleProvider config = new SimpleProvider(defaultConfig);
        config.deployService("sessionTest", service);

        AxisServer server = new AxisServer(config);

        // Set up the client side (using the WSDD above)
        Service svc = new Service(clientProvider);
        Call call = (Call)svc.createCall();
        svc.setMaintainSession(true);
        call.setTransport(new LocalTransport(server));

        // Try it - first invocation should return 1.
        Integer count = (Integer)call.invoke("sessionTest", "counter", null);
        assertNotNull("count was null!", count);
        assertEquals("count was wrong", 1, count.intValue());

        // We should have init()ed a single service object
        assertEquals("Wrong # of calls to init()!", 1, initCalls);

        // Next invocation should return 2, assuming the session-based
        // counter is working.
        count = (Integer)call.invoke("sessionTest", "counter", null);
        assertEquals("count was wrong", 2, count.intValue());

        // We should still have 1
        assertEquals("Wrong # of calls to init()!", 1, initCalls);

        // Now start fresh and confirm a new session
        Service svc2 = new Service(clientProvider);
        Call call2 = (Call)svc2.createCall();
        svc2.setMaintainSession(true);
        call2.setTransport(new LocalTransport(server));

        // New session should cause us to return 1 again.
        count = (Integer)call2.invoke("sessionTest", "counter", null);
        assertNotNull("count was null on third call!", count);
        assertEquals("New session count was incorrect", 1,
                     count.intValue());

        // We should have init()ed 2 service objects now
        assertEquals("Wrong # of calls to init()!", 2, initCalls);
        // And no destroy()s yet...
        assertEquals("Shouldn't have called destroy() yet!", 0, destroyCalls);

        // Wait around a few seconds to let the first session time out
        Thread.sleep(4000);

        // And now we should get a new session, therefore going back to 1
        count = (Integer)call.invoke("sessionTest", "counter", null);
        assertEquals("count after timeout was incorrect", 1, count.intValue());

        // Check init/destroy counts
        assertEquals("Wrong # of calls to init()!", 3, initCalls);
        assertEquals("Wrong # of calls to destroy()!", 2, destroyCalls);