KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > servicemix > soap > marshalers > SoapMessageMarshalerTest


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.soap.marshalers;
18
19 import java.io.ByteArrayInputStream JavaDoc;
20 import java.io.ByteArrayOutputStream JavaDoc;
21 import java.io.File JavaDoc;
22 import java.io.InputStream JavaDoc;
23 import java.net.URI JavaDoc;
24 import java.util.Iterator JavaDoc;
25 import java.util.Properties JavaDoc;
26
27 import javax.activation.DataHandler JavaDoc;
28 import javax.activation.FileDataSource JavaDoc;
29 import javax.mail.Session JavaDoc;
30 import javax.mail.internet.MimeBodyPart JavaDoc;
31 import javax.mail.internet.MimeMessage JavaDoc;
32 import javax.mail.internet.MimeMultipart JavaDoc;
33 import javax.xml.namespace.QName JavaDoc;
34 import javax.xml.transform.dom.DOMSource JavaDoc;
35 import javax.xml.transform.stream.StreamSource JavaDoc;
36
37 import junit.framework.TestCase;
38
39 import org.apache.commons.logging.Log;
40 import org.apache.commons.logging.LogFactory;
41 import org.apache.servicemix.jbi.jaxp.BytesSource;
42 import org.apache.servicemix.jbi.jaxp.SourceTransformer;
43 import org.apache.servicemix.jbi.util.DOMUtil;
44 import org.apache.servicemix.jbi.util.FileUtil;
45 import org.apache.xpath.CachedXPathAPI;
46 import org.w3c.dom.Document JavaDoc;
47 import org.w3c.dom.DocumentFragment JavaDoc;
48 import org.w3c.dom.Element JavaDoc;
49 import org.w3c.dom.Node JavaDoc;
50 import org.w3c.dom.traversal.NodeIterator;
51
52 /**
53  *
54  * @author Guillaume Nodet
55  * @version $Revision: 1.5 $
56  */

57 public class SoapMessageMarshalerTest extends TestCase {
58
59     private static final Log log = LogFactory.getLog(SoapMessageMarshalerTest.class);
60     
61     private SourceTransformer sourceTransformer = new SourceTransformer();
62     
63     public void testReadNonSoapMessage() throws Exception JavaDoc {
64         SoapMarshaler marshaler = new SoapMarshaler(false);
65         SoapMessage msg = marshaler.createReader().read(getClass().getResourceAsStream("soap.xml"));
66         assertNotNull(msg);
67         assertFalse(msg.hasAttachments());
68         assertFalse(msg.hasHeaders());
69         assertNotNull(msg.getSource());
70         Node JavaDoc node = sourceTransformer.toDOMNode(msg.getSource());
71         checkServiceNameNamespace(node);
72         checkUserIdNamespace(node);
73     }
74     
75     public void testWriteAndReadNonSoapMessage() throws Exception JavaDoc {
76         SoapMarshaler marshaler = new SoapMarshaler(false);
77         SoapMessage msg = new SoapMessage();
78         msg.setSource(new StreamSource JavaDoc(getClass().getResourceAsStream("soap.xml")));
79         ByteArrayOutputStream JavaDoc baos = new ByteArrayOutputStream JavaDoc();
80         marshaler.createWriter(msg).write(baos);
81         log.info(baos.toString());
82         
83         SoapMessage msg2 = marshaler.createReader().read(new ByteArrayInputStream JavaDoc(baos.toByteArray()));
84         assertNotNull(msg2);
85         assertFalse(msg2.hasAttachments());
86         assertFalse(msg2.hasHeaders());
87         assertNotNull(msg2.getSource());
88         Node JavaDoc node = sourceTransformer.toDOMNode(msg2.getSource());
89         checkServiceNameNamespace(node);
90         checkUserIdNamespace(node);
91     }
92     
93     public void testWriteAndReadNonSoapMessageWithAttachments() throws Exception JavaDoc {
94         SoapMarshaler marshaler = new SoapMarshaler(false);
95         SoapMessage msg = new SoapMessage();
96         msg.setSource(new StreamSource JavaDoc(getClass().getResourceAsStream("soap.xml")));
97         msg.addAttachment("servicemix.jpg", new DataHandler JavaDoc(new FileDataSource JavaDoc(new File JavaDoc(new URI JavaDoc(getClass().getResource("servicemix.jpg").toString())))));
98         ByteArrayOutputStream JavaDoc baos = new ByteArrayOutputStream JavaDoc();
99         SoapWriter writer = marshaler.createWriter(msg);
100         writer.write(baos);
101         log.info(baos.toString());
102         
103         SoapMessage msg2 = marshaler.createReader().read(new ByteArrayInputStream JavaDoc(baos.toByteArray()), writer.getContentType());
104         assertNotNull(msg2);
105         assertTrue(msg2.hasAttachments());
106         assertEquals(1, msg2.getAttachments().size());
107         DataHandler JavaDoc handler1 = (DataHandler JavaDoc) msg.getAttachments().get(msg.getAttachments().keySet().iterator().next());
108         DataHandler JavaDoc handler2 = (DataHandler JavaDoc) msg2.getAttachments().get(msg2.getAttachments().keySet().iterator().next());
109         assertNotNull(handler2);
110         InputStream JavaDoc is1 = handler1.getInputStream();
111         InputStream JavaDoc is2 = handler2.getInputStream();
112         ByteArrayOutputStream JavaDoc baos1 = new ByteArrayOutputStream JavaDoc();
113         ByteArrayOutputStream JavaDoc baos2 = new ByteArrayOutputStream JavaDoc();
114         FileUtil.copyInputStream(is1, baos1);
115         FileUtil.copyInputStream(is2, baos2);
116         assertEquals(baos1.toString(), baos2.toString());
117         assertFalse(msg2.hasHeaders());
118         assertNotNull(msg2.getSource());
119         Node JavaDoc node = sourceTransformer.toDOMNode(msg2.getSource());
120         checkServiceNameNamespace(node);
121         checkUserIdNamespace(node);
122     }
123     
124     public void testWriteNonSoapMessageWithHeaders() throws Exception JavaDoc {
125         SoapMarshaler marshaler = new SoapMarshaler(false);
126         SoapMessage msg = new SoapMessage();
127         msg.addHeader(new QName JavaDoc("test"), null);
128         msg.setSource(null);
129         try {
130             marshaler.createWriter(msg).write(new ByteArrayOutputStream JavaDoc());
131             fail("Exception should have been thrown");
132         } catch (Exception JavaDoc e) {
133             // ok
134
}
135     }
136     
137     public void testSoapMessage() throws Exception JavaDoc {
138         SoapMarshaler marshaler = new SoapMarshaler(true);
139         
140         SoapMessage msg = marshaler.createReader().read(getClass().getResourceAsStream("soap.xml"));
141         assertNotNull(msg);
142         assertFalse(msg.hasAttachments());
143         assertTrue(msg.hasHeaders());
144         assertNotNull(msg.getSource());
145         
146         Iterator JavaDoc headers = msg.getHeaders().values().iterator();
147         assertTrue(headers.hasNext());
148         assertNotNull(headers.next());
149         assertTrue(headers.hasNext());
150         checkServiceNameNamespace((DocumentFragment JavaDoc) headers.next());
151         assertFalse(headers.hasNext());
152
153         Node JavaDoc node2 = sourceTransformer.toDOMNode(msg.getSource());
154         checkUserIdNamespace(node2);
155         
156         msg.setSource(new DOMSource JavaDoc(node2));
157         ByteArrayOutputStream JavaDoc baos = new ByteArrayOutputStream JavaDoc();
158         marshaler.createWriter(msg).write(baos);
159         Node JavaDoc node = sourceTransformer.toDOMNode(new BytesSource(baos.toByteArray()));
160         checkUserIdNamespace(node);
161     }
162     
163     public void testSoapMessage2() throws Exception JavaDoc {
164         SoapMarshaler marshaler = new SoapMarshaler(true);
165         SoapMessage msg = marshaler.createReader().read(getClass().getResourceAsStream("soap2.xml"));
166         assertNotNull(msg);
167     }
168     
169     public void testSoap11Message() throws Exception JavaDoc {
170         SoapMarshaler marshaler = new SoapMarshaler(true);
171         marshaler.setSoapUri(SoapMarshaler.SOAP_11_URI);
172         SoapMessage msg = new SoapMessage();
173         ByteArrayOutputStream JavaDoc baos = new ByteArrayOutputStream JavaDoc();
174         marshaler.createWriter(msg).write(baos);
175         Node JavaDoc node = sourceTransformer.toDOMNode(new BytesSource(baos.toByteArray()));
176         assertNotNull(node);
177         Element JavaDoc root = ((Document JavaDoc) node).getDocumentElement();
178         assertEquals(SoapMarshaler.SOAP_11_URI, root.getNamespaceURI());
179     }
180     
181     public void testWriteAndReadSoapMessageWithAttachments() throws Exception JavaDoc {
182         SoapMarshaler marshaler = new SoapMarshaler(true);
183         SoapMessage msg = marshaler.createReader().read(getClass().getResourceAsStream("soap.xml"));
184         msg.addAttachment("servicemix.jpg", new DataHandler JavaDoc(new FileDataSource JavaDoc(new File JavaDoc(new URI JavaDoc(getClass().getResource("servicemix.jpg").toString())))));
185         ByteArrayOutputStream JavaDoc baos = new ByteArrayOutputStream JavaDoc();
186         SoapWriter writer = marshaler.createWriter(msg);
187         writer.write(baos);
188         log.info(baos.toString());
189         
190         SoapMessage msg2 = marshaler.createReader().read(new ByteArrayInputStream JavaDoc(baos.toByteArray()), writer.getContentType());
191         assertNotNull(msg2);
192         assertTrue(msg2.hasAttachments());
193         assertEquals(1, msg2.getAttachments().size());
194         DataHandler JavaDoc handler = (DataHandler JavaDoc) msg2.getAttachments().get(msg2.getAttachments().keySet().iterator().next());
195         assertNotNull(handler);
196         assertNotNull(msg2.getSource());
197         checkUserIdNamespace(sourceTransformer.toDOMNode(msg2.getSource()));
198         assertTrue(msg2.hasHeaders());
199         Iterator JavaDoc headers = msg2.getHeaders().values().iterator();
200         assertTrue(headers.hasNext());
201         assertNotNull(headers.next());
202         assertTrue(headers.hasNext());
203         checkServiceNameNamespace((DocumentFragment JavaDoc) headers.next());
204         assertFalse(headers.hasNext());
205     }
206
207     public void testReadNonSoapMessageWithAttachmentsAndNoSource() throws Exception JavaDoc {
208         Session JavaDoc session = Session.getDefaultInstance(new Properties JavaDoc(), null);
209         MimeMessage JavaDoc message = new MimeMessage JavaDoc(session);
210         MimeMultipart JavaDoc multipart = new MimeMultipart JavaDoc();
211         MimeBodyPart JavaDoc mimePart = new MimeBodyPart JavaDoc();
212         mimePart.setText("This is a dummy text");
213         mimePart.setContentID("<bla1>");
214         multipart.addBodyPart(mimePart);
215         mimePart = new MimeBodyPart JavaDoc();
216         mimePart.setContentID("<bla2>");
217         mimePart.setContent("<html>dummy</html>", "text/html");
218         multipart.addBodyPart(mimePart);
219         message.setContent(multipart);
220         message.setHeader("Content-Type", multipart.getContentType());
221         message.saveChanges();
222         ByteArrayOutputStream JavaDoc baos = new ByteArrayOutputStream JavaDoc();
223         message.writeTo(baos);
224         log.info(baos.toString());
225         
226         SoapMarshaler marshaler = new SoapMarshaler(true);
227         SoapMessage msg = marshaler.createReader().read(new ByteArrayInputStream JavaDoc(baos.toByteArray()), multipart.getContentType());
228         assertTrue(msg.getSource().equals(SoapReader.EMPTY_CONTENT));
229         SoapWriter writer = marshaler.createWriter(msg);
230         baos = new ByteArrayOutputStream JavaDoc();
231         writer.write(baos);
232         log.info(baos.toString());
233     }
234   
235     protected void checkUserIdNamespace(Node JavaDoc node) throws Exception JavaDoc {
236         CachedXPathAPI cachedXPathAPI = new CachedXPathAPI();
237         NodeIterator iterator = cachedXPathAPI.selectNodeIterator(node, "//*[local-name() = 'userId']");
238         Element JavaDoc root = (Element JavaDoc) iterator.nextNode();
239         QName JavaDoc qname = DOMUtil.createQName(root, root.getAttributeNS("http://www.w3.org/2001/XMLSchema-instance", "type"));
240         assertEquals("http://www.w3.org/2001/XMLSchema", qname.getNamespaceURI());
241         assertEquals("string", qname.getLocalPart());
242     }
243     
244     protected void checkServiceNameNamespace(Node JavaDoc node) throws Exception JavaDoc {
245         CachedXPathAPI cachedXPathAPI = new CachedXPathAPI();
246         NodeIterator iterator = cachedXPathAPI.selectNodeIterator(node, "//*[local-name() = 'ServiceName']");
247         Element JavaDoc root = (Element JavaDoc) iterator.nextNode();
248         assertEquals(new QName JavaDoc("http://schemas.xmlsoap.org/ws/2003/03/addressing", "ServiceName"),
249                      new QName JavaDoc(root.getNamespaceURI(), root.getLocalName()));
250         QName JavaDoc qname = DOMUtil.createQName(root, DOMUtil.getElementText(root));
251         assertEquals(new QName JavaDoc("uri:test", "MyConsumerService"), qname);
252     }
253     
254 }
255
Popular Tags