messagetests.java

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

JAVA
886
字号
/*
 * 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.message;

import java.io.ByteArrayOutputStream;
import java.io.StringReader;

import javax.xml.namespace.QName;
import javax.xml.soap.SOAPEnvelope;
import javax.xml.soap.SOAPMessage;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamReader;

import junit.framework.TestCase;
import org.apache.axiom.om.OMElement;
import org.apache.axiom.om.OMOutputFormat;
import org.apache.axiom.om.impl.llom.OMSourcedElementImpl;
import org.apache.axiom.soap.impl.builder.StAXSOAPModelBuilder;
import org.apache.axis2.jaxws.message.databinding.JAXBBlockContext;
import org.apache.axis2.jaxws.message.factory.JAXBBlockFactory;
import org.apache.axis2.jaxws.message.factory.MessageFactory;
import org.apache.axis2.jaxws.message.factory.SAAJConverterFactory;
import org.apache.axis2.jaxws.message.factory.XMLStringBlockFactory;
import org.apache.axis2.jaxws.message.util.SAAJConverter;
import org.apache.axis2.jaxws.registry.FactoryRegistry;
import org.apache.axis2.jaxws.TestLogger;
import test.EchoStringResponse;
import test.ObjectFactory;

/**
 * MessageTests
 * Tests to create and validate Message processing
 * These are not client/server tests.  Instead the tests simulate the processing of a Message during
 * client/server processing.
 */
public class MessageTests extends TestCase {

	// String test variables
	private static final String soap11env = "http://schemas.xmlsoap.org/soap/envelope/";
	private static final String soap12env = "http://www.w3.org/2003/05/soap-envelope";
    private static final String sampleEnvelopeHead11 =
        "<soapenv:Envelope xmlns:soapenv=\"" + soap11env + "\">" +
        "<soapenv:Header /><soapenv:Body>";
    
    private static final String sampleEnvelopeHead12 =
        "<soapenv:Envelope xmlns:soapenv=\"" + soap12env + "\">" +
        "<soapenv:Header /><soapenv:Body>";
    
    private static final String sampleEnvelopeTail = 
        "</soapenv:Body></soapenv:Envelope>";
    
    private static final String sampleText =
		"<pre:a xmlns:pre=\"urn://sample\">" +
		"<b>Hello</b>" +
		"<c>World</c>" +
		"</pre:a>";
    
    private static final String sampleDouble =
        "<pre:a xmlns:pre=\"urn://sample\">" +
        "<b>Hello</b>" +
        "<c>World</c>" +
        "</pre:a>" +
        "<pre:aa xmlns:pre=\"urn://sample\">" +
        "<b>Hello</b>" +
        "<c>World</c>" +
        "</pre:aa>";
	
    private static final String sampleEnvelope11 = 
        sampleEnvelopeHead11 +
        sampleText +
        sampleEnvelopeTail;
    
    private static final String sampleEnvelope12 = 
        sampleEnvelopeHead12 +
        sampleText +
        sampleEnvelopeTail;
        
    private static final String sampleJAXBText = 
        "<echoStringResponse xmlns=\"http://test\">" +
        "<echoStringReturn>sample return value</echoStringReturn>" + 
        "</echoStringResponse>";
    
    private static final String sampleJAXBEnvelope11 = 
        sampleEnvelopeHead11 + 
        sampleJAXBText + 
        sampleEnvelopeTail;
    
    private static final String sampleJAXBEnvelope12 = 
        sampleEnvelopeHead12 + 
        sampleJAXBText + 
        sampleEnvelopeTail;

    private static final String sampleEnvelopeNoHeader11 =
        "<soapenv:Envelope xmlns:soapenv=\""+ soap11env +"\">" +
        "<soapenv:Body>" + 
        sampleText + 
        "</soapenv:Body></soapenv:Envelope>";
    
    private static final String sampleEnvelopeNoHeader12 =
        "<soapenv:Envelope xmlns:soapenv=\""+ soap12env +"\">" +
        "<soapenv:Body>" + 
        sampleText + 
        "</soapenv:Body></soapenv:Envelope>";
    
    
    
	private static final QName sampleQName = new QName("urn://sample", "a");
	
	private static XMLInputFactory inputFactory = XMLInputFactory.newInstance();
	
	public MessageTests() {
		super();
	}

	public MessageTests(String arg0) {
		super(arg0);
	}
	
	/**
	 * Create a Block representing an XMLString and simulate a 
	 * normal Dispatch<String> flow.
     * In addition the test makes sure that the XMLString block is not
     * expanded during this process.  (Expanding the block degrades performance).
	 * @throws Exception
	 */
	public void testStringOutflow() throws Exception {
		
		// Create a SOAP 1.1 Message
		MessageFactory mf = (MessageFactory)
			FactoryRegistry.getFactory(MessageFactory.class);
		Message m = mf.create(Protocol.soap11);
		
		// Get the BlockFactory
		XMLStringBlockFactory f = (XMLStringBlockFactory)
			FactoryRegistry.getFactory(XMLStringBlockFactory.class);
		
		// Create a Block using the sample string as the content.  This simulates
		// what occurs on the outbound JAX-WS dispatch<String> client
		Block block = f.createFrom(sampleText, null, null);
		
		// Add the block to the message as normal body content.
		m.setBodyBlock(block);
		
		// Check to see if the message is a fault.  The client/server will always call this method.
        // The Message must respond appropriately without doing a conversion.
        boolean isFault = m.isFault();
        assertTrue(!isFault);
        assertTrue("XMLPart Representation is " + m.getXMLPartContentType(),
                    "SPINE".equals(m.getXMLPartContentType()));
        
		// On an outbound flow, we need to convert the Message 
        // to an OMElement, specifically an OM SOAPEnvelope, 
        // so we can set it on the Axis2 MessageContext
        org.apache.axiom.soap.SOAPEnvelope env = 
            (org.apache.axiom.soap.SOAPEnvelope) m.getAsOMElement();
        
        // Check to see if the message is a fault.  The client/server will always call this method.
        // The Message must respond appropriately without doing a conversion.
        isFault = m.isFault();
        assertTrue(!isFault);
        assertTrue("XMLPart Representation is " + m.getXMLPartContentType(),
                    "OM".equals(m.getXMLPartContentType()));
        
        // PERFORMANCE CHECK:
        // The element in the body should be an OMSourcedElement
        OMElement o = env.getBody().getFirstElement();
        assertTrue(o instanceof OMSourcedElementImpl);
        assertTrue(((OMSourcedElementImpl)o).isExpanded() == false);
        
        // Serialize the Envelope using the same mechanism as the 
        // HTTP client.
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        env.serializeAndConsume(baos, new OMOutputFormat());
        
		String newText = baos.toString();
        TestLogger.logger.debug(newText);
		assertTrue(newText.contains(sampleText));
		assertTrue(newText.contains("soap"));
		assertTrue(newText.contains("Envelope"));
		assertTrue(newText.contains("Body"));
		
		// The block should be consumed at this point
		assertTrue(block.isConsumed());
	}

	/**
	 * Create a Block representing an XMLString and simulate a 
	 * normal Dispatch<String> flow with an application handler.
	 * @throws Exception
	 */
	public void testStringOutflow2() throws Exception {
		
		// Create a SOAP 1.1 Message
		MessageFactory mf = (MessageFactory)
			FactoryRegistry.getFactory(MessageFactory.class);
		Message m = mf.create(Protocol.soap11);
		
		// Get the BlockFactory
		XMLStringBlockFactory f = (XMLStringBlockFactory)
			FactoryRegistry.getFactory(XMLStringBlockFactory.class);
		
		// Create a Block using the sample string as the content.  This simulates
		// what occurs on the outbound JAX-WS dispatch<String> client
		Block block = f.createFrom(sampleText, null, null);
		
		// Add the block to the message as normal body content.
		m.setBodyBlock(block);
		
		// If there is a JAX-WS handler, the Message is converted into a SOAPEnvelope
		SOAPEnvelope soapEnvelope = m.getAsSOAPEnvelope();
        
		// Check to see if the message is a fault.  The client/server will always call this method.
        // The Message must respond appropriately without doing a conversion.
        boolean isFault = m.isFault();
        assertTrue(!isFault);
        assertTrue("XMLPart Representation is " + m.getXMLPartContentType(),
                    "SOAPENVELOPE".equals(m.getXMLPartContentType()));
		
		// Normally the handler would not touch the body...but for our scenario, assume that it does.
		String name = soapEnvelope.getBody().getFirstChild().getLocalName();
		assertTrue("a".equals(name));
		
		// The block should be consumed at this point
		assertTrue(block.isConsumed());
		
		// After the handler processing the message is obtained as an OM
		OMElement om = m.getAsOMElement();
		        
        // Serialize the Envelope using the same mechanism as the 
        // HTTP client.
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        om.serializeAndConsume(baos, new OMOutputFormat());
        
		// To check that the output is correct, get the String contents of the 
		// reader
        String newText = baos.toString();
        TestLogger.logger.debug(newText);
		assertTrue(newText.contains(sampleText));
		assertTrue(newText.contains("soap"));
		assertTrue(newText.contains("Envelope"));
		assertTrue(newText.contains("Body"));
		
	}
    
    /**
     * Create a Block representing an empty XMLString and simulate a 
     * normal Dispatch<String> flow with an application handler.
     * @throws Exception
     * 
     * DISABLED THIS TEST. THE TEST IS NOT VALID BECAUSE AN ATTEMPT WAS 
     * MADE TO ADD A BLOCK THAT IS NOT AN ELEMENT.
     */
    public void _testStringOutflowEmptyString() throws Exception {
        
        // Create a SOAP 1.1 Message
        MessageFactory mf = (MessageFactory)
            FactoryRegistry.getFactory(MessageFactory.class);
        Message m = mf.create(Protocol.soap11);
        
        // Get the BlockFactory
        XMLStringBlockFactory f = (XMLStringBlockFactory)
            FactoryRegistry.getFactory(XMLStringBlockFactory.class);
        
        // Sample text is whitespace.  There is no element
        
        String whiteSpaceText = "<!-- Comment -->";
        // Create a Block using the sample string as the content.  This simulates
        // what occurs on the outbound JAX-WS dispatch<String> client
        Block block = f.createFrom(whiteSpaceText, null, null);
        
        // Add the block to the message as normal body content.
        m.setBodyBlock(block);
        
        // If there is a JAX-WS handler, the Message is converted into a SOAPEnvelope
        SOAPEnvelope soapEnvelope = m.getAsSOAPEnvelope();
        
        // Check to see if the message is a fault.  The client/server will always call this method.
        // The Message must respond appropriately without doing a conversion.
        boolean isFault = m.isFault();
        assertTrue(!isFault);

⌨️ 快捷键说明

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