001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */
019: package org.apache.axis2.jaxws.message;
020:
021: import java.io.ByteArrayOutputStream;
022: import java.io.StringReader;
023:
024: import javax.xml.namespace.QName;
025: import javax.xml.soap.SOAPEnvelope;
026: import javax.xml.soap.SOAPMessage;
027: import javax.xml.stream.XMLInputFactory;
028: import javax.xml.stream.XMLStreamReader;
029:
030: import junit.framework.TestCase;
031: import org.apache.axiom.om.OMElement;
032: import org.apache.axiom.om.OMOutputFormat;
033: import org.apache.axiom.om.impl.llom.OMSourcedElementImpl;
034: import org.apache.axiom.soap.impl.builder.StAXSOAPModelBuilder;
035: import org.apache.axis2.jaxws.message.databinding.JAXBBlockContext;
036: import org.apache.axis2.jaxws.message.factory.JAXBBlockFactory;
037: import org.apache.axis2.jaxws.message.factory.MessageFactory;
038: import org.apache.axis2.jaxws.message.factory.SAAJConverterFactory;
039: import org.apache.axis2.jaxws.message.factory.XMLStringBlockFactory;
040: import org.apache.axis2.jaxws.message.util.SAAJConverter;
041: import org.apache.axis2.jaxws.registry.FactoryRegistry;
042: import org.apache.axis2.jaxws.TestLogger;
043: import test.EchoStringResponse;
044: import test.ObjectFactory;
045:
046: /**
047: * MessageTests
048: * Tests to create and validate Message processing
049: * These are not client/server tests. Instead the tests simulate the processing of a Message during
050: * client/server processing.
051: */
052: public class MessageTests extends TestCase {
053:
054: // String test variables
055: private static final String soap11env = "http://schemas.xmlsoap.org/soap/envelope/";
056: private static final String soap12env = "http://www.w3.org/2003/05/soap-envelope";
057: private static final String sampleEnvelopeHead11 = "<soapenv:Envelope xmlns:soapenv=\""
058: + soap11env + "\">" + "<soapenv:Header /><soapenv:Body>";
059:
060: private static final String sampleEnvelopeHead12 = "<soapenv:Envelope xmlns:soapenv=\""
061: + soap12env + "\">" + "<soapenv:Header /><soapenv:Body>";
062:
063: private static final String sampleEnvelopeTail = "</soapenv:Body></soapenv:Envelope>";
064:
065: private static final String sampleText = "<pre:a xmlns:pre=\"urn://sample\">"
066: + "<b>Hello</b>" + "<c>World</c>" + "</pre:a>";
067:
068: private static final String sampleDouble = "<pre:a xmlns:pre=\"urn://sample\">"
069: + "<b>Hello</b>"
070: + "<c>World</c>"
071: + "</pre:a>"
072: + "<pre:aa xmlns:pre=\"urn://sample\">"
073: + "<b>Hello</b>"
074: + "<c>World</c>" + "</pre:aa>";
075:
076: private static final String sampleEnvelope11 = sampleEnvelopeHead11
077: + sampleText + sampleEnvelopeTail;
078:
079: private static final String sampleEnvelope12 = sampleEnvelopeHead12
080: + sampleText + sampleEnvelopeTail;
081:
082: private static final String sampleJAXBText = "<echoStringResponse xmlns=\"http://test\">"
083: + "<echoStringReturn>sample return value</echoStringReturn>"
084: + "</echoStringResponse>";
085:
086: private static final String sampleJAXBEnvelope11 = sampleEnvelopeHead11
087: + sampleJAXBText + sampleEnvelopeTail;
088:
089: private static final String sampleJAXBEnvelope12 = sampleEnvelopeHead12
090: + sampleJAXBText + sampleEnvelopeTail;
091:
092: private static final String sampleEnvelopeNoHeader11 = "<soapenv:Envelope xmlns:soapenv=\""
093: + soap11env
094: + "\">"
095: + "<soapenv:Body>"
096: + sampleText
097: + "</soapenv:Body></soapenv:Envelope>";
098:
099: private static final String sampleEnvelopeNoHeader12 = "<soapenv:Envelope xmlns:soapenv=\""
100: + soap12env
101: + "\">"
102: + "<soapenv:Body>"
103: + sampleText
104: + "</soapenv:Body></soapenv:Envelope>";
105:
106: private static final QName sampleQName = new QName("urn://sample",
107: "a");
108:
109: private static XMLInputFactory inputFactory = XMLInputFactory
110: .newInstance();
111:
112: public MessageTests() {
113: super ();
114: }
115:
116: public MessageTests(String arg0) {
117: super (arg0);
118: }
119:
120: /**
121: * Create a Block representing an XMLString and simulate a
122: * normal Dispatch<String> flow.
123: * In addition the test makes sure that the XMLString block is not
124: * expanded during this process. (Expanding the block degrades performance).
125: * @throws Exception
126: */
127: public void testStringOutflow() throws Exception {
128:
129: // Create a SOAP 1.1 Message
130: MessageFactory mf = (MessageFactory) FactoryRegistry
131: .getFactory(MessageFactory.class);
132: Message m = mf.create(Protocol.soap11);
133:
134: // Get the BlockFactory
135: XMLStringBlockFactory f = (XMLStringBlockFactory) FactoryRegistry
136: .getFactory(XMLStringBlockFactory.class);
137:
138: // Create a Block using the sample string as the content. This simulates
139: // what occurs on the outbound JAX-WS dispatch<String> client
140: Block block = f.createFrom(sampleText, null, null);
141:
142: // Add the block to the message as normal body content.
143: m.setBodyBlock(block);
144:
145: // Check to see if the message is a fault. The client/server will always call this method.
146: // The Message must respond appropriately without doing a conversion.
147: boolean isFault = m.isFault();
148: assertTrue(!isFault);
149: assertTrue("XMLPart Representation is "
150: + m.getXMLPartContentType(), "SPINE".equals(m
151: .getXMLPartContentType()));
152:
153: // On an outbound flow, we need to convert the Message
154: // to an OMElement, specifically an OM SOAPEnvelope,
155: // so we can set it on the Axis2 MessageContext
156: org.apache.axiom.soap.SOAPEnvelope env = (org.apache.axiom.soap.SOAPEnvelope) m
157: .getAsOMElement();
158:
159: // Check to see if the message is a fault. The client/server will always call this method.
160: // The Message must respond appropriately without doing a conversion.
161: isFault = m.isFault();
162: assertTrue(!isFault);
163: assertTrue("XMLPart Representation is "
164: + m.getXMLPartContentType(), "OM".equals(m
165: .getXMLPartContentType()));
166:
167: // PERFORMANCE CHECK:
168: // The element in the body should be an OMSourcedElement
169: OMElement o = env.getBody().getFirstElement();
170: assertTrue(o instanceof OMSourcedElementImpl);
171: assertTrue(((OMSourcedElementImpl) o).isExpanded() == false);
172:
173: // Serialize the Envelope using the same mechanism as the
174: // HTTP client.
175: ByteArrayOutputStream baos = new ByteArrayOutputStream();
176: env.serializeAndConsume(baos, new OMOutputFormat());
177:
178: String newText = baos.toString();
179: TestLogger.logger.debug(newText);
180: assertTrue(newText.contains(sampleText));
181: assertTrue(newText.contains("soap"));
182: assertTrue(newText.contains("Envelope"));
183: assertTrue(newText.contains("Body"));
184:
185: // The block should be consumed at this point
186: assertTrue(block.isConsumed());
187: }
188:
189: /**
190: * Create a Block representing an XMLString and simulate a
191: * normal Dispatch<String> flow with an application handler.
192: * @throws Exception
193: */
194: public void testStringOutflow2() throws Exception {
195:
196: // Create a SOAP 1.1 Message
197: MessageFactory mf = (MessageFactory) FactoryRegistry
198: .getFactory(MessageFactory.class);
199: Message m = mf.create(Protocol.soap11);
200:
201: // Get the BlockFactory
202: XMLStringBlockFactory f = (XMLStringBlockFactory) FactoryRegistry
203: .getFactory(XMLStringBlockFactory.class);
204:
205: // Create a Block using the sample string as the content. This simulates
206: // what occurs on the outbound JAX-WS dispatch<String> client
207: Block block = f.createFrom(sampleText, null, null);
208:
209: // Add the block to the message as normal body content.
210: m.setBodyBlock(block);
211:
212: // If there is a JAX-WS handler, the Message is converted into a SOAPEnvelope
213: SOAPEnvelope soapEnvelope = m.getAsSOAPEnvelope();
214:
215: // Check to see if the message is a fault. The client/server will always call this method.
216: // The Message must respond appropriately without doing a conversion.
217: boolean isFault = m.isFault();
218: assertTrue(!isFault);
219: assertTrue("XMLPart Representation is "
220: + m.getXMLPartContentType(), "SOAPENVELOPE".equals(m
221: .getXMLPartContentType()));
222:
223: // Normally the handler would not touch the body...but for our scenario, assume that it does.
224: String name = soapEnvelope.getBody().getFirstChild()
225: .getLocalName();
226: assertTrue("a".equals(name));
227:
228: // The block should be consumed at this point
229: assertTrue(block.isConsumed());
230:
231: // After the handler processing the message is obtained as an OM
232: OMElement om = m.getAsOMElement();
233:
234: // Serialize the Envelope using the same mechanism as the
235: // HTTP client.
236: ByteArrayOutputStream baos = new ByteArrayOutputStream();
237: om.serializeAndConsume(baos, new OMOutputFormat());
238:
239: // To check that the output is correct, get the String contents of the
240: // reader
241: String newText = baos.toString();
242: TestLogger.logger.debug(newText);
243: assertTrue(newText.contains(sampleText));
244: assertTrue(newText.contains("soap"));
245: assertTrue(newText.contains("Envelope"));
246: assertTrue(newText.contains("Body"));
247:
248: }
249:
250: /**
251: * Create a Block representing an empty XMLString and simulate a
252: * normal Dispatch<String> flow with an application handler.
253: * @throws Exception
254: *
255: * DISABLED THIS TEST. THE TEST IS NOT VALID BECAUSE AN ATTEMPT WAS
256: * MADE TO ADD A BLOCK THAT IS NOT AN ELEMENT.
257: */
258: public void _testStringOutflowEmptyString() throws Exception {
259:
260: // Create a SOAP 1.1 Message
261: MessageFactory mf = (MessageFactory) FactoryRegistry
262: .getFactory(MessageFactory.class);
263: Message m = mf.create(Protocol.soap11);
264:
265: // Get the BlockFactory
266: XMLStringBlockFactory f = (XMLStringBlockFactory) FactoryRegistry
267: .getFactory(XMLStringBlockFactory.class);
268:
269: // Sample text is whitespace. There is no element
270:
271: String whiteSpaceText = "<!-- Comment -->";
272: // Create a Block using the sample string as the content. This simulates
273: // what occurs on the outbound JAX-WS dispatch<String> client
274: Block block = f.createFrom(whiteSpaceText, null, null);
275:
276: // Add the block to the message as normal body content.
277: m.setBodyBlock(block);
278:
279: // If there is a JAX-WS handler, the Message is converted into a SOAPEnvelope
280: SOAPEnvelope soapEnvelope = m.getAsSOAPEnvelope();
281:
282: // Check to see if the message is a fault. The client/server will always call this method.
283: // The Message must respond appropriately without doing a conversion.
284: boolean isFault = m.isFault();
285: assertTrue(!isFault);
286: assertTrue("XMLPart Representation is "
287: + m.getXMLPartContentType(), "SOAPENVELOPE".equals(m
288: .getXMLPartContentType()));
289:
290: // Normally the handler would not touch the body...but for our scenario, assume that it does.
291: // The whitespace is not preserved, so there should be no first child in the body
292: assertTrue(soapEnvelope.getBody().getFirstChild() == null);
293:
294: // The block should be consumed at this point
295: assertTrue(block.isConsumed());
296:
297: // After the handler processing the message is obtained as an OM
298: OMElement om = m.getAsOMElement();
299:
300: // Serialize the Envelope using the same mechanism as the
301: // HTTP client.
302: ByteArrayOutputStream baos = new ByteArrayOutputStream();
303: om.serializeAndConsume(baos, new OMOutputFormat());
304:
305: // To check that the output is correct, get the String contents of the
306: // reader
307: String newText = baos.toString();
308: TestLogger.logger.debug(newText);
309: assertTrue(newText.contains("soap"));
310: assertTrue(newText.contains("Envelope"));
311: assertTrue(newText.contains("Body"));
312:
313: }
314:
315: /**
316: * Create a Block representing an XMLString with 2 elements and simulate a
317: * normal Dispatch<String> flow with an application handler.
318: * @throws Exception
319: *
320: * @REVIEW This test is disabled because (a) it fails and (b) we don't believe this
321: * is allowed due by WSI.
322: */
323: public void _testStringOutflowDoubleElement() throws Exception {
324:
325: // Create a SOAP 1.1 Message
326: MessageFactory mf = (MessageFactory) FactoryRegistry
327: .getFactory(MessageFactory.class);
328: Message m = mf.create(Protocol.soap11);
329:
330: // Get the BlockFactory
331: XMLStringBlockFactory f = (XMLStringBlockFactory) FactoryRegistry
332: .getFactory(XMLStringBlockFactory.class);
333:
334: // Create a Block using the sample string as the content. This simulates
335: // what occurs on the outbound JAX-WS dispatch<String> client
336: // In this case the sample string contains 2 elements a and aa
337: Block block = f.createFrom(this .sampleDouble, null, null);
338:
339: // Add the block to the message as normal body content.
340: m.setBodyBlock(block);
341:
342: // If there is a JAX-WS handler, the Message is converted into a SOAPEnvelope
343: SOAPEnvelope soapEnvelope = m.getAsSOAPEnvelope();
344:
345: // Check to see if the message is a fault. The client/server will always call this method.
346: // The Message must respond appropriately without doing a conversion.
347: boolean isFault = m.isFault();
348: assertTrue(!isFault);
349: assertTrue("XMLPart Representation is "
350: + m.getXMLPartContentType(), "SOAPENVELOPE".equals(m
351: .getXMLPartContentType()));
352:
353: // Normally the handler would not touch the body...but for our scenario, assume that it does.
354: String name = soapEnvelope.getBody().getFirstChild()
355: .getLocalName();
356: assertTrue("a".equals(name));
357: name = soapEnvelope.getBody().getLastChild().getLocalName();
358: assertTrue("aa".equals(name));
359:
360: // The block should be consumed at this point
361: assertTrue(block.isConsumed());
362:
363: // After the handler processing the message is obtained as an OM
364: OMElement om = m.getAsOMElement();
365:
366: // Serialize the Envelope using the same mechanism as the
367: // HTTP client.
368: ByteArrayOutputStream baos = new ByteArrayOutputStream();
369: om.serializeAndConsume(baos, new OMOutputFormat());
370:
371: // To check that the output is correct, get the String contents of the
372: // reader
373: String newText = baos.toString();
374: TestLogger.logger.debug(newText);
375: assertTrue(newText.contains(sampleText));
376: assertTrue(newText.contains("soap"));
377: assertTrue(newText.contains("Envelope"));
378: assertTrue(newText.contains("Body"));
379:
380: }
381:
382: /**
383: * Create a Block representing an XMLString and simulate a
384: * normal Dispatch<String> input flow
385: * @throws Exception
386: */
387: public void testStringInflow_soap11() throws Exception {
388: _testStringInflow(sampleEnvelope11);
389: }
390:
391: public void testStringInflow_soap12() throws Exception {
392: _testStringInflow(sampleEnvelope12);
393: }
394:
395: public void _testStringInflow(String sampleEnvelope)
396: throws Exception {
397:
398: // On inbound, there will already be an OM
399: // which represents the message. The following code simulates the input
400: // OM
401: StringReader sr = new StringReader(sampleEnvelope);
402: XMLStreamReader inflow = inputFactory.createXMLStreamReader(sr);
403: StAXSOAPModelBuilder builder = new StAXSOAPModelBuilder(inflow,
404: null);
405: OMElement omElement = builder.getSOAPEnvelope();
406:
407: // The JAX-WS layer creates a Message from the OM
408: MessageFactory mf = (MessageFactory) FactoryRegistry
409: .getFactory(MessageFactory.class);
410: Message m = mf.createFrom(omElement, null);
411:
412: // Check to see if the message is a fault. The client/server will always call this method.
413: // The Message must respond appropriately without doing a conversion.
414: boolean isFault = m.isFault();
415: assertTrue(!isFault);
416: assertTrue("XMLPart Representation is "
417: + m.getXMLPartContentType(), "OM".equals(m
418: .getXMLPartContentType()));
419:
420: // Assuming no handlers are installed, the next thing that will happen
421: // is the proxy code will ask for the business object (String).
422: XMLStringBlockFactory blockFactory = (XMLStringBlockFactory) FactoryRegistry
423: .getFactory(XMLStringBlockFactory.class);
424: Block block = m.getBodyBlock(null, blockFactory);
425: Object bo = block.getBusinessObject(true);
426: assertTrue(bo instanceof String);
427:
428: // The block should be consumed
429: assertTrue(block.isConsumed());
430:
431: // Check the String for accuracy
432: assertTrue(sampleText.equals(bo.toString()));
433:
434: }
435:
436: /**
437: * Create a Block representing an XMLString and simulate a
438: * normal Dispatch<String> input flow with a JAX-WS Handler
439: * @throws Exception
440: */
441: public void testStringInflow2_soap11() throws Exception {
442: _testStringInflow2(sampleEnvelope11);
443: }
444:
445: public void testStringInflow2_soap12() throws Exception {
446: // Only run test if an SAAJ 1.3 MessageFactory is available
447: javax.xml.soap.MessageFactory mf = null;
448: try {
449: mf = getSAAJConverter().createMessageFactory(soap12env);
450: } catch (Exception e) {
451: }
452: if (mf != null) {
453: _testStringInflow2(sampleEnvelope12);
454: }
455: }
456:
457: public void _testStringInflow2(String sampleEnvelope)
458: throws Exception {
459:
460: // On inbound, there will already be an OM
461: // which represents the message. The following code simulates the input
462: // OM
463: StringReader sr = new StringReader(sampleEnvelope);
464: XMLStreamReader inflow = inputFactory.createXMLStreamReader(sr);
465: StAXSOAPModelBuilder builder = new StAXSOAPModelBuilder(inflow,
466: null);
467: OMElement omElement = builder.getSOAPEnvelope();
468:
469: // The JAX-WS layer creates a Message from the OM
470: MessageFactory mf = (MessageFactory) FactoryRegistry
471: .getFactory(MessageFactory.class);
472: Message m = mf.createFrom(omElement, null);
473:
474: // Check to see if the message is a fault. The client/server will always call this method.
475: // The Message must respond appropriately without doing a conversion.
476: boolean isFault = m.isFault();
477: assertTrue(!isFault);
478: assertTrue("XMLPart Representation is "
479: + m.getXMLPartContentType(), "OM".equals(m
480: .getXMLPartContentType()));
481:
482: // If there is a JAX-WS handler, the Message is converted into a SOAPEnvelope
483: SOAPEnvelope soapEnvelope = m.getAsSOAPEnvelope();
484:
485: // Check to see if the message is a fault. The client/server will always call this method.
486: // The Message must respond appropriately without doing a conversion.
487: isFault = m.isFault();
488: assertTrue(!isFault);
489: assertTrue("XMLPart Representation is "
490: + m.getXMLPartContentType(), "SOAPENVELOPE".equals(m
491: .getXMLPartContentType()));
492:
493: // Normally the handler would not touch the body...but for our scenario, assume that it does.
494: String name = soapEnvelope.getBody().getFirstChild()
495: .getLocalName();
496: assertTrue("a".equals(name));
497:
498: // The next thing that will happen
499: // is the proxy code will ask for the business object (String).
500: XMLStringBlockFactory blockFactory = (XMLStringBlockFactory) FactoryRegistry
501: .getFactory(XMLStringBlockFactory.class);
502: Block block = m.getBodyBlock(null, blockFactory);
503: Object bo = block.getBusinessObject(true);
504: assertTrue(bo instanceof String);
505:
506: // The block should be consumed
507: assertTrue(block.isConsumed());
508:
509: // Check the String for accuracy
510: assertTrue(sampleText.equals(bo.toString()));
511:
512: }
513:
514: /**
515: * Create a Block representing an XMLString and simulate a
516: * normal Dispatch<String> input flow with a JAX-WS Handler that needs the whole Message
517: * @throws Exception
518: */
519: public void testStringInflow3_soap11() throws Exception {
520: _testStringInflow3(sampleEnvelope11);
521: }
522:
523: public void testStringInflow3_soap12() throws Exception {
524: //Only run test if an SAAJ 1.3 MessageFactory is available
525: javax.xml.soap.MessageFactory mf = null;
526: try {
527: mf = getSAAJConverter().createMessageFactory(soap12env);
528: } catch (Exception e) {
529: }
530: if (mf != null) {
531: _testStringInflow3(sampleEnvelope12);
532: }
533: }
534:
535: public void _testStringInflow3(String sampleEnvelope)
536: throws Exception {
537:
538: // On inbound, there will already be an OM
539: // which represents the message. The following code simulates the input
540: // OM
541: StringReader sr = new StringReader(sampleEnvelope);
542: XMLStreamReader inflow = inputFactory.createXMLStreamReader(sr);
543: StAXSOAPModelBuilder builder = new StAXSOAPModelBuilder(inflow,
544: null);
545: OMElement omElement = builder.getSOAPEnvelope();
546:
547: // The JAX-WS layer creates a Message from the OM
548: MessageFactory mf = (MessageFactory) FactoryRegistry
549: .getFactory(MessageFactory.class);
550: Message m = mf.createFrom(omElement, null);
551:
552: // Check to see if the message is a fault. The client/server will always call this method.
553: // The Message must respond appropriately without doing a conversion.
554: boolean isFault = m.isFault();
555: assertTrue(!isFault);
556: assertTrue("XMLPart Representation is "
557: + m.getXMLPartContentType(), "OM".equals(m
558: .getXMLPartContentType()));
559:
560: // If there is a JAX-WS handler, the Message is converted into a SOAPEnvelope
561: SOAPMessage sm = m.getAsSOAPMessage();
562:
563: // Check to see if the message is a fault. The client/server will always call this method.
564: // The Message must respond appropriately without doing a conversion.
565: isFault = m.isFault();
566: assertTrue(!isFault);
567: assertTrue("XMLPart Representation is "
568: + m.getXMLPartContentType(), "SOAPENVELOPE".equals(m
569: .getXMLPartContentType()));
570:
571: // Normally the handler would not touch the body...but for our scenario, assume that it does.
572: String name = sm.getSOAPBody().getFirstChild().getLocalName();
573: assertTrue("a".equals(name));
574:
575: // The next thing that will happen
576: // is the proxy code will ask for the business object (String).
577: XMLStringBlockFactory blockFactory = (XMLStringBlockFactory) FactoryRegistry
578: .getFactory(XMLStringBlockFactory.class);
579: Block block = m.getBodyBlock(null, blockFactory);
580: Object bo = block.getBusinessObject(true);
581: assertTrue(bo instanceof String);
582:
583: // The block should be consumed
584: assertTrue(block.isConsumed());
585:
586: // Check the String for accuracy
587: assertTrue(sampleText.equals(bo.toString()));
588:
589: }
590:
591: /**
592: * Create a Block representing an XMLString, but this time use one that
593: * doesn't have a <soap:Header> element in it.
594: * @throws Exception
595: */
596: public void testStringInflow4_soap11() throws Exception {
597: _testStringInflow4(sampleEnvelopeNoHeader11);
598: }
599:
600: public void testStringInflow4_soap12() throws Exception {
601: _testStringInflow4(sampleEnvelopeNoHeader12);
602: }
603:
604: public void _testStringInflow4(String sampleEnvelopeNoHeader)
605: throws Exception {
606: // On inbound, there will already be an OM
607: // which represents the message. The following code simulates the input
608: // OM
609: StringReader sr = new StringReader(sampleEnvelopeNoHeader);
610: XMLStreamReader inflow = inputFactory.createXMLStreamReader(sr);
611: StAXSOAPModelBuilder builder = new StAXSOAPModelBuilder(inflow,
612: null);
613: OMElement omElement = builder.getSOAPEnvelope();
614:
615: // The JAX-WS layer creates a Message from the OM
616: MessageFactory mf = (MessageFactory) FactoryRegistry
617: .getFactory(MessageFactory.class);
618: Message m = mf.createFrom(omElement, null);
619:
620: // Check to see if the message is a fault. The client/server will always call this method.
621: // The Message must respond appropriately without doing a conversion.
622: boolean isFault = m.isFault();
623: assertTrue(!isFault);
624: assertTrue("XMLPart Representation is "
625: + m.getXMLPartContentType(), "OM".equals(m
626: .getXMLPartContentType()));
627:
628: // The next thing that will happen
629: // is the proxy code will ask for the business object (String).
630: XMLStringBlockFactory blockFactory = (XMLStringBlockFactory) FactoryRegistry
631: .getFactory(XMLStringBlockFactory.class);
632: Block block = m.getBodyBlock(null, blockFactory);
633: Object bo = block.getBusinessObject(true);
634: assertTrue(bo instanceof String);
635:
636: // The block should be consumed
637: assertTrue(block.isConsumed());
638:
639: // Check the String for accuracy
640: assertTrue(sampleText.equals(bo.toString()));
641:
642: }
643:
644: /**
645: * Create a JAXBBlock containing a JAX-B business object
646: * and simulate a normal Dispatch<Object> output flow
647: * @throws Exception
648: */
649: public void testJAXBOutflow() throws Exception {
650: // Create a SOAP 1.1 Message
651: MessageFactory mf = (MessageFactory) FactoryRegistry
652: .getFactory(MessageFactory.class);
653: Message m = mf.create(Protocol.soap11);
654:
655: // Get the BlockFactory
656: JAXBBlockFactory bf = (JAXBBlockFactory) FactoryRegistry
657: .getFactory(JAXBBlockFactory.class);
658:
659: // Create the JAX-B object
660: ObjectFactory of = new ObjectFactory();
661: EchoStringResponse obj = of.createEchoStringResponse();
662: obj.setEchoStringReturn("sample return value");
663:
664: // Create the JAXBContext
665: JAXBBlockContext context = new JAXBBlockContext(
666: EchoStringResponse.class.getPackage().getName());
667:
668: // Create a JAXBBlock using the Echo object as the content. This simulates
669: // what occurs on the outbound JAX-WS Dispatch<Object> client
670: Block block = bf.createFrom(obj, context, null);
671:
672: // Add the block to the message as normal body content.
673: m.setBodyBlock(block);
674:
675: // Check to see if the message is a fault. The client/server will always call this method.
676: // The Message must respond appropriately without doing a conversion.
677: boolean isFault = m.isFault();
678: assertTrue(!isFault);
679: assertTrue("XMLPart Representation is "
680: + m.getXMLPartContentType(), "SPINE".equals(m
681: .getXMLPartContentType()));
682:
683: // On an outbound flow, we need to convert the Message
684: // to an OMElement, specifically an OM SOAPEnvelope,
685: // so we can set it on the Axis2 MessageContext
686: org.apache.axiom.soap.SOAPEnvelope env = (org.apache.axiom.soap.SOAPEnvelope) m
687: .getAsOMElement();
688:
689: // Check to see if the message is a fault. The client/server will always call this method.
690: // The Message must respond appropriately without doing a conversion.
691: isFault = m.isFault();
692: assertTrue(!isFault);
693: assertTrue("XMLPart Representation is "
694: + m.getXMLPartContentType(), "OM".equals(m
695: .getXMLPartContentType()));
696:
697: // Serialize the Envelope using the same mechanism as the
698: // HTTP client.
699: ByteArrayOutputStream baos = new ByteArrayOutputStream();
700: env.serializeAndConsume(baos, new OMOutputFormat());
701:
702: // To check that the output is correct, get the String contents of the
703: // reader
704: String newText = baos.toString();
705: TestLogger.logger.debug(newText);
706: assertTrue(newText.contains(sampleJAXBText));
707: assertTrue(newText.contains("soap"));
708: assertTrue(newText.contains("Envelope"));
709: assertTrue(newText.contains("Body"));
710: }
711:
712: /**
713: * Same as JAXBOutputflow, but has an additional check
714: * to make sure that the JAXB serialization is deferrred
715: * until the actual serialization of the message.
716: * @throws Exception
717: */
718: public void testJAXBOutflowPerf() throws Exception {
719: // Create a SOAP 1.1 Message
720: MessageFactory mf = (MessageFactory) FactoryRegistry
721: .getFactory(MessageFactory.class);
722: Message m = mf.create(Protocol.soap11);
723:
724: // Get the BlockFactory
725: JAXBBlockFactory bf = (JAXBBlockFactory) FactoryRegistry
726: .getFactory(JAXBBlockFactory.class);
727:
728: // Create the JAX-B object
729: ObjectFactory of = new ObjectFactory();
730: EchoStringResponse obj = of.createEchoStringResponse();
731: obj.setEchoStringReturn("sample return value");
732:
733: // Create the JAXBContext
734: JAXBBlockContext context = new JAXBBlockContext(
735: EchoStringResponse.class.getPackage().getName());
736:
737: // Create a JAXBBlock using the Echo object as the content. This simulates
738: // what occurs on the outbound JAX-WS Dispatch<Object> client
739: Block block = bf.createFrom(obj, context, null);
740:
741: // Add the block to the message as normal body content.
742: m.setBodyBlock(block);
743:
744: // Check to see if the message is a fault. The client/server will always call this method.
745: // The Message must respond appropriately without doing a conversion.
746: boolean isFault = m.isFault();
747: assertTrue(!isFault);
748: assertTrue("XMLPart Representation is "
749: + m.getXMLPartContentType(), "SPINE".equals(m
750: .getXMLPartContentType()));
751:
752: // On an outbound flow, we need to convert the Message
753: // to an OMElement, specifically an OM SOAPEnvelope,
754: // so we can set it on the Axis2 MessageContext
755: org.apache.axiom.soap.SOAPEnvelope env = (org.apache.axiom.soap.SOAPEnvelope) m
756: .getAsOMElement();
757:
758: // Check to see if the message is a fault. The client/server will always call this method.
759: // The Message must respond appropriately without doing a conversion.
760: isFault = m.isFault();
761: assertTrue(!isFault);
762: assertTrue("XMLPart Representation is "
763: + m.getXMLPartContentType(), "OM".equals(m
764: .getXMLPartContentType()));
765:
766: // PERFORMANCE CHECK:
767: // The element in the body should be an OMSourcedElement
768: OMElement o = env.getBody().getFirstElement();
769: assertTrue(o instanceof OMSourcedElementImpl);
770: assertTrue(((OMSourcedElementImpl) o).isExpanded() == false);
771:
772: // Serialize the Envelope using the same mechanism as the
773: // HTTP client.
774: ByteArrayOutputStream baos = new ByteArrayOutputStream();
775: env.serializeAndConsume(baos, new OMOutputFormat());
776:
777: // To check that the output is correct, get the String contents of the
778: // reader
779: String newText = baos.toString();
780: TestLogger.logger.debug(newText);
781: assertTrue(newText.contains(sampleJAXBText));
782: assertTrue(newText.contains("soap"));
783: assertTrue(newText.contains("Envelope"));
784: assertTrue(newText.contains("Body"));
785: }
786:
787: private final int NO_PERSIST = 0;
788: private final int PERSIST = 1;
789: private final int SAVE_AND_PERSIST = 2;
790:
791: public void testJAXBInflow_soap11() throws Exception {
792: _testJAXBInflow(sampleJAXBEnvelope11, NO_PERSIST);
793: }
794:
795: public void testJAXBInflow_soap12() throws Exception {
796: _testJAXBInflow(sampleJAXBEnvelope12, NO_PERSIST);
797: }
798:
799: public void testJAXBInflow_soap11_withPersist() throws Exception {
800: _testJAXBInflow(sampleJAXBEnvelope11, PERSIST);
801: }
802:
803: public void testJAXBInflow_soap12_withPersist() throws Exception {
804: _testJAXBInflow(sampleJAXBEnvelope12, PERSIST);
805: }
806:
807: public void testJAXBInflow_soap11_withSaveAndPersist()
808: throws Exception {
809: _testJAXBInflow(sampleJAXBEnvelope11, SAVE_AND_PERSIST);
810: }
811:
812: public void testJAXBInflow_soap12_withSaveAndPersist()
813: throws Exception {
814: _testJAXBInflow(sampleJAXBEnvelope12, SAVE_AND_PERSIST);
815: }
816:
817: public void _testJAXBInflow(String sampleJAXBEnvelope, int persist)
818: throws Exception {
819: // Create a SOAP OM out of the sample incoming XML. This
820: // simulates what Axis2 will be doing with the inbound message.
821: StringReader sr = new StringReader(sampleJAXBEnvelope);
822: XMLStreamReader inflow = inputFactory.createXMLStreamReader(sr);
823: StAXSOAPModelBuilder builder = new StAXSOAPModelBuilder(inflow,
824: null);
825: OMElement omElement = builder.getSOAPEnvelope();
826:
827: // Create a SOAP 1.1 Message from the sample incoming XML
828: MessageFactory mf = (MessageFactory) FactoryRegistry
829: .getFactory(MessageFactory.class);
830: Message m = mf.createFrom(omElement, null);
831:
832: // Check to see if the message is a fault. The client/server will always call this method.
833: // The Message must respond appropriately without doing a conversion.
834: boolean isFault = m.isFault();
835: assertTrue(!isFault);
836: assertTrue("XMLPart Representation is "
837: + m.getXMLPartContentType(), "OM".equals(m
838: .getXMLPartContentType()));
839:
840: String saveMsgText = "";
841: if (persist == SAVE_AND_PERSIST) {
842: // Simulate saving the message so that it can be fully rebuilt.
843: saveMsgText = m.getAsOMElement().toString();
844: }
845:
846: // Get the BlockFactory
847: JAXBBlockFactory bf = (JAXBBlockFactory) FactoryRegistry
848: .getFactory(JAXBBlockFactory.class);
849:
850: // Create the JAXBContext instance that will be used
851: // to deserialize the JAX-B object content in the message.
852: JAXBBlockContext context = new JAXBBlockContext(
853: EchoStringResponse.class.getPackage().getName());
854:
855: // Get the JAXBBlock that wraps the content
856: Block b = m.getBodyBlock(context, bf);
857:
858: // Check to see if the message is a fault. The client/server will always call this method.
859: // The Message must respond appropriately without doing a conversion.
860: isFault = m.isFault();
861: assertTrue(!isFault);
862: assertTrue("XMLPart Representation is "
863: + m.getXMLPartContentType(), "SPINE".equals(m
864: .getXMLPartContentType()));
865:
866: // Get the business object from the block, which should be a
867: // JAX-B object
868: Object bo = b.getBusinessObject(true);
869: m.setPostPivot();
870:
871: // Simulate restoring the message
872: if (persist == SAVE_AND_PERSIST) {
873: sr = new StringReader(saveMsgText);
874: XMLStreamReader saveMsgReader = inputFactory
875: .createXMLStreamReader(sr);
876: builder = new StAXSOAPModelBuilder(saveMsgReader, null);
877: omElement = builder.getSOAPEnvelope();
878: m = mf.createFrom(omElement, null);
879: }
880:
881: // Check to make sure the right object was returned
882: assertNotNull(bo);
883: assertTrue(bo instanceof EchoStringResponse);
884:
885: // Check to make sure the content of that object is correct
886: EchoStringResponse esr = (EchoStringResponse) bo;
887: assertNotNull(esr.getEchoStringReturn());
888: assertTrue(esr.getEchoStringReturn().equals(
889: "sample return value"));
890:
891: // Simulate outbound
892: if (persist == PERSIST) {
893: String persistMsg = m.getAsOMElement().toString();
894: // We should be able to persist the message, but the persisted message WON'T contain the echoStringResponse contents
895: assertTrue(persistMsg.contains("Body"));
896: assertTrue(persistMsg.contains("echoStringResponse"));
897: assertTrue(!persistMsg.contains("sample return value"));
898:
899: } else if (persist == SAVE_AND_PERSIST) {
900: String persistMsg = m.getAsOMElement().toString();
901: // We should be able to persist the message, and the persisted message WILL contain the echoStringResponse contents
902: assertTrue(persistMsg.contains("Body"));
903: assertTrue(persistMsg.contains("echoStringResponse"));
904: assertTrue(persistMsg.contains("sample return value"));
905: }
906:
907: // After persistance: Sandesha may inspect the body. Make sure this does not cause an error
908: org.apache.axiom.soap.SOAPEnvelope env = (org.apache.axiom.soap.SOAPEnvelope) m
909: .getAsOMElement();
910: QName qName = new QName("uri://fake", "fake");
911: env.getBody().getFirstChildWithName(qName);
912: }
913:
914: SAAJConverter converter = null;
915:
916: private SAAJConverter getSAAJConverter() {
917: if (converter == null) {
918: SAAJConverterFactory factory = (SAAJConverterFactory) FactoryRegistry
919: .getFactory(SAAJConverterFactory.class);
920: converter = factory.getSAAJConverter();
921: }
922: return converter;
923: }
924: }
|