1 package test.jaxrpc; 2 3 import junit.framework.TestCase; 4 import org.apache.axis.AxisFault; 5 import org.apache.axis.Constants; 6 import org.apache.axis.Handler; 7 import org.apache.axis.Message; 8 import org.apache.axis.MessageContext; 9 import org.apache.axis.handlers.BasicHandler; 10 import org.apache.axis.handlers.HandlerInfoChainFactory; 11 import org.apache.axis.handlers.soap.SOAPService; 12 import org.apache.axis.message.Detail; 13 import org.apache.axis.server.AxisServer; 14 15 import javax.xml.rpc.JAXRPCException ; 16 import javax.xml.rpc.handler.HandlerChain ; 17 import javax.xml.rpc.handler.HandlerInfo ; 18 import javax.xml.rpc.soap.SOAPFaultException ; 19 import java.util.ArrayList ; 20 import java.util.HashMap ; 21 import java.util.List ; 22 import java.util.Map ; 23 24 public class TestSOAPService 25 extends TestCase { 26 HandlerInfo handlerInfo0 = null; 27 HandlerInfo handlerInfo1 = null; 28 HandlerInfo handlerInfo2 = null; 29 30 Map handler0Config = null; 31 Map handler1Config = null; 32 Map handler2Config = null; 33 34 41 protected void setUp() throws Exception { 42 handlerInfo0 = new HandlerInfo (); 43 handlerInfo0.setHandlerClass(AAAHandler.class); 44 handlerInfo1 = new HandlerInfo (); 45 handlerInfo1.setHandlerClass(AAAHandler.class); 46 handlerInfo2 = new HandlerInfo (); 47 handlerInfo2.setHandlerClass(AAAHandler.class); 48 handler0Config = new HashMap (); 49 handler1Config = new HashMap (); 50 handler2Config = new HashMap (); 51 handlerInfo0.setHandlerConfig(handler0Config); 52 handlerInfo1.setHandlerConfig(handler1Config); 53 handlerInfo2.setHandlerConfig(handler2Config); 54 } 55 56 70 public void testPositiveCourseFlow() throws Exception { 71 TestHandlerInfoChainFactory factory = buildInfoChainFactory(); 72 SOAPService soapService = new SOAPService(); 73 soapService.setOption(Constants.ATTR_HANDLERINFOCHAIN, factory); 74 soapService.init(); 75 soapService.invoke(new TestMessageContext()); 76 AAAHandler handlerZero = factory.getHandlers()[0]; 77 AAAHandler handlerOne = factory.getHandlers()[1]; 78 AAAHandler handlerTwo = factory.getHandlers()[2]; 79 assertHandlerRuntime("handlerZero", handlerZero, 1, 1, 0); 80 assertHandlerRuntime("handlerOne", handlerOne, 1, 1, 0); 81 assertHandlerRuntime("handlerTwo", handlerTwo, 1, 1, 0); 82 } 83 84 96 public void testRequestHandlerReturnsFalse() throws Exception { 97 SOAPService soapService = new SOAPService(); 98 99 handler1Config.put("HANDLE_REQUEST_RETURN_VALUE", Boolean.FALSE); 101 TestHandlerInfoChainFactory factory = buildInfoChainFactory(); 102 soapService.setOption(Constants.ATTR_HANDLERINFOCHAIN, factory); 103 soapService.init(); 104 MessageContext msgContext = new TestMessageContext(); 105 soapService.invoke(msgContext); 106 AAAHandler handlerZero = factory.getHandlers()[0]; 107 AAAHandler handlerOne = factory.getHandlers()[1]; 108 AAAHandler handlerTwo = factory.getHandlers()[2]; 109 assertHandlerRuntime("handlerZero", handlerZero, 1, 1, 0); 110 assertHandlerRuntime("handlerOne", handlerOne, 1, 1, 0); 111 assertHandlerRuntime("handlerTwo", handlerTwo, 0, 0, 0); 112 } 113 114 117 public void testRequestHandlerThrowsSFE() throws Exception { 118 SOAPService soapService = new SOAPService(); 119 120 handler1Config.put("HANDLE_REQUEST_RETURN_VALUE", 122 new SOAPFaultException (null, "f", "f", new Detail())); 123 TestHandlerInfoChainFactory factory = buildInfoChainFactory(); 124 soapService.setOption(Constants.ATTR_HANDLERINFOCHAIN, factory); 125 soapService.init(); 126 MessageContext msgContext = new TestMessageContext(); 127 soapService.invoke(msgContext); 128 AAAHandler handlerZero = factory.getHandlers()[0]; 129 AAAHandler handlerOne = factory.getHandlers()[1]; 130 AAAHandler handlerTwo = factory.getHandlers()[2]; 131 assertHandlerRuntime("handlerZero", handlerZero, 1, 0, 1); 132 assertHandlerRuntime("handlerOne", handlerOne, 1, 0, 1); 133 assertHandlerRuntime("handlerTwo", handlerTwo, 0, 0, 0); 134 } 135 136 139 public void testRequestHandlerThrowsJAXRPCException() throws Exception { 140 SOAPService soapService = new SOAPService(); 141 142 handler1Config.put("HANDLE_REQUEST_RETURN_VALUE", 144 new JAXRPCException ()); 145 TestHandlerInfoChainFactory factory = buildInfoChainFactory(); 146 soapService.setOption(Constants.ATTR_HANDLERINFOCHAIN, factory); 147 soapService.init(); 148 MessageContext msgContext = new TestMessageContext(); 149 try { 150 soapService.invoke(msgContext); 151 } catch (AxisFault e) { 152 AAAHandler handlerZero = factory.getHandlers()[0]; 153 AAAHandler handlerOne = factory.getHandlers()[1]; 154 AAAHandler handlerTwo = factory.getHandlers()[2]; 155 assertHandlerRuntime("handlerZero", handlerZero, 1, 0, 0); 156 assertHandlerRuntime("handlerOne", handlerOne, 1, 0, 0); 157 assertHandlerRuntime("handlerTwo", handlerTwo, 0, 0, 0); 158 } 159 } 160 161 public void testRequestHandlerThrowsRuntimeException() throws Exception { 162 SOAPService soapService = new SOAPService(); 163 164 handler1Config.put("HANDLE_REQUEST_RETURN_VALUE", 166 new RuntimeException ()); 167 TestHandlerInfoChainFactory factory = buildInfoChainFactory(); 168 soapService.setOption(Constants.ATTR_HANDLERINFOCHAIN, factory); 169 soapService.init(); 170 MessageContext msgContext = new TestMessageContext(); 171 try { 172 soapService.invoke(msgContext); 173 } catch (AxisFault e) { 174 AAAHandler handlerZero = factory.getHandlers()[0]; 175 AAAHandler handlerOne = factory.getHandlers()[1]; 176 AAAHandler handlerTwo = factory.getHandlers()[2]; 177 assertHandlerRuntime("handlerZero", handlerZero, 1, 0, 0); 178 assertHandlerRuntime("handlerOne", handlerOne, 1, 0, 0); 179 assertHandlerRuntime("handlerTwo", handlerTwo, 0, 0, 0); 180 } 181 } 182 183 public void testResponseHandlerReturnsFalse() throws Exception { 184 SOAPService soapService = new SOAPService(); 185 186 handler2Config.put("HANDLE_RESPONSE_RETURN_VALUE", Boolean.FALSE); 188 TestHandlerInfoChainFactory factory = buildInfoChainFactory(); 189 soapService.setOption(Constants.ATTR_HANDLERINFOCHAIN, factory); 190 soapService.init(); 191 MessageContext msgContext = new TestMessageContext(); 192 soapService.invoke(msgContext); 193 AAAHandler handlerZero = factory.getHandlers()[0]; 194 AAAHandler handlerOne = factory.getHandlers()[1]; 195 AAAHandler handlerTwo = factory.getHandlers()[2]; 196 assertHandlerRuntime("handlerZero", handlerZero, 1, 0, 0); 197 assertHandlerRuntime("handlerOne", handlerOne, 1, 0, 0); 198 assertHandlerRuntime("handlerTwo", handlerTwo, 1, 1, 0); 199 } 200 201 public void testResponseHandlerThrowsJAXRPCException() throws Exception { 202 SOAPService soapService = new SOAPService(); 203 204 handler1Config.put("HANDLE_RESPONSE_RETURN_VALUE", 206 new JAXRPCException ()); 207 TestHandlerInfoChainFactory factory = buildInfoChainFactory(); 208 soapService.setOption(Constants.ATTR_HANDLERINFOCHAIN, factory); 209 soapService.init(); 210 MessageContext msgContext = new TestMessageContext(); 211 try { 212 soapService.invoke(msgContext); 213 fail("Expected AxisFault to be thrown"); 214 } catch (AxisFault e) { 215 AAAHandler handlerZero = factory.getHandlers()[0]; 216 AAAHandler handlerOne = factory.getHandlers()[1]; 217 AAAHandler handlerTwo = factory.getHandlers()[2]; 218 assertHandlerRuntime("handlerZero", handlerZero, 1, 0, 0); 219 assertHandlerRuntime("handlerOne", handlerOne, 1, 1, 0); 220 assertHandlerRuntime("handlerTwo", handlerTwo, 1, 1, 0); 221 } 222 } 223 224 public void testResponseHandlerThrowsRuntimeException() throws Exception { 225 SOAPService soapService = new SOAPService(); 226 227 handler1Config.put("HANDLE_RESPONSE_RETURN_VALUE", 229 new RuntimeException ()); 230 TestHandlerInfoChainFactory factory = buildInfoChainFactory(); 231 soapService.setOption(Constants.ATTR_HANDLERINFOCHAIN, factory); 232 soapService.init(); 233 MessageContext msgContext = new TestMessageContext(); 234 try { 235 soapService.invoke(msgContext); 236 fail("Expected AxisFault to be thrown"); 237 } catch (AxisFault e) { 238 AAAHandler handlerZero = factory.getHandlers()[0]; 239 AAAHandler handlerOne = factory.getHandlers()[1]; 240 AAAHandler handlerTwo = factory.getHandlers()[2]; 241 assertHandlerRuntime("handlerZero", handlerZero, 1, 0, 0); 242 assertHandlerRuntime("handlerOne", handlerOne, 1, 1, 0); 243 assertHandlerRuntime("handlerTwo", handlerTwo, 1, 1, 0); 244 } 245 } 246 247 public void testHandleFaultReturnsFalse() throws Exception { 248 SOAPService soapService = new SOAPService(); 249 250 handler2Config.put("HANDLE_REQUEST_RETURN_VALUE", 252 new SOAPFaultException (null, "f", "f", new Detail())); 253 handler1Config.put("HANDLE_FAULT_RETURN_VALUE", Boolean.FALSE); 254 TestHandlerInfoChainFactory factory = buildInfoChainFactory(); 255 soapService.setOption(Constants.ATTR_HANDLERINFOCHAIN, factory); 256 soapService.init(); 257 MessageContext msgContext = new TestMessageContext(); 258 soapService.invoke(msgContext); 259 AAAHandler handlerZero = factory.getHandlers()[0]; 260 AAAHandler handlerOne = factory.getHandlers()[1]; 261 AAAHandler handlerTwo = factory.getHandlers()[2]; 262 assertHandlerRuntime("handlerZero", handlerZero, 1, 0, 0); 263 assertHandlerRuntime("handlerOne", handlerOne, 1, 0, 1); 264 assertHandlerRuntime("handlerTwo", handlerTwo, 1, 0, 1); 265 } 266 267 280 public void testFaultHandlerThrowsJAXRPCException() throws Exception { 281 SOAPService soapService = new SOAPService(); 282 283 handler2Config.put("HANDLE_REQUEST_RETURN_VALUE", 285 new SOAPFaultException (null, "f", "f", new Detail())); 286 handler1Config.put("HANDLE_FAULT_RETURN_VALUE", new JAXRPCException ()); 287 TestHandlerInfoChainFactory factory = buildInfoChainFactory(); 288 soapService.setOption(Constants.ATTR_HANDLERINFOCHAIN, factory); 289 soapService.init(); 290 MessageContext msgContext = new TestMessageContext(); 291 try { 292 soapService.invoke(msgContext); 293 fail("Expected AxisFault to be thrown"); 294 } catch (AxisFault e) { 295 AAAHandler handlerZero = factory.getHandlers()[0]; 296 AAAHandler handlerOne = factory.getHandlers()[1]; 297 AAAHandler handlerTwo = factory.getHandlers()[2]; 298 assertHandlerRuntime("handlerZero", handlerZero, 1, 0, 0); 299 assertHandlerRuntime("handlerOne", handlerOne, 1, 0, 1); 300 assertHandlerRuntime("handlerTwo", handlerTwo, 1, 0, 1); 301 } 302 } 303 304 317 public void testFaultHandlerThrowsRuntimeException() throws Exception { 318 SOAPService soapService = new SOAPService(); 319 320 handler2Config.put("HANDLE_REQUEST_RETURN_VALUE", 322 new SOAPFaultException (null, "f", "f", new Detail())); 323 handler1Config.put("HANDLE_FAULT_RETURN_VALUE", new RuntimeException ()); 324 TestHandlerInfoChainFactory factory = buildInfoChainFactory(); 325 soapService.setOption(Constants.ATTR_HANDLERINFOCHAIN, factory); 326 soapService.init(); 327 MessageContext msgContext = new TestMessageContext(); 328 try { 329 soapService.invoke(msgContext); 330 fail("Expected AxisFault to be thrown"); 331 } catch (AxisFault e) { 332 AAAHandler handlerZero = factory.getHandlers()[0]; 333 AAAHandler handlerOne = factory.getHandlers()[1]; 334 AAAHandler handlerTwo = factory.getHandlers()[2]; 335 assertHandlerRuntime("handlerZero", handlerZero, 1, 0, 0); 336 assertHandlerRuntime("handlerOne", handlerOne, 1, 0, 1); 337 assertHandlerRuntime("handlerTwo", handlerTwo, 1, 0, 1); 338 } 339 } 340 341 355 public void testServiceObjectThrowsAxisFault() throws Exception { 356 Handler serviceHandler = new MockServiceHandler(); 357 SOAPService soapService = new SOAPService(null, null, serviceHandler); 358 TestHandlerInfoChainFactory factory = buildInfoChainFactory(); 359 soapService.setOption(Constants.ATTR_HANDLERINFOCHAIN, factory); 360 soapService.init(); 361 MessageContext msgContext = new TestMessageContext(); 362 try { 363 soapService.invoke(msgContext); 364 fail("Expected AxisFault to be thrown"); 365 } catch (AxisFault e) { 366 AAAHandler handlerZero = factory.getHandlers()[0]; 367 AAAHandler handlerOne = factory.getHandlers()[1]; 368 AAAHandler handlerTwo = factory.getHandlers()[2]; 369 assertHandlerRuntime("handlerZero", handlerZero, 1, 0, 1); 370 assertHandlerRuntime("handlerOne", handlerOne, 1, 0, 1); 371 assertHandlerRuntime("handlerTwo", handlerTwo, 1, 0, 1); 372 } 373 } 374 375 388 protected void assertHandlerRuntime(String message, AAAHandler handler, 389 int numHandleRequest, 390 int numHandleResponse, 391 int numHandleFault) { 392 assertEquals(message + ": handleRequest", numHandleRequest, 393 handler.getHandleRequestInvocations()); 394 assertEquals(message + ": handleResponse", numHandleResponse, 395 handler.getHandleResponseInvocations()); 396 assertEquals(message + ": handleFault", numHandleFault, 397 handler.getHandleFaultInvocations()); 398 } 399 400 405 protected TestHandlerInfoChainFactory buildInfoChainFactory() { 406 List handlerInfos = new ArrayList (); 407 handlerInfos.add(handlerInfo0); 408 handlerInfos.add(handlerInfo1); 409 handlerInfos.add(handlerInfo2); 410 TestHandlerInfoChainFactory factory = new TestHandlerInfoChainFactory( 411 handlerInfos); 412 return factory; 413 } 414 415 419 private class MockServiceHandler extends BasicHandler { 420 public void invoke(MessageContext msgContext) throws AxisFault { 421 throw new AxisFault(); 422 } 423 } 424 425 433 private class TestHandlerInfoChainFactory extends HandlerInfoChainFactory { 434 AAAHandler[] handlers; 435 436 public TestHandlerInfoChainFactory(List handlerInfos) { 437 super(handlerInfos); 438 } 439 440 public HandlerChain createHandlerChain() { 441 HandlerChain chain = super.createHandlerChain(); 442 handlers = new AAAHandler[chain.size()]; 443 for (int i = 0; i < chain.size(); i++) { 444 handlers[i] = (AAAHandler) chain.get(i); 445 } 446 return chain; 447 } 448 449 public AAAHandler[] getHandlers() { 450 return handlers; 451 } 452 453 } 454 455 private class TestMessageContext extends org.apache.axis.MessageContext { 456 457 public String listByAreaCode = "<soap:Envelope\n" + 458 "xmlns:s0=\"http://www.tilisoft.com/ws/LocInfo/literalTypes\"\n" + 459 "xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\"\n" + 460 "xmlns:xs=\"http://www.w3.org/2001/XMLSchema\">\n" + 461 "<soap:Header>\n" + 462 "<WSABIHeader>\n" + 463 "<SubscriptionId>192168001100108165800640600008</SubscriptionId>\n" + 464 "</WSABIHeader>\n" + 465 "</soap:Header>\n" + 466 "<soap:Body>\n" + 467 "<s0:ListByAreaCode>\n" + 468 "<s0:AreaCode>617</s0:AreaCode>\n" + 469 "</s0:ListByAreaCode>\n" + 470 "</soap:Body>\n" + 471 "</soap:Envelope>\n"; 472 473 public TestMessageContext() { 474 super(new AxisServer()); 475 Message message = new Message(listByAreaCode); 476 message.setMessageType(Message.REQUEST); 477 setRequestMessage(message); 478 } 479 } 480 481 } 482 | Popular Tags |