KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > mime > MimeTest


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 mime;
59
60 import java.awt.Image JavaDoc;
61 import java.io.BufferedInputStream JavaDoc;
62 import java.io.ByteArrayInputStream JavaDoc;
63 import java.io.File JavaDoc;
64 import java.io.FileInputStream JavaDoc;
65 import java.io.FileNotFoundException JavaDoc;
66 import java.io.IOException JavaDoc;
67 import java.io.InputStream JavaDoc;
68 import java.util.Arrays JavaDoc;
69
70 import javax.activation.DataHandler JavaDoc;
71 import javax.activation.FileDataSource JavaDoc;
72 import javax.mail.internet.MimeMultipart JavaDoc;
73 import javax.swing.ImageIcon JavaDoc;
74 import javax.xml.namespace.QName JavaDoc;
75 import javax.xml.transform.Source JavaDoc;
76 import junit.framework.Test;
77 import junit.framework.TestCase;
78 import junit.framework.TestSuite;
79
80 import org.apache.wsif.WSIFException;
81 import org.apache.wsif.WSIFMessage;
82 import org.apache.wsif.WSIFOperation;
83 import org.apache.wsif.WSIFPort;
84 import org.apache.wsif.WSIFService;
85 import org.apache.wsif.WSIFServiceFactory;
86 import org.apache.wsif.util.WSIFPluggableProviders;
87 import util.TestUtilities;
88
89 /**
90  * Junit test for Mime attachments
91  *
92  * There should be tests here for...
93  * DataHandler, DataSource and other datatypes (not sure of the list)
94  * Sync and async
95  * rpc-style and doc-style
96  * Other mime types
97  * Axis and Soap 2.3
98  * Soap Http and Jms
99  * Does this function make sense for NativeJms, Java, EJB, etc?
100  * Sending attachments and receiving them
101  * Send or receive a null attachment
102  * Sending or receiving an object that extends DataHandler, DataSource, etc
103  * Multiple mime parts, multiple soap bodies, or no soap body at all.
104  * Is the Mime type that is specified in the WSDL passed into Axis?
105  *
106  * @author Mark Whitlock
107  */

108 public class MimeTest extends TestCase {
109     private final static String JavaDoc wsdlPath =
110         TestUtilities.getWsdlPath("java\\test\\mime");
111     private final static String JavaDoc imageLocation = wsdlPath + "axis.jpg";
112     private final static String JavaDoc flatfileLocation = wsdlPath + "test.txt";
113     private final static String JavaDoc flatfileLocation2 = wsdlPath + "test2.txt";
114     private final static String JavaDoc portName =
115         TestUtilities.getSoapServer().toUpperCase() + "Port";
116
117     private final static String JavaDoc SEND_DH = "SEND-DH";
118     private final static String JavaDoc RECEIVE_DH = "RECEIVE-DH";
119     private final static String JavaDoc SEND_PLAINTEXT = "SEND-PLAINTEXT";
120     private final static String JavaDoc RECEIVE_PLAINTEXT = "RECEIVE-PLAINTEXT";
121     private final static String JavaDoc BOUNCE_IMAGE = "BOUNCE-IMAGE";
122     private final static String JavaDoc BOUNCE_IMAGE2 = "BOUNCE-IMAGE2";
123     private final static String JavaDoc BOUNCE_IMAGE3 = "BOUNCE-IMAGE3";
124     private final static String JavaDoc BOUNCE_IMAGE4_DEFAULT = "BOUNCE-IMAGE4-DEFAULT";
125     private final static String JavaDoc BOUNCE_IMAGE4_FALSE = "BOUNCE-IMAGE4-FALSE";
126     private final static String JavaDoc BOUNCE_IMAGE4_NULL = "BOUNCE-IMAGE4-NULL";
127     private final static String JavaDoc SEND_SOURCE = "SEND-SOURCE";
128     private final static String JavaDoc RECEIVE_SOURCE = "RECEIVE-SOURCE";
129     private final static String JavaDoc SEND_MIMEMULTIPART = "SEND-MIMEMULTIPART";
130     private final static String JavaDoc RECEIVE_MIMEMULTIPART = "RECEIVE-MIMEMULTIPART";
131     private final static String JavaDoc OR_MULTIPARTS1 = "OR-MULTIPARTS1";
132     private final static String JavaDoc OR_MULTIPARTS2 = "OR-MULTIPARTS2";
133     private final static String JavaDoc AND_MULTIPARTS = "AND-MULTIPARTS";
134     private final static String JavaDoc MULTI_OUT_PARTS = "MULTI-OUT-PARTS";
135     private final static String JavaDoc MULTI_INOUT_PARTS = "MULTI-INOUT-PARTS";
136     private final static String JavaDoc NO_CONTENT = "NO-CONTENT";
137     private final static String JavaDoc TYPE_STAR = "TYPE-STAR";
138     private final static String JavaDoc SOAP_BODY_PARTS1 = "SOAP_BODY_PARTS1";
139     private final static String JavaDoc SOAP_BODY_PARTS2 = "SOAP_BODY_PARTS2";
140     private final static String JavaDoc SOAP_BODY_PARTS3 = "SOAP_BODY_PARTS3";
141     private final static String JavaDoc SOAP_BODY_PARTS4 = "SOAP_BODY_PARTS4";
142     private final static String JavaDoc ARRAY_OF_BINARY = "ARRAY-OF-BINARY";
143     private final static String JavaDoc OPTIONAL_SOAP_BODY = "OPTIONAL_SOAP_BODY";
144     private final static String JavaDoc MIX_MIME_PARTS = "MIX_MIME_PARTS";
145     private final static String JavaDoc MAP_TYPE = "MAP-TYPE";
146     private final static String JavaDoc BAD_NO_PART = "BAD-NO-PART";
147     private final static String JavaDoc BAD_PART = "BAD-PART";
148     private final static String JavaDoc BAD_NESTED = "BAD-NESTED";
149     private final static String JavaDoc BAD_MIX_SOAP_MIME = "BAD-MIX-SOAP-MIME";
150     private final static String JavaDoc BAD_MULTIPLE_SOAP_BODIES = "BAD-MULTIPLE-SOAP-BODIES";
151     private final static String JavaDoc BAD_SOAP_BODY_TYPE = "BAD-SOAP-BODY-TYPE";
152
153     /**
154      * Milliseconds to sleep while the user looks at the displayed images.
155      */

156     private int SLEEPY_TIME = 0;
157     
158     private final static String JavaDoc[] rhyme =
159         { "The owl and the pussy cat went to sea in a beautiful pea-green boat,",
160           "They took some honey and plenty of money wrapped up in a five pound note" };
161
162     private final boolean SYNC = true;
163     private final boolean ASYNC = false;
164     
165     public MimeTest(String JavaDoc name) {
166         super(name);
167
168         try {
169             SLEEPY_TIME =
170                 Integer.parseInt(
171                     TestUtilities.getWsifProperty("wsif.displaytime"));
172         } finally {
173             if (SLEEPY_TIME <= 0)
174                 SLEEPY_TIME = 2000;
175         }
176     }
177
178     public static void main(String JavaDoc[] args) {
179        // TestUtilities.startListeners(TestUtilities.ADDRESSBOOK_LISTENER);
180
junit.textui.TestRunner.run (suite());
181        // TestUtilities.stopListeners();
182
}
183
184     public static Test suite() {
185         return new TestSuite(MimeTest.class);
186     }
187
188     public void setUp() {
189         TestUtilities.setUpExtensionsAndProviders();
190     }
191
192     public void testSendHandler() {
193         doit(SEND_DH, "Mime.wsdl");
194     }
195
196     public void testReceiveHandler() {
197         doit(RECEIVE_DH, "Mime.wsdl");
198     }
199
200     /*
201      * Attachments that are Strings, Images, are not supported at present.
202      * Attachments can only be DataHandlers.
203      *
204      * public void testSendPlainText() {
205      * doit(SEND_PLAINTEXT, "Mime.wsdl");
206      * }
207      *
208      * public void testReceivePlainText() {
209      * doit(RECEIVE_PLAINTEXT, "Mime.wsdl");
210      * }
211      *
212      * public void testBounceImage() {
213      * doit(BOUNCE_IMAGE, "Mime.wsdl");
214      * }
215      */

216
217     public void testBounceImage2() {
218         doit(BOUNCE_IMAGE2, "Mime.wsdl");
219     }
220
221     public void testBounceImage3() {
222         doit(BOUNCE_IMAGE3, "Mime.wsdl");
223     }
224
225     public void testBounceImage4Default() {
226         doit(BOUNCE_IMAGE4_DEFAULT, "Mime.wsdl");
227     }
228     
229     public void testBounceImage4False() {
230         doit(BOUNCE_IMAGE4_FALSE, "Mime.wsdl");
231     }
232
233     /*
234      * This next test fails because axis will not allow null
235      * parameters to be passed in.
236      *
237      * public void testBounceImage4Null() {
238      * doit(BOUNCE_IMAGE4_NULL, "Mime.wsdl");
239      * }
240      */

241
242     /*
243      * bounceImage5 doesn't work because the soap:body (which contains a
244      * boolean) is within the mime:part. bounceImage4 works because the
245      * soap:body is a direct child of the binding input. When I try to
246      * run bounceImage5, WSDL4J says the soap:body doesn't contain any
247      * parts. But having the soap:body inside the mime:part is valid WSDL
248      * (according to the wsdl4j PopulatedExtensionRegistry). So what does
249      * it mean?
250      */

251
252     /*
253      * public void testSendSource() {
254      * doit(SEND_SOURCE, "Mime.wsdl");
255      * }
256      *
257      * public void testReceiveSource() {
258      * doit(RECEIVE_SOURCE, "Mime.wsdl");
259      * }
260      *
261      * public void testSendMimeMultipart() {
262      * doit(SEND_MIMEMULTIPART, "Mime.wsdl");
263      * }
264      *
265      * public void testReceiveMimeMultipart() {
266      * doit(RECEIVE_MIMEMULTIPART, "Mime.wsdl");
267      * }
268      */

269
270     /*
271      * Sending MIME messages using Axis over Jms should be supported
272      * by WSIF, but currently this does not work. I think the problem
273      * is that WSIFJmsSender does not set up the necessary headers needed
274      * for MIME. I don't know whether these headers can go inline in the
275      * message or whether they will have to go as JMS properties which
276      * will then get converted into HTTP headers by the JMS2HTTPBridge.
277      * For more information about the headers please see
278      * org.apache.axis.transport.http.HTTPSender. For the moment I have
279      * commented out this Jms test (and omitted other JMS tests like
280      * the async tests) because it fails.
281      *
282      * public void testJmsSend() {
283      * doit("SOAPJMSPort", SEND, "Mime.wsdl");
284      * }
285      */

286
287     public void testOrMultiParts1() {
288         doit(OR_MULTIPARTS1, "Mime.wsdl");
289     }
290
291     public void testOrMultiParts2() {
292         doit(OR_MULTIPARTS2, "Mime.wsdl");
293     }
294
295     public void testAndMultiParts() {
296         doit(AND_MULTIPARTS, "Mime.wsdl");
297     }
298
299     /*
300      * public void testMultiOutParts() {
301      * doit(MULTI_OUT_PARTS, "Mime.wsdl");
302      * }
303      *
304      * public void testMultiInoutParts() {
305      * doit(MULTI_INOUT_PARTS, "Mime.wsdl");
306      * }
307      */

308
309     public void testNoContent() {
310         doit(NO_CONTENT, "Mime.wsdl");
311     }
312
313     public void testTypeStar() {
314         doit(TYPE_STAR, "Mime.wsdl");
315     }
316
317     public void testSoapBodyParts1() {
318         doit(SOAP_BODY_PARTS1, "Mime.wsdl");
319     }
320
321     public void testSoapBodyParts2() {
322         doit(SOAP_BODY_PARTS2, "Mime.wsdl");
323     }
324
325     public void testSoapBodyParts3() {
326         doit(SOAP_BODY_PARTS3, "Mime.wsdl");
327     }
328
329     public void testSoapBodyParts4() {
330         doit(SOAP_BODY_PARTS4, "Mime.wsdl");
331     }
332
333     public void testArrayOfBinary() {
334         doit(ARRAY_OF_BINARY, "Mime.wsdl");
335     }
336
337     public void testMapType() {
338         doit(MAP_TYPE, "Mime.wsdl");
339     }
340
341     public void testOptionalSoapBody() {
342         doit(OPTIONAL_SOAP_BODY, "Mime.wsdl");
343     }
344
345     public void testMixMimeParts() {
346         doit(MIX_MIME_PARTS, "Mime.wsdl");
347     }
348
349     /* ************ ERRORS **************** */
350
351     public void testBadNoPart() {
352         doit(BAD_NO_PART, "MimeBadNoPart.wsdl");
353     }
354
355     public void testBadPart() {
356         doit(BAD_PART, "MimeBadPart.wsdl");
357     }
358
359     public void testBadNested() {
360         doit(BAD_NESTED, "MimeBadNested.wsdl");
361     }
362
363     public void testBadMixSoapMime() {
364         doit(BAD_MIX_SOAP_MIME, "MimeBadMixSoapMime.wsdl");
365     }
366
367     public void testBadMultipleSoapBodies() {
368         doit(BAD_MULTIPLE_SOAP_BODIES, "MimeBadMultipleSoapBodies.wsdl");
369     }
370
371     public void testBadSoapBodyType() {
372         doit(BAD_SOAP_BODY_TYPE, "MimeBadSoapBodyType.wsdl");
373     }
374
375     /**
376      * doit should probably do the mapTypes() but unfortunately plain text
377      * means that Strings are serialized with the JAFDataHandlerSerializer
378      * and I can't find a way to differentiate between strings that should
379      * be treated as a mime part and those that should not. Basically if I
380      * mapTypes(plaintext,String) all Strings get mapped to a DataHandler,
381      * and soap on the server looks for a web service that takes a
382      * DataHandler, not a String. So be careful where mapTypes(plaintext,String)
383      * is done.
384      */

385     private void doit(String JavaDoc cmd, String JavaDoc wsdl) {
386         if (!TestUtilities.areWeTesting("mime"))
387             return;
388             
389         WSIFPluggableProviders.overrideDefaultProvider(
390             "http://schemas.xmlsoap.org/wsdl/soap/",
391             new org.apache.wsif.providers.soap.apacheaxis
392
                .WSIFDynamicProvider_ApacheAxis());
393
394         try {
395             WSIFServiceFactory factory = WSIFServiceFactory.newInstance();
396             WSIFService service =
397                 factory.getService(
398                     wsdlPath + wsdl,
399                     null,
400                     null,
401                     "http://mime/",
402                     "Mime");
403
404             // Many of the error tests are badly behaved and would
405
// throw an exception here.
406
Mime stub = null;
407             if (!cmd.startsWith("BAD"))
408                 stub = (Mime) service.getStub(portName, Mime.class);
409
410             if (cmd.equals(SEND_DH))
411                 send_dh(service,stub);
412             else if (cmd.equals(RECEIVE_DH))
413                 receive_dh(service,stub);
414             else if (cmd.equals(SEND_PLAINTEXT))
415                 send_plaintext(service,stub);
416             else if (cmd.equals(RECEIVE_PLAINTEXT))
417                 receive_plaintext(service,stub);
418             else if (cmd.equals(BOUNCE_IMAGE))
419                 bounce_image(service,stub);
420             else if (cmd.equals(BOUNCE_IMAGE2))
421                 bounce_image2(service,stub,portName);
422             else if (cmd.equals(BOUNCE_IMAGE3))
423                 bounce_image3(service,stub,portName);
424             else if (cmd.equals(BOUNCE_IMAGE4_DEFAULT))
425                 bounce_image4_default(service,stub,portName);
426             else if (cmd.equals(BOUNCE_IMAGE4_FALSE))
427                 bounce_image4_false(service,stub,portName);
428             else if (cmd.equals(BOUNCE_IMAGE4_NULL))
429                 bounce_image4_null(service,stub,portName);
430             else if (cmd.equals(SEND_SOURCE))
431                 send_source(service,stub);
432             else if (cmd.equals(RECEIVE_SOURCE))
433                 receive_source(service,stub);
434             else if (cmd.equals(SEND_MIMEMULTIPART))
435                 send_mimemultipart(service,stub);
436             else if (cmd.equals(RECEIVE_MIMEMULTIPART))
437                 receive_mimemultipart(service,stub);
438             else if (cmd.equals(OR_MULTIPARTS1))
439                 or_multiparts1(service,stub);
440             else if (cmd.equals(OR_MULTIPARTS2))
441                 or_multiparts2(service,stub);
442             else if (cmd.equals(AND_MULTIPARTS))
443                 and_multiparts(service,stub);
444             else if (cmd.equals(MULTI_OUT_PARTS))
445                 multi_out_parts(service,stub);
446             else if (cmd.equals(MULTI_INOUT_PARTS))
447                 multi_inout_parts(service,stub);
448             else if (cmd.equals(NO_CONTENT))
449                 no_content(service,stub);
450             else if (cmd.equals(TYPE_STAR))
451                 type_star(service,stub);
452             else if (cmd.equals(SOAP_BODY_PARTS1))
453                 soap_body_parts1(service,stub);
454             else if (cmd.equals(SOAP_BODY_PARTS2))
455                 soap_body_parts2(service,stub);
456             else if (cmd.equals(SOAP_BODY_PARTS3))
457                 soap_body_parts3(service,stub);
458             else if (cmd.equals(SOAP_BODY_PARTS4))
459                 soap_body_parts4(service,stub);
460             else if (cmd.equals(ARRAY_OF_BINARY))
461                 array_of_binary(service,stub);
462             else if (cmd.equals(MAP_TYPE))
463                 map_type(service,stub);
464             else if (cmd.equals(OPTIONAL_SOAP_BODY))
465                 optional_soap_body(service,stub);
466             else if (cmd.equals(MIX_MIME_PARTS))
467                 mix_mime_parts(service,stub);
468             else if (cmd.equals(BAD_NO_PART))
469                 bad_no_part(service);
470             else if (cmd.equals(BAD_PART))
471                 bad_part(service);
472             else if (cmd.equals(BAD_NESTED))
473                 bad_nested(service);
474             else if (cmd.equals(BAD_MIX_SOAP_MIME))
475                 bad_mix_soap_mime(service);
476             else if (cmd.equals(BAD_MULTIPLE_SOAP_BODIES))
477                 bad_multiple_soap_bodies(service);
478             else if (cmd.equals(BAD_SOAP_BODY_TYPE))
479                 bad_soap_body_type(service);
480             else
481                 assertTrue(false);
482
483         } catch (Exception JavaDoc e) {
484             System.err.println(
485                 "MimeTest(" + portName + ") caught exception " + e);
486             e.printStackTrace();
487             assertTrue(false);
488         } finally {
489             WSIFPluggableProviders.overrideDefaultProvider(
490                 "http://schemas.xmlsoap.org/wsdl/soap/",
491                 null);
492                 
493             WSIFFrame.close();
494         }
495
496     }
497
498     private void send_dh(WSIFService service, Mime stub) throws Exception JavaDoc {
499         DataHandler JavaDoc dh = new DataHandler JavaDoc(new FileDataSource JavaDoc(flatfileLocation));
500         String JavaDoc buff = stub.dataHandlerToString(dh);
501         assertTrue(compareFiles(flatfileLocation, buff));
502     }
503
504     private void receive_dh(WSIFService service, Mime stub) throws Exception JavaDoc {
505         DataHandler JavaDoc dh = stub.stringToDataHandler(rhyme[0]);
506         assertTrue(compareFiles(dh, rhyme[0]));
507     }
508
509     private void send_plaintext(WSIFService service, Mime stub)
510         throws Exception JavaDoc {
511         service.mapType(
512             new QName JavaDoc("http://mime/", "plaintext"),
513             String JavaDoc.class);
514         String JavaDoc buff = stub.plainTextToString(rhyme[0]);
515         assertTrue(rhyme[0].equals(buff));
516     }
517
518     private void receive_plaintext(WSIFService service, Mime stub)
519         throws Exception JavaDoc {
520         service.mapType(
521             new QName JavaDoc("http://mime/", "plaintext"),
522             String JavaDoc.class);
523         String JavaDoc buff = stub.stringToPlainText(rhyme[1]);
524         assertTrue(rhyme[1].equals(buff));
525     }
526
527     private void bounce_image(WSIFService service, Mime stub) throws Exception JavaDoc {
528         service.mapType(
529             new QName JavaDoc("http://mime/", "image"),
530             Image JavaDoc.class);
531
532         // This blocks until the image is loaded.
533
Image JavaDoc im1 = new ImageIcon JavaDoc(imageLocation).getImage();
534         WSIFFrame.display(im1,"Original image");
535
536         // Could use Image image2 = Toolkit.getDefaultToolkit().getImage("image.gif");
537
// which loads the image in the background.
538

539         Image JavaDoc im2 = stub.bounceImage(im1);
540         WSIFFrame.display(im2,"Bounced image");
541
542         System.out.println("Sleeping");
543         Thread.sleep(SLEEPY_TIME);
544         System.out.println("Woken up");
545     }
546
547     /**
548      * This test does not do any mapTypes() to test that WSIF will
549      * automatically register mime parts as a DataHandler.
550      */

551     private void bounce_image2(WSIFService service, Mime stub, String JavaDoc portName)
552         throws Exception JavaDoc {
553         DataHandler JavaDoc dh1 = new DataHandler JavaDoc(new FileDataSource JavaDoc(imageLocation));
554
555         WSIFPort port = service.getPort(portName);
556         WSIFOperation op = port.createOperation("bounceImage2");
557         WSIFMessage in = op.createInputMessage();
558         WSIFMessage out = op.createOutputMessage();
559         WSIFMessage fault = op.createFaultMessage();
560         in.setObjectPart("file",dh1);
561
562         boolean success = op.executeRequestResponseOperation(in,out,fault);
563         assertTrue(success);
564
565         DataHandler JavaDoc dh2 = (DataHandler JavaDoc)(out.getObjectPart("file2"));
566
567 // This commented out code displays the image, so proving that the
568
// bounced image is correct.
569
//
570
// InputStream is = dh2.getInputStream();
571
// byte[] bBuff = new byte[is.available()];
572
// is.read(bBuff);
573
// Image im = new ImageIcon(bBuff).getImage();
574
// WSIFFrame.display(im,"Image");
575

576         assertTrue(compareFiles(dh1,dh2));
577     }
578
579     private void bounce_image3(WSIFService service, Mime stub, String JavaDoc portName)
580         throws Exception JavaDoc {
581         DataHandler JavaDoc dh1 = new DataHandler JavaDoc(new FileDataSource JavaDoc(imageLocation));
582         DataHandler JavaDoc dh2 = stub.bounceImage2(dh1);
583         assertTrue(compareFiles(dh1,dh2));
584     }
585
586     private void bounce_image4_default(WSIFService service, Mime stub, String JavaDoc portName)
587         throws Exception JavaDoc {
588         DataHandler JavaDoc dh1 = new DataHandler JavaDoc(new FileDataSource JavaDoc(imageLocation));
589         DataHandler JavaDoc dh2 = stub.bounceImage4(true, dh1);
590         assertTrue(compareFiles(dh1,dh2));
591     }
592
593     private void bounce_image4_false(WSIFService service, Mime stub, String JavaDoc portName)
594         throws Exception JavaDoc {
595         DataHandler JavaDoc dh1 = new DataHandler JavaDoc(new FileDataSource JavaDoc(imageLocation));
596         DataHandler JavaDoc dh2 = stub.bounceImage4(false, dh1);
597         assertTrue(dh2==null);
598     }
599
600     private void bounce_image4_null(WSIFService service, Mime stub, String JavaDoc portName)
601         throws Exception JavaDoc {
602         DataHandler JavaDoc dh2 = stub.bounceImage4(true, null);
603         assertTrue(dh2==null);
604     }
605
606     private void send_source(WSIFService service, Mime stub) throws Exception JavaDoc {
607             service.mapType(
608                 new QName JavaDoc("http://mime/", "source"),
609                 Source JavaDoc.class);
610 // String buff = stub.plainTextToString(rhyme[0]);
611
// assertTrue(rhyme[0].equals(buff));
612
assertTrue(false);
613     }
614
615     private void receive_source(WSIFService service, Mime stub) throws Exception JavaDoc {
616             service.mapType(
617                 new QName JavaDoc("http://mime/", "source"),
618                 Source JavaDoc.class);
619 // String buff = stub.stringToPlainText(rhyme[1]);
620
// assertTrue(rhyme[1].equals(buff));
621
assertTrue(false);
622     }
623
624     private void send_mimemultipart(WSIFService service, Mime stub) throws Exception JavaDoc {
625             service.mapType(
626                 new QName JavaDoc("http://mime/", "mimemultipart"),
627                 MimeMultipart JavaDoc.class);
628 // String buff = stub.plainTextToString(rhyme[0]);
629
// assertTrue(rhyme[0].equals(buff));
630
assertTrue(false);
631     }
632
633     private void receive_mimemultipart(WSIFService service, Mime stub) throws Exception JavaDoc {
634             service.mapType(
635                 new QName JavaDoc("http://mime/", "mimemultipart"),
636                 MimeMultipart JavaDoc.class);
637 // String buff = stub.stringToPlainText(rhyme[1]);
638
// assertTrue(rhyme[1].equals(buff));
639
assertTrue(false);
640     }
641
642     private void or_multiparts1(WSIFService service, Mime stub) throws Exception JavaDoc {
643         DataHandler JavaDoc dh = new DataHandler JavaDoc(new FileDataSource JavaDoc(flatfileLocation));
644         String JavaDoc buff = stub.orMultiMimeParts(dh);
645         assertTrue("text/plain".equals(buff));
646     }
647
648     private void or_multiparts2(WSIFService service, Mime stub) throws Exception JavaDoc {
649         DataHandler JavaDoc dh = new DataHandler JavaDoc(new FileDataSource JavaDoc(imageLocation));
650         String JavaDoc buff = stub.orMultiMimeParts(dh);
651         assertTrue("image/jpeg".equals(buff));
652     }
653
654     private void and_multiparts(WSIFService service, Mime stub) throws Exception JavaDoc {
655         DataHandler JavaDoc dh1 = new DataHandler JavaDoc(new FileDataSource JavaDoc(flatfileLocation));
656         DataHandler JavaDoc dh2 = new DataHandler JavaDoc(new FileDataSource JavaDoc(flatfileLocation2));
657         String JavaDoc buff = stub.andMultiMimeParts(dh1,dh2);
658         assertTrue(concat(flatfileLocation,flatfileLocation2).equals(buff));
659     }
660
661     private void multi_out_parts(WSIFService service, Mime stub) throws Exception JavaDoc {
662         assertTrue(false);
663     }
664
665     private void multi_inout_parts(WSIFService service, Mime stub) throws Exception JavaDoc {
666         assertTrue(false);
667     }
668
669     private void no_content(WSIFService service, Mime stub) throws Exception JavaDoc {
670         assertTrue(rhyme[0].equals(stub.noContent(rhyme[0])));
671     }
672
673     private void type_star(WSIFService service, Mime stub) throws Exception JavaDoc {
674         DataHandler JavaDoc dh = new DataHandler JavaDoc(new FileDataSource JavaDoc(flatfileLocation));
675         String JavaDoc buff = stub.dataHandlerToString(dh);
676         assertTrue(compareFiles(flatfileLocation, buff));
677     }
678
679     /**
680      * These next tests have to be run stubless because they test the parts="..."
681      * in the soap:body and the WSIFClientProxy doesn't look in the binding
682      * and so would not be able to find this signature in the portType.
683      */

684     private void soap_body_parts1(WSIFService service, Mime stub)
685         throws Exception JavaDoc {
686         DataHandler JavaDoc dh = new DataHandler JavaDoc(new FileDataSource JavaDoc(flatfileLocation));
687         WSIFPort port = service.getPort(portName);
688         WSIFOperation op = port.createOperation("soapBodyParts1");
689         WSIFMessage in = op.createInputMessage();
690         WSIFMessage out = op.createOutputMessage();
691         WSIFMessage fault = op.createFaultMessage();
692         in.setBooleanPart("shouldBounce",true);
693         in.setObjectPart("file",dh);
694
695         boolean success = op.executeRequestResponseOperation(in,out,fault);
696         assertTrue(success);
697
698         String JavaDoc s = (String JavaDoc)(out.getObjectPart("buff"));
699         assertTrue("1".equals(s));
700     }
701
702     private void soap_body_parts2(WSIFService service, Mime stub)
703         throws Exception JavaDoc {
704         DataHandler JavaDoc dh = new DataHandler JavaDoc(new FileDataSource JavaDoc(flatfileLocation));
705         WSIFPort port = service.getPort(portName);
706         WSIFOperation op = port.createOperation("soapBodyParts2");
707         WSIFMessage in = op.createInputMessage();
708         WSIFMessage out = op.createOutputMessage();
709         WSIFMessage fault = op.createFaultMessage();
710         in.setObjectPart("file",dh);
711
712         boolean success = op.executeRequestResponseOperation(in,out,fault);
713         assertTrue(success);
714
715         String JavaDoc s = (String JavaDoc)(out.getObjectPart("buff"));
716         assertTrue("2".equals(s));
717     }
718
719     private void soap_body_parts3(WSIFService service, Mime stub)
720         throws Exception JavaDoc {
721         WSIFPort port = service.getPort(portName);
722         WSIFOperation op = port.createOperation("soapBodyParts3");
723         WSIFMessage in = op.createInputMessage();
724         WSIFMessage out = op.createOutputMessage();
725         WSIFMessage fault = op.createFaultMessage();
726         in.setBooleanPart("shouldBounce",true);
727
728         boolean success = op.executeRequestResponseOperation(in,out,fault);
729         assertTrue(success);
730
731         String JavaDoc s = (String JavaDoc)(out.getObjectPart("buff"));
732         assertTrue("3".equals(s));
733     }
734
735     private void soap_body_parts4(WSIFService service, Mime stub)
736         throws Exception JavaDoc {
737         WSIFPort port = service.getPort(portName);
738         WSIFOperation op = port.createOperation("soapBodyParts4");
739         WSIFMessage in = op.createInputMessage();
740         WSIFMessage out = op.createOutputMessage();
741         WSIFMessage fault = op.createFaultMessage();
742
743         boolean success = op.executeRequestResponseOperation(in,out,fault);
744         assertTrue(success);
745
746         String JavaDoc s = (String JavaDoc)(out.getObjectPart("buff"));
747         assertTrue("4".equals(s));
748     }
749
750     private void array_of_binary(WSIFService service, Mime stub) throws Exception JavaDoc {
751         DataHandler JavaDoc dh = new DataHandler JavaDoc(new FileDataSource JavaDoc(flatfileLocation));
752         String JavaDoc buff = stub.arrayOfBinary(dh);
753         assertTrue(compareFiles(flatfileLocation, buff));
754     }
755
756     private void map_type(WSIFService service, Mime stub) throws Exception JavaDoc {
757         service.mapType(
758             new QName JavaDoc("http://mime/", "datahandler"),
759             DataHandler JavaDoc.class);
760
761         DataHandler JavaDoc dh = new DataHandler JavaDoc(new FileDataSource JavaDoc(flatfileLocation));
762         String JavaDoc buff = stub.dataHandlerToString(dh);
763         assertTrue(compareFiles(flatfileLocation, buff));
764     }
765
766     private void optional_soap_body(WSIFService service, Mime stub)
767         throws Exception JavaDoc {
768         DataHandler JavaDoc dh1 = new DataHandler JavaDoc(new FileDataSource JavaDoc(imageLocation));
769         DataHandler JavaDoc dh2 = stub.optionalSoapBody(dh1);
770         assertTrue(compareFiles(dh1,dh2));
771     }
772
773     private void mix_mime_parts(WSIFService service, Mime stub)
774         throws Exception JavaDoc
775     {
776         DataHandler JavaDoc dh1 = new DataHandler JavaDoc(new FileDataSource JavaDoc(flatfileLocation));
777         DataHandler JavaDoc dh2 =
778             new DataHandler JavaDoc(new FileDataSource JavaDoc(flatfileLocation2));
779         String JavaDoc position1 = "Position One";
780         String JavaDoc position2 = "Position Two";
781         String JavaDoc position3 = "Position Three";
782
783         WSIFPort port = service.getPort(portName);
784         WSIFOperation op = port.createOperation("mixMimeParts");
785         WSIFMessage in = op.createInputMessage();
786         WSIFMessage out = op.createOutputMessage();
787         WSIFMessage fault = op.createFaultMessage();
788         in.setObjectPart("position1", position1);
789         in.setObjectPart("file1", dh1);
790         in.setObjectPart("position2", position2);
791         in.setObjectPart("file2", dh2);
792         in.setObjectPart("position3", position3);
793
794         boolean success = op.executeRequestResponseOperation(in, out, fault);
795         assertTrue(success);
796
797         String JavaDoc s = (String JavaDoc) (out.getObjectPart("buff"));
798         String JavaDoc expected =
799             position1 + readFile(dh1) + position2 + readFile(dh2) + position3;
800         assertTrue(expected.equals(s));
801     }
802
803     /* *********************** ERRORS ********************************** */
804     
805     private void bad_no_part(WSIFService service) throws Exception JavaDoc {
806         DataHandler JavaDoc dh = new DataHandler JavaDoc(new FileDataSource JavaDoc(flatfileLocation));
807         boolean exceptionCaught = false;
808         try {
809             Mime stub = (Mime) service.getStub(portName, Mime.class);
810             String JavaDoc buff = stub.badNoPart(dh);
811         } catch (WSIFException we) {
812             exceptionCaught = true;
813             System.out.println("Expected exception="+we);
814             we.printStackTrace();
815         }
816         assertTrue(exceptionCaught);
817     }
818
819     private void bad_part(WSIFService service) throws Exception JavaDoc {
820         DataHandler JavaDoc dh = new DataHandler JavaDoc(new FileDataSource JavaDoc(flatfileLocation));
821         boolean exceptionCaught = false;
822         try {
823             Mime stub = (Mime) service.getStub(portName, Mime.class);
824             String JavaDoc buff = stub.badPart(dh);
825         } catch (WSIFException we) {
826             exceptionCaught = true;
827             System.out.println("Expected exception="+we);
828             we.printStackTrace();
829         }
830         assertTrue(exceptionCaught);
831     }
832
833     private void bad_nested(WSIFService service) throws Exception JavaDoc {
834         DataHandler JavaDoc dh = new DataHandler JavaDoc(new FileDataSource JavaDoc(flatfileLocation));
835         boolean exceptionCaught = false;
836         try {
837             Mime stub = (Mime) service.getStub(portName, Mime.class);
838             stub.badNested(dh);
839         } catch (WSIFException we) {
840             exceptionCaught = true;
841             System.out.println("Expected exception="+we);
842             we.printStackTrace();
843         }
844         assertTrue(exceptionCaught);
845     }
846
847     private void bad_mix_soap_mime(WSIFService service) throws Exception JavaDoc {
848         DataHandler JavaDoc dh = new DataHandler JavaDoc(new FileDataSource JavaDoc(flatfileLocation));
849         boolean exceptionCaught = false;
850         try {
851             Mime stub = (Mime) service.getStub(portName, Mime.class);
852             String JavaDoc buff = stub.badMixSoapMime(dh);
853         } catch (WSIFException we) {
854             exceptionCaught = true;
855             System.out.println("Expected exception="+we);
856             we.printStackTrace();
857         }
858         assertTrue(exceptionCaught);
859     }
860
861     private void bad_multiple_soap_bodies(WSIFService service) throws Exception JavaDoc {
862         DataHandler JavaDoc dh = new DataHandler JavaDoc(new FileDataSource JavaDoc(flatfileLocation));
863         boolean exceptionCaught = false;
864         try {
865             Mime stub = (Mime) service.getStub(portName, Mime.class);
866             String JavaDoc buff = stub.badMultipleSoapBodies(dh);
867         } catch (WSIFException we) {
868             exceptionCaught = true;
869             System.out.println("Expected exception="+we);
870             we.printStackTrace();
871         }
872         assertTrue(exceptionCaught);
873     }
874
875     private void bad_soap_body_type(WSIFService service) throws Exception JavaDoc {
876         DataHandler JavaDoc dh = new DataHandler JavaDoc(new FileDataSource JavaDoc(flatfileLocation));
877         boolean exceptionCaught = false;
878         try {
879             Mime stub = (Mime) service.getStub(portName, Mime.class);
880             String JavaDoc buff = stub.badSoapBodyType(dh);
881         } catch (WSIFException we) {
882             exceptionCaught = true;
883             System.out.println("Expected exception="+we);
884             we.printStackTrace();
885         }
886         assertTrue(exceptionCaught);
887     }
888
889     /* ******************* UTILITIES ************************************* */
890     
891     private boolean compareFiles(DataHandler JavaDoc dh1, DataHandler JavaDoc dh2)
892         throws FileNotFoundException JavaDoc, IOException JavaDoc {
893         InputStream JavaDoc is1 = dh1.getInputStream();
894         InputStream JavaDoc is2 = dh2.getInputStream();
895         boolean success = false;
896         try {
897             success = compareFiles(is1, is2);
898         } finally {
899             if (null != is1)
900                 is1.close();
901             if (null != is2)
902                 is2.close();
903         }
904         return success;
905     }
906
907     private boolean compareFiles(DataHandler JavaDoc dh, String JavaDoc buff)
908         throws FileNotFoundException JavaDoc, IOException JavaDoc {
909         InputStream JavaDoc is = dh.getInputStream();
910         boolean success = false;
911         try {
912             success = compareFiles(is, buff);
913         } finally {
914             if (null != is)
915                 is.close();
916         }
917         return success;
918     }
919
920     private boolean compareFiles(String JavaDoc one, String JavaDoc buff)
921         throws FileNotFoundException JavaDoc, IOException JavaDoc {
922         BufferedInputStream JavaDoc oneStream = null;
923         File JavaDoc f1 = new File JavaDoc(one);
924
925         boolean success = false;
926         try {
927             oneStream =
928                 new BufferedInputStream JavaDoc(
929                     new FileInputStream JavaDoc(one),
930                     buff.length());
931             success = compareFiles(oneStream, buff);
932         } finally {
933             if (null != oneStream)
934                 oneStream.close();
935         }
936         return success;
937     }
938
939     private boolean compareFiles(InputStream JavaDoc is, String JavaDoc buff)
940         throws FileNotFoundException JavaDoc, IOException JavaDoc {
941         return compareFiles(is, new ByteArrayInputStream JavaDoc(buff.getBytes()));
942     }
943     
944     private boolean compareFiles(InputStream JavaDoc is1, InputStream JavaDoc is2)
945         throws FileNotFoundException JavaDoc, IOException JavaDoc {
946
947         int avail1 = is1.available();
948         int avail2 = is2.available();
949         if (avail1 != avail2) return false;
950         if (avail1==0) return true;
951
952         byte[] buff1 = new byte[avail1];
953         byte[] buff2 = new byte[avail2];
954
955         Arrays.fill(buff1, (byte) 0);
956         Arrays.fill(buff2, (byte) 0);
957
958         int bread1 = -1;
959         int bread2 = -1;
960         bread1 = is1.read(buff1, 0, avail1);
961         bread2 = is2.read(buff2, 0, avail2);
962         String JavaDoc s1 = new String JavaDoc(buff1);
963         String JavaDoc s2 = new String JavaDoc(buff2);
964         if (!s1.equals(s2))
965             return false;
966         return true;
967     }
968     
969     private String JavaDoc concat(String JavaDoc one, String JavaDoc two)
970         throws FileNotFoundException JavaDoc, IOException JavaDoc {
971         InputStream JavaDoc is1 = null;
972         InputStream JavaDoc is2 = null;
973         try {
974             is1 = (new DataHandler JavaDoc(new FileDataSource JavaDoc(one))).getInputStream();
975             is2 = (new DataHandler JavaDoc(new FileDataSource JavaDoc(two))).getInputStream();
976             int avail1 = is1.available();
977             int avail2 = is2.available();
978
979             byte[] buff1 = new byte[avail1];
980             byte[] buff2 = new byte[avail2];
981
982             Arrays.fill(buff1, (byte) 0);
983             Arrays.fill(buff2, (byte) 0);
984
985             int bread1 = -1;
986             int bread2 = -1;
987             bread1 = is1.read(buff1, 0, avail1);
988             bread2 = is2.read(buff2, 0, avail2);
989             String JavaDoc s1 = new String JavaDoc(buff1);
990             String JavaDoc s2 = new String JavaDoc(buff2);
991             return s1 + s2;
992
993         } finally {
994             if (null != is1)
995                 is1.close();
996             if (null != is2)
997                 is2.close();
998         }
999     }
1000
1001    private String JavaDoc readFile(DataHandler JavaDoc dh) {
1002        try {
1003            InputStream JavaDoc is = dh.getInputStream();
1004            byte[] bBuff = new byte[is.available()];
1005            is.read(bBuff);
1006            String JavaDoc sBuff = new String JavaDoc(bBuff);
1007            return sBuff;
1008        } catch (IOException JavaDoc ioe) {
1009            ioe.printStackTrace();
1010            return "";
1011        }
1012    }
1013}
1014
Popular Tags