1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
|
package test.session;
import junit.framework.TestCase;
import org.apache.axis.EngineConfiguration;
import org.apache.axis.MessageContext;
import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import org.apache.axis.configuration.DefaultEngineConfigurationFactory;
import org.apache.axis.configuration.SimpleProvider;
import org.apache.axis.configuration.XMLStringProvider;
import org.apache.axis.deployment.wsdd.WSDDConstants;
import org.apache.axis.handlers.SimpleSessionHandler;
import org.apache.axis.handlers.soap.SOAPService;
import org.apache.axis.providers.java.RPCProvider;
import org.apache.axis.server.AxisServer;
import org.apache.axis.session.Session;
import org.apache.axis.session.SimpleSession;
import org.apache.axis.transport.local.LocalTransport;
import javax.xml.rpc.ServiceException;
import javax.xml.rpc.server.ServiceLifecycle;
/**
* Test the SimpleSession implementation (using SOAP headers for session
* maintenance)
*
* @author Glen Daniels (gdaniels@apache.org)
*/
public class TestSimpleSession extends TestCase implements ServiceLifecycle {
static final String clientWSDD =
"<deployment xmlns=\"http://xml.apache.org/axis/wsdd/\" " +
"xmlns:java=\"" + WSDDConstants.URI_WSDD_JAVA + "\">\n" +
" <handler type=\"java:org.apache.axis.handlers.SimpleSessionHandler\" " +
"name=\"SimpleSessionHandler\"/>\n" +
" <service name=\"sessionTest\">\n" +
" <requestFlow><handler type=\"SimpleSessionHandler\"/></requestFlow>\n" +
" <responseFlow><handler type=\"SimpleSessionHandler\"/></responseFlow>\n" +
" </service>\n" +
" <transport name=\"local\" " +
"pivot=\"java:org.apache.axis.transport.local.LocalSender\"/>\n" +
"</deployment>";
static XMLStringProvider clientProvider = new XMLStringProvider(clientWSDD);
/**
* Default constructor for use as service
*/
public TestSimpleSession()
{
super("serviceTest");
}
public TestSimpleSession(String name)
{
super(name);
}
public void testSessionAPI() {
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"));
}
/**
* 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.
*
*/
public void testSessionService() throws Exception
{
// 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);
}
/**
* This is our service method for testing session data. Simply
* increments a session-scoped counter.
*/
public Integer counter() throws Exception
{
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 static void main(String args[]) throws Exception
{
TestSimpleSession test = new TestSimpleSession("test");
test.testSessionAPI();
test.testSessionService();
}
private static int initCalls = 0;
private static int destroyCalls = 0;
/**
* 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
*/
public void init(Object context) throws ServiceException {
initCalls++;
}
/**
* 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.
*/
public void destroy() {
destroyCalls++;
}
}
|