1 17 package org.apache.servicemix.components.saaj; 18 19 import java.io.ByteArrayOutputStream ; 20 import java.io.InputStream ; 21 22 import javax.jbi.messaging.NormalizedMessage; 23 import javax.xml.namespace.QName ; 24 import javax.xml.soap.MessageFactory ; 25 import javax.xml.soap.MimeHeaders ; 26 import javax.xml.soap.SOAPMessage ; 27 import javax.xml.transform.stream.StreamSource ; 28 29 import junit.framework.TestCase; 30 31 import org.apache.commons.logging.Log; 32 import org.apache.commons.logging.LogFactory; 33 import org.apache.servicemix.jbi.jaxp.SourceTransformer; 34 import org.apache.servicemix.jbi.jaxp.StringSource; 35 import org.apache.servicemix.jbi.messaging.NormalizedMessageImpl; 36 import org.apache.servicemix.jbi.util.DOMUtil; 37 import org.apache.xpath.CachedXPathAPI; 38 import org.w3c.dom.Element ; 39 import org.w3c.dom.Node ; 40 import org.w3c.dom.traversal.NodeIterator; 41 42 public class SaajMarshalerTest extends TestCase { 43 44 private static final transient Log log = LogFactory.getLog(SaajMarshalerTest.class); 45 46 public void testAxisToNMS() throws Exception { 47 MessageFactory messageFactory = new org.apache.axis.soap.MessageFactoryImpl(); 48 testToNMS(messageFactory); 49 } 50 51 public void testAxisCreateSOAPMessage() throws Exception { 52 MessageFactory messageFactory = new org.apache.axis.soap.MessageFactoryImpl(); 53 testCreateSOAPMessage(messageFactory); 54 } 55 56 62 63 69 70 76 77 protected void testToNMS(MessageFactory messageFactory) throws Exception { 78 MimeHeaders headers = new MimeHeaders (); 79 headers.addHeader("Content-Type", "text/xml;"); 80 InputStream is = getClass().getClassLoader().getResourceAsStream("org/apache/servicemix/components/http/soap-response.xml"); 81 SOAPMessage sm = messageFactory.createMessage(headers, is); 82 NormalizedMessage nm = new NormalizedMessageImpl(); 83 new SaajMarshaler().toNMS(nm, sm); 84 85 Node node = new SourceTransformer().toDOMNode(new SourceTransformer().toStreamSource(nm.getContent())); 86 log.info(new SourceTransformer().toString(node)); 87 88 CachedXPathAPI cachedXPathAPI = new CachedXPathAPI(); 89 NodeIterator iterator = cachedXPathAPI.selectNodeIterator(node, "//*[local-name() = 'userId']"); 90 Element root = (Element ) iterator.nextNode(); 91 QName qname = DOMUtil.createQName(root, root.getAttributeNS("http://www.w3.org/2001/XMLSchema-instance", "type")); 92 assertEquals("http://www.w3.org/2001/XMLSchema", qname.getNamespaceURI()); 93 assertEquals("string", qname.getLocalPart()); 94 } 95 96 protected void testCreateSOAPMessage(MessageFactory messageFactory) throws Exception { 97 MimeHeaders headers = new MimeHeaders (); 98 headers.addHeader("Content-Type", "text/xml;"); 99 100 InputStream is = getClass().getClassLoader().getResourceAsStream("org/apache/servicemix/components/saaj/xml-request.xml"); 101 log.info("Raw XML: " + new SourceTransformer().toString(new StreamSource (is))); 102 103 is = getClass().getClassLoader().getResourceAsStream("org/apache/servicemix/components/saaj/xml-request.xml"); 104 NormalizedMessage nm = new NormalizedMessageImpl(); 105 nm.setContent(new StreamSource (is)); 106 107 SaajMarshaler marshaler = new SaajMarshaler(); 108 marshaler.setMessageFactory(messageFactory); 109 SOAPMessage msg = marshaler.createSOAPMessage(nm); 110 111 ByteArrayOutputStream baos = new ByteArrayOutputStream (); 112 msg.writeTo(baos); 113 String soapEnv = new String (baos.toByteArray()); 114 log.info("Prepared SOAP: " + soapEnv); 115 Node node2 = new SourceTransformer().toDOMNode(new StringSource(soapEnv)); 116 117 CachedXPathAPI cachedXPathAPI = new CachedXPathAPI(); 118 NodeIterator iterator = cachedXPathAPI.selectNodeIterator(node2, "//*[local-name() = 'userId']"); 119 Element root = (Element ) iterator.nextNode(); 120 QName qname = DOMUtil.createQName(root, root.getAttributeNS("http://www.w3.org/2001/XMLSchema-instance", "type")); 121 assertEquals("http://www.w3.org/2001/XMLSchema", qname.getNamespaceURI()); 122 assertEquals("string", qname.getLocalPart()); 123 } 124 125 } 126 | Popular Tags |