blocktests.java
来自「开源的axis2框架的源码。用于开发WEBSERVER」· Java 代码 · 共 1,055 行 · 第 1/3 页
JAVA
1,055 行
/*
* 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.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.StringReader;
import java.io.StringWriter;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBIntrospector;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import javax.xml.bind.util.JAXBSource;
import javax.xml.namespace.QName;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLOutputFactory;
import javax.xml.stream.XMLStreamReader;
import javax.xml.stream.XMLStreamWriter;
import javax.xml.transform.Source;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.sax.SAXSource;
import javax.xml.transform.stream.StreamSource;
import junit.framework.TestCase;
import org.apache.axiom.om.OMElement;
import org.apache.axiom.om.OMOutputFormat;
import org.apache.axiom.om.impl.builder.StAXOMBuilder;
import org.apache.axis2.jaxws.message.databinding.JAXBBlockContext;
import org.apache.axis2.jaxws.message.databinding.JAXBUtils;
import org.apache.axis2.jaxws.message.factory.BlockFactory;
import org.apache.axis2.jaxws.message.factory.JAXBBlockFactory;
import org.apache.axis2.jaxws.message.factory.MessageFactory;
import org.apache.axis2.jaxws.message.factory.OMBlockFactory;
import org.apache.axis2.jaxws.message.factory.SourceBlockFactory;
import org.apache.axis2.jaxws.message.factory.XMLStringBlockFactory;
import org.apache.axis2.jaxws.message.util.Reader2Writer;
import org.apache.axis2.jaxws.registry.FactoryRegistry;
import org.apache.axis2.jaxws.TestLogger;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.xml.sax.InputSource;
import test.EchoString;
import test.ObjectFactory;
/**
* BlockTests
* Tests to create and validate blocks.
* These are not client/server tests.
*/
public class BlockTests extends TestCase {
// String test variables
private static final String sampleText =
"<pre:a xmlns:pre=\"urn://sample\">" +
"<b>Hello</b>" +
"<c>World</c>" +
"</pre:a>";
private static final QName sampleQName = new QName("urn://sample", "a");
private static XMLInputFactory inputFactory = XMLInputFactory.newInstance();
private static XMLOutputFactory outputFactory = XMLOutputFactory.newInstance();
public BlockTests() {
super();
}
public BlockTests(String arg0) {
super(arg0);
}
/**
* Create a Block representing an XMLString and simulate a
* normal Dispatch<String> flow
* @throws Exception
*/
public void testStringOutflow() throws Exception {
// 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);
// We didn't pass in a qname, so the following should return false
assertTrue(!block.isQNameAvailable());
// Assuming no handlers are installed, the next thing that will happen
// is a XMLStreamReader will be requested...to go to OM. At this point the
// block should be consumed.
XMLStreamReader reader = block.getXMLStreamReader(true);
// The block should be consumed
assertTrue(block.isConsumed());
// To check that the output is correct, get the String contents of the
// reader
Reader2Writer r2w = new Reader2Writer(reader);
String newText = r2w.getAsString();
assertTrue(sampleText.equals(newText));
}
/**
* Create a Block representing an XMLString and
* simulate a different Dispatch<String> flow
* @throws Exception
*/
public void testStringOutflow2() throws Exception {
// 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);
// We didn't pass in a qname, so the following should return false
assertTrue(!block.isQNameAvailable());
// Assume that we need to find the QName (perhaps to identify the operation and
// determine if handlers are installed). This is not very perfomant since
// it causes an underlying parse of the String...but we need to support this.
QName qName = block.getQName();
assertTrue("Expected: " + sampleQName + " but found: " + qName, sampleQName.equals(qName));
// Assuming no handlers are installed, the next thing that will happen
// is a XMLStreamReader will be requested...to go to OM. At this point the
// block should be consumed.
XMLStreamReader reader = block.getXMLStreamReader(true);
// The block should be consumed
assertTrue(block.isConsumed());
// To check that the output is correct, get the String contents of the
// reader
Reader2Writer r2w = new Reader2Writer(reader);
String newText = r2w.getAsString();
assertTrue(sampleText.equals(newText));
}
/**
* Create a Block representing an XMLString and
* simulate a different String parameter flow
* @throws Exception
*/
public void testStringOutflow3() throws Exception {
// 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 String parameter on the client.
// In this case, we know the QName prior to creating the Block...so let's pass it in.
Block block = f.createFrom(sampleText, null, sampleQName);
// Make sure the QName is correct.
QName qName = block.getQName();
assertTrue(sampleQName.equals(qName));
// Assuming no handlers are installed, the next thing that will happen
// is a XMLStreamReader will be requested...to go to OM. At this point the
// block should be consumed.
XMLStreamReader reader = block.getXMLStreamReader(true);
// The block should be consumed
assertTrue(block.isConsumed());
// To check that the output is correct, get the String contents of the
// reader
Reader2Writer r2w = new Reader2Writer(reader);
String newText = r2w.getAsString();
assertTrue(sampleText.equals(newText));
}
/**
* Create a Block representing an XMLString and simulate a
* normal Dispatch<String> input flow
* @throws Exception
*/
public void testStringInflow() throws Exception {
// Get the BlockFactory
XMLStringBlockFactory f = (XMLStringBlockFactory)
FactoryRegistry.getFactory(XMLStringBlockFactory.class);
// On inbound, there will already be a XMLStreamReader (probably from OM)
// which represents the message. We will simulate this with inflow.
StringReader sr = new StringReader(sampleText);
XMLStreamReader inflow = inputFactory.createXMLStreamReader(sr);
// Create a Block from the inflow.
Block block = f.createFrom(inflow, null, null);
// Assuming no handlers are installed, the next thing that will happen
// is the proxy code will ask for the business object (String).
Object bo = block.getBusinessObject(true);
assertTrue(bo instanceof String);
// The block should be consumed
assertTrue(block.isConsumed());
// Check the String for accuracy
assertTrue(sampleText.equals(bo.toString()));
}
/**
* Create a Block representing an XMLString and simulate a
* slightly more complicated Dispatch<String> inflow
* @throws Exception
*/
public void testStringInflow2() throws Exception {
// Get the BlockFactory
XMLStringBlockFactory f = (XMLStringBlockFactory)
FactoryRegistry.getFactory(XMLStringBlockFactory.class);
// On inbound, there will already be a XMLStreamReader (probably from OM)
// which represents the message. We will simulate this with inflow.
StringReader sr = new StringReader(sampleText);
XMLStreamReader inflow = inputFactory.createXMLStreamReader(sr);
// Create a Block from the inflow.
Block block = f.createFrom(inflow, null, null);
// Let's assume we need to get the QName to find the operation name.
// This will cause an underlying parse
QName qName = block.getQName();
assertTrue(sampleQName.equals(qName));
// Assuming no handlers are installed, the next thing that will happen
// is the proxy code will ask for the business object (String).
Object bo = block.getBusinessObject(true);
assertTrue(bo instanceof String);
// The block should be consumed
assertTrue(block.isConsumed());
// Check the String for accuracy
assertTrue(sampleText.equals(bo.toString()));
}
/**
* Create a Block representing an XMLString and simulate a
* slightly more complicated String inflow
* @throws Exception
*/
public void testStringInflow3() throws Exception {
// Get the BlockFactory
XMLStringBlockFactory f = (XMLStringBlockFactory)
FactoryRegistry.getFactory(XMLStringBlockFactory.class);
// On inbound, there will already be a XMLStreamReader (probably from OM)
// which represents the message. We will simulate this with inflow.
StringReader sr = new StringReader(sampleText);
XMLStreamReader inflow = inputFactory.createXMLStreamReader(sr);
// Create a Block from the inflow. Assume that we know the QName already
Block block = f.createFrom(inflow, null, sampleQName);
// Let's assume we need to get the QName to find the operation name.
QName qName = block.getQName();
assertTrue(sampleQName.equals(qName));
// Assuming no handlers are installed, the next thing that will happen
// is the proxy code will ask for the business object (String).
Object bo = block.getBusinessObject(true);
assertTrue(bo instanceof String);
// The block should be consumed
assertTrue(block.isConsumed());
// Check the String for accuracy
assertTrue(sampleText.equals(bo.toString()));
}
/**
* Create a Block representing an JAXB and simulate a
* normal Dispatch<JAXB> flow
* @throws Exception
*/
public void testJAXBOutflow() throws Exception {
// Get the BlockFactory
JAXBBlockFactory f = (JAXBBlockFactory)
FactoryRegistry.getFactory(JAXBBlockFactory.class);
// Create a jaxb object
ObjectFactory factory = new ObjectFactory();
EchoString jaxb = factory.createEchoString();
jaxb.setInput("Hello World");
JAXBBlockContext context = new JAXBBlockContext(EchoString.class.getPackage().getName());
JAXBIntrospector jbi = JAXBUtils.getJAXBIntrospector(context.getJAXBContext());
QName expectedQName = jbi.getElementName(jaxb);
// Create a Block using the sample string as the content. This simulates
// what occurs on the outbound JAX-WS dispatch<JAXB> client
Block block = f.createFrom(jaxb, context, null);
// JAXB objects set the qname from their internal data
assertTrue(block.isQNameAvailable());
// Assume that we need to find the QName (perhaps to identify the operation and
// determine if handlers are installed). This is not very perfomant since
// it causes an underlying parse of the String...but we need to support this.
QName qName = block.getQName();
assertTrue("Expected: " + expectedQName + " but found: " + qName, expectedQName.equals(qName));
// Assuming no handlers are installed, the next thing that will happen
// is a XMLStreamReader will be requested...to go to OM. At this point the
// block should be consumed.
XMLStreamReader reader = block.getXMLStreamReader(true);
// The block should be consumed
assertTrue(block.isConsumed());
// To check that the output is correct, get the String contents of the
// reader
Reader2Writer r2w = new Reader2Writer(reader);
String newText = r2w.getAsString();
assertTrue(newText.contains("Hello World"));
assertTrue(newText.contains("echoString"));
}
/**
* Create a Block representing an JAXB and simulate a
* slightly more complicated Dispatch<JAXB> flow
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?