0001: /*
0002: * Licensed to the Apache Software Foundation (ASF) under one
0003: * or more contributor license agreements. See the NOTICE file
0004: * distributed with this work for additional information
0005: * regarding copyright ownership. The ASF licenses this file
0006: * to you under the Apache License, Version 2.0 (the
0007: * "License"); you may not use this file except in compliance
0008: * with the License. You may obtain a copy of the License at
0009: *
0010: * http://www.apache.org/licenses/LICENSE-2.0
0011: *
0012: * Unless required by applicable law or agreed to in writing,
0013: * software distributed under the License is distributed on an
0014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
0015: * KIND, either express or implied. See the License for the
0016: * specific language governing permissions and limitations
0017: * under the License.
0018: */
0019: package org.apache.axis2.jaxws.message;
0020:
0021: import java.io.ByteArrayInputStream;
0022: import java.io.ByteArrayOutputStream;
0023: import java.io.StringReader;
0024: import java.io.StringWriter;
0025:
0026: import javax.xml.bind.JAXBContext;
0027: import javax.xml.bind.JAXBIntrospector;
0028: import javax.xml.bind.Marshaller;
0029: import javax.xml.bind.Unmarshaller;
0030: import javax.xml.bind.util.JAXBSource;
0031: import javax.xml.namespace.QName;
0032: import javax.xml.parsers.DocumentBuilder;
0033: import javax.xml.parsers.DocumentBuilderFactory;
0034: import javax.xml.stream.XMLInputFactory;
0035: import javax.xml.stream.XMLOutputFactory;
0036: import javax.xml.stream.XMLStreamReader;
0037: import javax.xml.stream.XMLStreamWriter;
0038: import javax.xml.transform.Source;
0039: import javax.xml.transform.dom.DOMSource;
0040: import javax.xml.transform.sax.SAXSource;
0041: import javax.xml.transform.stream.StreamSource;
0042:
0043: import junit.framework.TestCase;
0044: import org.apache.axiom.om.OMElement;
0045: import org.apache.axiom.om.OMOutputFormat;
0046: import org.apache.axiom.om.impl.builder.StAXOMBuilder;
0047: import org.apache.axis2.jaxws.message.databinding.JAXBBlockContext;
0048: import org.apache.axis2.jaxws.message.databinding.JAXBUtils;
0049: import org.apache.axis2.jaxws.message.factory.BlockFactory;
0050: import org.apache.axis2.jaxws.message.factory.JAXBBlockFactory;
0051: import org.apache.axis2.jaxws.message.factory.MessageFactory;
0052: import org.apache.axis2.jaxws.message.factory.OMBlockFactory;
0053: import org.apache.axis2.jaxws.message.factory.SourceBlockFactory;
0054: import org.apache.axis2.jaxws.message.factory.XMLStringBlockFactory;
0055: import org.apache.axis2.jaxws.message.util.Reader2Writer;
0056: import org.apache.axis2.jaxws.registry.FactoryRegistry;
0057: import org.apache.axis2.jaxws.TestLogger;
0058: import org.w3c.dom.Document;
0059: import org.w3c.dom.Node;
0060: import org.xml.sax.InputSource;
0061: import test.EchoString;
0062: import test.ObjectFactory;
0063:
0064: /**
0065: * BlockTests
0066: * Tests to create and validate blocks.
0067: * These are not client/server tests.
0068: */
0069: public class BlockTests extends TestCase {
0070:
0071: // String test variables
0072: private static final String sampleText = "<pre:a xmlns:pre=\"urn://sample\">"
0073: + "<b>Hello</b>" + "<c>World</c>" + "</pre:a>";
0074: private static final QName sampleQName = new QName("urn://sample",
0075: "a");
0076:
0077: private static XMLInputFactory inputFactory = XMLInputFactory
0078: .newInstance();
0079: private static XMLOutputFactory outputFactory = XMLOutputFactory
0080: .newInstance();
0081:
0082: public BlockTests() {
0083: super ();
0084: }
0085:
0086: public BlockTests(String arg0) {
0087: super (arg0);
0088: }
0089:
0090: /**
0091: * Create a Block representing an XMLString and simulate a
0092: * normal Dispatch<String> flow
0093: * @throws Exception
0094: */
0095: public void testStringOutflow() throws Exception {
0096: // Get the BlockFactory
0097: XMLStringBlockFactory f = (XMLStringBlockFactory) FactoryRegistry
0098: .getFactory(XMLStringBlockFactory.class);
0099:
0100: // Create a Block using the sample string as the content. This simulates
0101: // what occurs on the outbound JAX-WS dispatch<String> client
0102: Block block = f.createFrom(sampleText, null, null);
0103:
0104: // We didn't pass in a qname, so the following should return false
0105: assertTrue(!block.isQNameAvailable());
0106:
0107: // Assuming no handlers are installed, the next thing that will happen
0108: // is a XMLStreamReader will be requested...to go to OM. At this point the
0109: // block should be consumed.
0110: XMLStreamReader reader = block.getXMLStreamReader(true);
0111:
0112: // The block should be consumed
0113: assertTrue(block.isConsumed());
0114:
0115: // To check that the output is correct, get the String contents of the
0116: // reader
0117: Reader2Writer r2w = new Reader2Writer(reader);
0118: String newText = r2w.getAsString();
0119: assertTrue(sampleText.equals(newText));
0120:
0121: }
0122:
0123: /**
0124: * Create a Block representing an XMLString and
0125: * simulate a different Dispatch<String> flow
0126: * @throws Exception
0127: */
0128: public void testStringOutflow2() throws Exception {
0129: // Get the BlockFactory
0130: XMLStringBlockFactory f = (XMLStringBlockFactory) FactoryRegistry
0131: .getFactory(XMLStringBlockFactory.class);
0132:
0133: // Create a Block using the sample string as the content. This simulates
0134: // what occurs on the outbound JAX-WS dispatch<String> client
0135: Block block = f.createFrom(sampleText, null, null);
0136:
0137: // We didn't pass in a qname, so the following should return false
0138: assertTrue(!block.isQNameAvailable());
0139:
0140: // Assume that we need to find the QName (perhaps to identify the operation and
0141: // determine if handlers are installed). This is not very perfomant since
0142: // it causes an underlying parse of the String...but we need to support this.
0143: QName qName = block.getQName();
0144: assertTrue("Expected: " + sampleQName + " but found: " + qName,
0145: sampleQName.equals(qName));
0146:
0147: // Assuming no handlers are installed, the next thing that will happen
0148: // is a XMLStreamReader will be requested...to go to OM. At this point the
0149: // block should be consumed.
0150: XMLStreamReader reader = block.getXMLStreamReader(true);
0151:
0152: // The block should be consumed
0153: assertTrue(block.isConsumed());
0154:
0155: // To check that the output is correct, get the String contents of the
0156: // reader
0157: Reader2Writer r2w = new Reader2Writer(reader);
0158: String newText = r2w.getAsString();
0159: assertTrue(sampleText.equals(newText));
0160:
0161: }
0162:
0163: /**
0164: * Create a Block representing an XMLString and
0165: * simulate a different String parameter flow
0166: * @throws Exception
0167: */
0168: public void testStringOutflow3() throws Exception {
0169: // Get the BlockFactory
0170: XMLStringBlockFactory f = (XMLStringBlockFactory) FactoryRegistry
0171: .getFactory(XMLStringBlockFactory.class);
0172:
0173: // Create a Block using the sample string as the content. This simulates
0174: // what occurs on the outbound JAX-WS String parameter on the client.
0175: // In this case, we know the QName prior to creating the Block...so let's pass it in.
0176: Block block = f.createFrom(sampleText, null, sampleQName);
0177:
0178: // Make sure the QName is correct.
0179: QName qName = block.getQName();
0180: assertTrue(sampleQName.equals(qName));
0181:
0182: // Assuming no handlers are installed, the next thing that will happen
0183: // is a XMLStreamReader will be requested...to go to OM. At this point the
0184: // block should be consumed.
0185: XMLStreamReader reader = block.getXMLStreamReader(true);
0186:
0187: // The block should be consumed
0188: assertTrue(block.isConsumed());
0189:
0190: // To check that the output is correct, get the String contents of the
0191: // reader
0192: Reader2Writer r2w = new Reader2Writer(reader);
0193: String newText = r2w.getAsString();
0194: assertTrue(sampleText.equals(newText));
0195: }
0196:
0197: /**
0198: * Create a Block representing an XMLString and simulate a
0199: * normal Dispatch<String> input flow
0200: * @throws Exception
0201: */
0202: public void testStringInflow() throws Exception {
0203: // Get the BlockFactory
0204: XMLStringBlockFactory f = (XMLStringBlockFactory) FactoryRegistry
0205: .getFactory(XMLStringBlockFactory.class);
0206:
0207: // On inbound, there will already be a XMLStreamReader (probably from OM)
0208: // which represents the message. We will simulate this with inflow.
0209: StringReader sr = new StringReader(sampleText);
0210: XMLStreamReader inflow = inputFactory.createXMLStreamReader(sr);
0211:
0212: // Create a Block from the inflow.
0213: Block block = f.createFrom(inflow, null, null);
0214:
0215: // Assuming no handlers are installed, the next thing that will happen
0216: // is the proxy code will ask for the business object (String).
0217: Object bo = block.getBusinessObject(true);
0218: assertTrue(bo instanceof String);
0219:
0220: // The block should be consumed
0221: assertTrue(block.isConsumed());
0222:
0223: // Check the String for accuracy
0224: assertTrue(sampleText.equals(bo.toString()));
0225:
0226: }
0227:
0228: /**
0229: * Create a Block representing an XMLString and simulate a
0230: * slightly more complicated Dispatch<String> inflow
0231: * @throws Exception
0232: */
0233: public void testStringInflow2() throws Exception {
0234: // Get the BlockFactory
0235: XMLStringBlockFactory f = (XMLStringBlockFactory) FactoryRegistry
0236: .getFactory(XMLStringBlockFactory.class);
0237:
0238: // On inbound, there will already be a XMLStreamReader (probably from OM)
0239: // which represents the message. We will simulate this with inflow.
0240: StringReader sr = new StringReader(sampleText);
0241: XMLStreamReader inflow = inputFactory.createXMLStreamReader(sr);
0242:
0243: // Create a Block from the inflow.
0244: Block block = f.createFrom(inflow, null, null);
0245:
0246: // Let's assume we need to get the QName to find the operation name.
0247: // This will cause an underlying parse
0248: QName qName = block.getQName();
0249: assertTrue(sampleQName.equals(qName));
0250:
0251: // Assuming no handlers are installed, the next thing that will happen
0252: // is the proxy code will ask for the business object (String).
0253: Object bo = block.getBusinessObject(true);
0254: assertTrue(bo instanceof String);
0255:
0256: // The block should be consumed
0257: assertTrue(block.isConsumed());
0258:
0259: // Check the String for accuracy
0260: assertTrue(sampleText.equals(bo.toString()));
0261:
0262: }
0263:
0264: /**
0265: * Create a Block representing an XMLString and simulate a
0266: * slightly more complicated String inflow
0267: * @throws Exception
0268: */
0269: public void testStringInflow3() throws Exception {
0270: // Get the BlockFactory
0271: XMLStringBlockFactory f = (XMLStringBlockFactory) FactoryRegistry
0272: .getFactory(XMLStringBlockFactory.class);
0273:
0274: // On inbound, there will already be a XMLStreamReader (probably from OM)
0275: // which represents the message. We will simulate this with inflow.
0276: StringReader sr = new StringReader(sampleText);
0277: XMLStreamReader inflow = inputFactory.createXMLStreamReader(sr);
0278:
0279: // Create a Block from the inflow. Assume that we know the QName already
0280: Block block = f.createFrom(inflow, null, sampleQName);
0281:
0282: // Let's assume we need to get the QName to find the operation name.
0283: QName qName = block.getQName();
0284: assertTrue(sampleQName.equals(qName));
0285:
0286: // Assuming no handlers are installed, the next thing that will happen
0287: // is the proxy code will ask for the business object (String).
0288: Object bo = block.getBusinessObject(true);
0289: assertTrue(bo instanceof String);
0290:
0291: // The block should be consumed
0292: assertTrue(block.isConsumed());
0293:
0294: // Check the String for accuracy
0295: assertTrue(sampleText.equals(bo.toString()));
0296:
0297: }
0298:
0299: /**
0300: * Create a Block representing an JAXB and simulate a
0301: * normal Dispatch<JAXB> flow
0302: * @throws Exception
0303: */
0304: public void testJAXBOutflow() throws Exception {
0305: // Get the BlockFactory
0306: JAXBBlockFactory f = (JAXBBlockFactory) FactoryRegistry
0307: .getFactory(JAXBBlockFactory.class);
0308:
0309: // Create a jaxb object
0310: ObjectFactory factory = new ObjectFactory();
0311: EchoString jaxb = factory.createEchoString();
0312: jaxb.setInput("Hello World");
0313: JAXBBlockContext context = new JAXBBlockContext(
0314: EchoString.class.getPackage().getName());
0315:
0316: JAXBIntrospector jbi = JAXBUtils.getJAXBIntrospector(context
0317: .getJAXBContext());
0318: QName expectedQName = jbi.getElementName(jaxb);
0319:
0320: // Create a Block using the sample string as the content. This simulates
0321: // what occurs on the outbound JAX-WS dispatch<JAXB> client
0322: Block block = f.createFrom(jaxb, context, null);
0323:
0324: // JAXB objects set the qname from their internal data
0325: assertTrue(block.isQNameAvailable());
0326:
0327: // Assume that we need to find the QName (perhaps to identify the operation and
0328: // determine if handlers are installed). This is not very perfomant since
0329: // it causes an underlying parse of the String...but we need to support this.
0330: QName qName = block.getQName();
0331: assertTrue("Expected: " + expectedQName + " but found: "
0332: + qName, expectedQName.equals(qName));
0333:
0334: // Assuming no handlers are installed, the next thing that will happen
0335: // is a XMLStreamReader will be requested...to go to OM. At this point the
0336: // block should be consumed.
0337: XMLStreamReader reader = block.getXMLStreamReader(true);
0338:
0339: // The block should be consumed
0340: assertTrue(block.isConsumed());
0341:
0342: // To check that the output is correct, get the String contents of the
0343: // reader
0344: Reader2Writer r2w = new Reader2Writer(reader);
0345: String newText = r2w.getAsString();
0346: assertTrue(newText.contains("Hello World"));
0347: assertTrue(newText.contains("echoString"));
0348:
0349: }
0350:
0351: /**
0352: * Create a Block representing an JAXB and simulate a
0353: * slightly more complicated Dispatch<JAXB> flow
0354: * @throws Exception
0355: */
0356: public void testJAXBOutflow2() throws Exception {
0357: // Get the BlockFactory
0358: JAXBBlockFactory f = (JAXBBlockFactory) FactoryRegistry
0359: .getFactory(JAXBBlockFactory.class);
0360:
0361: // Create a jaxb object
0362: ObjectFactory factory = new ObjectFactory();
0363: EchoString jaxb = factory.createEchoString();
0364: jaxb.setInput("Hello World");
0365: JAXBBlockContext context = new JAXBBlockContext(
0366: EchoString.class.getPackage().getName());
0367:
0368: JAXBIntrospector jbi = JAXBUtils.getJAXBIntrospector(context
0369: .getJAXBContext());
0370: QName expectedQName = jbi.getElementName(jaxb);
0371:
0372: // Create a Block using the sample string as the content. This simulates
0373: // what occurs with an outbound JAX-WS JAXB parameter
0374: Block block = f.createFrom(jaxb, context, expectedQName);
0375:
0376: // We did pass in a qname, so the following should return false
0377: assertTrue(block.isQNameAvailable());
0378:
0379: // Assume that we need to find the QName (perhaps to identify the operation and
0380: // determine if handlers are installed). This is not very perfomant since
0381: // it causes an underlying parse of the String...but we need to support this.
0382: QName qName = block.getQName();
0383: assertTrue("Expected: " + expectedQName + " but found: "
0384: + qName, expectedQName.equals(qName));
0385:
0386: // Assuming no handlers are installed, the next thing that will happen
0387: // is a XMLStreamReader will be requested...to go to OM. At this point the
0388: // block should be consumed.
0389: XMLStreamReader reader = block.getXMLStreamReader(true);
0390:
0391: // The block should be consumed
0392: assertTrue(block.isConsumed());
0393:
0394: // To check that the output is correct, get the String contents of the
0395: // reader
0396: Reader2Writer r2w = new Reader2Writer(reader);
0397: String newText = r2w.getAsString();
0398: assertTrue(newText.contains("Hello World"));
0399: assertTrue(newText.contains("echoString"));
0400:
0401: }
0402:
0403: /**
0404: * Create a Block representing an JAXB and simulate a
0405: * normal Dispatch<JAXB> input flow
0406: * @throws Exception
0407: */
0408: public void testJAXBInflow() throws Exception {
0409: // Get the BlockFactory
0410: JAXBBlockFactory f = (JAXBBlockFactory) FactoryRegistry
0411: .getFactory(JAXBBlockFactory.class);
0412:
0413: // Create a jaxb object
0414: ObjectFactory factory = new ObjectFactory();
0415: EchoString jaxb = factory.createEchoString();
0416: jaxb.setInput("Hello World");
0417: JAXBBlockContext context = new JAXBBlockContext(
0418: EchoString.class.getPackage().getName());
0419:
0420: // On inbound, there will already be a XMLStreamReader (probably from OM)
0421: // which represents the message. We will simulate this with inflow.
0422: StringWriter sw = new StringWriter();
0423: XMLStreamWriter writer = outputFactory
0424: .createXMLStreamWriter(sw);
0425: Marshaller marshaller = JAXBUtils.getJAXBMarshaller(context
0426: .getJAXBContext());
0427: marshaller.marshal(jaxb, writer);
0428: JAXBUtils.releaseJAXBMarshaller(context.getJAXBContext(),
0429: marshaller);
0430: writer.flush();
0431: sw.flush();
0432: StringReader sr = new StringReader(sw.toString());
0433: XMLStreamReader inflow = inputFactory.createXMLStreamReader(sr);
0434:
0435: // Create a Block from the inflow.
0436: Block block = f.createFrom(inflow, context, null);
0437:
0438: // Assuming no handlers are installed, the next thing that will happen
0439: // is the proxy code will ask for the business object.
0440: Object bo = block.getBusinessObject(true);
0441: assertTrue(bo instanceof EchoString);
0442:
0443: // The block should be consumed
0444: assertTrue(block.isConsumed());
0445:
0446: // Check for accuracy
0447: assertTrue("Unexpected:" + ((EchoString) bo).getInput(),
0448: ((EchoString) bo).getInput().equals(jaxb.getInput()));
0449:
0450: }
0451:
0452: /**
0453: * Create a Block representing an JAXB and simulate a
0454: * normal Dispatch<JAXB> input flow
0455: * @throws Exception
0456: */
0457: public void testJAXBInflow2() throws Exception {
0458: // Get the BlockFactory
0459: JAXBBlockFactory f = (JAXBBlockFactory) FactoryRegistry
0460: .getFactory(JAXBBlockFactory.class);
0461:
0462: // Create a jaxb object
0463: ObjectFactory factory = new ObjectFactory();
0464: EchoString jaxb = factory.createEchoString();
0465: jaxb.setInput("Hello World");
0466: JAXBBlockContext context = new JAXBBlockContext(
0467: EchoString.class.getPackage().getName());
0468:
0469: JAXBIntrospector jbi = JAXBUtils.getJAXBIntrospector(context
0470: .getJAXBContext());
0471: QName expectedQName = jbi.getElementName(jaxb);
0472:
0473: // On inbound, there will already be a XMLStreamReader (probably from OM)
0474: // which represents the message. We will simulate this with inflow.
0475: StringWriter sw = new StringWriter();
0476: XMLStreamWriter writer = outputFactory
0477: .createXMLStreamWriter(sw);
0478: Marshaller marshaller = JAXBUtils.getJAXBMarshaller(context
0479: .getJAXBContext());
0480: marshaller.marshal(jaxb, writer);
0481: JAXBUtils.releaseJAXBMarshaller(context.getJAXBContext(),
0482: marshaller);
0483: writer.flush();
0484: sw.flush();
0485: StringReader sr = new StringReader(sw.toString());
0486: XMLStreamReader inflow = inputFactory.createXMLStreamReader(sr);
0487:
0488: // Create a Block from the inflow.
0489: Block block = f.createFrom(inflow, context, null);
0490:
0491: // Assume that we need to find the QName (perhaps to identify the operation and
0492: // determine if handlers are installed). This is not very perfomant since
0493: // it causes an underlying parse of the String...but we need to support this.
0494: QName qName = block.getQName();
0495: assertTrue("Expected: " + expectedQName + " but found: "
0496: + qName, expectedQName.equals(qName));
0497:
0498: // Assuming no handlers are installed, the next thing that will happen
0499: // is the proxy code will ask for the business object.
0500: Object bo = block.getBusinessObject(true);
0501: assertTrue(bo instanceof EchoString);
0502:
0503: // The block should be consumed
0504: assertTrue(block.isConsumed());
0505:
0506: // Check for accuracy
0507: assertTrue("Unexpected:" + ((EchoString) bo).getInput(),
0508: ((EchoString) bo).getInput().equals(jaxb.getInput()));
0509:
0510: }
0511:
0512: /**
0513: * Create a Block representing an JAXB and simulate a
0514: * normal Dispatch<JAXB> input flow
0515: * @throws Exception
0516: */
0517: public void testJAXBInflow3() throws Exception {
0518: // Get the BlockFactory
0519: JAXBBlockFactory f = (JAXBBlockFactory) FactoryRegistry
0520: .getFactory(JAXBBlockFactory.class);
0521:
0522: // Create a jaxb object
0523: ObjectFactory factory = new ObjectFactory();
0524: EchoString jaxb = factory.createEchoString();
0525: jaxb.setInput("Hello World");
0526: JAXBBlockContext context = new JAXBBlockContext(
0527: EchoString.class.getPackage().getName());
0528:
0529: JAXBIntrospector jbi = JAXBUtils.getJAXBIntrospector(context
0530: .getJAXBContext());
0531: QName expectedQName = jbi.getElementName(jaxb);
0532:
0533: // On inbound, there will already be a XMLStreamReader (probably from OM)
0534: // which represents the message. We will simulate this with inflow.
0535: StringWriter sw = new StringWriter();
0536: XMLStreamWriter writer = outputFactory
0537: .createXMLStreamWriter(sw);
0538: Marshaller marshaller = JAXBUtils.getJAXBMarshaller(context
0539: .getJAXBContext());
0540: marshaller.marshal(jaxb, writer);
0541: JAXBUtils.releaseJAXBMarshaller(context.getJAXBContext(),
0542: marshaller);
0543: writer.flush();
0544: sw.flush();
0545: StringReader sr = new StringReader(sw.toString());
0546: XMLStreamReader inflow = inputFactory.createXMLStreamReader(sr);
0547:
0548: // Create a Block from the inflow.
0549: Block block = f.createFrom(inflow, context, expectedQName);
0550:
0551: // We passed in a qname, so the following should return false
0552: assertTrue(block.isQNameAvailable());
0553:
0554: // Assume that we need to find the QName (perhaps to identify the operation and
0555: // determine if handlers are installed). This is not very perfomant since
0556: // it causes an underlying parse of the String...but we need to support this.
0557: QName qName = block.getQName();
0558: assertTrue("Expected: " + expectedQName + " but found: "
0559: + qName, expectedQName.equals(qName));
0560:
0561: // Assuming no handlers are installed, the next thing that will happen
0562: // is the proxy code will ask for the business object.
0563: Object bo = block.getBusinessObject(true);
0564: assertTrue(bo instanceof EchoString);
0565:
0566: // The block should be consumed
0567: assertTrue(block.isConsumed());
0568:
0569: // Check for accuracy
0570: assertTrue("Unexpected:" + ((EchoString) bo).getInput(),
0571: ((EchoString) bo).getInput().equals(jaxb.getInput()));
0572:
0573: }
0574:
0575: /**
0576: * Create a Block representing an OM and simulate a
0577: * normal Dispatch<OMElement> flow
0578: * @throws Exception
0579: */
0580: public void testOMOutflow() throws Exception {
0581: // Get the BlockFactory
0582: OMBlockFactory f = (OMBlockFactory) FactoryRegistry
0583: .getFactory(OMBlockFactory.class);
0584:
0585: // Create a Block using the sample string as the content. This simulates
0586: // what occurs on the outbound JAX-WS dispatch<OMElement> client
0587: StringReader sr = new StringReader(sampleText);
0588: XMLStreamReader inputReader = inputFactory
0589: .createXMLStreamReader(sr);
0590: StAXOMBuilder builder = new StAXOMBuilder(inputReader);
0591: OMElement om = builder.getDocumentElement();
0592: Block block = f.createFrom(om, null, null);
0593:
0594: // Assuming no handlers are installed, the next thing that will happen
0595: // is a XMLStreamReader will be requested...to go to OM. At this point the
0596: // block should be consumed.
0597: XMLStreamReader reader = block.getXMLStreamReader(true);
0598:
0599: // The block should be consumed
0600: assertTrue(block.isConsumed());
0601:
0602: // To check that the output is correct, get the String contents of the
0603: // reader
0604: Reader2Writer r2w = new Reader2Writer(reader);
0605: String newText = r2w.getAsString();
0606: assertTrue(sampleText.equals(newText));
0607:
0608: }
0609:
0610: /**
0611: * Create a Block representing an OM and simulate a
0612: * different Dispatch<OMElement> flow
0613: * @throws Exception
0614: */
0615: public void testOMOutflow2() throws Exception {
0616: // Get the BlockFactory
0617: OMBlockFactory f = (OMBlockFactory) FactoryRegistry
0618: .getFactory(OMBlockFactory.class);
0619:
0620: // Create a Block using the sample string as the content. This simulates
0621: // what occurs on the outbound JAX-WS dispatch<OMElement> client
0622: StringReader sr = new StringReader(sampleText);
0623: XMLStreamReader inputReader = inputFactory
0624: .createXMLStreamReader(sr);
0625: StAXOMBuilder builder = new StAXOMBuilder(inputReader);
0626: OMElement om = builder.getDocumentElement();
0627: Block block = f.createFrom(om, null, null);
0628:
0629: // Assume that we need to find the QName (perhaps to identify the operation and
0630: // determine if handlers are installed). This is not very perfomant since
0631: // it causes an underlying parse of the String...but we need to support this.
0632: QName qName = block.getQName();
0633: assertTrue("Expected: " + sampleQName + " but found: " + qName,
0634: sampleQName.equals(qName));
0635:
0636: // Assuming no handlers are installed, the next thing that will happen
0637: // is a XMLStreamReader will be requested...to go to OM. At this point the
0638: // block should be consumed.
0639: XMLStreamReader reader = block.getXMLStreamReader(true);
0640:
0641: // The block should be consumed
0642: assertTrue(block.isConsumed());
0643:
0644: // To check that the output is correct, get the String contents of the
0645: // reader
0646: Reader2Writer r2w = new Reader2Writer(reader);
0647: String newText = r2w.getAsString();
0648: assertTrue(sampleText.equals(newText));
0649:
0650: }
0651:
0652: /**
0653: * Create a Block representing an XMLString and simulate a
0654: * Dispatch<OMElement> inflow
0655: * @throws Exception
0656: */
0657: public void testOMInflow() throws Exception {
0658: // Get the BlockFactory
0659: OMBlockFactory f = (OMBlockFactory) FactoryRegistry
0660: .getFactory(OMBlockFactory.class);
0661:
0662: // On inbound, there will already be a XMLStreamReader (probably from OM)
0663: // which represents the message. We will simulate this with inflow.
0664: StringReader sr = new StringReader(sampleText);
0665: XMLStreamReader inflow = inputFactory.createXMLStreamReader(sr);
0666:
0667: // Create a Block from the inflow.
0668: Block block = f.createFrom(inflow, null, null);
0669:
0670: // Let's assume we need to get the QName to find the operation name.
0671: // This will cause an underlying parse
0672: QName qName = block.getQName();
0673: assertTrue(sampleQName.equals(qName));
0674:
0675: // Assuming no handlers are installed, the next thing that will happen
0676: // is the proxy code will ask for the business object (String).
0677: Object bo = block.getBusinessObject(true);
0678: assertTrue(bo instanceof OMElement);
0679:
0680: // The block should be consumed
0681: assertTrue(block.isConsumed());
0682:
0683: // Check the String for accuracy
0684: assertTrue(sampleText.equals(bo.toString()));
0685:
0686: }
0687:
0688: /**
0689: * Create a Block representing a Source and simulate a
0690: * normal Dispatch<Source> flow
0691: * @throws Exception
0692: */
0693: public void testStreamSourceOutflow() throws Exception {
0694: // Get the BlockFactory
0695: SourceBlockFactory f = (SourceBlockFactory) FactoryRegistry
0696: .getFactory(SourceBlockFactory.class);
0697:
0698: StreamSource ss = new StreamSource(new StringReader(sampleText));
0699:
0700: // Create a Block using the sample string as the content. This simulates
0701: // what occurs on the outbound JAX-WS dispatch<Source> client
0702: Block block = f.createFrom(ss, null, null);
0703:
0704: // We didn't pass in a qname, so the following should return false
0705: assertTrue(!block.isQNameAvailable());
0706:
0707: // Assuming no handlers are installed, the next thing that will happen
0708: // is a XMLStreamReader will be requested...to go to OM. At this point the
0709: // block should be consumed.
0710: XMLStreamReader reader = block.getXMLStreamReader(true);
0711:
0712: // The block should be consumed
0713: assertTrue(block.isConsumed());
0714:
0715: // To check that the output is correct, get the String contents of the
0716: // reader
0717: Reader2Writer r2w = new Reader2Writer(reader);
0718: String newText = r2w.getAsString();
0719: assertTrue(sampleText.equals(newText));
0720:
0721: }
0722:
0723: /**
0724: * Create a Block representing a Source and
0725: * simulate a different Dispatch<Source> flow
0726: * @throws Exception
0727: */
0728: public void testStreamSourceOutflow2() throws Exception {
0729: // Get the BlockFactory
0730: SourceBlockFactory f = (SourceBlockFactory) FactoryRegistry
0731: .getFactory(SourceBlockFactory.class);
0732:
0733: StreamSource ss = new StreamSource(new StringReader(sampleText));
0734:
0735: // Create a Block using the sample string as the content. This simulates
0736: // what occurs on the outbound JAX-WS dispatch<Source> client
0737: Block block = f.createFrom(ss, null, null);
0738:
0739: // We didn't pass in a qname, so the following should return false
0740: assertTrue(!block.isQNameAvailable());
0741:
0742: // Assume that we need to find the QName (perhaps to identify the operation and
0743: // determine if handlers are installed). This is not very perfomant since
0744: // it causes an underlying parse of the String...but we need to support this.
0745: QName qName = block.getQName();
0746: assertTrue("Expected: " + sampleQName + " but found: " + qName,
0747: sampleQName.equals(qName));
0748:
0749: // Assuming no handlers are installed, the next thing that will happen
0750: // is a XMLStreamReader will be requested...to go to OM. At this point the
0751: // block should be consumed.
0752: XMLStreamReader reader = block.getXMLStreamReader(true);
0753:
0754: // The block should be consumed
0755: assertTrue(block.isConsumed());
0756:
0757: // To check that the output is correct, get the String contents of the
0758: // reader
0759: Reader2Writer r2w = new Reader2Writer(reader);
0760: String newText = r2w.getAsString();
0761: assertTrue(sampleText.equals(newText));
0762:
0763: }
0764:
0765: /**
0766: * Create a Block representing a Source and
0767: * simulate a different Source parameter flow
0768: * @throws Exception
0769: */
0770: public void testStreamSourceOutflow3() throws Exception {
0771: // Get the BlockFactory
0772: SourceBlockFactory f = (SourceBlockFactory) FactoryRegistry
0773: .getFactory(SourceBlockFactory.class);
0774:
0775: StreamSource ss = new StreamSource(new StringReader(sampleText));
0776:
0777: // Create a Block using the sample string as the content. This simulates
0778: // what occurs on the outbound JAX-WS String parameter on the client.
0779: // In this case, we know the QName prior to creating the Block...so let's pass it in.
0780: Block block = f.createFrom(ss, null, sampleQName);
0781:
0782: // We passed in a qname, so it should be immediately available
0783: assertTrue(block.isQNameAvailable());
0784:
0785: // Make sure the QName is correct.
0786: QName qName = block.getQName();
0787: assertTrue(sampleQName.equals(qName));
0788:
0789: // Assuming no handlers are installed, the next thing that will happen
0790: // is a XMLStreamReader will be requested...to go to OM. At this point the
0791: // block should be consumed.
0792: XMLStreamReader reader = block.getXMLStreamReader(true);
0793:
0794: // The block should be consumed
0795: assertTrue(block.isConsumed());
0796:
0797: // To check that the output is correct, get the String contents of the
0798: // reader
0799: Reader2Writer r2w = new Reader2Writer(reader);
0800: String newText = r2w.getAsString();
0801: assertTrue(sampleText.equals(newText));
0802: }
0803:
0804: /**
0805: * Create a Block representing an XMLString and simulate a
0806: * normal Dispatch<Source> input flow
0807: * @throws Exception
0808: */
0809: public void testStreamSourceInflow() throws Exception {
0810: // Get the BlockFactory
0811: SourceBlockFactory f = (SourceBlockFactory) FactoryRegistry
0812: .getFactory(SourceBlockFactory.class);
0813:
0814: // On inbound, there will already be a XMLStreamReader (probably from OM)
0815: // which represents the message. We will simulate this with inflow.
0816: StringReader sr = new StringReader(sampleText);
0817: XMLStreamReader inflow = inputFactory.createXMLStreamReader(sr);
0818:
0819: // Create a Block from the inflow.
0820: Block block = f.createFrom(inflow, null, null);
0821:
0822: // Assuming no handlers are installed, the next thing that will happen
0823: // is the proxy code will ask for the business object (String).
0824: Object bo = block.getBusinessObject(true);
0825: assertTrue(bo instanceof Source);
0826:
0827: // The block should be consumed
0828: assertTrue(block.isConsumed());
0829:
0830: // Check the String for accuracy
0831: XMLStreamReader reader = inputFactory
0832: .createXMLStreamReader((Source) bo);
0833: Reader2Writer r2w = new Reader2Writer(reader);
0834: String newText = r2w.getAsString();
0835: assertTrue(sampleText.equals(newText));
0836:
0837: }
0838:
0839: /**
0840: * Create a Block representing an XMLString and simulate a
0841: * slightly more complicated Dispatch<Source> inflow
0842: * @throws Exception
0843: */
0844: public void testStreamSourceInflow2() throws Exception {
0845:
0846: // Get the BlockFactory
0847: SourceBlockFactory f = (SourceBlockFactory) FactoryRegistry
0848: .getFactory(SourceBlockFactory.class);
0849:
0850: // On inbound, there will already be a XMLStreamReader (probably from OM)
0851: // which represents the message. We will simulate this with inflow.
0852: StringReader sr = new StringReader(sampleText);
0853: XMLStreamReader inflow = inputFactory.createXMLStreamReader(sr);
0854:
0855: // Create a Block from the inflow.
0856: Block block = f.createFrom(inflow, null, null);
0857:
0858: // Let's assume we need to get the QName to find the operation name.
0859: // This will cause an underlying parse
0860: QName qName = block.getQName();
0861: assertTrue(sampleQName.equals(qName));
0862:
0863: // Assuming no handlers are installed, the next thing that will happen
0864: // is the proxy code will ask for the business object (String).
0865: Object bo = block.getBusinessObject(true);
0866: assertTrue(bo instanceof Source);
0867:
0868: // The block should be consumed
0869: assertTrue(block.isConsumed());
0870:
0871: // Check the String for accuracy
0872: XMLStreamReader reader = inputFactory
0873: .createXMLStreamReader((Source) bo);
0874: Reader2Writer r2w = new Reader2Writer(reader);
0875: String newText = r2w.getAsString();
0876: assertTrue(sampleText.equals(newText));
0877:
0878: }
0879:
0880: /**
0881: * Create a Block representing an Source and simulate a
0882: * slightly more complicated Source inflow
0883: * @throws Exception
0884: */
0885: public void testStreamSourceInflow3() throws Exception {
0886:
0887: // Get the BlockFactory
0888: SourceBlockFactory f = (SourceBlockFactory) FactoryRegistry
0889: .getFactory(SourceBlockFactory.class);
0890:
0891: // On inbound, there will already be a XMLStreamReader (probably from OM)
0892: // which represents the message. We will simulate this with inflow.
0893: StringReader sr = new StringReader(sampleText);
0894: XMLStreamReader inflow = inputFactory.createXMLStreamReader(sr);
0895:
0896: // Create a Block from the inflow. Assume that we know the QName already
0897: Block block = f.createFrom(inflow, null, sampleQName);
0898:
0899: // We passed in a qname, so the following should return false
0900: assertTrue(block.isQNameAvailable());
0901:
0902: // Let's assume we need to get the QName to find the operation name.
0903: QName qName = block.getQName();
0904: assertTrue(sampleQName.equals(qName));
0905:
0906: // Assuming no handlers are installed, the next thing that will happen
0907: // is the proxy code will ask for the business object (String).
0908: Object bo = block.getBusinessObject(true);
0909: assertTrue(bo instanceof Source);
0910:
0911: // The block should be consumed
0912: assertTrue(block.isConsumed());
0913:
0914: // Check the String for accuracy
0915: XMLStreamReader reader = inputFactory
0916: .createXMLStreamReader((Source) bo);
0917: Reader2Writer r2w = new Reader2Writer(reader);
0918: String newText = r2w.getAsString();
0919: assertTrue(sampleText.equals(newText));
0920:
0921: }
0922:
0923: /*
0924: * Testing JAXBSource, Creating Source Block using JAXBSource and then
0925: * Serializing it.
0926: */
0927: public void testJAXBSourceInFlow1() throws Exception {
0928: // Create a jaxb object
0929: try {
0930: ObjectFactory factory = new ObjectFactory();
0931: EchoString jaxb = factory.createEchoString();
0932: jaxb.setInput("Hello World");
0933: JAXBContext context = JAXBContext.newInstance("test");
0934:
0935: JAXBSource src = new JAXBSource(context.createMarshaller(),
0936: jaxb);
0937: BlockFactory f = (SourceBlockFactory) FactoryRegistry
0938: .getFactory(SourceBlockFactory.class);
0939:
0940: Block block = f.createFrom(src, null, null);
0941:
0942: MessageFactory mf = (MessageFactory) FactoryRegistry
0943: .getFactory(MessageFactory.class);
0944: Message msg = mf.create(Protocol.soap11);
0945: msg.setBodyBlock(block);
0946: org.apache.axiom.soap.SOAPEnvelope env = (org.apache.axiom.soap.SOAPEnvelope) msg
0947: .getAsOMElement();
0948: // Serialize the Envelope using the same mechanism as the
0949: // HTTP client.
0950: ByteArrayOutputStream baos = new ByteArrayOutputStream();
0951: env.serializeAndConsume(baos, new OMOutputFormat());
0952:
0953: // To check that the output is correct, get the String contents of the
0954: // reader
0955: String newText = baos.toString();
0956: TestLogger.logger.debug(newText);
0957: assertTrue(block.isConsumed());
0958: } catch (Exception e) {
0959: e.printStackTrace();
0960: }
0961: }
0962:
0963: public void testJAXBSourceOutflow() throws Exception {
0964:
0965: //Sample text for JAXBSource
0966: String echoSample = "<echoString xmlns=\"http://test\"><input>Hello World</input></echoString>";
0967:
0968: // Get the BlockFactory
0969: SourceBlockFactory f = (SourceBlockFactory) FactoryRegistry
0970: .getFactory(SourceBlockFactory.class);
0971: //Create a JAXBSource
0972:
0973: JAXBContext context = JAXBContext.newInstance("test");
0974:
0975: Unmarshaller u = context.createUnmarshaller();
0976: ByteArrayInputStream inputStream = new ByteArrayInputStream(
0977: echoSample.getBytes());
0978: EchoString jaxb = (EchoString) u.unmarshal(inputStream);
0979: JAXBSource src = new JAXBSource(context.createMarshaller(),
0980: jaxb);
0981:
0982: // Create a Block using the sample string as the content. This simulates
0983: // what occurs on the outbound JAX-WS dispatch<Source> client
0984: Block block = f.createFrom(src, null, null);
0985:
0986: // We didn't pass in a qname, so the following should return false
0987: assertTrue(!block.isQNameAvailable());
0988:
0989: // Assuming no handlers are installed, the next thing that will happen
0990: // is a XMLStreamReader will be requested...to go to OM. At this point the
0991: // block should be consumed.
0992: XMLStreamReader reader = block.getXMLStreamReader(true);
0993:
0994: // The block should be consumed
0995: assertTrue(block.isConsumed());
0996:
0997: // To check that the output is correct, get the String contents of the
0998: // reader
0999: Reader2Writer r2w = new Reader2Writer(reader);
1000: String newText = r2w.getAsString();
1001: assertTrue(echoSample.equals(newText));
1002: }
1003:
1004: /**
1005: * Create a Block representing a DOMSource instance and simulate an
1006: * outbound flow
1007: * @throws Exception
1008: */
1009: public void testDOMSourceOutflow() throws Exception {
1010: // Get the BlockFactory
1011: SourceBlockFactory f = (SourceBlockFactory) FactoryRegistry
1012: .getFactory(SourceBlockFactory.class);
1013:
1014: // Turn the content into a stream
1015: ByteArrayInputStream bais = new ByteArrayInputStream(sampleText
1016: .getBytes());
1017:
1018: // Create a DOM tree from the sample text
1019: DocumentBuilderFactory domFactory = DocumentBuilderFactory
1020: .newInstance();
1021: domFactory.setNamespaceAware(true);
1022: DocumentBuilder domBuilder = domFactory.newDocumentBuilder();
1023: Document domTree = domBuilder.parse(bais);
1024: Node node = domTree.getDocumentElement();
1025: TestLogger.logger.debug(node.toString());
1026:
1027: // Create a DOMSource object from the DOM tree
1028: DOMSource ds = new DOMSource(node);
1029: node = ds.getNode();
1030:
1031: // Create a Block using the sample string as the content. This simulates
1032: // what occurs on the outbound JAX-WS dispatch<Source> client
1033: Block block = f.createFrom(ds, null, null);
1034:
1035: // We didn't pass in a qname, so the following should return false
1036: assertTrue(!block.isQNameAvailable());
1037:
1038: // Assuming no handlers are installed, the next thing that will happen
1039: // is a XMLStreamReader will be requested...to go to OM. At this point the
1040: // block should be consumed.
1041: XMLStreamReader reader = block.getXMLStreamReader(true);
1042:
1043: // The block should be consumed
1044: assertTrue(block.isConsumed());
1045:
1046: // To check that the output is correct, get the String contents of the
1047: // reader
1048: Reader2Writer r2w = new Reader2Writer(reader);
1049: String newText = r2w.getAsString();
1050: assertTrue(sampleText.equals(newText));
1051: }
1052:
1053: /**
1054: * Create a Block representing a SAXSource instance and simulate an
1055: * outbound flow
1056: * @throws Exception
1057: */
1058: public void testSAXSourceOutflow() throws Exception {
1059: // Get the BlockFactory
1060: SourceBlockFactory f = (SourceBlockFactory) FactoryRegistry
1061: .getFactory(SourceBlockFactory.class);
1062:
1063: // Create a SAXSource from the sample text
1064: byte[] bytes = sampleText.getBytes();
1065: ByteArrayInputStream stream = new ByteArrayInputStream(bytes);
1066: InputSource input = new InputSource(stream);
1067: SAXSource ss = new SAXSource(input);
1068:
1069: // Create a Block using the sample string as the content. This simulates
1070: // what occurs on the outbound JAX-WS dispatch<Source> client
1071: Block block = f.createFrom(ss, null, null);
1072:
1073: // We didn't pass in a qname, so the following should return false
1074: assertTrue(!block.isQNameAvailable());
1075:
1076: // Assuming no handlers are installed, the next thing that will happen
1077: // is a XMLStreamReader will be requested...to go to OM. At this point the
1078: // block should be consumed.
1079: XMLStreamReader reader = block.getXMLStreamReader(true);
1080:
1081: // The block should be consumed
1082: assertTrue(block.isConsumed());
1083:
1084: // To check that the output is correct, get the String contents of the
1085: // reader
1086: Reader2Writer r2w = new Reader2Writer(reader);
1087: String newText = r2w.getAsString();
1088: assertTrue(sampleText.equals(newText));
1089: }
1090:
1091: }
|