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 ; 21 import javax.xml.rpc.server.ServiceLifecycle ; 22 23 29 public class TestSimpleSession extends TestCase implements ServiceLifecycle { 30 static final String 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 47 public TestSimpleSession() 48 { 49 super("serviceTest"); 50 } 51 52 public TestSimpleSession(String name) 53 { 54 super(name); 55 } 56 57 public void testSessionAPI() { 58 SimpleSession session = new SimpleSession(); 59 Object val = new Float (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 79 public void testSessionService() throws Exception 80 { 81 SimpleSessionHandler sessionHandler = new SimpleSessionHandler(); 83 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 Service svc = new Service(clientProvider); 105 Call call = (Call)svc.createCall(); 106 svc.setMaintainSession(true); 107 call.setTransport(new LocalTransport(server)); 108 109 Integer count = (Integer )call.invoke("sessionTest", "counter", null); 111 assertNotNull("count was null!", count); 112 assertEquals("count was wrong", 1, count.intValue()); 113 114 assertEquals("Wrong # of calls to init()!", 1, initCalls); 116 117 count = (Integer )call.invoke("sessionTest", "counter", null); 120 assertEquals("count was wrong", 2, count.intValue()); 121 122 assertEquals("Wrong # of calls to init()!", 1, initCalls); 124 125 Service svc2 = new Service(clientProvider); 127 Call call2 = (Call)svc2.createCall(); 128 svc2.setMaintainSession(true); 129 call2.setTransport(new LocalTransport(server)); 130 131 count = (Integer )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 assertEquals("Wrong # of calls to init()!", 2, initCalls); 139 assertEquals("Shouldn't have called destroy() yet!", 0, destroyCalls); 141 142 Thread.sleep(4000); 144 145 count = (Integer )call.invoke("sessionTest", "counter", null); 147 assertEquals("count after timeout was incorrect", 1, count.intValue()); 148 149 assertEquals("Wrong # of calls to init()!", 3, initCalls); 151 assertEquals("Wrong # of calls to destroy()!", 2, destroyCalls); 152 } 153 154 158 public Integer counter() throws Exception 159 { 160 Session session = MessageContext.getCurrentContext().getSession(); 161 if (session == null) 162 throw new Exception ("No session in MessageContext!"); 163 Integer count = (Integer )session.get("counter"); 164 if (count == null) { 165 count = new Integer (0); 166 } 167 count = new Integer (count.intValue() + 1); 168 session.set("counter", count); 169 return count; 170 } 171 172 public static void main(String args[]) throws Exception 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 195 public void init(Object context) throws ServiceException { 196 initCalls++; 197 } 198 199 205 public void destroy() { 206 destroyCalls++; 207 } 208 } 209 | Popular Tags |