handlerchainprocessortests.java

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

JAVA
1,016
字号
/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements. See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership. The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
package org.apache.axis2.jaxws.handler;

import java.util.ArrayList;
import java.util.Set;

import javax.xml.ws.ProtocolException;
import javax.xml.ws.handler.Handler;
import javax.xml.ws.handler.LogicalHandler;
import javax.xml.ws.handler.soap.SOAPHandler;
import javax.xml.ws.handler.soap.SOAPMessageContext;

import junit.framework.TestCase;

import org.apache.axis2.jaxws.core.MessageContext;
import org.apache.axis2.jaxws.message.Protocol;

/*
 * There are myriad scenarios to test here:
 * Handler implementations can implement two classes:  SOAPHandler or LogicalHandler (2)
 * They implement two critical methods:  handleMessage and handleFault (2)
 * These methods have four possible results:  true, false, ProtocolException, other exception
 * 
 * Besides the possible behaviors of the Handler implementations, we also
 * have to consider whether the message is incoming or outgoing, whether
 * it's a response or a request, and if a response is expected.
 * 
 * Do our best to cover all scenarios.
 * 
 * The testHandleMessage_* methods test the HandlerChainProcessor.processChain() method
 * The testHandleFault_* methods test the HandlerChainProcessor.processFault() method
 * 
 */
public class HandlerChainProcessorTests extends TestCase {

    // String result is how we'll verify the right methods from
    // the Handler implementations were called
    private String result = new String();

    private enum ResultDesired {
        TRUE, FALSE, PROTOCOL_EXCEPTION, OTHER_EXCEPTION
    };

    // use the following to dictate how the Handler methods behave
    private ResultDesired soaphandler1_MessageResultDesired;
    private ResultDesired soaphandler1_FaultResultDesired;
    private ResultDesired soaphandler2_MessageResultDesired;
    private ResultDesired soaphandler2_FaultResultDesired;
    private ResultDesired logicalhandler1_MessageResultDesired;
    private ResultDesired logicalhandler1_FaultResultDesired;
    private ResultDesired logicalhandler2_MessageResultDesired;
    private ResultDesired logicalhandler2_FaultResultDesired;

    ArrayList<Handler> handlers = new ArrayList<Handler>();

    @Override
    protected void setUp() throws Exception {
        // HandlerChainProcessor expects a sorted chain
        handlers.add(new LogicalHandler2());
        handlers.add(new LogicalHandler1());
        handlers.add(new SOAPHandler1());
        handlers.add(new SOAPHandler2());
    }

    /*
     * empty list
     */
    public void testHandleMessage_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.processChain(mc1.getMEPContext(),
                                    HandlerChainProcessor.Direction.IN,
                                    HandlerChainProcessor.MEP.REQUEST,
                                    true);
            MessageContext mc2 = new MessageContext();
            mc2.setMEPContext(new MEPContext(mc2));
            processor2.processChain(mc2.getMEPContext(),
                                    HandlerChainProcessor.Direction.IN,
                                    HandlerChainProcessor.MEP.REQUEST,
                                    true);
        } catch (Exception e) {
            local_exception = e;
        }

        // no exceptions!
        assertNull(local_exception);
    }

    /*
     * one protocol handler
     * processing expected:  Logical and SOAP, reverse order, close
     */
    public void testHandleMessage_oneproto1() {

        // reset result
        result = "";

        // use a local list
        ArrayList<Handler> local_list = new ArrayList<Handler>();
        local_list.add(new SOAPHandler1());

        // we want all good responses:
        soaphandler1_MessageResultDesired = ResultDesired.TRUE;
        soaphandler1_FaultResultDesired = ResultDesired.TRUE;

        HandlerChainProcessor processor = new HandlerChainProcessor(local_list, Protocol.soap11);
        MessageContext mc1 = new MessageContext();
        mc1.setMEPContext(new MEPContext(mc1));
        processor.processChain(mc1.getMEPContext(),
                               HandlerChainProcessor.Direction.IN,
                               HandlerChainProcessor.MEP.REQUEST,
                               false);

        assertEquals("S1m:S1c:", result);

    }

    /*
     * one protocol handler in a logical context
     * no handlers will be processed
     */
    public void testHandleMessage_oneproto2() {

        // reset result
        result = "";

        // use a local list
        ArrayList<Handler> local_list = new ArrayList<Handler>();
        local_list.add(new SOAPHandler1());

        // we want all good responses:
        soaphandler1_MessageResultDesired = ResultDesired.TRUE;
        soaphandler1_FaultResultDesired = ResultDesired.TRUE;

        HandlerChainProcessor processor = new HandlerChainProcessor(local_list, Protocol.soap11);
        MessageContext mc1 = new MessageContext();
        mc1.setMEPContext(new MEPContext(mc1));
        processor.processChain(mc1.getMEPContext(),
                               HandlerChainProcessor.Direction.IN,
                               HandlerChainProcessor.MEP.REQUEST,
                               false);

        assertEquals("S1m:S1c:", result);
    }

    /*
     * one logical handler
     * processing expected:  Logical and SOAP, reverse order, close
     */
    public void testHandleMessage_onelogical() {

        // reset result
        result = "";

        // use a local list
        ArrayList<Handler> local_list = new ArrayList<Handler>();
        local_list.add(new LogicalHandler1());

        // we want all good responses:
        logicalhandler1_MessageResultDesired = ResultDesired.TRUE;
        logicalhandler1_FaultResultDesired = ResultDesired.TRUE;

        HandlerChainProcessor processor = new HandlerChainProcessor(local_list, Protocol.soap11);
        MessageContext mc1 = new MessageContext();
        mc1.setMEPContext(new MEPContext(mc1));
        processor.processChain(mc1.getMEPContext(),
                               HandlerChainProcessor.Direction.IN,
                               HandlerChainProcessor.MEP.REQUEST,
                               false);

        assertEquals("L1m:L1c:", result);
    }

    /*
     * incoming request (we must be on the server), response expected
     * processing expected:  Logical and SOAP, reverse order, no closing
     */
    public void testHandleMessage_true1() {

        // reset result
        result = "";

        // we want all good responses:
        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.processChain(mc1.getMEPContext(),
                               HandlerChainProcessor.Direction.IN,
                               HandlerChainProcessor.MEP.REQUEST,
                               true);

        assertEquals("S2m:S1m:L1m:L2m:", result);

    }

    /*
     * incoming request (we must be on the server), response NOT expected
     * processing expected:  Logical and SOAP, reverse order, close
     */
    public void testHandleMessage_true2() {

        // reset result
        result = "";

        // we want all good responses:
        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.processChain(mc1.getMEPContext(),
                               HandlerChainProcessor.Direction.IN,
                               HandlerChainProcessor.MEP.REQUEST,
                               false);

        assertEquals("S2m:S1m:L1m:L2m:L2c:L1c:S1c:S2c:", result);

    }

    /*
     * incoming response (we must be on the client), response expected (ignored)
     * processing expected:  Logical and SOAP, reverse order, close
     */
    public void testHandleMessage_true3() {

        // reset result
        result = "";

        // we want all good responses:
        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.processChain(mc1.getMEPContext(),
                               HandlerChainProcessor.Direction.IN,
                               HandlerChainProcessor.MEP.RESPONSE,
                               true);

        /*
         * since this is client inbound response, the original outbound invocation
         * would have been L2m:L1m:S1m:S2m, so the closes would be S2c:S1c:L1c:L2c
         */

        assertEquals("S2m:S1m:L1m:L2m:S2c:S1c:L1c:L2c:", result);

    }

    /*
     * outgoing request (we must be on the client), response expected
     * processing expected:  Logical and SOAP, normal order, no closing
     */
    public void testHandleMessage_true4() {

        // reset result
        result = "";

        // we want all good responses:
        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.processChain(mc1.getMEPContext(),
                               HandlerChainProcessor.Direction.OUT,
                               HandlerChainProcessor.MEP.REQUEST,
                               true);

        assertEquals("L2m:L1m:S1m:S2m:", result);
    }

    /*
     * outgoing request (we must be on the client), response NOT expected
     * processing expected:  Logical and SOAP, normal order, close
     */
    public void testHandleMessage_true5() {

        // reset result
        result = "";

        // we want all good responses:
        soaphandler1_MessageResultDesired = ResultDesired.TRUE;
        soaphandler1_FaultResultDesired = ResultDesired.TRUE;
        soaphandler2_MessageResultDesired = ResultDesired.TRUE;
        soaphandler2_FaultResultDesired = ResultDesired.TRUE;

⌨️ 快捷键说明

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