handlerexecutiontest.java

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

JAVA
428
字号
/*
 * 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 java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import javax.xml.namespace.QName;

import org.apache.axiom.om.OMAbstractFactory;
import org.apache.axiom.om.OMElement;
import org.apache.axiom.om.OMFactory;
import org.apache.axis2.AxisFault;
import org.apache.axis2.client.ServiceClient;
import org.apache.axis2.context.MessageContext;
import org.apache.axis2.description.AxisOperation;
import org.apache.axis2.description.AxisService;
import org.apache.axis2.engine.util.FaultThrowingService;
import org.apache.axis2.handlers.AbstractHandler;
import org.apache.axis2.integration.LocalTestCase;
import org.apache.axis2.integration.TestingUtils;
import org.apache.axis2.phaseresolver.PhaseMetadata;

public class HandlerExecutionTest extends LocalTestCase {
    private static ArrayList testResults;
    private AxisService testService;
    private AxisService testFailingService;
    private QName failingServiceName = new QName("FaultThrowingService");
    private QName failingOperationName = new QName("echoWithFault");
    private static TestHandler middleGlobalInHandler;
    private TestHandler firstOperationInHandler;
    private TestHandler middleOperationInHandler;
    private TestHandler middleOperationOutHandler;

    private void registerOperationLevelHandlers(AxisOperation operation) {
        ArrayList operationSpecificPhases = new ArrayList();
        operationSpecificPhases.add(new Phase(PhaseMetadata.PHASE_POLICY_DETERMINATION));
        operation.setRemainingPhasesInFlow(operationSpecificPhases);
        ArrayList phaseList = operation.getRemainingPhasesInFlow();
        for (int i = 0; i < phaseList.size(); i++) {
            Phase operationSpecificPhase = (Phase)phaseList.get(i);
            if (PhaseMetadata.PHASE_POLICY_DETERMINATION
                    .equals(operationSpecificPhase.getPhaseName())) {
                operationSpecificPhase.addHandler(firstOperationInHandler);
                operationSpecificPhase.addHandler(middleOperationInHandler);
                operationSpecificPhase.addHandler(new TestHandler("In6"));
            }
        }

        operationSpecificPhases = new ArrayList();
        operationSpecificPhases.add(new Phase(PhaseMetadata.PHASE_POLICY_DETERMINATION));
        operation.setPhasesOutFlow(operationSpecificPhases);
        phaseList = operation.getPhasesOutFlow();
        for (int i = 0; i < phaseList.size(); i++) {
            Phase operationSpecificPhase = (Phase)phaseList.get(i);
            if (PhaseMetadata.PHASE_POLICY_DETERMINATION
                    .equals(operationSpecificPhase.getPhaseName())) {
                operationSpecificPhase.addHandler(new TestHandler("Out1"));
                operationSpecificPhase.addHandler(middleOperationOutHandler);
                operationSpecificPhase.addHandler(new TestHandler("Out3"));
            }
        }

    }

    protected void setUp() throws Exception {
    	super.setUp();
        testResults = new ArrayList();

            ArrayList globalInPhases =
                    serverConfig.getAxisConfiguration().getInFlowPhases();
            for (int i = 0; i < globalInPhases.size(); i++) {
                Phase globalInPhase = (Phase)globalInPhases.get(i);
                if (PhaseMetadata.PHASE_PRE_DISPATCH.equals(globalInPhase.getPhaseName())) {
                    globalInPhase.addHandler(new TestHandler("In1"));
                    middleGlobalInHandler = new TestHandler("In2");
                    globalInPhase.addHandler(middleGlobalInHandler);
                    globalInPhase.addHandler(new TestHandler("In3"));
                }
            }
        firstOperationInHandler = new TestHandler("In4");
        middleOperationInHandler = new TestHandler("In5");
        middleOperationOutHandler = new TestHandler("Out2");

        testService = deployClassAsService(Echo.SERVICE_NAME, Echo.class);
        registerOperationLevelHandlers(testService.getOperation(new QName(Echo.ECHO_OM_ELEMENT_OP_NAME)));

        testFailingService = deployClassAsService(failingServiceName.getLocalPart(), FaultThrowingService.class);
        registerOperationLevelHandlers(testFailingService.getOperation(failingOperationName));

    }

    private ServiceClient createClient() throws Exception {
    	return getClient(Echo.SERVICE_NAME, Echo.ECHO_OM_ELEMENT_OP_NAME);
    }

    private void executeClient() throws Exception {
        OMElement payload = TestingUtils.createDummyOMElement();
        OMElement result = createClient().sendReceive(payload);

        TestingUtils.compareWithCreatedOMElement(result);
    }

    public void testSuccessfulInvocation() throws Exception {
        System.out.println("Starting testSuccessfulInvocation");

        OMElement payload = TestingUtils.createDummyOMElement();
        ServiceClient sender = createClient();

        AxisOperation operation =
                sender.getAxisService().getOperation(ServiceClient.ANON_OUT_IN_OP);
        ArrayList operationSpecificPhases = new ArrayList();
        operationSpecificPhases.add(new Phase(PhaseMetadata.PHASE_POLICY_DETERMINATION));
        operation.setRemainingPhasesInFlow(operationSpecificPhases);
        ArrayList phaseList = operation.getRemainingPhasesInFlow();
        for (int i = 0; i < phaseList.size(); i++) {
            Phase operationSpecificPhase = (Phase)phaseList.get(i);
            if (PhaseMetadata.PHASE_POLICY_DETERMINATION
                    .equals(operationSpecificPhase.getPhaseName())) {
                operationSpecificPhase.addHandler(new TestHandler("CIn1"));
                operationSpecificPhase.addHandler(new TestHandler("CIn2"));
                operationSpecificPhase.addHandler(new TestHandler("CIn3"));
            }
        }

        operationSpecificPhases = new ArrayList();
        operationSpecificPhases.add(new Phase(PhaseMetadata.PHASE_POLICY_DETERMINATION));
        operation.setPhasesOutFlow(operationSpecificPhases);
        phaseList = operation.getPhasesOutFlow();
        for (int i = 0; i < phaseList.size(); i++) {
            Phase operationSpecificPhase = (Phase)phaseList.get(i);
            if (PhaseMetadata.PHASE_POLICY_DETERMINATION
                    .equals(operationSpecificPhase.getPhaseName())) {
                operationSpecificPhase.addHandler(new TestHandler("COut1"));
                operationSpecificPhase.addHandler(new TestHandler("COut2"));
                operationSpecificPhase.addHandler(new TestHandler("COut3"));
            }
        }

        OMElement result = sender.sendReceive(payload);

        TestingUtils.compareWithCreatedOMElement(result);

        List expectedExecutionState = Arrays.asList(new String[] { "COut1", "COut2", "COut3", "In1",
                "In2", "In3", "In4", "In5", "In6", "Out1", "Out2", "Out3", "FCOut3", "FCOut2",
                "FCOut1", "FCIn6", "FCIn5", "FCIn4", "FCIn3", "FCIn2", "FCIn1", "FCCOut3",
                "FCCOut2", "FCCOut1", "CIn1", "CIn2", "CIn3", "FCCIn3", "FCCIn2", "FCCIn1" });
        assertEquals(expectedExecutionState, testResults);
    }

    public void testServersideFailureInService() throws Exception {
        System.out.println("Starting testServersideFailureInService");

        OMFactory omFactory = OMAbstractFactory.getOMFactory();
        OMElement payload = omFactory.createOMElement("EchoOMElement", null);
        payload.setText(FaultThrowingService.THROW_FAULT_AS_AXIS_FAULT);

        ServiceClient sender = getClient(failingServiceName.getLocalPart(), failingOperationName.getLocalPart());
        sender.getOptions().setExceptionToBeThrownOnSOAPFault(true);
        
        AxisOperation operation =
                sender.getAxisService().getOperation(ServiceClient.ANON_OUT_IN_OP);
        ArrayList operationSpecificPhases = new ArrayList();
        operationSpecificPhases.add(new Phase(PhaseMetadata.PHASE_POLICY_DETERMINATION));
        operation.setRemainingPhasesInFlow(operationSpecificPhases);
        ArrayList phaseList = operation.getRemainingPhasesInFlow();
        for (int i = 0; i < phaseList.size(); i++) {
            Phase operationSpecificPhase = (Phase)phaseList.get(i);
            if (PhaseMetadata.PHASE_POLICY_DETERMINATION
                    .equals(operationSpecificPhase.getPhaseName())) {
                operationSpecificPhase.addHandler(new TestHandler("CIn1"));
                operationSpecificPhase.addHandler(new TestHandler("CIn2"));
                operationSpecificPhase.addHandler(new TestHandler("CIn3"));
            }
        }

        operationSpecificPhases = new ArrayList();
        operationSpecificPhases.add(new Phase(PhaseMetadata.PHASE_POLICY_DETERMINATION));
        operation.setPhasesOutFlow(operationSpecificPhases);
        phaseList = operation.getPhasesOutFlow();
        for (int i = 0; i < phaseList.size(); i++) {
            Phase operationSpecificPhase = (Phase)phaseList.get(i);
            if (PhaseMetadata.PHASE_POLICY_DETERMINATION
                    .equals(operationSpecificPhase.getPhaseName())) {
                operationSpecificPhase.addHandler(new TestHandler("COut1"));
                operationSpecificPhase.addHandler(new TestHandler("COut2"));
                operationSpecificPhase.addHandler(new TestHandler("COut3"));
            }
        }
        try{
        	sender.sendReceive(payload);
        	fail("Expecting exception to be thrown.");
        }catch(AxisFault af){
        	assertEquals("TestFault", af.getFaultCode().getLocalPart());
        	assertTrue(af.getReason().indexOf("FaultReason") > -1);
        }

⌨️ 快捷键说明

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