addressinginhandlertestbase.java

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

JAVA
388
字号
/*
 * 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.handlers.addressing;

import junit.framework.TestCase;
import org.apache.axiom.om.OMAttribute;
import org.apache.axiom.om.OMElement;
import org.apache.axiom.soap.SOAPEnvelope;
import org.apache.axiom.soap.SOAPHeader;
import org.apache.axiom.soap.impl.builder.StAXSOAPModelBuilder;
import org.apache.axis2.AxisFault;
import org.apache.axis2.addressing.AddressingConstants;
import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.addressing.RelatesTo;
import org.apache.axis2.client.Options;
import org.apache.axis2.context.MessageContext;
import org.apache.axis2.handlers.util.TestUtil;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import javax.xml.namespace.QName;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Map;

public abstract class AddressingInHandlerTestBase extends TestCase {
    private Log log = LogFactory.getLog(getClass());
    AddressingInHandler inHandler;
    String addressingNamespace;
    TestUtil testUtil = new TestUtil();
    private String testFileName = "soapmessage.xml";

    String versionDirectory;

    private String action = "http://ws.apache.org/tests/action";
    private String messageID = "uuid:920C5190-0B8F-11D9-8CED-F22EDEEBF7E5";
    String fromAddress;
    String secondRelationshipType;
    private String faultAddress = "http://example.com/fabrikam/fault";
    private String replyAddress = "http://example.com/fabrikam/acct";
    private String toAddress = "http://localhost:8081/axis/services/BankPort";

    /** @param testName  */
    public AddressingInHandlerTestBase(String testName) {
        super(testName);
    }

    protected void basicExtractAddressingInformationFromHeaders(String testMessagePath,
                                                                MessageContext mc)
            throws Exception {
        StAXSOAPModelBuilder omBuilder = testUtil.getOMBuilder(testMessagePath);
        SOAPHeader header = ((SOAPEnvelope)omBuilder.getDocumentElement()).getHeader();
        ArrayList addressingHeaderBlocks = header.getHeaderBlocksWithNSURI(addressingNamespace);
        inHandler.extractAddressingInformation(header, mc, addressingHeaderBlocks,
                                               addressingNamespace);
    }

    protected Options extractAddressingInformationFromHeaders() {
        try {
            String testfile = "valid-messages/" + versionDirectory + "/" + testFileName;

            MessageContext mc = new MessageContext();

            basicExtractAddressingInformationFromHeaders(testfile, mc);

            Options options = mc.getOptions();

            if (options == null) {
                fail("Addressing Information Headers have not been retrieved properly");
            }
            assertEquals("action header is not correct",
                         options.getAction(),
                         action);
            assertActionHasExtensibilityAttribute(mc);
            assertEquals("message id header is not correct",
                         options.getMessageId().trim(),
                         messageID.trim());
            assertMessageIDHasExtensibilityAttribute(mc);

            assertFullFromEPR(options.getFrom());
            assertFullFaultEPR(options.getFaultTo());
            assertFullReplyToEPR(options.getReplyTo());

            assertRelationships(options);

            return options;

        } catch (Exception e) {
            e.printStackTrace();
            log.info(e.getMessage());
            fail(" An Exception has occured " + e.getMessage());
        }

        return null;
    }

    private void testExtractAddressingInformationFromHeadersInvalidCardinality(String headerName) {
        String testfile = "invalid-cardinality-messages/" + versionDirectory +
                "/invalidCardinality" + headerName + "Message.xml";
        try {
            MessageContext mc = new MessageContext();
            try {
                basicExtractAddressingInformationFromHeaders(testfile, mc);
                fail("An AxisFault should have been thrown due to 2 wsa:" + headerName +
                        " headers.");
            } catch (AxisFault af) {
                if (headerName.equals(AddressingConstants.WSA_REPLY_TO)) {
                    assertNull("No ReplyTo should be set on the MessageContext", mc.getReplyTo());
                } else {
                    assertReplyToEPR(mc.getReplyTo());
                }

                if (headerName.equals(AddressingConstants.WSA_FAULT_TO)) {
                    assertNull("No FaultTo should be set on the MessageContext", mc.getFaultTo());
                } else {
                    assertFaultEPR(mc.getFaultTo());
                }

                if (headerName.equals(AddressingConstants.WSA_ACTION)) {
                    assertNull("No Action should be set on the MessageContext", mc.getWSAAction());
                } else {
                    assertEquals("WSAAction property is not correct", mc.getWSAAction(), action);
                }

                if (headerName.equals(AddressingConstants.WSA_MESSAGE_ID)) {
                    assertNull("No MessageID should be set on the MessageContext",
                               mc.getMessageID());
                } else {
                    assertEquals("MessageID property is not correct", mc.getMessageID().trim(),
                                 messageID.trim());
                }

                if (headerName.equals(AddressingConstants.WSA_FROM)) {
                    assertNull("No From should be set on the MessageContext", mc.getFrom());
                } else {
                    assertFromEPR(mc.getFrom());
                }

                if (headerName.equals(AddressingConstants.WSA_TO)) {
                    assertNull("No To should be set on the MessageContext", mc.getTo());
                } else {
                    assertToEPR(mc.getTo());
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.info(e.getMessage());
            fail(" An Exception has occured " + e.getMessage());
        }
    }

    protected Options testMessageWithOmittedHeaders(String testName) throws Exception {
        String testfile =
                "omitted-header-messages/" + versionDirectory + "/" + testName + "Message.xml";

        MessageContext mc = new MessageContext();
        basicExtractAddressingInformationFromHeaders(testfile, mc);

        return mc.getOptions();
    }

    public void testExtractAddressingInformationFromHeadersInvalidCardinalityReplyTo() {
        testExtractAddressingInformationFromHeadersInvalidCardinality(
                AddressingConstants.WSA_REPLY_TO);
    }

    public void testExtractAddressingInformationFromHeadersInvalidCardinalityFaultTo() {
        testExtractAddressingInformationFromHeadersInvalidCardinality(
                AddressingConstants.WSA_FAULT_TO);
    }

    public void testExtractAddressingInformationFromHeadersInvalidCardinalityAction() {
        testExtractAddressingInformationFromHeadersInvalidCardinality(
                AddressingConstants.WSA_ACTION);
    }

    public void testExtractAddressingInformationFromHeadersInvalidCardinalityMessageID() {

⌨️ 快捷键说明

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