blocktests.java
来自「开源的axis2框架的源码。用于开发WEBSERVER」· Java 代码 · 共 1,055 行 · 第 1/3 页
JAVA
1,055 行
// Get the BlockFactory
SourceBlockFactory f = (SourceBlockFactory)
FactoryRegistry.getFactory(SourceBlockFactory.class);
StreamSource ss = new StreamSource(new StringReader(sampleText));
// Create a Block using the sample string as the content. This simulates
// what occurs on the outbound JAX-WS dispatch<Source> client
Block block = f.createFrom(ss, 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 a Source and
* simulate a different Source parameter flow
* @throws Exception
*/
public void testStreamSourceOutflow3() throws Exception {
// Get the BlockFactory
SourceBlockFactory f = (SourceBlockFactory)
FactoryRegistry.getFactory(SourceBlockFactory.class);
StreamSource ss = new StreamSource(new StringReader(sampleText));
// 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(ss, null, sampleQName);
// We passed in a qname, so it should be immediately available
assertTrue(block.isQNameAvailable());
// 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<Source> input flow
* @throws Exception
*/
public void testStreamSourceInflow() throws Exception {
// Get the BlockFactory
SourceBlockFactory f = (SourceBlockFactory)
FactoryRegistry.getFactory(SourceBlockFactory.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 Source);
// The block should be consumed
assertTrue(block.isConsumed());
// Check the String for accuracy
XMLStreamReader reader = inputFactory.createXMLStreamReader((Source) bo);
Reader2Writer r2w = new Reader2Writer(reader);
String newText = r2w.getAsString();
assertTrue(sampleText.equals(newText));
}
/**
* Create a Block representing an XMLString and simulate a
* slightly more complicated Dispatch<Source> inflow
* @throws Exception
*/
public void testStreamSourceInflow2() throws Exception {
// Get the BlockFactory
SourceBlockFactory f = (SourceBlockFactory)
FactoryRegistry.getFactory(SourceBlockFactory.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 Source);
// The block should be consumed
assertTrue(block.isConsumed());
// Check the String for accuracy
XMLStreamReader reader = inputFactory.createXMLStreamReader((Source) bo);
Reader2Writer r2w = new Reader2Writer(reader);
String newText = r2w.getAsString();
assertTrue(sampleText.equals(newText));
}
/**
* Create a Block representing an Source and simulate a
* slightly more complicated Source inflow
* @throws Exception
*/
public void testStreamSourceInflow3() throws Exception {
// Get the BlockFactory
SourceBlockFactory f = (SourceBlockFactory)
FactoryRegistry.getFactory(SourceBlockFactory.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);
// We passed in a qname, so the following should return false
assertTrue(block.isQNameAvailable());
// 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 Source);
// The block should be consumed
assertTrue(block.isConsumed());
// Check the String for accuracy
XMLStreamReader reader = inputFactory.createXMLStreamReader((Source) bo);
Reader2Writer r2w = new Reader2Writer(reader);
String newText = r2w.getAsString();
assertTrue(sampleText.equals(newText));
}
/*
* Testing JAXBSource, Creating Source Block using JAXBSource and then
* Serializing it.
*/
public void testJAXBSourceInFlow1()throws Exception{
// Create a jaxb object
try{
ObjectFactory factory = new ObjectFactory();
EchoString jaxb = factory.createEchoString();
jaxb.setInput("Hello World");
JAXBContext context = JAXBContext.newInstance("test");
JAXBSource src = new JAXBSource(context.createMarshaller(), jaxb);
BlockFactory f = (SourceBlockFactory)
FactoryRegistry.getFactory(SourceBlockFactory.class);
Block block =f.createFrom(src, null, null);
MessageFactory mf = (MessageFactory) FactoryRegistry.getFactory(MessageFactory.class);
Message msg = mf.create(Protocol.soap11);
msg.setBodyBlock(block);
org.apache.axiom.soap.SOAPEnvelope env = (org.apache.axiom.soap.SOAPEnvelope)msg.getAsOMElement();
// Serialize the Envelope using the same mechanism as the
// HTTP client.
ByteArrayOutputStream baos = new ByteArrayOutputStream();
env.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(block.isConsumed());
}catch(Exception e){
e.printStackTrace();
}
}
public void testJAXBSourceOutflow() throws Exception {
//Sample text for JAXBSource
String echoSample = "<echoString xmlns=\"http://test\"><input>Hello World</input></echoString>";
// Get the BlockFactory
SourceBlockFactory f = (SourceBlockFactory)
FactoryRegistry.getFactory(SourceBlockFactory.class);
//Create a JAXBSource
JAXBContext context = JAXBContext.newInstance("test");
Unmarshaller u = context.createUnmarshaller();
ByteArrayInputStream inputStream = new ByteArrayInputStream(echoSample.getBytes());
EchoString jaxb = (EchoString)u.unmarshal(inputStream);
JAXBSource src = new JAXBSource(context.createMarshaller(), jaxb);
// Create a Block using the sample string as the content. This simulates
// what occurs on the outbound JAX-WS dispatch<Source> client
Block block = f.createFrom(src, 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(echoSample.equals(newText));
}
/**
* Create a Block representing a DOMSource instance and simulate an
* outbound flow
* @throws Exception
*/
public void testDOMSourceOutflow() throws Exception {
// Get the BlockFactory
SourceBlockFactory f = (SourceBlockFactory)
FactoryRegistry.getFactory(SourceBlockFactory.class);
// Turn the content into a stream
ByteArrayInputStream bais = new ByteArrayInputStream(sampleText.getBytes());
// Create a DOM tree from the sample text
DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
domFactory.setNamespaceAware(true);
DocumentBuilder domBuilder = domFactory.newDocumentBuilder();
Document domTree = domBuilder.parse(bais);
Node node = domTree.getDocumentElement();
TestLogger.logger.debug(node.toString());
// Create a DOMSource object from the DOM tree
DOMSource ds = new DOMSource(node);
node = ds.getNode();
// Create a Block using the sample string as the content. This simulates
// what occurs on the outbound JAX-WS dispatch<Source> client
Block block = f.createFrom(ds, 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 a SAXSource instance and simulate an
* outbound flow
* @throws Exception
*/
public void testSAXSourceOutflow() throws Exception {
// Get the BlockFactory
SourceBlockFactory f = (SourceBlockFactory)
FactoryRegistry.getFactory(SourceBlockFactory.class);
// Create a SAXSource from the sample text
byte[] bytes = sampleText.getBytes();
ByteArrayInputStream stream = new ByteArrayInputStream(bytes);
InputSource input = new InputSource(stream);
SAXSource ss = new SAXSource(input);
// Create a Block using the sample string as the content. This simulates
// what occurs on the outbound JAX-WS dispatch<Source> client
Block block = f.createFrom(ss, 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));
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?