Source Code Cross Referenced for HandlerInvocationTest.java in  » Web-Services-apache-cxf-2.0.1 » systests » org » apache » cxf » systest » handlers » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Web Services apache cxf 2.0.1 » systests » org.apache.cxf.systest.handlers 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


0001:        /**
0002:         * Licensed to the Apache Software Foundation (ASF) under one
0003:         * or more contributor license agreements. See the NOTICE file
0004:         * distributed with this work for additional information
0005:         * regarding copyright ownership. The ASF licenses this file
0006:         * to you under the Apache License, Version 2.0 (the
0007:         * "License"); you may not use this file except in compliance
0008:         * with the License. You may obtain a copy of the License at
0009:         *
0010:         * http://www.apache.org/licenses/LICENSE-2.0
0011:         *
0012:         * Unless required by applicable law or agreed to in writing,
0013:         * software distributed under the License is distributed on an
0014:         * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
0015:         * KIND, either express or implied. See the License for the
0016:         * specific language governing permissions and limitations
0017:         * under the License.
0018:         */package org.apache.cxf.systest.handlers;
0019:
0020:        import java.io.InputStream;
0021:        import java.net.URL;
0022:        import java.util.ArrayList;
0023:        import java.util.Iterator;
0024:        import java.util.List;
0025:        import javax.xml.bind.JAXBContext;
0026:        import javax.xml.bind.Unmarshaller;
0027:        import javax.xml.namespace.QName;
0028:        import javax.xml.soap.MessageFactory;
0029:        import javax.xml.soap.SOAPMessage;
0030:        import javax.xml.transform.Source;
0031:        import javax.xml.ws.BindingProvider;
0032:        import javax.xml.ws.Dispatch;
0033:        import javax.xml.ws.LogicalMessage;
0034:        import javax.xml.ws.ProtocolException;
0035:        import javax.xml.ws.Service;
0036:        import javax.xml.ws.WebServiceException;
0037:        import javax.xml.ws.handler.Handler;
0038:        import javax.xml.ws.handler.HandlerResolver;
0039:        import javax.xml.ws.handler.LogicalMessageContext;
0040:        import javax.xml.ws.handler.MessageContext;
0041:        import javax.xml.ws.handler.PortInfo;
0042:        import javax.xml.ws.handler.soap.SOAPMessageContext;
0043:        import javax.xml.ws.soap.SOAPFaultException;
0044:
0045:        import org.w3c.dom.Node;
0046:        import org.w3c.dom.NodeList;
0047:
0048:        import org.apache.cxf.BusException;
0049:        import org.apache.cxf.common.util.PackageUtils;
0050:        import org.apache.cxf.testutil.common.AbstractBusClientServerTestBase;
0051:        import org.apache.handler_test.HandlerTest;
0052:        import org.apache.handler_test.HandlerTestService;
0053:        import org.apache.handler_test.PingException;
0054:        import org.apache.handler_test.types.PingOneWay;
0055:        import org.apache.handler_test.types.PingResponse;
0056:        import org.junit.Before;
0057:        import org.junit.BeforeClass;
0058:        import org.junit.Test;
0059:
0060:        public class HandlerInvocationTest extends
0061:                AbstractBusClientServerTestBase {
0062:
0063:            private final QName serviceName = new QName(
0064:                    "http://apache.org/handler_test", "HandlerTestService");
0065:            private final QName portName = new QName(
0066:                    "http://apache.org/handler_test", "SoapPort");
0067:
0068:            private URL wsdl;
0069:            private HandlerTestService service;
0070:            private HandlerTest handlerTest;
0071:
0072:            @BeforeClass
0073:            public static void startServers() throws Exception {
0074:                assertTrue("server did not launch correctly",
0075:                        launchServer(Server.class));
0076:            }
0077:
0078:            @Before
0079:            public void setUp() throws BusException {
0080:                try {
0081:                    super .createBus();
0082:
0083:                    wsdl = HandlerInvocationTest.class
0084:                            .getResource("/wsdl/handler_test.wsdl");
0085:                    service = new HandlerTestService(wsdl, serviceName);
0086:                    handlerTest = service.getPort(portName, HandlerTest.class);
0087:                } catch (Exception ex) {
0088:                    ex.printStackTrace();
0089:                    fail(ex.toString());
0090:                }
0091:            }
0092:
0093:            @Test
0094:            public void testAddHandlerThroughHandlerResolverClientSide() {
0095:                TestHandler<LogicalMessageContext> handler1 = new TestHandler<LogicalMessageContext>(
0096:                        false);
0097:                TestHandler<LogicalMessageContext> handler2 = new TestHandler<LogicalMessageContext>(
0098:                        false);
0099:
0100:                MyHandlerResolver myHandlerResolver = new MyHandlerResolver(
0101:                        handler1, handler2);
0102:
0103:                service.setHandlerResolver(myHandlerResolver);
0104:
0105:                HandlerTest handlerTestNew = service.getPort(portName,
0106:                        HandlerTest.class);
0107:
0108:                handlerTestNew.pingOneWay();
0109:
0110:                String bindingID = myHandlerResolver.bindingID;
0111:                assertEquals("http://schemas.xmlsoap.org/wsdl/soap/http",
0112:                        bindingID);
0113:                assertEquals(1, handler1.getHandleMessageInvoked());
0114:                assertEquals(1, handler2.getHandleMessageInvoked());
0115:            }
0116:
0117:            @Test
0118:            public void testLogicalHandlerOneWay() {
0119:                TestHandler<LogicalMessageContext> handler1 = new TestHandler<LogicalMessageContext>(
0120:                        false);
0121:                TestHandler<LogicalMessageContext> handler2 = new TestHandler<LogicalMessageContext>(
0122:                        false);
0123:                addHandlersToChain((BindingProvider) handlerTest, handler1,
0124:                        handler2);
0125:
0126:                handlerTest.pingOneWay();
0127:
0128:                assertEquals(1, handler1.getHandleMessageInvoked());
0129:                assertEquals(1, handler2.getHandleMessageInvoked());
0130:            }
0131:
0132:            @Test
0133:            public void testLogicalHandlerTwoWay() throws Exception {
0134:                TestHandler<LogicalMessageContext> handler1 = new TestHandler<LogicalMessageContext>(
0135:                        false);
0136:                TestHandler<LogicalMessageContext> handler2 = new TestHandler<LogicalMessageContext>(
0137:                        false);
0138:                addHandlersToChain((BindingProvider) handlerTest, handler1,
0139:                        handler2);
0140:
0141:                handlerTest.pingWithArgs("hello");
0142:
0143:                assertEquals(2, handler1.getHandleMessageInvoked());
0144:                assertEquals(2, handler2.getHandleMessageInvoked());
0145:            }
0146:
0147:            @Test
0148:            public void testSOAPHandlerHandleMessageReturnTrueClient()
0149:                    throws Exception {
0150:                TestHandler<LogicalMessageContext> handler1 = new TestHandler<LogicalMessageContext>(
0151:                        false);
0152:                TestHandler<LogicalMessageContext> handler2 = new TestHandler<LogicalMessageContext>(
0153:                        false) {
0154:                    public boolean handleMessage(LogicalMessageContext ctx) {
0155:                        super .handleMessage(ctx);
0156:                        try {
0157:                            Boolean outbound = (Boolean) ctx
0158:                                    .get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);
0159:                            if (!outbound) {
0160:                                LogicalMessage msg = ctx.getMessage();
0161:                                Source source = msg.getPayload();
0162:                                assertNotNull(source);
0163:                            }
0164:                        } catch (Exception e) {
0165:                            e.printStackTrace();
0166:                            fail(e.toString());
0167:                        }
0168:                        return true;
0169:                    }
0170:                };
0171:
0172:                TestSOAPHandler soapHandler1 = new TestSOAPHandler(false);
0173:                TestSOAPHandler soapHandler2 = new TestSOAPHandler(false);
0174:
0175:                addHandlersToChain((BindingProvider) handlerTest, handler1,
0176:                        handler2, soapHandler1, soapHandler2);
0177:
0178:                List<String> resp = handlerTest.ping();
0179:                assertNotNull(resp);
0180:
0181:                assertEquals("handle message was not invoked", 2, handler1
0182:                        .getHandleMessageInvoked());
0183:                assertEquals("handle message was not invoked", 2, handler2
0184:                        .getHandleMessageInvoked());
0185:                assertEquals("handle message was not invoked", 2, soapHandler1
0186:                        .getHandleMessageInvoked());
0187:                assertEquals("handle message was not invoked", 2, soapHandler2
0188:                        .getHandleMessageInvoked());
0189:
0190:                assertEquals("close must be called", 1, handler1
0191:                        .getCloseInvoked());
0192:                assertEquals("close must be called", 1, handler2
0193:                        .getCloseInvoked());
0194:                assertEquals("close must be called", 1, soapHandler1
0195:                        .getCloseInvoked());
0196:                assertEquals("close must be called", 1, soapHandler2
0197:                        .getCloseInvoked());
0198:
0199:                assertTrue(soapHandler2.getInvokeOrderOfClose() < soapHandler1
0200:                        .getInvokeOrderOfClose());
0201:                assertTrue(soapHandler1.getInvokeOrderOfClose() < handler2
0202:                        .getInvokeOrderOfClose());
0203:                assertTrue(handler2.getInvokeOrderOfClose() < handler1
0204:                        .getInvokeOrderOfClose());
0205:
0206:                // the server has encoded into the response the order in
0207:                // which the handlers have been invoked, parse it and make
0208:                // sure everything is ok expected order for inbound interceptors
0209:                String[] handlerNames = { "soapHandler4", "soapHandler3",
0210:                        "handler2", "handler1", "servant", "handler1",
0211:                        "handler2", "soapHandler3", "soapHandler4" };
0212:
0213:                assertEquals(handlerNames.length, resp.size());
0214:
0215:                Iterator iter = resp.iterator();
0216:                for (String expected : handlerNames) {
0217:                    assertEquals(expected, iter.next());
0218:                }
0219:            }
0220:
0221:            @Test
0222:            public void testLogicalHandlerHandleMessageReturnFalseClientOutBound()
0223:                    throws Exception {
0224:                final String clientHandlerMessage = "handler2 client side";
0225:
0226:                TestHandler<LogicalMessageContext> handler1 = new TestHandler<LogicalMessageContext>(
0227:                        false);
0228:                TestHandler<LogicalMessageContext> handler2 = new TestHandler<LogicalMessageContext>(
0229:                        false) {
0230:                    public boolean handleMessage(LogicalMessageContext ctx) {
0231:                        super .handleMessage(ctx);
0232:                        try {
0233:                            Boolean outbound = (Boolean) ctx
0234:                                    .get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);
0235:                            if (outbound) {
0236:                                LogicalMessage msg = ctx.getMessage();
0237:                                assertNotNull("logical message is null", msg);
0238:                                JAXBContext jaxbCtx = JAXBContext
0239:                                        .newInstance(PackageUtils
0240:                                                .getPackageName(PingOneWay.class));
0241:                                PingResponse resp = new PingResponse();
0242:                                resp.getHandlersInfo()
0243:                                        .add(clientHandlerMessage);
0244:
0245:                                msg.setPayload(resp, jaxbCtx);
0246:                                return false;
0247:                            }
0248:                        } catch (Exception e) {
0249:                            e.printStackTrace();
0250:                            fail(e.toString());
0251:                        }
0252:                        return true;
0253:                    }
0254:                };
0255:                TestHandler<LogicalMessageContext> handler3 = new TestHandler<LogicalMessageContext>(
0256:                        false);
0257:                TestSOAPHandler soapHandler1 = new TestSOAPHandler(false);
0258:
0259:                addHandlersToChain((BindingProvider) handlerTest, handler1,
0260:                        handler2, handler3, soapHandler1);
0261:
0262:                List<String> resp = handlerTest.ping();
0263:                assertEquals(clientHandlerMessage, resp.get(0));
0264:
0265:                assertEquals("the first handler must be invoked twice", 2,
0266:                        handler1.getHandleMessageInvoked());
0267:                assertEquals(
0268:                        "the second handler must be invoked once only on outbound",
0269:                        1, handler2.getHandleMessageInvoked());
0270:                assertEquals("the third handler must not be invoked", 0,
0271:                        handler3.getHandleMessageInvoked());
0272:                assertEquals("the last handler must not be invoked", 0,
0273:                        soapHandler1.getHandleMessageInvoked());
0274:
0275:                //outbound MEP processing ceased, the message direction was changed to inbound, essentially this is
0276:                //only one MEP. So close is called only once at the end of inbound MEP, and the close order is 
0277:                //reversed to the outbound handler invoking order.
0278:                assertEquals("close must be called", 1, handler1
0279:                        .getCloseInvoked());
0280:                assertEquals("close must be called", 1, handler2
0281:                        .getCloseInvoked());
0282:                assertEquals("close must be called", 0, handler3
0283:                        .getCloseInvoked());
0284:                assertEquals("close must be called", 0, soapHandler1
0285:                        .getCloseInvoked());
0286:                assertTrue(handler2.getInvokeOrderOfClose() < handler1
0287:                        .getInvokeOrderOfClose());
0288:            }
0289:
0290:            @Test
0291:            public void testLogicalHandlerHandleMessageReturnFalseClientInBound()
0292:                    throws Exception {
0293:                TestHandler<LogicalMessageContext> handler1 = new TestHandler<LogicalMessageContext>(
0294:                        false);
0295:                TestHandler<LogicalMessageContext> handler2 = new TestHandler<LogicalMessageContext>(
0296:                        false) {
0297:                    public boolean handleMessage(LogicalMessageContext ctx) {
0298:                        super .handleMessage(ctx);
0299:                        Boolean outbound = (Boolean) ctx
0300:                                .get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);
0301:                        if (!outbound) {
0302:                            return false;
0303:                        }
0304:
0305:                        return true;
0306:                    }
0307:                };
0308:                TestHandler<LogicalMessageContext> handler3 = new TestHandler<LogicalMessageContext>(
0309:                        false);
0310:                TestSOAPHandler soapHandler1 = new TestSOAPHandler(false);
0311:
0312:                addHandlersToChain((BindingProvider) handlerTest, handler1,
0313:                        handler2, handler3, soapHandler1);
0314:
0315:                handlerTest.ping();
0316:
0317:                assertEquals(1, handler1.getHandleMessageInvoked());
0318:                assertEquals(2, handler2.getHandleMessageInvoked());
0319:                assertEquals(2, handler3.getHandleMessageInvoked());
0320:                assertEquals(2, soapHandler1.getHandleMessageInvoked());
0321:
0322:                assertEquals("close must be called", 1, handler1
0323:                        .getCloseInvoked());
0324:                assertEquals("close must be called", 1, handler2
0325:                        .getCloseInvoked());
0326:                assertEquals("close must be called", 1, handler3
0327:                        .getCloseInvoked());
0328:                assertEquals("close must be called", 1, soapHandler1
0329:                        .getCloseInvoked());
0330:                assertTrue(soapHandler1.getInvokeOrderOfClose() < handler3
0331:                        .getInvokeOrderOfClose());
0332:                assertTrue(handler3.getInvokeOrderOfClose() < handler2
0333:                        .getInvokeOrderOfClose());
0334:                assertTrue(handler2.getInvokeOrderOfClose() < handler1
0335:                        .getInvokeOrderOfClose());
0336:            }
0337:
0338:            @Test
0339:            public void testSOAPHandlerHandleMessageReturnFalseClientOutbound()
0340:                    throws Exception {
0341:                final String clientHandlerMessage = "client side";
0342:                TestHandler<LogicalMessageContext> handler1 = new TestHandler<LogicalMessageContext>(
0343:                        false);
0344:                TestHandler<LogicalMessageContext> handler2 = new TestHandler<LogicalMessageContext>(
0345:                        false) {
0346:                    public boolean handleMessage(LogicalMessageContext ctx) {
0347:                        super .handleMessage(ctx);
0348:                        try {
0349:                            Boolean outbound = (Boolean) ctx
0350:                                    .get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);
0351:                            if (outbound) {
0352:                                LogicalMessage msg = ctx.getMessage();
0353:                                assertNotNull("logical message is null", msg);
0354:                                JAXBContext jaxbCtx = JAXBContext
0355:                                        .newInstance(PackageUtils
0356:                                                .getPackageName(PingOneWay.class));
0357:                                PingResponse resp = new PingResponse();
0358:                                resp.getHandlersInfo()
0359:                                        .add(clientHandlerMessage);
0360:
0361:                                msg.setPayload(resp, jaxbCtx);
0362:                            }
0363:
0364:                        } catch (Exception e) {
0365:                            e.printStackTrace();
0366:                            fail(e.toString());
0367:                        }
0368:                        return true;
0369:                    }
0370:                };
0371:                TestSOAPHandler soapHandler1 = new TestSOAPHandler(false);
0372:                TestSOAPHandler soapHandler2 = new TestSOAPHandler<SOAPMessageContext>(
0373:                        false) {
0374:                    public boolean handleMessage(SOAPMessageContext ctx) {
0375:                        super .handleMessage(ctx);
0376:                        Boolean outbound = (Boolean) ctx
0377:                                .get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);
0378:                        if (outbound) {
0379:                            return false;
0380:                        }
0381:                        return true;
0382:                    }
0383:                };
0384:                addHandlersToChain((BindingProvider) handlerTest, handler1,
0385:                        handler2, soapHandler1, soapHandler2);
0386:
0387:                List<String> resp = handlerTest.ping();
0388:                assertEquals(clientHandlerMessage, resp.get(0));
0389:
0390:                assertEquals(2, handler1.getHandleMessageInvoked());
0391:                assertEquals(2, handler2.getHandleMessageInvoked());
0392:                assertEquals(2, soapHandler1.getHandleMessageInvoked());
0393:                assertEquals(1, soapHandler2.getHandleMessageInvoked());
0394:
0395:                assertEquals("close must be called", 1, handler1
0396:                        .getCloseInvoked());
0397:                assertEquals("close must be called", 1, handler2
0398:                        .getCloseInvoked());
0399:                assertEquals("close must be called", 1, soapHandler1
0400:                        .getCloseInvoked());
0401:                assertEquals("close must be called", 1, soapHandler2
0402:                        .getCloseInvoked());
0403:                assertTrue(soapHandler2.getInvokeOrderOfClose() < soapHandler1
0404:                        .getInvokeOrderOfClose());
0405:                assertTrue(soapHandler1.getInvokeOrderOfClose() < handler2
0406:                        .getInvokeOrderOfClose());
0407:                assertTrue(handler2.getInvokeOrderOfClose() < handler1
0408:                        .getInvokeOrderOfClose());
0409:            }
0410:
0411:            @Test
0412:            public void testSOAPHandlerHandleMessageReturnFalseClientInbound()
0413:                    throws Exception {
0414:                TestHandler<LogicalMessageContext> handler1 = new TestHandler<LogicalMessageContext>(
0415:                        false);
0416:                TestHandler<LogicalMessageContext> handler2 = new TestHandler<LogicalMessageContext>(
0417:                        false);
0418:                TestSOAPHandler soapHandler1 = new TestSOAPHandler(false);
0419:                TestSOAPHandler soapHandler2 = new TestSOAPHandler<SOAPMessageContext>(
0420:                        false) {
0421:                    public boolean handleMessage(SOAPMessageContext ctx) {
0422:                        super .handleMessage(ctx);
0423:                        Boolean outbound = (Boolean) ctx
0424:                                .get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);
0425:                        if (!outbound) {
0426:                            return false;
0427:                        }
0428:                        return true;
0429:                    }
0430:                };
0431:                addHandlersToChain((BindingProvider) handlerTest, handler1,
0432:                        handler2, soapHandler1, soapHandler2);
0433:
0434:                handlerTest.ping();
0435:
0436:                assertEquals(1, handler1.getHandleMessageInvoked());
0437:                assertEquals(1, handler2.getHandleMessageInvoked());
0438:                assertEquals(1, soapHandler1.getHandleMessageInvoked());
0439:                assertEquals(2, soapHandler2.getHandleMessageInvoked());
0440:
0441:                assertEquals("close must be called", 1, handler1
0442:                        .getCloseInvoked());
0443:                assertEquals("close must be called", 1, handler2
0444:                        .getCloseInvoked());
0445:                assertEquals("close must be called", 1, soapHandler1
0446:                        .getCloseInvoked());
0447:                assertEquals("close must be called", 1, soapHandler2
0448:                        .getCloseInvoked());
0449:                assertTrue(soapHandler2.getInvokeOrderOfClose() < soapHandler1
0450:                        .getInvokeOrderOfClose());
0451:                assertTrue(soapHandler1.getInvokeOrderOfClose() < handler2
0452:                        .getInvokeOrderOfClose());
0453:                assertTrue(handler2.getInvokeOrderOfClose() < handler1
0454:                        .getInvokeOrderOfClose());
0455:            }
0456:
0457:            @Test
0458:            public void testLogicalHandlerHandleMessageReturnsFalseServerInbound()
0459:                    throws PingException {
0460:                String[] expectedHandlers = { "soapHandler4", "soapHandler3",
0461:                        "handler2", "soapHandler3", "soapHandler4" };
0462:
0463:                List<String> resp = handlerTest
0464:                        .pingWithArgs("handler2 inbound stop");
0465:
0466:                assertEquals(expectedHandlers.length, resp.size());
0467:
0468:                int i = 0;
0469:                for (String expected : expectedHandlers) {
0470:                    assertEquals(expected, resp.get(i++));
0471:                }
0472:            }
0473:
0474:            @Test
0475:            public void testSOAPHandlerHandleMessageReturnsFalseServerInbound()
0476:                    throws PingException {
0477:                String[] expectedHandlers = { "soapHandler4", "soapHandler3",
0478:                        "soapHandler4" };
0479:                List<String> resp = handlerTest
0480:                        .pingWithArgs("soapHandler3 inbound stop");
0481:                assertEquals(expectedHandlers.length, resp.size());
0482:                int i = 0;
0483:                for (String expected : expectedHandlers) {
0484:                    assertEquals(expected, resp.get(i++));
0485:                }
0486:            }
0487:
0488:            @Test
0489:            public void testSOAPHandlerHandleMessageReturnsFalseServerOutbound()
0490:                    throws PingException {
0491:                String[] expectedHandlers = { "soapHandler3 outbound stop",
0492:                        "soapHandler4", "soapHandler3", "handler2", "handler1",
0493:                        "handler1", "handler2", "soapHandler3" };
0494:                List<String> resp = handlerTest
0495:                        .pingWithArgs("soapHandler3 outbound stop");
0496:
0497:                assertEquals(expectedHandlers.length, resp.size());
0498:                int i = 0;
0499:                for (String expected : expectedHandlers) {
0500:                    assertEquals(expected, resp.get(i++));
0501:                }
0502:            }
0503:
0504:            @Test
0505:            public void testLogicalHandlerHandleMessageThrowsProtocolExceptionClientOutbound()
0506:                    throws Exception {
0507:                final String clientHandlerMessage = "handler1 client side";
0508:
0509:                TestHandler<LogicalMessageContext> handler1 = new TestHandler<LogicalMessageContext>(
0510:                        false);
0511:                TestHandler<LogicalMessageContext> handler2 = new TestHandler<LogicalMessageContext>(
0512:                        false) {
0513:                    public boolean handleMessage(LogicalMessageContext ctx) {
0514:                        super .handleMessage(ctx);
0515:                        Boolean outbound = (Boolean) ctx
0516:                                .get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);
0517:                        if (outbound) {
0518:                            throw new ProtocolException(clientHandlerMessage);
0519:                        }
0520:                        return true;
0521:                    }
0522:                };
0523:                TestSOAPHandler soapHandler1 = new TestSOAPHandler(false);
0524:
0525:                addHandlersToChain((BindingProvider) handlerTest, handler1,
0526:                        handler2, soapHandler1);
0527:
0528:                try {
0529:                    handlerTest.ping();
0530:                    fail("did not get expected exception");
0531:                } catch (ProtocolException e) {
0532:                    assertEquals(clientHandlerMessage, e.getMessage());
0533:                }
0534:
0535:                assertEquals(1, handler1.getHandleMessageInvoked());
0536:                assertEquals(1, handler2.getHandleMessageInvoked());
0537:                assertEquals(0, soapHandler1.getHandleMessageInvoked());
0538:
0539:                assertEquals(0, handler2.getHandleFaultInvoked());
0540:                assertEquals(1, handler1.getHandleFaultInvoked());
0541:                assertEquals(0, soapHandler1.getHandleFaultInvoked());
0542:
0543:                assertEquals(1, handler1.getCloseInvoked());
0544:                assertEquals(1, handler2.getCloseInvoked());
0545:                assertEquals(0, soapHandler1.getCloseInvoked());
0546:                assertTrue(handler2.getInvokeOrderOfClose() < handler1
0547:                        .getInvokeOrderOfClose());
0548:            }
0549:
0550:            @Test
0551:            public void testLogicalHandlerHandleMessageThrowsProtocolExceptionClientInbound()
0552:                    throws Exception {
0553:                final String clientHandlerMessage = "handler1 client side";
0554:
0555:                TestHandler<LogicalMessageContext> handler1 = new TestHandler<LogicalMessageContext>(
0556:                        false);
0557:                TestHandler<LogicalMessageContext> handler2 = new TestHandler<LogicalMessageContext>(
0558:                        false) {
0559:                    public boolean handleMessage(LogicalMessageContext ctx) {
0560:                        super .handleMessage(ctx);
0561:                        Boolean outbound = (Boolean) ctx
0562:                                .get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);
0563:                        if (!outbound) {
0564:                            throw new ProtocolException(clientHandlerMessage);
0565:                        }
0566:                        return true;
0567:                    }
0568:                };
0569:                TestSOAPHandler soapHandler1 = new TestSOAPHandler(false);
0570:
0571:                addHandlersToChain((BindingProvider) handlerTest, handler1,
0572:                        handler2, soapHandler1);
0573:
0574:                try {
0575:                    handlerTest.ping();
0576:                    fail("did not get expected exception");
0577:                } catch (ProtocolException e) {
0578:                    assertEquals(clientHandlerMessage, e.getMessage());
0579:                }
0580:
0581:                assertEquals(1, handler1.getHandleMessageInvoked());
0582:                assertEquals(2, handler2.getHandleMessageInvoked());
0583:                assertEquals(2, soapHandler1.getHandleMessageInvoked());
0584:
0585:                assertEquals(0, handler2.getHandleFaultInvoked());
0586:                assertEquals(0, handler1.getHandleFaultInvoked());
0587:                assertEquals(0, soapHandler1.getHandleFaultInvoked());
0588:
0589:                assertEquals(1, handler1.getCloseInvoked());
0590:                assertEquals(1, handler2.getCloseInvoked());
0591:                assertEquals(1, soapHandler1.getCloseInvoked());
0592:                assertTrue(handler2.getInvokeOrderOfClose() < handler1
0593:                        .getInvokeOrderOfClose());
0594:            }
0595:
0596:            @Test
0597:            public void testLogicalHandlerHandleMessageThrowsRuntimeExceptionClientOutbound()
0598:                    throws Exception {
0599:                final String clientHandlerMessage = "handler1 client side";
0600:
0601:                TestHandler<LogicalMessageContext> handler1 = new TestHandler<LogicalMessageContext>(
0602:                        false);
0603:                TestHandler<LogicalMessageContext> handler2 = new TestHandler<LogicalMessageContext>(
0604:                        false) {
0605:                    public boolean handleMessage(LogicalMessageContext ctx) {
0606:                        super .handleMessage(ctx);
0607:                        Boolean outbound = (Boolean) ctx
0608:                                .get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);
0609:                        if (outbound) {
0610:                            throw new RuntimeException(clientHandlerMessage);
0611:                        }
0612:                        return true;
0613:                    }
0614:                };
0615:                TestSOAPHandler soapHandler1 = new TestSOAPHandler(false);
0616:
0617:                addHandlersToChain((BindingProvider) handlerTest, handler1,
0618:                        handler2, soapHandler1);
0619:
0620:                try {
0621:                    handlerTest.ping();
0622:                    fail("did not get expected exception");
0623:                } catch (RuntimeException e) {
0624:                    assertTrue(e.getMessage().contains(clientHandlerMessage));
0625:                }
0626:
0627:                assertEquals(1, handler1.getHandleMessageInvoked());
0628:                assertEquals(1, handler2.getHandleMessageInvoked());
0629:                assertEquals(0, soapHandler1.getHandleMessageInvoked());
0630:
0631:                assertEquals(0, handler2.getHandleFaultInvoked());
0632:                assertEquals(0, handler1.getHandleFaultInvoked());
0633:                assertEquals(0, soapHandler1.getHandleFaultInvoked());
0634:
0635:                assertEquals(1, handler1.getCloseInvoked());
0636:                assertEquals(1, handler2.getCloseInvoked());
0637:                assertEquals(0, soapHandler1.getCloseInvoked());
0638:                assertTrue(handler2.getInvokeOrderOfClose() < handler1
0639:                        .getInvokeOrderOfClose());
0640:            }
0641:
0642:            @Test
0643:            public void testLogicalHandlerHandleMessageThrowsRuntimeExceptionClientInbound()
0644:                    throws Exception {
0645:                final String clientHandlerMessage = "handler1 client side";
0646:
0647:                TestHandler<LogicalMessageContext> handler1 = new TestHandler<LogicalMessageContext>(
0648:                        false);
0649:                TestHandler<LogicalMessageContext> handler2 = new TestHandler<LogicalMessageContext>(
0650:                        false) {
0651:                    public boolean handleMessage(LogicalMessageContext ctx) {
0652:                        super .handleMessage(ctx);
0653:                        Boolean outbound = (Boolean) ctx
0654:                                .get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);
0655:                        if (!outbound) {
0656:                            throw new RuntimeException(clientHandlerMessage);
0657:                        }
0658:                        return true;
0659:                    }
0660:                };
0661:                TestSOAPHandler soapHandler1 = new TestSOAPHandler(false);
0662:
0663:                addHandlersToChain((BindingProvider) handlerTest, handler1,
0664:                        handler2, soapHandler1);
0665:
0666:                try {
0667:                    handlerTest.ping();
0668:                    fail("did not get expected exception");
0669:                } catch (RuntimeException e) {
0670:                    assertTrue(e.getMessage().contains(clientHandlerMessage));
0671:                }
0672:
0673:                assertEquals(1, handler1.getHandleMessageInvoked());
0674:                assertEquals(2, handler2.getHandleMessageInvoked());
0675:                assertEquals(2, soapHandler1.getHandleMessageInvoked());
0676:
0677:                assertEquals(0, handler2.getHandleFaultInvoked());
0678:                assertEquals(0, handler1.getHandleFaultInvoked());
0679:                assertEquals(0, soapHandler1.getHandleFaultInvoked());
0680:
0681:                assertEquals(1, handler1.getCloseInvoked());
0682:                assertEquals(1, handler2.getCloseInvoked());
0683:                assertEquals(1, soapHandler1.getCloseInvoked());
0684:                assertTrue(handler2.getInvokeOrderOfClose() < handler1
0685:                        .getInvokeOrderOfClose());
0686:            }
0687:
0688:            @Test
0689:            public void testSOAPHandlerHandleMessageThrowsProtocolExceptionClientOutbound()
0690:                    throws Exception {
0691:                final String clientHandlerMessage = "handler1 client side";
0692:
0693:                TestHandler<LogicalMessageContext> handler1 = new TestHandler<LogicalMessageContext>(
0694:                        false);
0695:                TestHandler<LogicalMessageContext> handler2 = new TestHandler<LogicalMessageContext>(
0696:                        false);
0697:                TestSOAPHandler soapHandler1 = new TestSOAPHandler<SOAPMessageContext>(
0698:                        false) {
0699:                    public boolean handleMessage(SOAPMessageContext ctx) {
0700:                        super .handleMessage(ctx);
0701:                        Boolean outbound = (Boolean) ctx
0702:                                .get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);
0703:                        if (outbound) {
0704:                            throw new ProtocolException(clientHandlerMessage);
0705:                        }
0706:                        return true;
0707:                    }
0708:                };
0709:                TestSOAPHandler soapHandler2 = new TestSOAPHandler(false);
0710:
0711:                addHandlersToChain((BindingProvider) handlerTest, handler1,
0712:                        handler2, soapHandler1, soapHandler2);
0713:
0714:                try {
0715:                    handlerTest.ping();
0716:                    fail("did not get expected exception");
0717:                } catch (ProtocolException e) {
0718:                    assertEquals(clientHandlerMessage, e.getMessage());
0719:                }
0720:
0721:                assertEquals(1, handler1.getHandleMessageInvoked());
0722:                assertEquals(1, handler2.getHandleMessageInvoked());
0723:                assertEquals(1, soapHandler1.getHandleMessageInvoked());
0724:                assertEquals(0, soapHandler2.getHandleMessageInvoked());
0725:
0726:                assertEquals(1, handler2.getHandleFaultInvoked());
0727:                assertEquals(1, handler1.getHandleFaultInvoked());
0728:                assertEquals(0, soapHandler1.getHandleFaultInvoked());
0729:                assertEquals(0, soapHandler2.getHandleFaultInvoked());
0730:
0731:                assertEquals(1, handler1.getCloseInvoked());
0732:                assertEquals(1, handler2.getCloseInvoked());
0733:                assertEquals(1, soapHandler1.getCloseInvoked());
0734:                assertEquals(0, soapHandler2.getCloseInvoked());
0735:                assertTrue(handler2.getInvokeOrderOfClose() < handler1
0736:                        .getInvokeOrderOfClose());
0737:            }
0738:
0739:            @Test
0740:            public void testSOAPHandlerHandleMessageThrowsProtocolExceptionClientInbound()
0741:                    throws Exception {
0742:                final String clientHandlerMessage = "handler1 client side";
0743:
0744:                TestHandler<LogicalMessageContext> handler1 = new TestHandler<LogicalMessageContext>(
0745:                        false);
0746:                TestHandler<LogicalMessageContext> handler2 = new TestHandler<LogicalMessageContext>(
0747:                        false);
0748:                TestSOAPHandler soapHandler1 = new TestSOAPHandler<SOAPMessageContext>(
0749:                        false) {
0750:                    public boolean handleMessage(SOAPMessageContext ctx) {
0751:                        super .handleMessage(ctx);
0752:                        Boolean outbound = (Boolean) ctx
0753:                                .get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);
0754:                        if (!outbound) {
0755:                            throw new ProtocolException(clientHandlerMessage);
0756:                        }
0757:                        return true;
0758:                    }
0759:                };
0760:                TestSOAPHandler soapHandler2 = new TestSOAPHandler(false);
0761:
0762:                addHandlersToChain((BindingProvider) handlerTest, handler1,
0763:                        handler2, soapHandler1, soapHandler2);
0764:
0765:                try {
0766:                    handlerTest.ping();
0767:                    fail("did not get expected exception");
0768:                } catch (ProtocolException e) {
0769:                    assertEquals(clientHandlerMessage, e.getMessage());
0770:                }
0771:
0772:                assertEquals(1, handler1.getHandleMessageInvoked());
0773:                assertEquals(1, handler2.getHandleMessageInvoked());
0774:                assertEquals(2, soapHandler1.getHandleMessageInvoked());
0775:                assertEquals(2, soapHandler2.getHandleMessageInvoked());
0776:
0777:                assertEquals(0, handler2.getHandleFaultInvoked());
0778:                assertEquals(0, handler1.getHandleFaultInvoked());
0779:                assertEquals(0, soapHandler1.getHandleFaultInvoked());
0780:                assertEquals(0, soapHandler2.getHandleFaultInvoked());
0781:
0782:                assertEquals(1, handler1.getCloseInvoked());
0783:                assertEquals(1, handler2.getCloseInvoked());
0784:                assertEquals(1, soapHandler1.getCloseInvoked());
0785:                assertEquals(1, soapHandler2.getCloseInvoked());
0786:                assertTrue(handler2.getInvokeOrderOfClose() < handler1
0787:                        .getInvokeOrderOfClose());
0788:            }
0789:
0790:            @Test
0791:            public void testSOAPHandlerHandleMessageThrowsRuntimeExceptionClientOutbound()
0792:                    throws Exception {
0793:                final String clientHandlerMessage = "handler1 client side";
0794:
0795:                TestHandler<LogicalMessageContext> handler1 = new TestHandler<LogicalMessageContext>(
0796:                        false);
0797:                TestHandler<LogicalMessageContext> handler2 = new TestHandler<LogicalMessageContext>(
0798:                        false);
0799:                TestSOAPHandler soapHandler1 = new TestSOAPHandler<SOAPMessageContext>(
0800:                        false) {
0801:                    public boolean handleMessage(SOAPMessageContext ctx) {
0802:                        super .handleMessage(ctx);
0803:                        Boolean outbound = (Boolean) ctx
0804:                                .get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);
0805:                        if (outbound) {
0806:                            throw new RuntimeException(clientHandlerMessage);
0807:                        }
0808:                        return true;
0809:                    }
0810:                };
0811:                TestSOAPHandler soapHandler2 = new TestSOAPHandler(false);
0812:
0813:                addHandlersToChain((BindingProvider) handlerTest, handler1,
0814:                        handler2, soapHandler1, soapHandler2);
0815:                try {
0816:                    handlerTest.ping();
0817:                    fail("did not get expected exception");
0818:                } catch (RuntimeException e) {
0819:                    assertTrue(e.getMessage().contains(clientHandlerMessage));
0820:                }
0821:
0822:                assertEquals(1, handler1.getHandleMessageInvoked());
0823:                assertEquals(1, handler2.getHandleMessageInvoked());
0824:                assertEquals(1, soapHandler1.getHandleMessageInvoked());
0825:                assertEquals(0, soapHandler2.getHandleMessageInvoked());
0826:
0827:                assertEquals(0, handler2.getHandleFaultInvoked());
0828:                assertEquals(0, handler1.getHandleFaultInvoked());
0829:                assertEquals(0, soapHandler1.getHandleFaultInvoked());
0830:                assertEquals(0, soapHandler2.getHandleFaultInvoked());
0831:
0832:                assertEquals(1, handler1.getCloseInvoked());
0833:                assertEquals(1, handler2.getCloseInvoked());
0834:                assertEquals(1, soapHandler1.getCloseInvoked());
0835:                assertEquals(0, soapHandler2.getCloseInvoked());
0836:                assertTrue(handler2.getInvokeOrderOfClose() < handler1
0837:                        .getInvokeOrderOfClose());
0838:            }
0839:
0840:            //REVISIT: following tests only works when start the server alone(i.e, not within HandlerInvocationTest)
0841:            //otherwise, there is no response received from the server. But I have run similar test scenarios with
0842:            //standalone CXF client and Tomcat deployed CXF server, they all works. Can not figure out the problem,  
0843:            //so just comment out these tests for the time being.
0844:
0845:            @Test
0846:            public void testSOAPHandlerHandleMessageThrowsRuntimeExceptionServerInbound()
0847:                    throws PingException {
0848:                try {
0849:                    handlerTest
0850:                            .pingWithArgs("soapHandler3 inbound throw RuntimeException");
0851:                    fail("did not get expected exception");
0852:                } catch (RuntimeException e) {
0853:                    /*            e.printStackTrace();
0854:                     ByteArrayOutputStream baos = new ByteArrayOutputStream();
0855:                     PrintStream ps = new PrintStream(baos, true);
0856:                     e.printStackTrace(ps);
0857:                     assertTrue("Did not get expected exception message",  baos.toString()
0858:                     .indexOf("HandleMessage throws exception") > -1);
0859:                     assertTrue("Did not get expected javax.xml.ws.soap.SOAPFaultException", baos.toString()
0860:                     .indexOf("javax.xml.ws.soap.SOAPFaultException") > -1);*/
0861:                }
0862:            }
0863:
0864:            @Test
0865:            public void testSOAPHandlerHandleMessageThrowsRuntimeExceptionServerOutbound()
0866:                    throws PingException {
0867:                try {
0868:                    handlerTest
0869:                            .pingWithArgs("soapHandler3 outbound throw RuntimeException");
0870:                    fail("did not get expected exception");
0871:                } catch (RuntimeException e) {
0872:                    //e.printStackTrace();
0873:                    /*            ByteArrayOutputStream baos = new ByteArrayOutputStream();
0874:                     PrintStream ps = new PrintStream(baos, true);
0875:                     e.printStackTrace(ps);
0876:                     assertTrue("Did not get expected exception message",  baos.toString()
0877:                     .indexOf("HandleMessage throws exception") > -1);
0878:                     assertTrue("Did not get expected javax.xml.ws.soap.SOAPFaultException", baos.toString()
0879:                     .indexOf("javax.xml.ws.soap.SOAPFaultException") > -1);*/
0880:                }
0881:            }
0882:
0883:            @Test
0884:            public void testSOAPHandlerHandleMessageThrowsProtocolExceptionServerInbound()
0885:                    throws PingException {
0886:                try {
0887:                    handlerTest
0888:                            .pingWithArgs("soapHandler3 inbound throw ProtocolException");
0889:                    fail("did not get expected WebServiceException");
0890:                } catch (WebServiceException e) {
0891:                    /*            e.printStackTrace();
0892:                     ByteArrayOutputStream baos = new ByteArrayOutputStream();
0893:                     PrintStream ps = new PrintStream(baos, true);
0894:                     e.printStackTrace(ps);
0895:                     assertTrue("Did not get expected exception message",  baos.toString()
0896:                     .indexOf("HandleMessage throws exception") > -1);
0897:                     assertTrue("Did not get expected javax.xml.ws.soap.SOAPFaultException", baos.toString()
0898:                     .indexOf("javax.xml.ws.soap.SOAPFaultException") > -1);*/
0899:                }
0900:            }
0901:
0902:            /*-------------------------------------------------------
0903:             * This is the expected order
0904:             *-------------------------------------------------------
0905:             * soapHandler3.handleMessage().doInbound()
0906:             * soapHandler4.handleMessage().doInbound()
0907:             * soapHandler4 Throwing an inbound ProtocolException
0908:             * soapHandler3.handleFault()
0909:             * soapHandler3 Throwing an outbound RuntimeException
0910:             * soapHandler4.close()
0911:             * soapHandler3.close()
0912:             */
0913:            @Test
0914:            public void testSOAPHandlerHandleFaultThrowsRuntimeExceptionServerOutbound()
0915:                    throws PingException {
0916:                try {
0917:                    handlerTest
0918:                            .pingWithArgs("soapHandler3 inbound throw ProtocolException "
0919:                                    + "soapHandler4HandleFaultThrowsRunException");
0920:                    fail("did not get expected WebServiceException");
0921:                } catch (WebServiceException e) {
0922:                    //e.printStackTrace();
0923:                    /*            ByteArrayOutputStream baos = new ByteArrayOutputStream();
0924:                     PrintStream ps = new PrintStream(baos, true);
0925:                     e.printStackTrace(ps);
0926:                     assertTrue("Did not get expected exception message",  baos.toString()
0927:                     .indexOf("soapHandler4 HandleFault throws RuntimeException") > -1);
0928:                     assertTrue("Did not get expected javax.xml.ws.soap.SOAPFaultException", baos.toString()
0929:                     .indexOf("javax.xml.ws.soap.SOAPFaultException") > -1);*/
0930:                }
0931:            }
0932:
0933:            @Test
0934:            public void testSOAPHandlerHandleFaultThrowsSOAPFaultExceptionServerOutbound()
0935:                    throws PingException {
0936:                try {
0937:                    handlerTest
0938:                            .pingWithArgs("soapHandler3 inbound throw ProtocolException "
0939:                                    + "soapHandler4HandleFaultThrowsSOAPFaultException");
0940:                    fail("did not get expected SOAPFaultException");
0941:                } catch (SOAPFaultException e) {
0942:                    /*            e.printStackTrace();
0943:                     ByteArrayOutputStream baos = new ByteArrayOutputStream();
0944:                     PrintStream ps = new PrintStream(baos, true);
0945:                     e.printStackTrace(ps);
0946:                     assertTrue("Did not get expected exception message",  baos.toString()
0947:                     .indexOf("soapHandler4 HandleFault throws SOAPFaultException") > -1);
0948:                     assertTrue("Did not get expected javax.xml.ws.soap.SOAPFaultException", baos.toString()
0949:                     .indexOf("javax.xml.ws.soap.SOAPFaultException") > -1);*/
0950:                }
0951:            }
0952:
0953:            @Test
0954:            public void testSOAPHandlerHandleMessageThrowsProtocolExceptionServerOutbound()
0955:                    throws PingException {
0956:                try {
0957:                    handlerTest
0958:                            .pingWithArgs("soapHandler3 outbound throw ProtocolException");
0959:                    fail("did not get expected WebServiceException");
0960:                } catch (WebServiceException e) {
0961:                    /*            e.printStackTrace();
0962:                     ByteArrayOutputStream baos = new ByteArrayOutputStream();
0963:                     PrintStream ps = new PrintStream(baos, true);
0964:                     e.printStackTrace(ps);
0965:                     assertTrue("Did not get expected exception message",  baos.toString()
0966:                     .indexOf("HandleMessage throws exception") > -1);
0967:                     assertTrue("Did not get expected javax.xml.ws.soap.SOAPFaultException", baos.toString()
0968:                     .indexOf("javax.xml.ws.soap.SOAPFaultException") > -1);*/
0969:                }
0970:            }
0971:
0972:            @Test
0973:            public void testServerSOAPInboundHandlerThrowsSOAPFaultToClientHandlers()
0974:                    throws Exception {
0975:                TestHandler<LogicalMessageContext> handler1 = new TestHandler<LogicalMessageContext>(
0976:                        false);
0977:                TestHandler<LogicalMessageContext> handler2 = new TestHandler<LogicalMessageContext>(
0978:                        false) {
0979:                    public boolean handleFault(LogicalMessageContext ctx) {
0980:                        super .handleFault(ctx);
0981:                        try {
0982:                            Boolean outbound = (Boolean) ctx
0983:                                    .get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);
0984:                            if (!outbound) {
0985:                                LogicalMessage msg = ctx.getMessage();
0986:                                Source source = msg.getPayload();
0987:                                assertNotNull(source);
0988:                            }
0989:                        } catch (Exception e) {
0990:                            e.printStackTrace();
0991:                            fail(e.toString());
0992:                        }
0993:                        return true;
0994:                    }
0995:                };
0996:
0997:                TestSOAPHandler soapHandler1 = new TestSOAPHandler(false);
0998:                TestSOAPHandler soapHandler2 = new TestSOAPHandler(false) {
0999:                    public boolean handleFault(SOAPMessageContext ctx) {
1000:                        super .handleFault(ctx);
1001:                        Boolean outbound = (Boolean) ctx
1002:                                .get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);
1003:                        if (!outbound) {
1004:                            try {
1005:                                SOAPMessage msg = ctx.getMessage();
1006:                                assertNotNull(msg);
1007:                            } catch (Exception e) {
1008:                                e.printStackTrace();
1009:                                fail(e.toString());
1010:                            }
1011:                        }
1012:                        return true;
1013:                    }
1014:                };
1015:
1016:                addHandlersToChain((BindingProvider) handlerTest, handler1,
1017:                        handler2, soapHandler1, soapHandler2);
1018:
1019:                try {
1020:                    handlerTest
1021:                            .pingWithArgs("soapHandler3 inbound throw SOAPFaultException");
1022:                    fail("did not get expected SOAPFaultException");
1023:                } catch (SOAPFaultException e) {
1024:                    //e.printStackTrace();            
1025:                    /*            ByteArrayOutputStream baos = new ByteArrayOutputStream();
1026:                     PrintStream ps = new PrintStream(baos, true);
1027:                     e.printStackTrace(ps);
1028:                     assertTrue("Did not get expected exception message",  baos.toString()
1029:                     .indexOf("HandleMessage throws exception") > -1);
1030:                     assertTrue("Did not get expected javax.xml.ws.soap.SOAPFaultException", baos.toString()
1031:                     .indexOf("javax.xml.ws.soap.SOAPFaultException") > -1);*/
1032:
1033:                }
1034:
1035:                /*        assertEquals("handle message was not invoked", 1, handler1.getHandleMessageInvoked());
1036:                 assertEquals("handle message was not invoked", 1, handler2.getHandleMessageInvoked());
1037:                 assertEquals("handle message was not invoked", 1, soapHandler1.getHandleMessageInvoked());
1038:                 assertEquals("handle message was not invoked", 1, soapHandler2.getHandleMessageInvoked());
1039:                
1040:                 assertEquals("handle message was not invoked", 1, handler1.getHandleFaultInvoked());
1041:                 assertEquals("handle message was not invoked", 1, handler2.getHandleFaultInvoked());
1042:                 assertEquals("handle message was not invoked", 1, soapHandler1.getHandleFaultInvoked());
1043:                 assertEquals("handle message was not invoked", 1, soapHandler2.getHandleFaultInvoked());       
1044:                
1045:                 assertEquals("close must be called", 1, handler1.getCloseInvoked());
1046:                 assertEquals("close must be called", 1, handler2.getCloseInvoked());
1047:                 assertEquals("close must be called", 1, soapHandler1.getCloseInvoked());
1048:                 assertEquals("close must be called", 1, soapHandler2.getCloseInvoked());
1049:                
1050:                 assertTrue(soapHandler2.getInvokeOrderOfClose()
1051:                 < soapHandler1.getInvokeOrderOfClose());   
1052:                 assertTrue(soapHandler1.getInvokeOrderOfClose()
1053:                 < handler2.getInvokeOrderOfClose());          
1054:                 assertTrue(handler2.getInvokeOrderOfClose()
1055:                 < handler1.getInvokeOrderOfClose());  */
1056:            }
1057:
1058:            /*-------------------------------------------------------
1059:             * This is the expected order
1060:             *-------------------------------------------------------
1061:             * soapHandler4.handleMessage().doInbound()
1062:             * soapHandler3.handleMessage().doInbound()
1063:             * handler2.handleMessage().doInbound()
1064:             * handler1.handleMessage().doInbound()
1065:             * servant throws RuntimeException
1066:             * handler1.handleFault()
1067:             * handler2.handleFault()
1068:             * soapHandler3.handleFault()
1069:             * soapHandler4.handleFault()
1070:             * handler1.close()
1071:             * handler2.close()
1072:             * soapHandler3.close()
1073:             * soapHandler4.close()
1074:             * */
1075:            @Test
1076:            public void testServerEndpointRemoteRuntimeException()
1077:                    throws PingException {
1078:                try {
1079:                    handlerTest
1080:                            .pingWithArgs("servant throw WebServiceException");
1081:                    fail("did not get expected WebServiceException");
1082:                } catch (WebServiceException e) {
1083:                    /*            e.printStackTrace();
1084:                     ByteArrayOutputStream baos = new ByteArrayOutputStream();
1085:                     PrintStream ps = new PrintStream(baos, true);
1086:                     e.printStackTrace(ps);
1087:                     assertTrue("Did not get expected exception message",  baos.toString()
1088:                     .indexOf("RemoteException with nested RuntimeException") > -1);*/
1089:                }
1090:            }
1091:
1092:            /*-------------------------------------------------------
1093:             * This is the expected order
1094:             *-------------------------------------------------------
1095:             * soapHandler3.handleMessage().doInbound()
1096:             * soapHandler4.handleMessage().doInbound()
1097:             * handler2.handleMessage().doInbound()
1098:             * handler1.handleMessage().doInbound()
1099:             * handler1 Throwing an inbound ProtocolException
1100:             * handler2.handleFault()
1101:             * handler2 Throwing an outbound RuntimeException
1102:             * handler1.close()
1103:             * handler1.close()
1104:             * soapHandler4.close()
1105:             * soapHandler3.close()
1106:             */
1107:            @Test
1108:            public void testLogicalHandlerHandleFaultThrowsRuntimeExceptionServerOutbound()
1109:                    throws PingException {
1110:                try {
1111:                    handlerTest
1112:                            .pingWithArgs("handler1 inbound throw ProtocolException "
1113:                                    + "handler2HandleFaultThrowsRunException");
1114:                    fail("did not get expected WebServiceException");
1115:                } catch (WebServiceException e) {
1116:                    /*            e.printStackTrace();
1117:                     ByteArrayOutputStream baos = new ByteArrayOutputStream();
1118:                     PrintStream ps = new PrintStream(baos, true);
1119:                     e.printStackTrace(ps);
1120:                     assertTrue("Did not get expected exception message", baos.toString()
1121:                     .indexOf("handler2 HandleFault throws RuntimeException") > -1);
1122:                     assertTrue("Did not get expected javax.xml.ws.soap.SOAPFaultException", baos.toString()
1123:                     .indexOf("javax.xml.ws.soap.SOAPFaultException") > -1);*/
1124:
1125:                }
1126:            }
1127:
1128:            @Test
1129:            public void testLogicalHandlerHandleFaultThrowsSOAPFaultExceptionServerOutbound()
1130:                    throws PingException {
1131:                try {
1132:                    handlerTest
1133:                            .pingWithArgs("handler1 inbound throw ProtocolException "
1134:                                    + "handler2HandleFaultThrowsSOAPFaultException");
1135:                    fail("did not get expected SOAPFaultException");
1136:                } catch (SOAPFaultException e) {
1137:                    /*            e.printStackTrace();
1138:                     ByteArrayOutputStream baos = new ByteArrayOutputStream();
1139:                     PrintStream ps = new PrintStream(baos, true);
1140:                     e.printStackTrace(ps);
1141:                     assertTrue("Did not get expected exception message", baos.toString()
1142:                     .indexOf("handler2 HandleFault throws SOAPFaultException") > -1);
1143:                     assertTrue("Did not get expected javax.xml.ws.soap.SOAPFaultException", baos.toString()
1144:                     .indexOf("javax.xml.ws.soap.SOAPFaultException") > -1);*/
1145:                }
1146:            }
1147:
1148:            @Test
1149:            public void testLogicalHandlerHandleMessageThrowsProtocolExceptionServerInbound()
1150:                    throws PingException {
1151:                try {
1152:                    handlerTest
1153:                            .pingWithArgs("handler2 inbound throw ProtocolException");
1154:                    fail("did not get expected exception");
1155:                } catch (WebServiceException e) {
1156:                    assertTrue(e.getMessage().indexOf(
1157:                            "HandleMessage throws exception") >= 0);
1158:                }
1159:            }
1160:
1161:            @Test
1162:            public void testDescription() throws PingException {
1163:                TestHandler<LogicalMessageContext> handler = new TestHandler<LogicalMessageContext>(
1164:                        false) {
1165:                    public boolean handleMessage(LogicalMessageContext ctx) {
1166:                        super .handleMessage(ctx);
1167:                        assertTrue("wsdl description not found or invalid",
1168:                                isValidWsdlDescription(ctx
1169:                                        .get(MessageContext.WSDL_DESCRIPTION)));
1170:                        return true;
1171:                    }
1172:                };
1173:                TestSOAPHandler soapHandler = new TestSOAPHandler<SOAPMessageContext>(
1174:                        false) {
1175:                    public boolean handleMessage(SOAPMessageContext ctx) {
1176:                        super .handleMessage(ctx);
1177:                        assertTrue("wsdl description not found or invalid",
1178:                                isValidWsdlDescription(ctx
1179:                                        .get(MessageContext.WSDL_DESCRIPTION)));
1180:                        return true;
1181:                    }
1182:                };
1183:
1184:                addHandlersToChain((BindingProvider) handlerTest, handler,
1185:                        soapHandler);
1186:
1187:                List<String> resp = handlerTest.ping();
1188:                assertNotNull(resp);
1189:
1190:                assertEquals("handler was not invoked", 2, handler
1191:                        .getHandleMessageInvoked());
1192:                assertEquals("handle message was not invoked", 2, soapHandler
1193:                        .getHandleMessageInvoked());
1194:                assertTrue("close must be  called", handler.isCloseInvoked());
1195:                assertTrue("close must be  called", soapHandler
1196:                        .isCloseInvoked());
1197:            }
1198:
1199:            @Test
1200:            public void testHandlersInvokedForDispatch() throws Exception {
1201:                Dispatch<SOAPMessage> disp = service.createDispatch(portName,
1202:                        SOAPMessage.class, Service.Mode.MESSAGE);
1203:                TestHandler<LogicalMessageContext> handler1 = new TestHandler<LogicalMessageContext>(
1204:                        false);
1205:                TestHandler<LogicalMessageContext> handler2 = new TestHandler<LogicalMessageContext>(
1206:                        false);
1207:                TestSOAPHandler soapHandler1 = new TestSOAPHandler(false);
1208:                TestSOAPHandler soapHandler2 = new TestSOAPHandler(false);
1209:
1210:                addHandlersToChain((BindingProvider) disp, handler1, handler2,
1211:                        soapHandler1, soapHandler2);
1212:
1213:                InputStream is = getClass().getResourceAsStream("PingReq.xml");
1214:                SOAPMessage outMsg = MessageFactory.newInstance()
1215:                        .createMessage(null, is);
1216:
1217:                SOAPMessage inMsg = disp.invoke(outMsg);
1218:                assertNotNull(inMsg);
1219:
1220:                assertEquals("handle message was not invoked", 2, handler1
1221:                        .getHandleMessageInvoked());
1222:                assertEquals("handle message was not invoked", 2, handler2
1223:                        .getHandleMessageInvoked());
1224:                assertEquals("handle message was not invoked", 2, soapHandler1
1225:                        .getHandleMessageInvoked());
1226:                assertEquals("handle message was not invoked", 2, soapHandler2
1227:                        .getHandleMessageInvoked());
1228:
1229:                assertEquals("close must be called", 1, handler1
1230:                        .getCloseInvoked());
1231:                assertEquals("close must be called", 1, handler2
1232:                        .getCloseInvoked());
1233:                assertEquals("close must be called", 1, soapHandler1
1234:                        .getCloseInvoked());
1235:                assertEquals("close must be called", 1, soapHandler2
1236:                        .getCloseInvoked());
1237:
1238:                // the server has encoded into the response the order in
1239:                // which the handlers have been invoked, parse it and make
1240:                // sure everything is ok
1241:
1242:                // expected order for inbound interceptors
1243:                String[] handlerNames = { "soapHandler4", "soapHandler3",
1244:                        "handler2", "handler1", "servant", "handler1",
1245:                        "handler2", "soapHandler3", "soapHandler4" };
1246:
1247:                List<String> resp = getHandlerNames(inMsg.getSOAPBody()
1248:                        .getChildNodes());
1249:                assertEquals(handlerNames.length, resp.size());
1250:
1251:                Iterator iter = resp.iterator();
1252:                for (String expected : handlerNames) {
1253:                    assertEquals(expected, iter.next());
1254:                }
1255:            }
1256:
1257:            void addHandlersToChain(BindingProvider bp, Handler... handlers) {
1258:                List<Handler> handlerChain = bp.getBinding().getHandlerChain();
1259:                assertNotNull(handlerChain);
1260:                for (Handler h : handlers) {
1261:                    handlerChain.add(h);
1262:                }
1263:            }
1264:
1265:            List<String> getHandlerNames(NodeList nodes) throws Exception {
1266:                List<String> stringList = null;
1267:                Node elNode = null;
1268:                for (int idx = 0; idx < nodes.getLength(); idx++) {
1269:                    Node n = nodes.item(idx);
1270:                    if (n.getNodeType() == Node.ELEMENT_NODE) {
1271:                        elNode = n;
1272:                        break;
1273:                    }
1274:                }
1275:
1276:                JAXBContext jaxbCtx = JAXBContext
1277:                        .newInstance(PingResponse.class);
1278:                Unmarshaller um = jaxbCtx.createUnmarshaller();
1279:                Object obj = um.unmarshal(elNode);
1280:
1281:                if (obj instanceof  PingResponse) {
1282:                    PingResponse pr = PingResponse.class.cast(obj);
1283:                    stringList = pr.getHandlersInfo();
1284:                }
1285:                return stringList;
1286:            }
1287:
1288:            public class MyHandlerResolver implements  HandlerResolver {
1289:                List<Handler> chain = new ArrayList<Handler>();
1290:                String bindingID;
1291:
1292:                public MyHandlerResolver(Handler... handlers) {
1293:                    for (Handler h : handlers) {
1294:                        chain.add(h);
1295:                    }
1296:                }
1297:
1298:                public List<Handler> getHandlerChain(PortInfo portInfo) {
1299:                    bindingID = portInfo.getBindingID();
1300:                    return chain;
1301:                }
1302:
1303:            }
1304:
1305:            private boolean isValidWsdlDescription(Object wsdlDescription) {
1306:                return (wsdlDescription != null)
1307:                        && ((wsdlDescription instanceof  java.net.URI) || (wsdlDescription instanceof  java.net.URL));
1308:            }
1309:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.