handlerchainprocessortests.java

来自「开源的axis2框架的源码。用于开发WEBSERVER」· Java 代码 · 共 1,016 行 · 第 1/3 页

JAVA
1,016
字号
        mc1.setMEPContext(new MEPContext(mc1));
        Exception e = null;
        try {
            // handleFault processing, but notice S2f does not get called, and we get an exception
            processor.processChain(mc1.getMEPContext(),
                                   HandlerChainProcessor.Direction.IN,
                                   HandlerChainProcessor.MEP.REQUEST,
                                   true);
        } catch (ProtocolException pe) {
            e = pe;
        }

        assertNotNull(e);
        assertEquals("S2m:S1m:L1m:S1f:L1c:S1c:S2c:", result);
    }

    /*
     * incoming request (we must be on the server), response expected
     * a middle Handler.handleMessage throws ProtocolException, later a Handler.handleFault throws ProtocolException
     * processing expected:  Logical and SOAP, reverse order, handleFault, close
     */
    public void testHandleMessage_protocolex_runtimeex() {

        // reset result
        result = "";

        // we want one false response:
        soaphandler1_MessageResultDesired = ResultDesired.TRUE;
        soaphandler1_FaultResultDesired = ResultDesired.OTHER_EXCEPTION;
        soaphandler2_MessageResultDesired = ResultDesired.TRUE;
        soaphandler2_FaultResultDesired = ResultDesired.TRUE;
        logicalhandler1_MessageResultDesired = ResultDesired.PROTOCOL_EXCEPTION;
        logicalhandler1_FaultResultDesired = ResultDesired.TRUE;
        logicalhandler2_MessageResultDesired = ResultDesired.TRUE;
        logicalhandler2_FaultResultDesired = ResultDesired.TRUE;

        HandlerChainProcessor processor = new HandlerChainProcessor(handlers, Protocol.soap11);
        MessageContext mc1 = new MessageContext();
        mc1.setMEPContext(new MEPContext(mc1));
        Exception e = null;
        try {
            // same results as testHandlers_protocolex_protocolex
            processor.processChain(mc1.getMEPContext(),
                                   HandlerChainProcessor.Direction.IN,
                                   HandlerChainProcessor.MEP.REQUEST,
                                   true);
        } catch (RuntimeException pe) {
            e = pe;
        }

        assertNotNull(e);
        assertEquals("S2m:S1m:L1m:S1f:L1c:S1c:S2c:", result);
    }


    /*
     * empty list
     */
    public void testHandleFault_empty1() {

        Exception local_exception = null;

        HandlerChainProcessor processor1 = new HandlerChainProcessor(null, Protocol.soap11);
        HandlerChainProcessor processor2 =
                new HandlerChainProcessor(new ArrayList<Handler>(), Protocol.soap11);
        try {
            MessageContext mc1 = new MessageContext();
            mc1.setMEPContext(new MEPContext(mc1));
            processor1.processFault(mc1.getMEPContext(), HandlerChainProcessor.Direction.IN);
            MessageContext mc2 = new MessageContext();
            mc2.setMEPContext(new MEPContext(mc2));
            processor2.processFault(mc2.getMEPContext(), HandlerChainProcessor.Direction.IN);
        } catch (Exception e) {
            local_exception = e;
        }

        // no exceptions!
        assertNull(local_exception);
    }


    /*
     * outgoing response (we must be on the server), response expected (ignored)
     * processing expected:  Logical and SOAP, normal order, handleFault, close
     */
    public void testHandleFault_true1() {

        // reset result
        result = "";

        // we want one false response:
        soaphandler1_MessageResultDesired = ResultDesired.TRUE;
        soaphandler1_FaultResultDesired = ResultDesired.TRUE;
        soaphandler2_MessageResultDesired = ResultDesired.TRUE;
        soaphandler2_FaultResultDesired = ResultDesired.TRUE;
        logicalhandler1_MessageResultDesired = ResultDesired.TRUE;
        logicalhandler1_FaultResultDesired = ResultDesired.TRUE;
        logicalhandler2_MessageResultDesired = ResultDesired.TRUE;
        logicalhandler2_FaultResultDesired = ResultDesired.TRUE;

        HandlerChainProcessor processor = new HandlerChainProcessor(handlers, Protocol.soap11);
        MessageContext mc1 = new MessageContext();
        mc1.setMEPContext(new MEPContext(mc1));
        processor.processFault(mc1.getMEPContext(), HandlerChainProcessor.Direction.OUT);

        assertEquals("L2f:L1f:S1f:S2f:L2c:L1c:S1c:S2c:", result);
    }

    /*
     * outgoing response (we must be on the server)
     * a middle Handler.handleFault returns false
     * processing expected:  Logical and SOAP, normal order, handleFault, close (all)
     */
    public void testHandleFault_false1() {

        // reset result
        result = "";

        // we want one false response:
        soaphandler1_MessageResultDesired = ResultDesired.TRUE;
        soaphandler1_FaultResultDesired = ResultDesired.TRUE;
        soaphandler2_MessageResultDesired = ResultDesired.TRUE;
        soaphandler2_FaultResultDesired = ResultDesired.TRUE;
        logicalhandler1_MessageResultDesired = ResultDesired.TRUE;
        logicalhandler1_FaultResultDesired = ResultDesired.FALSE;
        logicalhandler2_MessageResultDesired = ResultDesired.TRUE;
        logicalhandler2_FaultResultDesired = ResultDesired.TRUE;

        HandlerChainProcessor processor = new HandlerChainProcessor(handlers, Protocol.soap11);
        MessageContext mc1 = new MessageContext();
        mc1.setMEPContext(new MEPContext(mc1));
        processor.processFault(mc1.getMEPContext(), HandlerChainProcessor.Direction.OUT);

        // notice all handlers are closed in this scenario
        assertEquals("L2f:L1f:L2c:L1c:S1c:S2c:", result);
    }

    /*
     * incoming response (we must be on the client)
     * a middle Handler.handleFault throws ProtocolException
     * processing expected:  Logical and SOAP, reverse order, handleFault, close (all)
     */
    public void testHandleFault_protocolex() {

        // reset result
        result = "";

        // we want one false response:
        soaphandler1_MessageResultDesired = ResultDesired.TRUE;
        soaphandler1_FaultResultDesired = ResultDesired.TRUE;
        soaphandler2_MessageResultDesired = ResultDesired.TRUE;
        soaphandler2_FaultResultDesired = ResultDesired.TRUE;
        logicalhandler1_MessageResultDesired = ResultDesired.TRUE;
        logicalhandler1_FaultResultDesired = ResultDesired.PROTOCOL_EXCEPTION;
        logicalhandler2_MessageResultDesired = ResultDesired.TRUE;
        logicalhandler2_FaultResultDesired = ResultDesired.TRUE;

        HandlerChainProcessor processor = new HandlerChainProcessor(handlers, Protocol.soap11);
        MessageContext mc1 = new MessageContext();
        mc1.setMEPContext(new MEPContext(mc1));
        Exception e = null;
        try {
            // notice all handlers are closed in this scenario, and we get an exception
            processor.processFault(mc1.getMEPContext(), HandlerChainProcessor.Direction.IN);
        } catch (ProtocolException pe) {
            e = pe;
        }

        assertNotNull(e);
        assertEquals("S2f:S1f:L1f:S2c:S1c:L1c:L2c:", result);
    }


    private class SOAPHandler1 implements SOAPHandler<SOAPMessageContext> {

        public Set getHeaders() {
            return null;
        }

        public void close(javax.xml.ws.handler.MessageContext messagecontext) {
            result = result.concat("S1c:");
        }

        public boolean handleFault(SOAPMessageContext messagecontext) {
            result = result.concat("S1f:");
            if (soaphandler1_FaultResultDesired == ResultDesired.TRUE)
                return true;
            else if (soaphandler1_FaultResultDesired == ResultDesired.FALSE)
                return false;
            else if (soaphandler1_FaultResultDesired == ResultDesired.PROTOCOL_EXCEPTION)
                throw new ProtocolException();
            else if (soaphandler1_FaultResultDesired == ResultDesired.OTHER_EXCEPTION)
                throw new RuntimeException();

            // default
            return true;
        }

        public boolean handleMessage(SOAPMessageContext messagecontext) {
            result = result.concat("S1m:");
            if (soaphandler1_MessageResultDesired == ResultDesired.TRUE)
                return true;
            else if (soaphandler1_MessageResultDesired == ResultDesired.FALSE)
                return false;
            else if (soaphandler1_MessageResultDesired == ResultDesired.PROTOCOL_EXCEPTION)
                throw new ProtocolException();
            else if (soaphandler1_MessageResultDesired == ResultDesired.OTHER_EXCEPTION)
                throw new RuntimeException();

            // default
            return true;
        }

    }


    private class SOAPHandler2 implements SOAPHandler<SOAPMessageContext> {

        public Set getHeaders() {
            return null;
        }

        public void close(javax.xml.ws.handler.MessageContext messagecontext) {
            result = result.concat("S2c:");
        }

        public boolean handleFault(SOAPMessageContext messagecontext) {
            result = result.concat("S2f:");
            if (soaphandler2_FaultResultDesired == ResultDesired.TRUE)
                return true;
            else if (soaphandler2_FaultResultDesired == ResultDesired.FALSE)
                return false;
            else if (soaphandler2_FaultResultDesired == ResultDesired.PROTOCOL_EXCEPTION)
                throw new ProtocolException();
            else if (soaphandler2_FaultResultDesired == ResultDesired.OTHER_EXCEPTION)
                throw new RuntimeException();

            // default
            return true;
        }

        public boolean handleMessage(SOAPMessageContext messagecontext) {
            result = result.concat("S2m:");
            if (soaphandler2_MessageResultDesired == ResultDesired.TRUE)
                return true;
            else if (soaphandler2_MessageResultDesired == ResultDesired.FALSE)
                return false;
            else if (soaphandler2_MessageResultDesired == ResultDesired.PROTOCOL_EXCEPTION)
                throw new ProtocolException();
            else if (soaphandler2_MessageResultDesired == ResultDesired.OTHER_EXCEPTION)
                throw new RuntimeException();

            // default
            return true;
        }

    }


    private class LogicalHandler1 implements LogicalHandler<LogicalMessageContext> {

        public void close(javax.xml.ws.handler.MessageContext messagecontext) {
            result = result.concat("L1c:");
        }

        public boolean handleFault(LogicalMessageContext messagecontext) {
            result = result.concat("L1f:");
            if (logicalhandler1_FaultResultDesired == ResultDesired.TRUE)
                return true;
            else if (logicalhandler1_FaultResultDesired == ResultDesired.FALSE)
                return false;
            else if (logicalhandler1_FaultResultDesired == ResultDesired.PROTOCOL_EXCEPTION)
                throw new ProtocolException();
            else if (logicalhandler1_FaultResultDesired == ResultDesired.OTHER_EXCEPTION)
                throw new RuntimeException();

            // default
            return true;
        }

        public boolean handleMessage(LogicalMessageContext messagecontext) {
            result = result.concat("L1m:");
            if (logicalhandler1_MessageResultDesired == ResultDesired.TRUE)
                return true;
            else if (logicalhandler1_MessageResultDesired == ResultDesired.FALSE)
                return false;
            else if (logicalhandler1_MessageResultDesired == ResultDesired.PROTOCOL_EXCEPTION)
                throw new ProtocolException();
            else if (logicalhandler1_MessageResultDesired == ResultDesired.OTHER_EXCEPTION)
                throw new RuntimeException();

            // default
            return true;
        }

    }


    private class LogicalHandler2 implements LogicalHandler<LogicalMessageContext> {

        public void close(javax.xml.ws.handler.MessageContext messagecontext) {
            result = result.concat("L2c:");
        }

        public boolean handleFault(LogicalMessageContext messagecontext) {
            result = result.concat("L2f:");
            if (logicalhandler2_FaultResultDesired == ResultDesired.TRUE)
                return true;
            else if (logicalhandler2_FaultResultDesired == ResultDesired.FALSE)
                return false;
            else if (logicalhandler2_FaultResultDesired == ResultDesired.PROTOCOL_EXCEPTION)
                throw new ProtocolException();
            else if (logicalhandler2_FaultResultDesired == ResultDesired.OTHER_EXCEPTION)
                throw new RuntimeException();

            // default
            return true;
        }

        public boolean handleMessage(LogicalMessageContext messagecontext) {
            result = result.concat("L2m:");
            if (logicalhandler2_MessageResultDesired == ResultDesired.TRUE)
                return true;
            else if (logicalhandler2_MessageResultDesired == ResultDesired.FALSE)
                return false;
            else if (logicalhandler2_MessageResultDesired == ResultDesired.PROTOCOL_EXCEPTION)
                throw new ProtocolException();
            else if (logicalhandler2_MessageResultDesired == ResultDesired.OTHER_EXCEPTION)
                throw new RuntimeException();

            // default
            return true;
        }

    }

}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?