messagecontextselfmanageddatatest.java

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

JAVA
1,580
字号
/*
 * 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.engine;

import junit.framework.TestCase;
import org.apache.axiom.om.OMAbstractFactory;
import org.apache.axiom.om.util.UUIDGenerator;
import org.apache.axiom.soap.SOAPFactory;
import org.apache.axis2.AxisFault;
import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.context.ConfigurationContext;
import org.apache.axis2.context.MessageContext;
import org.apache.axis2.context.OperationContext;
import org.apache.axis2.context.SelfManagedDataManager;
import org.apache.axis2.context.ServiceContext;
import org.apache.axis2.context.ServiceGroupContext;
import org.apache.axis2.description.AxisOperation;
import org.apache.axis2.description.AxisService;
import org.apache.axis2.description.AxisServiceGroup;
import org.apache.axis2.description.InOutAxisOperation;
import org.apache.axis2.description.TransportInDescription;
import org.apache.axis2.description.TransportOutDescription;
import org.apache.axis2.dispatchers.AddressingBasedDispatcher;
import org.apache.axis2.dispatchers.RequestURIBasedDispatcher;
import org.apache.axis2.dispatchers.SOAPActionBasedDispatcher;
import org.apache.axis2.dispatchers.SOAPMessageBodyBasedDispatcher;
import org.apache.axis2.handlers.AbstractHandler;
import org.apache.axis2.receivers.RawXMLINOnlyMessageReceiver;
import org.apache.axis2.receivers.RawXMLINOutMessageReceiver;
import org.apache.axis2.transport.http.CommonsHTTPTransportSender;
import org.apache.axis2.transport.http.SimpleHTTPServer;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import javax.xml.namespace.QName;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.Iterator;

public class MessageContextSelfManagedDataTest extends TestCase {
    protected static final Log log = LogFactory.getLog(MessageContextSelfManagedDataTest.class);
    
    private File theFile = null;
    boolean savedMessageContext = false;
    boolean restoredMessageContext = false;

    private String serviceGroupName = new String("NullServiceGroup");
    private QName serviceName = new QName("NullService");
    private QName operationName = new QName("DummyOp");

    private ConfigurationContext cfgContext = null;
    private ServiceGroupContext serviceGroupContext = null;
    private ServiceContext serviceContext = null;
    private OperationContext operationContext = null;

    private AxisConfiguration axisConfiguration = null;
    private AxisServiceGroup axisServiceGroup = null;
    private AxisService axisService = null;
    private AxisOperation axisOperation = null;

    private TransportOutDescription transportOut = null;
    private TransportOutDescription transportOut2 = null;
    private TransportOutDescription transportOut3 = null;
    private TransportInDescription transportIn = null;
    private TransportInDescription transportIn2 = null;
    private TransportInDescription transportIn3 = null;

    private MessageContext mc = null;

    private TempHandler01 handler01;
    private TempHandler02 handler02;
    private TempHandler02 subhandler;  // this handler is intended to be a few levels down in executionChain
    private TempHandler03 handler03;
    private TempHandler04 handler04;
    private Phase phase1;
    private Phase phase2;
    private Phase phase3;
    private Phase subPhase;

    // use this to count how many times methods get called for a particular test
    private int invokecallcount = 0;

    // key-value pairs to be used for self managed data

    private String key01 = "key01";
    private String testData01 = "TempHandler01_01";

    private String key02 = "key02";
    private String testData02 = "TempHandler01_02";

    private String key03 = "key03";
    private byte [] testData03 = {0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F, 0x01,
            0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F};

    private String key04 = "key04";
    private long [] testData04 = {0L, 1L, -6601664200673063531L, -7753637088257391858L};

    private String key05 = "key05";
    private int testData05 = 123456;


    public MessageContextSelfManagedDataTest(String arg0) {
        super(arg0);
        initAll();
    }


    protected void initAll() {
        try {
            prepare();
        }
        catch (Exception e) {
            log.debug(
                    "MessageContextSelfManagedDataTest:initAll:  error in setting up object graph [" +
                            e.getClass().getName() + " : " + e.getMessage() + "]");
        }

        if (handler01 == null) {
            handler01 = new TempHandler01(101);
        }

        if (handler02 == null) {
            handler02 = new TempHandler02(102);
        }

        if (handler03 == null) {
            handler03 = new TempHandler03(103);
        }

        if (handler04 == null) {
            handler04 = new TempHandler04(104);
        }

        if (subhandler == null) {
            subhandler = new TempHandler02(1000);
        }

    }


    //
    // prepare the object hierarchy for testing
    //
    private void prepare() throws Exception {
        //-----------------------------------------------------------------

        axisConfiguration = new AxisConfiguration();

        cfgContext = new ConfigurationContext(axisConfiguration);

        cfgContext.getAxisConfiguration().addMessageReceiver(
                "http://www.w3.org/2004/08/wsdl/in-only", new RawXMLINOnlyMessageReceiver());
        cfgContext.getAxisConfiguration().addMessageReceiver(
                "http://www.w3.org/2004/08/wsdl/in-out", new RawXMLINOutMessageReceiver());

        DispatchPhase dispatchPhase = new DispatchPhase();
        dispatchPhase.setName("Dispatch");

        AddressingBasedDispatcher abd = new AddressingBasedDispatcher();
        abd.initDispatcher();

        RequestURIBasedDispatcher rud = new RequestURIBasedDispatcher();
        rud.initDispatcher();

        SOAPActionBasedDispatcher sabd = new SOAPActionBasedDispatcher();
        sabd.initDispatcher();

        SOAPMessageBodyBasedDispatcher smbd = new SOAPMessageBodyBasedDispatcher();
        smbd.initDispatcher();

        dispatchPhase.addHandler(abd);
        dispatchPhase.addHandler(rud);
        dispatchPhase.addHandler(sabd);
        dispatchPhase.addHandler(smbd);

        cfgContext.getAxisConfiguration().getInFlowPhases().add(dispatchPhase);

        //-----------------------------------------------------------------

        axisServiceGroup = new AxisServiceGroup(axisConfiguration);
        axisServiceGroup.setServiceGroupName("ServiceGroupTest");


        axisService = new AxisService(serviceName.getLocalPart());
        axisServiceGroup.addService(axisService);


        axisOperation = new InOutAxisOperation(operationName);
        axisOperation.setMessageReceiver(new MessageReceiver() {
            public void receive(MessageContext messageCtx) {

            }
        });

        axisService.addOperation(axisOperation);
        axisService.mapActionToOperation(operationName.getLocalPart(), axisOperation);


        cfgContext.getAxisConfiguration().addService(axisService);

        //-----------------------------------------------------------------

        serviceGroupContext =
                cfgContext.createServiceGroupContext(axisService.getAxisServiceGroup());
        serviceGroupContext.setId("ServiceGroupContextTest");

        serviceContext = serviceGroupContext.getServiceContext(axisService);

        operationContext = serviceContext.createOperationContext(operationName);

        //-----------------------------------------------------------------

        transportOut = new TransportOutDescription("null");
        transportOut2 = new TransportOutDescription("happy");
        transportOut3 = new TransportOutDescription("golucky");
        transportOut.setSender(new CommonsHTTPTransportSender());
        transportOut2.setSender(new CommonsHTTPTransportSender());
        transportOut3.setSender(new CommonsHTTPTransportSender());
        axisConfiguration.addTransportOut(transportOut3);
        axisConfiguration.addTransportOut(transportOut2);
        axisConfiguration.addTransportOut(transportOut);

        transportIn = new TransportInDescription("null");
        transportIn2 = new TransportInDescription("always");
        transportIn3 = new TransportInDescription("thebest");
        transportIn.setReceiver(new SimpleHTTPServer());
        transportIn2.setReceiver(new SimpleHTTPServer());
        transportIn3.setReceiver(new SimpleHTTPServer());
        axisConfiguration.addTransportIn(transportIn2);
        axisConfiguration.addTransportIn(transportIn);
        axisConfiguration.addTransportIn(transportIn3);

    }


    /*
     * (non-Javadoc)
     * @see junit.framework.TestCase#setUp()
     * 
     * setUp gets called before each test* method.  In this class, a new
     * MessageContext object is created for each test* method.  The test*
     * methods are responsible for adding whichever handler they may need
     * in order to run the desired test.  See the handler class comments
     * for their respective functions.
     */
    protected void setUp() throws Exception {
        //org.apache.log4j.BasicConfigurator.configure();

        invokecallcount = 0;

        mc = cfgContext.createMessageContext();
        mc.setTransportIn(transportIn);
        mc.setTransportOut(transportOut);

        mc.setServerSide(true);
        mc.setProperty(MessageContext.TRANSPORT_OUT, System.out);

        SOAPFactory omFac = OMAbstractFactory.getSOAP11Factory();
        mc.setEnvelope(omFac.getDefaultEnvelope());

        phase1 = new Phase("1");
        phase1.addHandler(new TempHandler02(0));
        phase1.addHandler(new TempHandler02(1));

        phase2 = new Phase("2");
        phase2.addHandler(new TempHandler02(2));
        phase2.addHandler(handler02);
        phase2.addHandler(new TempHandler02(3));

        phase3 = new Phase("3");
        phase3.addHandler(new TempHandler02(4));
        phase3.addHandler(subhandler);
        phase3.addHandler(handler02);  // same instance, second insertion
        phase3.addHandler(new TempHandler02(5));

        /*
         * TODO:  WARNING WARNING WARNING
         * Ideally inserting subPhase here would make the axis2 engine call
         * the invoke of nested subhandler.  It does not do this.  Please see the
         * warning at bottom of testPause06 method.
         */
        subPhase = new Phase("sub");
        subPhase.addHandler(subhandler);
        phase3.addHandler(subPhase);
        phase3.addHandler(new TempHandler02(6));
        phase3.addHandler(new TempHandler02(7));

        axisOperation.getRemainingPhasesInFlow().add(phase1);
        axisOperation.getRemainingPhasesInFlow().add(phase2);
        axisOperation.getRemainingPhasesInFlow().add(phase3);


        mc.setMessageID(UUIDGenerator.getUUID());

        //operationContext.addMessageContext(mc);  gets done via the register
        axisOperation.registerOperationContext(mc, operationContext);
        mc.setOperationContext(operationContext);
        mc.setServiceContext(serviceContext);

        mc.setTo(new EndpointReference("axis2/services/NullService"));

        mc.setWSAAction(operationName.getLocalPart());
        mc.setSoapAction(operationName.getLocalPart());

    }

    //-------------------------------------------------------------------------
    // test cases
    //-------------------------------------------------------------------------


    /**
     * Test case for setting and removing data from a message context
     */
    public void testSelfManagedData01() {
        log.debug(
                "MessageContextSelfManagedDataTest::testSelfManagedData01()=======================================");
        try {
            ArrayList handlers = new ArrayList();
            handlers.add(handler01);
            cfgContext.getAxisConfiguration().setInPhasesUptoAndIncludingPostDispatch(handlers);

            mc.setTo(new EndpointReference("axis2/services/NullService"));
            mc.setWSAAction("DummyOp");

            AxisEngine engine = new AxisEngine(cfgContext);
            engine.receive(mc);

        }
        catch (Exception e) {
            e.printStackTrace();
        }

        assertEquals(testData01, handler01.getTestData01FromMessageContext(mc));
        assertEquals(testData02, handler01.getTestData02FromMessageContext(mc));

        boolean isOk3 = isEquals(testData03, handler01.getTestData03FromMessageContext(mc));
        assertTrue(isOk3);

        boolean isOk4 = isEquals(testData04, handler01.getTestData04FromMessageContext(mc));
        assertTrue(isOk4);

        assertEquals(1, invokecallcount);
    }


    /**
     * Test for setting, saving, restoring self managed data with no exceptions
     */
    public void testPause01_noExceptions() {
        log.debug(
                "MessageContextSelfManagedDataTest::testPause01_noExceptions()=======================================");

        try {
            ArrayList handlers = new ArrayList();
            handlers.add(handler02);
            cfgContext.getAxisConfiguration().setInPhasesUptoAndIncludingPostDispatch(handlers);

            mc.setTo(new EndpointReference("axis2/services/NullService"));
            mc.setWSAAction("DummyOp");

            AxisEngine engine = new AxisEngine(cfgContext);
            engine.receive(mc);
        }
        catch (Exception e) {
            e.printStackTrace();
        }

        // if we didn't get any exceptions during save/restore, these will be true
        assertTrue(savedMessageContext);
        assertTrue(restoredMessageContext);

⌨️ 快捷键说明

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