KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > servicemix > components > saaj > SaajMarshalerTest


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17 package org.apache.servicemix.components.saaj;
18
19 import java.io.ByteArrayOutputStream JavaDoc;
20 import java.io.InputStream JavaDoc;
21
22 import javax.jbi.messaging.NormalizedMessage;
23 import javax.xml.namespace.QName JavaDoc;
24 import javax.xml.soap.MessageFactory JavaDoc;
25 import javax.xml.soap.MimeHeaders JavaDoc;
26 import javax.xml.soap.SOAPMessage JavaDoc;
27 import javax.xml.transform.stream.StreamSource JavaDoc;
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 JavaDoc;
39 import org.w3c.dom.Node JavaDoc;
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 JavaDoc {
47         MessageFactory JavaDoc messageFactory = new org.apache.axis.soap.MessageFactoryImpl();
48         testToNMS(messageFactory);
49     }
50     
51     public void testAxisCreateSOAPMessage() throws Exception JavaDoc {
52         MessageFactory JavaDoc messageFactory = new org.apache.axis.soap.MessageFactoryImpl();
53         testCreateSOAPMessage(messageFactory);
54     }
55     
56     /*
57     public void testAxis2() throws Exception {
58         MessageFactory messageFactory = new org.apache.axis2.saaj.MessageFactoryImpl();
59         testFactory(messageFactory);
60     }
61     */

62     
63     /*
64     public void testSunToNMS() throws Exception {
65         MessageFactory messageFactory = new com.sun.xml.messaging.saaj.soap.MessageFactoryImpl();
66         testToNMS(messageFactory);
67     }
68     */

69     
70     /*
71     public void testSunCreateSOAPMessage() throws Exception {
72         MessageFactory messageFactory = new com.sun.xml.messaging.saaj.soap.MessageFactoryImpl();
73         testCreateSOAPMessage(messageFactory);
74     }
75     */

76     
77     protected void testToNMS(MessageFactory JavaDoc messageFactory) throws Exception JavaDoc {
78         MimeHeaders JavaDoc headers = new MimeHeaders JavaDoc();
79         headers.addHeader("Content-Type", "text/xml;");
80         InputStream JavaDoc is = getClass().getClassLoader().getResourceAsStream("org/apache/servicemix/components/http/soap-response.xml");
81         SOAPMessage JavaDoc sm = messageFactory.createMessage(headers, is);
82         NormalizedMessage nm = new NormalizedMessageImpl();
83         new SaajMarshaler().toNMS(nm, sm);
84
85         Node JavaDoc 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 JavaDoc root = (Element JavaDoc) iterator.nextNode();
91         QName JavaDoc 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 JavaDoc messageFactory) throws Exception JavaDoc {
97         MimeHeaders JavaDoc headers = new MimeHeaders JavaDoc();
98         headers.addHeader("Content-Type", "text/xml;");
99          
100         InputStream JavaDoc is = getClass().getClassLoader().getResourceAsStream("org/apache/servicemix/components/saaj/xml-request.xml");
101         log.info("Raw XML: " + new SourceTransformer().toString(new StreamSource JavaDoc(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 JavaDoc(is));
106            
107         SaajMarshaler marshaler = new SaajMarshaler();
108         marshaler.setMessageFactory(messageFactory);
109         SOAPMessage JavaDoc msg = marshaler.createSOAPMessage(nm);
110         
111         ByteArrayOutputStream JavaDoc baos = new ByteArrayOutputStream JavaDoc();
112         msg.writeTo(baos);
113         String JavaDoc soapEnv = new String JavaDoc(baos.toByteArray());
114         log.info("Prepared SOAP: " + soapEnv);
115         Node JavaDoc node2 = new SourceTransformer().toDOMNode(new StringSource(soapEnv));
116           
117         CachedXPathAPI cachedXPathAPI = new CachedXPathAPI();
118         NodeIterator iterator = cachedXPathAPI.selectNodeIterator(node2, "//*[local-name() = 'userId']");
119         Element JavaDoc root = (Element JavaDoc) iterator.nextNode();
120         QName JavaDoc 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