KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > docStyle > MessagingAttachmentsTest


1 /*
2  * The Apache Software License, Version 1.1
3  *
4  *
5  * Copyright (c) 2002 The Apache Software Foundation. All rights
6  * reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright
16  * notice, this list of conditions and the following disclaimer in
17  * the documentation and/or other materials provided with the
18  * distribution.
19  *
20  * 3. The end-user documentation included with the redistribution,
21  * if any, must include the following acknowledgment:
22  * "This product includes software developed by the
23  * Apache Software Foundation (http://www.apache.org/)."
24  * Alternately, this acknowledgment may appear in the software itself,
25  * if and wherever such third-party acknowledgments normally appear.
26  *
27  * 4. The names "WSIF" and "Apache Software Foundation" must
28  * not be used to endorse or promote products derived from this
29  * software without prior written permission. For written
30  * permission, please contact apache@apache.org.
31  *
32  * 5. Products derived from this software may not be called "Apache",
33  * nor may "Apache" appear in their name, without prior written
34  * permission of the Apache Software Foundation.
35  *
36  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
37  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
38  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
39  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
40  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
42  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
43  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
44  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
45  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
46  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
47  * SUCH DAMAGE.
48  * ====================================================================
49  *
50  * This software consists of voluntary contributions made by many
51  * individuals on behalf of the Apache Software Foundation and was
52  * originally based on software copyright (c) 2001, 2002, International
53  * Business Machines, Inc., http://www.apache.org. For more
54  * information on the Apache Software Foundation, please see
55  * <http://www.apache.org/>.
56  */

57
58 package docStyle;
59
60 import java.io.BufferedInputStream JavaDoc;
61 import java.io.ByteArrayInputStream JavaDoc;
62 import java.io.File JavaDoc;
63 import java.io.FileInputStream JavaDoc;
64 import java.io.FileNotFoundException JavaDoc;
65 import java.io.IOException JavaDoc;
66 import java.io.InputStream JavaDoc;
67 import java.io.StringReader JavaDoc;
68 import java.io.StringWriter JavaDoc;
69 import java.util.Arrays JavaDoc;
70
71 import javax.activation.DataHandler JavaDoc;
72 import javax.activation.FileDataSource JavaDoc;
73 import junit.framework.Test;
74 import junit.framework.TestCase;
75 import junit.framework.TestSuite;
76
77 import org.apache.wsif.WSIFMessage;
78 import org.apache.wsif.WSIFOperation;
79 import org.apache.wsif.WSIFPort;
80 import org.apache.wsif.WSIFService;
81 import org.apache.wsif.WSIFServiceFactory;
82 import org.apache.xerces.parsers.DOMParser;
83 import org.apache.xml.serialize.OutputFormat;
84 import org.apache.xml.serialize.XMLSerializer;
85 import org.w3c.dom.Element JavaDoc;
86 import org.w3c.dom.Node JavaDoc;
87 import org.w3c.dom.NodeList JavaDoc;
88 import org.xml.sax.InputSource JavaDoc;
89 import util.TestUtilities;
90
91 /**
92  * Junit test to test out the AXIS provider docstyle support
93  */

94 public class MessagingAttachmentsTest extends TestCase {
95     String JavaDoc wsdlLocation =
96         TestUtilities.getWsdlPath("java\\test\\docStyle\\wsifservice")
97             + "mime.wsdl";
98     static String JavaDoc server = TestUtilities.getSoapServer().toUpperCase();
99     
100     private final static String JavaDoc IMAGE_LOCATION =
101         TestUtilities.getWsdlPath("java\\test\\mime")
102         + "axis.jpg";
103
104     public MessagingAttachmentsTest(String JavaDoc name) {
105         super(name);
106     }
107
108     public static void main(String JavaDoc[] args) {
109 // TestUtilities.startListeners(
110
// TestUtilities.ADDRESSBOOK_LISTENER
111
// | TestUtilities.ASYNC_LISTENER
112
// | TestUtilities.NATIVEJMS_LISTENER);
113
junit.textui.TestRunner.run(suite());
114 // TestUtilities.stopListeners();
115
}
116
117     public static Test suite() {
118         return new TestSuite(MessagingAttachmentsTest.class);
119     }
120
121     public void setUp() {
122         TestUtilities.setUpExtensionsAndProviders();
123     }
124
125     public void testMsgAxisRcvAtt() {
126         doitMsgRcvAtt(server+"Port", "axis");
127     }
128     public void testMsgAxisSendAtt() {
129         doitMsgSendAtt(server+"Port", "axis");
130     }
131
132     private void doitMsgRcvAtt(String JavaDoc portName, String JavaDoc protocol) {
133         if (portName.toUpperCase().indexOf("JMS") != -1
134             && !TestUtilities.areWeTesting("jms"))
135             return;
136
137         TestUtilities.setProviderForProtocol(protocol);
138
139         try {
140             WSIFServiceFactory factory = WSIFServiceFactory.newInstance();
141             WSIFService service =
142                 factory.getService(
143                     wsdlLocation,
144                     null,
145                     null,
146                     "http://mime/",
147                     "Mime");
148
149             WSIFPort port = service.getPort(portName);
150             WSIFOperation operation = port.createOperation("stringToDataHandler");
151             WSIFMessage inMsg = operation.createInputMessage();
152             WSIFMessage outMsg = operation.createOutputMessage();
153             WSIFMessage faultMsg = operation.createFaultMessage();
154
155             String JavaDoc content = "The owl and the pussy cat went to sea in a beautiful pea-green boat,";
156
157             String JavaDoc inputDoc =
158                 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
159                 "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">" +
160                 " <soapenv:Body>" +
161                 " <ns1:stringToDataHandler soapenv:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\" xmlns:ns1=\"http://mime/\">" +
162                 " <ns1:buff xsi:type=\"xsd:string\">" +
163                 content +
164                 "</ns1:buff>" +
165                 " </ns1:stringToDataHandler>" +
166                 " </soapenv:Body>" +
167                 "</soapenv:Envelope>";
168
169             DOMParser parser = new DOMParser();
170
171             parser.parse(new InputSource JavaDoc(new StringReader JavaDoc(inputDoc)));
172             Element JavaDoc element = parser.getDocument().getDocumentElement();
173             NodeList JavaDoc l = element.getChildNodes();
174             Element JavaDoc e1 = (Element JavaDoc)l.item(1);
175             l = e1.getChildNodes();
176             element = (Element JavaDoc)l.item(1);
177             Node JavaDoc n = e1.getFirstChild();
178             n = n.getNextSibling();
179             element = (Element JavaDoc) n;
180 // printElement(element);
181

182             inMsg.setObjectPart("buff", element);
183
184             boolean ok =
185                 operation.executeRequestResponseOperation(
186                     inMsg,
187                     outMsg,
188                     faultMsg);
189             assertTrue("operation returned false!!", ok);
190             
191             Object JavaDoc o = outMsg.getObjectPart("return");
192             assertTrue(
193                 "return part not a DataHandler!!",
194                 (o instanceof DataHandler JavaDoc));
195                 
196             DataHandler JavaDoc dh = (DataHandler JavaDoc) o;
197             assertTrue("response Datahandler content wrong!!", compareFiles(dh, content));
198
199         } catch (Exception JavaDoc ex) {
200             ex.printStackTrace();
201             assertTrue(
202                 "AddressBookTest("
203                     + portName
204                     + ") caught exception "
205                     + ex.getLocalizedMessage(),
206                 false);
207         }
208     }
209
210     private void doitMsgSendAtt(String JavaDoc portName, String JavaDoc protocol) {
211         if (portName.toUpperCase().indexOf("JMS") != -1
212             && !TestUtilities.areWeTesting("jms"))
213             return;
214
215         TestUtilities.setProviderForProtocol(protocol);
216
217         try {
218             WSIFServiceFactory factory = WSIFServiceFactory.newInstance();
219             WSIFService service =
220                 factory.getService(
221                     wsdlLocation,
222                     null,
223                     null,
224                     "http://mime/",
225                     "Mime");
226
227             WSIFPort port = service.getPort(portName);
228             WSIFOperation operation = port.createOperation("bounceImage4");
229             WSIFMessage inMsg = operation.createInputMessage();
230             WSIFMessage outMsg = operation.createOutputMessage();
231             WSIFMessage faultMsg = operation.createFaultMessage();
232
233             String JavaDoc inputDoc =
234                   "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
235                   "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">" +
236                   " <soapenv:Body>" +
237                   " <ns1:bounceImage4 soapenv:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\" xmlns:ns1=\"http://mime/\">" +
238                   " <ns1:shouldBounce xsi:type=\"xsd:boolean\">true</ns1:shouldBounce>" +
239                   " <ns1:file HREF=\"cid:413B07CE410E48EB9D89DC0A4DDD715D\"/>" +
240                   " </ns1:bounceImage4>" +
241                   " </soapenv:Body>" +
242                   "</soapenv:Envelope>";
243
244             DOMParser parser = new DOMParser();
245
246             parser.parse(new InputSource JavaDoc(new StringReader JavaDoc(inputDoc)));
247             Element JavaDoc element = parser.getDocument().getDocumentElement();
248             NodeList JavaDoc l = element.getChildNodes();
249             Element JavaDoc e1 = (Element JavaDoc)l.item(1);
250             l = e1.getChildNodes();
251             element = (Element JavaDoc)l.item(1);
252             Node JavaDoc n = e1.getFirstChild();
253             n = n.getNextSibling();
254             element = (Element JavaDoc) n;
255 // printElement(element);
256

257             inMsg.setObjectPart("shouldBounce", element);
258             
259             DataHandler JavaDoc dh1 = new DataHandler JavaDoc(new FileDataSource JavaDoc(IMAGE_LOCATION));
260             inMsg.setObjectPart("file", dh1);
261
262             boolean ok =
263                 operation.executeRequestResponseOperation(
264                     inMsg,
265                     outMsg,
266                     faultMsg);
267             assertTrue("operation returned false!!", ok);
268             
269             Object JavaDoc o = outMsg.getObjectPart("return");
270             assertTrue(
271                 "return part not a DataHandler!!",
272                 (o instanceof DataHandler JavaDoc));
273                 
274             DataHandler JavaDoc dhResponse = (DataHandler JavaDoc) o;
275             assertTrue("response Datahandler content wrong!!", compareFiles(dhResponse, dh1));
276
277         } catch (Exception JavaDoc ex) {
278             ex.printStackTrace();
279             assertTrue(
280                 "AddressBookTest("
281                     + portName
282                     + ") caught exception "
283                     + ex.getLocalizedMessage(),
284                 false);
285         }
286     }
287
288     private void printElement(Element JavaDoc e) throws Exception JavaDoc {
289         OutputFormat of = new OutputFormat(e.getOwnerDocument(), "UTF-8", true);
290         XMLSerializer xmls = new XMLSerializer(of);
291         StringWriter JavaDoc sw = new StringWriter JavaDoc();
292         xmls.setOutputCharStream(sw);
293         xmls.serialize(e);
294         System.err.println("element=" + sw.toString());
295     }
296
297     private boolean compareFiles(DataHandler JavaDoc dh1, DataHandler JavaDoc dh2)
298         throws FileNotFoundException JavaDoc, IOException JavaDoc {
299         InputStream JavaDoc is1 = dh1.getInputStream();
300         InputStream JavaDoc is2 = dh2.getInputStream();
301         boolean success = false;
302         try {
303             success = compareFiles(is1, is2);
304         } finally {
305             if (null != is1)
306                 is1.close();
307             if (null != is2)
308                 is2.close();
309         }
310         return success;
311     }
312
313     private boolean compareFiles(DataHandler JavaDoc dh, String JavaDoc buff)
314         throws FileNotFoundException JavaDoc, IOException JavaDoc {
315         InputStream JavaDoc is = dh.getInputStream();
316         boolean success = false;
317         try {
318             success = compareFiles(is, buff);
319         } finally {
320             if (null != is)
321                 is.close();
322         }
323         return success;
324     }
325
326     private boolean compareFiles(String JavaDoc one, String JavaDoc buff)
327         throws FileNotFoundException JavaDoc, IOException JavaDoc {
328         BufferedInputStream JavaDoc oneStream = null;
329         File JavaDoc f1 = new File JavaDoc(one);
330
331         boolean success = false;
332         try {
333             oneStream =
334                 new BufferedInputStream JavaDoc(
335                     new FileInputStream JavaDoc(one),
336                     buff.length());
337             success = compareFiles(oneStream, buff);
338         } finally {
339             if (null != oneStream)
340                 oneStream.close();
341         }
342         return success;
343     }
344
345     private boolean compareFiles(InputStream JavaDoc is, String JavaDoc buff)
346         throws FileNotFoundException JavaDoc, IOException JavaDoc {
347         return compareFiles(is, new ByteArrayInputStream JavaDoc(buff.getBytes()));
348     }
349     
350     private boolean compareFiles(InputStream JavaDoc is1, InputStream JavaDoc is2)
351         throws FileNotFoundException JavaDoc, IOException JavaDoc {
352
353         int avail1 = is1.available();
354         int avail2 = is2.available();
355         if (avail1 != avail2) return false;
356         if (avail1==0) return true;
357
358         byte[] buff1 = new byte[avail1];
359         byte[] buff2 = new byte[avail2];
360
361         Arrays.fill(buff1, (byte) 0);
362         Arrays.fill(buff2, (byte) 0);
363
364         int bread1 = -1;
365         int bread2 = -1;
366         bread1 = is1.read(buff1, 0, avail1);
367         bread2 = is2.read(buff2, 0, avail2);
368         String JavaDoc s1 = new String JavaDoc(buff1);
369         String JavaDoc s2 = new String JavaDoc(buff2);
370         if (!s1.equals(s2))
371             return false;
372         return true;
373     }
374     
375     private String JavaDoc concat(String JavaDoc one, String JavaDoc two)
376         throws FileNotFoundException JavaDoc, IOException JavaDoc {
377         InputStream JavaDoc is1 = null;
378         InputStream JavaDoc is2 = null;
379         try {
380             is1 = (new DataHandler JavaDoc(new FileDataSource JavaDoc(one))).getInputStream();
381             is2 = (new DataHandler JavaDoc(new FileDataSource JavaDoc(two))).getInputStream();
382             int avail1 = is1.available();
383             int avail2 = is2.available();
384
385             byte[] buff1 = new byte[avail1];
386             byte[] buff2 = new byte[avail2];
387
388             Arrays.fill(buff1, (byte) 0);
389             Arrays.fill(buff2, (byte) 0);
390
391             int bread1 = -1;
392             int bread2 = -1;
393             bread1 = is1.read(buff1, 0, avail1);
394             bread2 = is2.read(buff2, 0, avail2);
395             String JavaDoc s1 = new String JavaDoc(buff1);
396             String JavaDoc s2 = new String JavaDoc(buff2);
397             return s1 + s2;
398
399         } finally {
400             if (null != is1)
401                 is1.close();
402             if (null != is2)
403                 is2.close();
404         }
405     }
406
407 }
408
Popular Tags