0001: /*
0002: * The Apache Software License, Version 1.1
0003: *
0004: *
0005: * Copyright (c) 2002 The Apache Software Foundation. All rights
0006: * reserved.
0007: *
0008: * Redistribution and use in source and binary forms, with or without
0009: * modification, are permitted provided that the following conditions
0010: * are met:
0011: *
0012: * 1. Redistributions of source code must retain the above copyright
0013: * notice, this list of conditions and the following disclaimer.
0014: *
0015: * 2. Redistributions in binary form must reproduce the above copyright
0016: * notice, this list of conditions and the following disclaimer in
0017: * the documentation and/or other materials provided with the
0018: * distribution.
0019: *
0020: * 3. The end-user documentation included with the redistribution,
0021: * if any, must include the following acknowledgment:
0022: * "This product includes software developed by the
0023: * Apache Software Foundation (http://www.apache.org/)."
0024: * Alternately, this acknowledgment may appear in the software itself,
0025: * if and wherever such third-party acknowledgments normally appear.
0026: *
0027: * 4. The names "WSIF" and "Apache Software Foundation" must
0028: * not be used to endorse or promote products derived from this
0029: * software without prior written permission. For written
0030: * permission, please contact apache@apache.org.
0031: *
0032: * 5. Products derived from this software may not be called "Apache",
0033: * nor may "Apache" appear in their name, without prior written
0034: * permission of the Apache Software Foundation.
0035: *
0036: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
0037: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
0038: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
0039: * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
0040: * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
0041: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
0042: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
0043: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
0044: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
0045: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
0046: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
0047: * SUCH DAMAGE.
0048: * ====================================================================
0049: *
0050: * This software consists of voluntary contributions made by many
0051: * individuals on behalf of the Apache Software Foundation and was
0052: * originally based on software copyright (c) 2001, 2002, International
0053: * Business Machines, Inc., http://www.apache.org. For more
0054: * information on the Apache Software Foundation, please see
0055: * <http://www.apache.org/>.
0056: */
0057:
0058: package mime;
0059:
0060: import java.awt.Image;
0061: import java.io.BufferedInputStream;
0062: import java.io.ByteArrayInputStream;
0063: import java.io.File;
0064: import java.io.FileInputStream;
0065: import java.io.FileNotFoundException;
0066: import java.io.IOException;
0067: import java.io.InputStream;
0068: import java.util.Arrays;
0069:
0070: import javax.activation.DataHandler;
0071: import javax.activation.FileDataSource;
0072: import javax.mail.internet.MimeMultipart;
0073: import javax.swing.ImageIcon;
0074: import javax.xml.namespace.QName;
0075: import javax.xml.transform.Source;
0076: import junit.framework.Test;
0077: import junit.framework.TestCase;
0078: import junit.framework.TestSuite;
0079:
0080: import org.apache.wsif.WSIFException;
0081: import org.apache.wsif.WSIFMessage;
0082: import org.apache.wsif.WSIFOperation;
0083: import org.apache.wsif.WSIFPort;
0084: import org.apache.wsif.WSIFService;
0085: import org.apache.wsif.WSIFServiceFactory;
0086: import org.apache.wsif.util.WSIFPluggableProviders;
0087: import util.TestUtilities;
0088:
0089: /**
0090: * Junit test for Mime attachments
0091: *
0092: * There should be tests here for...
0093: * DataHandler, DataSource and other datatypes (not sure of the list)
0094: * Sync and async
0095: * rpc-style and doc-style
0096: * Other mime types
0097: * Axis and Soap 2.3
0098: * Soap Http and Jms
0099: * Does this function make sense for NativeJms, Java, EJB, etc?
0100: * Sending attachments and receiving them
0101: * Send or receive a null attachment
0102: * Sending or receiving an object that extends DataHandler, DataSource, etc
0103: * Multiple mime parts, multiple soap bodies, or no soap body at all.
0104: * Is the Mime type that is specified in the WSDL passed into Axis?
0105: *
0106: * @author Mark Whitlock
0107: */
0108: public class MimeTest extends TestCase {
0109: private final static String wsdlPath = TestUtilities
0110: .getWsdlPath("java\\test\\mime");
0111: private final static String imageLocation = wsdlPath + "axis.jpg";
0112: private final static String flatfileLocation = wsdlPath
0113: + "test.txt";
0114: private final static String flatfileLocation2 = wsdlPath
0115: + "test2.txt";
0116: private final static String portName = TestUtilities
0117: .getSoapServer().toUpperCase()
0118: + "Port";
0119:
0120: private final static String SEND_DH = "SEND-DH";
0121: private final static String RECEIVE_DH = "RECEIVE-DH";
0122: private final static String SEND_PLAINTEXT = "SEND-PLAINTEXT";
0123: private final static String RECEIVE_PLAINTEXT = "RECEIVE-PLAINTEXT";
0124: private final static String BOUNCE_IMAGE = "BOUNCE-IMAGE";
0125: private final static String BOUNCE_IMAGE2 = "BOUNCE-IMAGE2";
0126: private final static String BOUNCE_IMAGE3 = "BOUNCE-IMAGE3";
0127: private final static String BOUNCE_IMAGE4_DEFAULT = "BOUNCE-IMAGE4-DEFAULT";
0128: private final static String BOUNCE_IMAGE4_FALSE = "BOUNCE-IMAGE4-FALSE";
0129: private final static String BOUNCE_IMAGE4_NULL = "BOUNCE-IMAGE4-NULL";
0130: private final static String SEND_SOURCE = "SEND-SOURCE";
0131: private final static String RECEIVE_SOURCE = "RECEIVE-SOURCE";
0132: private final static String SEND_MIMEMULTIPART = "SEND-MIMEMULTIPART";
0133: private final static String RECEIVE_MIMEMULTIPART = "RECEIVE-MIMEMULTIPART";
0134: private final static String OR_MULTIPARTS1 = "OR-MULTIPARTS1";
0135: private final static String OR_MULTIPARTS2 = "OR-MULTIPARTS2";
0136: private final static String AND_MULTIPARTS = "AND-MULTIPARTS";
0137: private final static String MULTI_OUT_PARTS = "MULTI-OUT-PARTS";
0138: private final static String MULTI_INOUT_PARTS = "MULTI-INOUT-PARTS";
0139: private final static String NO_CONTENT = "NO-CONTENT";
0140: private final static String TYPE_STAR = "TYPE-STAR";
0141: private final static String SOAP_BODY_PARTS1 = "SOAP_BODY_PARTS1";
0142: private final static String SOAP_BODY_PARTS2 = "SOAP_BODY_PARTS2";
0143: private final static String SOAP_BODY_PARTS3 = "SOAP_BODY_PARTS3";
0144: private final static String SOAP_BODY_PARTS4 = "SOAP_BODY_PARTS4";
0145: private final static String ARRAY_OF_BINARY = "ARRAY-OF-BINARY";
0146: private final static String OPTIONAL_SOAP_BODY = "OPTIONAL_SOAP_BODY";
0147: private final static String MIX_MIME_PARTS = "MIX_MIME_PARTS";
0148: private final static String MAP_TYPE = "MAP-TYPE";
0149: private final static String BAD_NO_PART = "BAD-NO-PART";
0150: private final static String BAD_PART = "BAD-PART";
0151: private final static String BAD_NESTED = "BAD-NESTED";
0152: private final static String BAD_MIX_SOAP_MIME = "BAD-MIX-SOAP-MIME";
0153: private final static String BAD_MULTIPLE_SOAP_BODIES = "BAD-MULTIPLE-SOAP-BODIES";
0154: private final static String BAD_SOAP_BODY_TYPE = "BAD-SOAP-BODY-TYPE";
0155:
0156: /**
0157: * Milliseconds to sleep while the user looks at the displayed images.
0158: */
0159: private int SLEEPY_TIME = 0;
0160:
0161: private final static String[] rhyme = {
0162: "The owl and the pussy cat went to sea in a beautiful pea-green boat,",
0163: "They took some honey and plenty of money wrapped up in a five pound note" };
0164:
0165: private final boolean SYNC = true;
0166: private final boolean ASYNC = false;
0167:
0168: public MimeTest(String name) {
0169: super (name);
0170:
0171: try {
0172: SLEEPY_TIME = Integer.parseInt(TestUtilities
0173: .getWsifProperty("wsif.displaytime"));
0174: } finally {
0175: if (SLEEPY_TIME <= 0)
0176: SLEEPY_TIME = 2000;
0177: }
0178: }
0179:
0180: public static void main(String[] args) {
0181: // TestUtilities.startListeners(TestUtilities.ADDRESSBOOK_LISTENER);
0182: junit.textui.TestRunner.run(suite());
0183: // TestUtilities.stopListeners();
0184: }
0185:
0186: public static Test suite() {
0187: return new TestSuite(MimeTest.class);
0188: }
0189:
0190: public void setUp() {
0191: TestUtilities.setUpExtensionsAndProviders();
0192: }
0193:
0194: public void testSendHandler() {
0195: doit(SEND_DH, "Mime.wsdl");
0196: }
0197:
0198: public void testReceiveHandler() {
0199: doit(RECEIVE_DH, "Mime.wsdl");
0200: }
0201:
0202: /*
0203: * Attachments that are Strings, Images, are not supported at present.
0204: * Attachments can only be DataHandlers.
0205: *
0206: * public void testSendPlainText() {
0207: * doit(SEND_PLAINTEXT, "Mime.wsdl");
0208: * }
0209: *
0210: * public void testReceivePlainText() {
0211: * doit(RECEIVE_PLAINTEXT, "Mime.wsdl");
0212: * }
0213: *
0214: * public void testBounceImage() {
0215: * doit(BOUNCE_IMAGE, "Mime.wsdl");
0216: * }
0217: */
0218:
0219: public void testBounceImage2() {
0220: doit(BOUNCE_IMAGE2, "Mime.wsdl");
0221: }
0222:
0223: public void testBounceImage3() {
0224: doit(BOUNCE_IMAGE3, "Mime.wsdl");
0225: }
0226:
0227: public void testBounceImage4Default() {
0228: doit(BOUNCE_IMAGE4_DEFAULT, "Mime.wsdl");
0229: }
0230:
0231: public void testBounceImage4False() {
0232: doit(BOUNCE_IMAGE4_FALSE, "Mime.wsdl");
0233: }
0234:
0235: /*
0236: * This next test fails because axis will not allow null
0237: * parameters to be passed in.
0238: *
0239: * public void testBounceImage4Null() {
0240: * doit(BOUNCE_IMAGE4_NULL, "Mime.wsdl");
0241: * }
0242: */
0243:
0244: /*
0245: * bounceImage5 doesn't work because the soap:body (which contains a
0246: * boolean) is within the mime:part. bounceImage4 works because the
0247: * soap:body is a direct child of the binding input. When I try to
0248: * run bounceImage5, WSDL4J says the soap:body doesn't contain any
0249: * parts. But having the soap:body inside the mime:part is valid WSDL
0250: * (according to the wsdl4j PopulatedExtensionRegistry). So what does
0251: * it mean?
0252: */
0253:
0254: /*
0255: * public void testSendSource() {
0256: * doit(SEND_SOURCE, "Mime.wsdl");
0257: * }
0258: *
0259: * public void testReceiveSource() {
0260: * doit(RECEIVE_SOURCE, "Mime.wsdl");
0261: * }
0262: *
0263: * public void testSendMimeMultipart() {
0264: * doit(SEND_MIMEMULTIPART, "Mime.wsdl");
0265: * }
0266: *
0267: * public void testReceiveMimeMultipart() {
0268: * doit(RECEIVE_MIMEMULTIPART, "Mime.wsdl");
0269: * }
0270: */
0271:
0272: /*
0273: * Sending MIME messages using Axis over Jms should be supported
0274: * by WSIF, but currently this does not work. I think the problem
0275: * is that WSIFJmsSender does not set up the necessary headers needed
0276: * for MIME. I don't know whether these headers can go inline in the
0277: * message or whether they will have to go as JMS properties which
0278: * will then get converted into HTTP headers by the JMS2HTTPBridge.
0279: * For more information about the headers please see
0280: * org.apache.axis.transport.http.HTTPSender. For the moment I have
0281: * commented out this Jms test (and omitted other JMS tests like
0282: * the async tests) because it fails.
0283: *
0284: * public void testJmsSend() {
0285: * doit("SOAPJMSPort", SEND, "Mime.wsdl");
0286: * }
0287: */
0288:
0289: public void testOrMultiParts1() {
0290: doit(OR_MULTIPARTS1, "Mime.wsdl");
0291: }
0292:
0293: public void testOrMultiParts2() {
0294: doit(OR_MULTIPARTS2, "Mime.wsdl");
0295: }
0296:
0297: public void testAndMultiParts() {
0298: doit(AND_MULTIPARTS, "Mime.wsdl");
0299: }
0300:
0301: /*
0302: * public void testMultiOutParts() {
0303: * doit(MULTI_OUT_PARTS, "Mime.wsdl");
0304: * }
0305: *
0306: * public void testMultiInoutParts() {
0307: * doit(MULTI_INOUT_PARTS, "Mime.wsdl");
0308: * }
0309: */
0310:
0311: public void testNoContent() {
0312: doit(NO_CONTENT, "Mime.wsdl");
0313: }
0314:
0315: public void testTypeStar() {
0316: doit(TYPE_STAR, "Mime.wsdl");
0317: }
0318:
0319: public void testSoapBodyParts1() {
0320: doit(SOAP_BODY_PARTS1, "Mime.wsdl");
0321: }
0322:
0323: public void testSoapBodyParts2() {
0324: doit(SOAP_BODY_PARTS2, "Mime.wsdl");
0325: }
0326:
0327: public void testSoapBodyParts3() {
0328: doit(SOAP_BODY_PARTS3, "Mime.wsdl");
0329: }
0330:
0331: public void testSoapBodyParts4() {
0332: doit(SOAP_BODY_PARTS4, "Mime.wsdl");
0333: }
0334:
0335: public void testArrayOfBinary() {
0336: doit(ARRAY_OF_BINARY, "Mime.wsdl");
0337: }
0338:
0339: public void testMapType() {
0340: doit(MAP_TYPE, "Mime.wsdl");
0341: }
0342:
0343: public void testOptionalSoapBody() {
0344: doit(OPTIONAL_SOAP_BODY, "Mime.wsdl");
0345: }
0346:
0347: public void testMixMimeParts() {
0348: doit(MIX_MIME_PARTS, "Mime.wsdl");
0349: }
0350:
0351: /* ************ ERRORS **************** */
0352:
0353: public void testBadNoPart() {
0354: doit(BAD_NO_PART, "MimeBadNoPart.wsdl");
0355: }
0356:
0357: public void testBadPart() {
0358: doit(BAD_PART, "MimeBadPart.wsdl");
0359: }
0360:
0361: public void testBadNested() {
0362: doit(BAD_NESTED, "MimeBadNested.wsdl");
0363: }
0364:
0365: public void testBadMixSoapMime() {
0366: doit(BAD_MIX_SOAP_MIME, "MimeBadMixSoapMime.wsdl");
0367: }
0368:
0369: public void testBadMultipleSoapBodies() {
0370: doit(BAD_MULTIPLE_SOAP_BODIES, "MimeBadMultipleSoapBodies.wsdl");
0371: }
0372:
0373: public void testBadSoapBodyType() {
0374: doit(BAD_SOAP_BODY_TYPE, "MimeBadSoapBodyType.wsdl");
0375: }
0376:
0377: /**
0378: * doit should probably do the mapTypes() but unfortunately plain text
0379: * means that Strings are serialized with the JAFDataHandlerSerializer
0380: * and I can't find a way to differentiate between strings that should
0381: * be treated as a mime part and those that should not. Basically if I
0382: * mapTypes(plaintext,String) all Strings get mapped to a DataHandler,
0383: * and soap on the server looks for a web service that takes a
0384: * DataHandler, not a String. So be careful where mapTypes(plaintext,String)
0385: * is done.
0386: */
0387: private void doit(String cmd, String wsdl) {
0388: if (!TestUtilities.areWeTesting("mime"))
0389: return;
0390:
0391: WSIFPluggableProviders
0392: .overrideDefaultProvider(
0393: "http://schemas.xmlsoap.org/wsdl/soap/",
0394: new org.apache.wsif.providers.soap.apacheaxis.WSIFDynamicProvider_ApacheAxis());
0395:
0396: try {
0397: WSIFServiceFactory factory = WSIFServiceFactory
0398: .newInstance();
0399: WSIFService service = factory.getService(wsdlPath + wsdl,
0400: null, null, "http://mime/", "Mime");
0401:
0402: // Many of the error tests are badly behaved and would
0403: // throw an exception here.
0404: Mime stub = null;
0405: if (!cmd.startsWith("BAD"))
0406: stub = (Mime) service.getStub(portName, Mime.class);
0407:
0408: if (cmd.equals(SEND_DH))
0409: send_dh(service, stub);
0410: else if (cmd.equals(RECEIVE_DH))
0411: receive_dh(service, stub);
0412: else if (cmd.equals(SEND_PLAINTEXT))
0413: send_plaintext(service, stub);
0414: else if (cmd.equals(RECEIVE_PLAINTEXT))
0415: receive_plaintext(service, stub);
0416: else if (cmd.equals(BOUNCE_IMAGE))
0417: bounce_image(service, stub);
0418: else if (cmd.equals(BOUNCE_IMAGE2))
0419: bounce_image2(service, stub, portName);
0420: else if (cmd.equals(BOUNCE_IMAGE3))
0421: bounce_image3(service, stub, portName);
0422: else if (cmd.equals(BOUNCE_IMAGE4_DEFAULT))
0423: bounce_image4_default(service, stub, portName);
0424: else if (cmd.equals(BOUNCE_IMAGE4_FALSE))
0425: bounce_image4_false(service, stub, portName);
0426: else if (cmd.equals(BOUNCE_IMAGE4_NULL))
0427: bounce_image4_null(service, stub, portName);
0428: else if (cmd.equals(SEND_SOURCE))
0429: send_source(service, stub);
0430: else if (cmd.equals(RECEIVE_SOURCE))
0431: receive_source(service, stub);
0432: else if (cmd.equals(SEND_MIMEMULTIPART))
0433: send_mimemultipart(service, stub);
0434: else if (cmd.equals(RECEIVE_MIMEMULTIPART))
0435: receive_mimemultipart(service, stub);
0436: else if (cmd.equals(OR_MULTIPARTS1))
0437: or_multiparts1(service, stub);
0438: else if (cmd.equals(OR_MULTIPARTS2))
0439: or_multiparts2(service, stub);
0440: else if (cmd.equals(AND_MULTIPARTS))
0441: and_multiparts(service, stub);
0442: else if (cmd.equals(MULTI_OUT_PARTS))
0443: multi_out_parts(service, stub);
0444: else if (cmd.equals(MULTI_INOUT_PARTS))
0445: multi_inout_parts(service, stub);
0446: else if (cmd.equals(NO_CONTENT))
0447: no_content(service, stub);
0448: else if (cmd.equals(TYPE_STAR))
0449: type_star(service, stub);
0450: else if (cmd.equals(SOAP_BODY_PARTS1))
0451: soap_body_parts1(service, stub);
0452: else if (cmd.equals(SOAP_BODY_PARTS2))
0453: soap_body_parts2(service, stub);
0454: else if (cmd.equals(SOAP_BODY_PARTS3))
0455: soap_body_parts3(service, stub);
0456: else if (cmd.equals(SOAP_BODY_PARTS4))
0457: soap_body_parts4(service, stub);
0458: else if (cmd.equals(ARRAY_OF_BINARY))
0459: array_of_binary(service, stub);
0460: else if (cmd.equals(MAP_TYPE))
0461: map_type(service, stub);
0462: else if (cmd.equals(OPTIONAL_SOAP_BODY))
0463: optional_soap_body(service, stub);
0464: else if (cmd.equals(MIX_MIME_PARTS))
0465: mix_mime_parts(service, stub);
0466: else if (cmd.equals(BAD_NO_PART))
0467: bad_no_part(service);
0468: else if (cmd.equals(BAD_PART))
0469: bad_part(service);
0470: else if (cmd.equals(BAD_NESTED))
0471: bad_nested(service);
0472: else if (cmd.equals(BAD_MIX_SOAP_MIME))
0473: bad_mix_soap_mime(service);
0474: else if (cmd.equals(BAD_MULTIPLE_SOAP_BODIES))
0475: bad_multiple_soap_bodies(service);
0476: else if (cmd.equals(BAD_SOAP_BODY_TYPE))
0477: bad_soap_body_type(service);
0478: else
0479: assertTrue(false);
0480:
0481: } catch (Exception e) {
0482: System.err.println("MimeTest(" + portName
0483: + ") caught exception " + e);
0484: e.printStackTrace();
0485: assertTrue(false);
0486: } finally {
0487: WSIFPluggableProviders.overrideDefaultProvider(
0488: "http://schemas.xmlsoap.org/wsdl/soap/", null);
0489:
0490: WSIFFrame.close();
0491: }
0492:
0493: }
0494:
0495: private void send_dh(WSIFService service, Mime stub)
0496: throws Exception {
0497: DataHandler dh = new DataHandler(new FileDataSource(
0498: flatfileLocation));
0499: String buff = stub.dataHandlerToString(dh);
0500: assertTrue(compareFiles(flatfileLocation, buff));
0501: }
0502:
0503: private void receive_dh(WSIFService service, Mime stub)
0504: throws Exception {
0505: DataHandler dh = stub.stringToDataHandler(rhyme[0]);
0506: assertTrue(compareFiles(dh, rhyme[0]));
0507: }
0508:
0509: private void send_plaintext(WSIFService service, Mime stub)
0510: throws Exception {
0511: service.mapType(new QName("http://mime/", "plaintext"),
0512: String.class);
0513: String buff = stub.plainTextToString(rhyme[0]);
0514: assertTrue(rhyme[0].equals(buff));
0515: }
0516:
0517: private void receive_plaintext(WSIFService service, Mime stub)
0518: throws Exception {
0519: service.mapType(new QName("http://mime/", "plaintext"),
0520: String.class);
0521: String buff = stub.stringToPlainText(rhyme[1]);
0522: assertTrue(rhyme[1].equals(buff));
0523: }
0524:
0525: private void bounce_image(WSIFService service, Mime stub)
0526: throws Exception {
0527: service
0528: .mapType(new QName("http://mime/", "image"),
0529: Image.class);
0530:
0531: // This blocks until the image is loaded.
0532: Image im1 = new ImageIcon(imageLocation).getImage();
0533: WSIFFrame.display(im1, "Original image");
0534:
0535: // Could use Image image2 = Toolkit.getDefaultToolkit().getImage("image.gif");
0536: // which loads the image in the background.
0537:
0538: Image im2 = stub.bounceImage(im1);
0539: WSIFFrame.display(im2, "Bounced image");
0540:
0541: System.out.println("Sleeping");
0542: Thread.sleep(SLEEPY_TIME);
0543: System.out.println("Woken up");
0544: }
0545:
0546: /**
0547: * This test does not do any mapTypes() to test that WSIF will
0548: * automatically register mime parts as a DataHandler.
0549: */
0550: private void bounce_image2(WSIFService service, Mime stub,
0551: String portName) throws Exception {
0552: DataHandler dh1 = new DataHandler(new FileDataSource(
0553: imageLocation));
0554:
0555: WSIFPort port = service.getPort(portName);
0556: WSIFOperation op = port.createOperation("bounceImage2");
0557: WSIFMessage in = op.createInputMessage();
0558: WSIFMessage out = op.createOutputMessage();
0559: WSIFMessage fault = op.createFaultMessage();
0560: in.setObjectPart("file", dh1);
0561:
0562: boolean success = op.executeRequestResponseOperation(in, out,
0563: fault);
0564: assertTrue(success);
0565:
0566: DataHandler dh2 = (DataHandler) (out.getObjectPart("file2"));
0567:
0568: // This commented out code displays the image, so proving that the
0569: // bounced image is correct.
0570: //
0571: // InputStream is = dh2.getInputStream();
0572: // byte[] bBuff = new byte[is.available()];
0573: // is.read(bBuff);
0574: // Image im = new ImageIcon(bBuff).getImage();
0575: // WSIFFrame.display(im,"Image");
0576:
0577: assertTrue(compareFiles(dh1, dh2));
0578: }
0579:
0580: private void bounce_image3(WSIFService service, Mime stub,
0581: String portName) throws Exception {
0582: DataHandler dh1 = new DataHandler(new FileDataSource(
0583: imageLocation));
0584: DataHandler dh2 = stub.bounceImage2(dh1);
0585: assertTrue(compareFiles(dh1, dh2));
0586: }
0587:
0588: private void bounce_image4_default(WSIFService service, Mime stub,
0589: String portName) throws Exception {
0590: DataHandler dh1 = new DataHandler(new FileDataSource(
0591: imageLocation));
0592: DataHandler dh2 = stub.bounceImage4(true, dh1);
0593: assertTrue(compareFiles(dh1, dh2));
0594: }
0595:
0596: private void bounce_image4_false(WSIFService service, Mime stub,
0597: String portName) throws Exception {
0598: DataHandler dh1 = new DataHandler(new FileDataSource(
0599: imageLocation));
0600: DataHandler dh2 = stub.bounceImage4(false, dh1);
0601: assertTrue(dh2 == null);
0602: }
0603:
0604: private void bounce_image4_null(WSIFService service, Mime stub,
0605: String portName) throws Exception {
0606: DataHandler dh2 = stub.bounceImage4(true, null);
0607: assertTrue(dh2 == null);
0608: }
0609:
0610: private void send_source(WSIFService service, Mime stub)
0611: throws Exception {
0612: service.mapType(new QName("http://mime/", "source"),
0613: Source.class);
0614: // String buff = stub.plainTextToString(rhyme[0]);
0615: // assertTrue(rhyme[0].equals(buff));
0616: assertTrue(false);
0617: }
0618:
0619: private void receive_source(WSIFService service, Mime stub)
0620: throws Exception {
0621: service.mapType(new QName("http://mime/", "source"),
0622: Source.class);
0623: // String buff = stub.stringToPlainText(rhyme[1]);
0624: // assertTrue(rhyme[1].equals(buff));
0625: assertTrue(false);
0626: }
0627:
0628: private void send_mimemultipart(WSIFService service, Mime stub)
0629: throws Exception {
0630: service.mapType(new QName("http://mime/", "mimemultipart"),
0631: MimeMultipart.class);
0632: // String buff = stub.plainTextToString(rhyme[0]);
0633: // assertTrue(rhyme[0].equals(buff));
0634: assertTrue(false);
0635: }
0636:
0637: private void receive_mimemultipart(WSIFService service, Mime stub)
0638: throws Exception {
0639: service.mapType(new QName("http://mime/", "mimemultipart"),
0640: MimeMultipart.class);
0641: // String buff = stub.stringToPlainText(rhyme[1]);
0642: // assertTrue(rhyme[1].equals(buff));
0643: assertTrue(false);
0644: }
0645:
0646: private void or_multiparts1(WSIFService service, Mime stub)
0647: throws Exception {
0648: DataHandler dh = new DataHandler(new FileDataSource(
0649: flatfileLocation));
0650: String buff = stub.orMultiMimeParts(dh);
0651: assertTrue("text/plain".equals(buff));
0652: }
0653:
0654: private void or_multiparts2(WSIFService service, Mime stub)
0655: throws Exception {
0656: DataHandler dh = new DataHandler(new FileDataSource(
0657: imageLocation));
0658: String buff = stub.orMultiMimeParts(dh);
0659: assertTrue("image/jpeg".equals(buff));
0660: }
0661:
0662: private void and_multiparts(WSIFService service, Mime stub)
0663: throws Exception {
0664: DataHandler dh1 = new DataHandler(new FileDataSource(
0665: flatfileLocation));
0666: DataHandler dh2 = new DataHandler(new FileDataSource(
0667: flatfileLocation2));
0668: String buff = stub.andMultiMimeParts(dh1, dh2);
0669: assertTrue(concat(flatfileLocation, flatfileLocation2).equals(
0670: buff));
0671: }
0672:
0673: private void multi_out_parts(WSIFService service, Mime stub)
0674: throws Exception {
0675: assertTrue(false);
0676: }
0677:
0678: private void multi_inout_parts(WSIFService service, Mime stub)
0679: throws Exception {
0680: assertTrue(false);
0681: }
0682:
0683: private void no_content(WSIFService service, Mime stub)
0684: throws Exception {
0685: assertTrue(rhyme[0].equals(stub.noContent(rhyme[0])));
0686: }
0687:
0688: private void type_star(WSIFService service, Mime stub)
0689: throws Exception {
0690: DataHandler dh = new DataHandler(new FileDataSource(
0691: flatfileLocation));
0692: String buff = stub.dataHandlerToString(dh);
0693: assertTrue(compareFiles(flatfileLocation, buff));
0694: }
0695:
0696: /**
0697: * These next tests have to be run stubless because they test the parts="..."
0698: * in the soap:body and the WSIFClientProxy doesn't look in the binding
0699: * and so would not be able to find this signature in the portType.
0700: */
0701: private void soap_body_parts1(WSIFService service, Mime stub)
0702: throws Exception {
0703: DataHandler dh = new DataHandler(new FileDataSource(
0704: flatfileLocation));
0705: WSIFPort port = service.getPort(portName);
0706: WSIFOperation op = port.createOperation("soapBodyParts1");
0707: WSIFMessage in = op.createInputMessage();
0708: WSIFMessage out = op.createOutputMessage();
0709: WSIFMessage fault = op.createFaultMessage();
0710: in.setBooleanPart("shouldBounce", true);
0711: in.setObjectPart("file", dh);
0712:
0713: boolean success = op.executeRequestResponseOperation(in, out,
0714: fault);
0715: assertTrue(success);
0716:
0717: String s = (String) (out.getObjectPart("buff"));
0718: assertTrue("1".equals(s));
0719: }
0720:
0721: private void soap_body_parts2(WSIFService service, Mime stub)
0722: throws Exception {
0723: DataHandler dh = new DataHandler(new FileDataSource(
0724: flatfileLocation));
0725: WSIFPort port = service.getPort(portName);
0726: WSIFOperation op = port.createOperation("soapBodyParts2");
0727: WSIFMessage in = op.createInputMessage();
0728: WSIFMessage out = op.createOutputMessage();
0729: WSIFMessage fault = op.createFaultMessage();
0730: in.setObjectPart("file", dh);
0731:
0732: boolean success = op.executeRequestResponseOperation(in, out,
0733: fault);
0734: assertTrue(success);
0735:
0736: String s = (String) (out.getObjectPart("buff"));
0737: assertTrue("2".equals(s));
0738: }
0739:
0740: private void soap_body_parts3(WSIFService service, Mime stub)
0741: throws Exception {
0742: WSIFPort port = service.getPort(portName);
0743: WSIFOperation op = port.createOperation("soapBodyParts3");
0744: WSIFMessage in = op.createInputMessage();
0745: WSIFMessage out = op.createOutputMessage();
0746: WSIFMessage fault = op.createFaultMessage();
0747: in.setBooleanPart("shouldBounce", true);
0748:
0749: boolean success = op.executeRequestResponseOperation(in, out,
0750: fault);
0751: assertTrue(success);
0752:
0753: String s = (String) (out.getObjectPart("buff"));
0754: assertTrue("3".equals(s));
0755: }
0756:
0757: private void soap_body_parts4(WSIFService service, Mime stub)
0758: throws Exception {
0759: WSIFPort port = service.getPort(portName);
0760: WSIFOperation op = port.createOperation("soapBodyParts4");
0761: WSIFMessage in = op.createInputMessage();
0762: WSIFMessage out = op.createOutputMessage();
0763: WSIFMessage fault = op.createFaultMessage();
0764:
0765: boolean success = op.executeRequestResponseOperation(in, out,
0766: fault);
0767: assertTrue(success);
0768:
0769: String s = (String) (out.getObjectPart("buff"));
0770: assertTrue("4".equals(s));
0771: }
0772:
0773: private void array_of_binary(WSIFService service, Mime stub)
0774: throws Exception {
0775: DataHandler dh = new DataHandler(new FileDataSource(
0776: flatfileLocation));
0777: String buff = stub.arrayOfBinary(dh);
0778: assertTrue(compareFiles(flatfileLocation, buff));
0779: }
0780:
0781: private void map_type(WSIFService service, Mime stub)
0782: throws Exception {
0783: service.mapType(new QName("http://mime/", "datahandler"),
0784: DataHandler.class);
0785:
0786: DataHandler dh = new DataHandler(new FileDataSource(
0787: flatfileLocation));
0788: String buff = stub.dataHandlerToString(dh);
0789: assertTrue(compareFiles(flatfileLocation, buff));
0790: }
0791:
0792: private void optional_soap_body(WSIFService service, Mime stub)
0793: throws Exception {
0794: DataHandler dh1 = new DataHandler(new FileDataSource(
0795: imageLocation));
0796: DataHandler dh2 = stub.optionalSoapBody(dh1);
0797: assertTrue(compareFiles(dh1, dh2));
0798: }
0799:
0800: private void mix_mime_parts(WSIFService service, Mime stub)
0801: throws Exception {
0802: DataHandler dh1 = new DataHandler(new FileDataSource(
0803: flatfileLocation));
0804: DataHandler dh2 = new DataHandler(new FileDataSource(
0805: flatfileLocation2));
0806: String position1 = "Position One";
0807: String position2 = "Position Two";
0808: String position3 = "Position Three";
0809:
0810: WSIFPort port = service.getPort(portName);
0811: WSIFOperation op = port.createOperation("mixMimeParts");
0812: WSIFMessage in = op.createInputMessage();
0813: WSIFMessage out = op.createOutputMessage();
0814: WSIFMessage fault = op.createFaultMessage();
0815: in.setObjectPart("position1", position1);
0816: in.setObjectPart("file1", dh1);
0817: in.setObjectPart("position2", position2);
0818: in.setObjectPart("file2", dh2);
0819: in.setObjectPart("position3", position3);
0820:
0821: boolean success = op.executeRequestResponseOperation(in, out,
0822: fault);
0823: assertTrue(success);
0824:
0825: String s = (String) (out.getObjectPart("buff"));
0826: String expected = position1 + readFile(dh1) + position2
0827: + readFile(dh2) + position3;
0828: assertTrue(expected.equals(s));
0829: }
0830:
0831: /* *********************** ERRORS ********************************** */
0832:
0833: private void bad_no_part(WSIFService service) throws Exception {
0834: DataHandler dh = new DataHandler(new FileDataSource(
0835: flatfileLocation));
0836: boolean exceptionCaught = false;
0837: try {
0838: Mime stub = (Mime) service.getStub(portName, Mime.class);
0839: String buff = stub.badNoPart(dh);
0840: } catch (WSIFException we) {
0841: exceptionCaught = true;
0842: System.out.println("Expected exception=" + we);
0843: we.printStackTrace();
0844: }
0845: assertTrue(exceptionCaught);
0846: }
0847:
0848: private void bad_part(WSIFService service) throws Exception {
0849: DataHandler dh = new DataHandler(new FileDataSource(
0850: flatfileLocation));
0851: boolean exceptionCaught = false;
0852: try {
0853: Mime stub = (Mime) service.getStub(portName, Mime.class);
0854: String buff = stub.badPart(dh);
0855: } catch (WSIFException we) {
0856: exceptionCaught = true;
0857: System.out.println("Expected exception=" + we);
0858: we.printStackTrace();
0859: }
0860: assertTrue(exceptionCaught);
0861: }
0862:
0863: private void bad_nested(WSIFService service) throws Exception {
0864: DataHandler dh = new DataHandler(new FileDataSource(
0865: flatfileLocation));
0866: boolean exceptionCaught = false;
0867: try {
0868: Mime stub = (Mime) service.getStub(portName, Mime.class);
0869: stub.badNested(dh);
0870: } catch (WSIFException we) {
0871: exceptionCaught = true;
0872: System.out.println("Expected exception=" + we);
0873: we.printStackTrace();
0874: }
0875: assertTrue(exceptionCaught);
0876: }
0877:
0878: private void bad_mix_soap_mime(WSIFService service)
0879: throws Exception {
0880: DataHandler dh = new DataHandler(new FileDataSource(
0881: flatfileLocation));
0882: boolean exceptionCaught = false;
0883: try {
0884: Mime stub = (Mime) service.getStub(portName, Mime.class);
0885: String buff = stub.badMixSoapMime(dh);
0886: } catch (WSIFException we) {
0887: exceptionCaught = true;
0888: System.out.println("Expected exception=" + we);
0889: we.printStackTrace();
0890: }
0891: assertTrue(exceptionCaught);
0892: }
0893:
0894: private void bad_multiple_soap_bodies(WSIFService service)
0895: throws Exception {
0896: DataHandler dh = new DataHandler(new FileDataSource(
0897: flatfileLocation));
0898: boolean exceptionCaught = false;
0899: try {
0900: Mime stub = (Mime) service.getStub(portName, Mime.class);
0901: String buff = stub.badMultipleSoapBodies(dh);
0902: } catch (WSIFException we) {
0903: exceptionCaught = true;
0904: System.out.println("Expected exception=" + we);
0905: we.printStackTrace();
0906: }
0907: assertTrue(exceptionCaught);
0908: }
0909:
0910: private void bad_soap_body_type(WSIFService service)
0911: throws Exception {
0912: DataHandler dh = new DataHandler(new FileDataSource(
0913: flatfileLocation));
0914: boolean exceptionCaught = false;
0915: try {
0916: Mime stub = (Mime) service.getStub(portName, Mime.class);
0917: String buff = stub.badSoapBodyType(dh);
0918: } catch (WSIFException we) {
0919: exceptionCaught = true;
0920: System.out.println("Expected exception=" + we);
0921: we.printStackTrace();
0922: }
0923: assertTrue(exceptionCaught);
0924: }
0925:
0926: /* ******************* UTILITIES ************************************* */
0927:
0928: private boolean compareFiles(DataHandler dh1, DataHandler dh2)
0929: throws FileNotFoundException, IOException {
0930: InputStream is1 = dh1.getInputStream();
0931: InputStream is2 = dh2.getInputStream();
0932: boolean success = false;
0933: try {
0934: success = compareFiles(is1, is2);
0935: } finally {
0936: if (null != is1)
0937: is1.close();
0938: if (null != is2)
0939: is2.close();
0940: }
0941: return success;
0942: }
0943:
0944: private boolean compareFiles(DataHandler dh, String buff)
0945: throws FileNotFoundException, IOException {
0946: InputStream is = dh.getInputStream();
0947: boolean success = false;
0948: try {
0949: success = compareFiles(is, buff);
0950: } finally {
0951: if (null != is)
0952: is.close();
0953: }
0954: return success;
0955: }
0956:
0957: private boolean compareFiles(String one, String buff)
0958: throws FileNotFoundException, IOException {
0959: BufferedInputStream oneStream = null;
0960: File f1 = new File(one);
0961:
0962: boolean success = false;
0963: try {
0964: oneStream = new BufferedInputStream(
0965: new FileInputStream(one), buff.length());
0966: success = compareFiles(oneStream, buff);
0967: } finally {
0968: if (null != oneStream)
0969: oneStream.close();
0970: }
0971: return success;
0972: }
0973:
0974: private boolean compareFiles(InputStream is, String buff)
0975: throws FileNotFoundException, IOException {
0976: return compareFiles(is, new ByteArrayInputStream(buff
0977: .getBytes()));
0978: }
0979:
0980: private boolean compareFiles(InputStream is1, InputStream is2)
0981: throws FileNotFoundException, IOException {
0982:
0983: int avail1 = is1.available();
0984: int avail2 = is2.available();
0985: if (avail1 != avail2)
0986: return false;
0987: if (avail1 == 0)
0988: return true;
0989:
0990: byte[] buff1 = new byte[avail1];
0991: byte[] buff2 = new byte[avail2];
0992:
0993: Arrays.fill(buff1, (byte) 0);
0994: Arrays.fill(buff2, (byte) 0);
0995:
0996: int bread1 = -1;
0997: int bread2 = -1;
0998: bread1 = is1.read(buff1, 0, avail1);
0999: bread2 = is2.read(buff2, 0, avail2);
1000: String s1 = new String(buff1);
1001: String s2 = new String(buff2);
1002: if (!s1.equals(s2))
1003: return false;
1004: return true;
1005: }
1006:
1007: private String concat(String one, String two)
1008: throws FileNotFoundException, IOException {
1009: InputStream is1 = null;
1010: InputStream is2 = null;
1011: try {
1012: is1 = (new DataHandler(new FileDataSource(one)))
1013: .getInputStream();
1014: is2 = (new DataHandler(new FileDataSource(two)))
1015: .getInputStream();
1016: int avail1 = is1.available();
1017: int avail2 = is2.available();
1018:
1019: byte[] buff1 = new byte[avail1];
1020: byte[] buff2 = new byte[avail2];
1021:
1022: Arrays.fill(buff1, (byte) 0);
1023: Arrays.fill(buff2, (byte) 0);
1024:
1025: int bread1 = -1;
1026: int bread2 = -1;
1027: bread1 = is1.read(buff1, 0, avail1);
1028: bread2 = is2.read(buff2, 0, avail2);
1029: String s1 = new String(buff1);
1030: String s2 = new String(buff2);
1031: return s1 + s2;
1032:
1033: } finally {
1034: if (null != is1)
1035: is1.close();
1036: if (null != is2)
1037: is2.close();
1038: }
1039: }
1040:
1041: private String readFile(DataHandler dh) {
1042: try {
1043: InputStream is = dh.getInputStream();
1044: byte[] bBuff = new byte[is.available()];
1045: is.read(bBuff);
1046: String sBuff = new String(bBuff);
1047: return sBuff;
1048: } catch (IOException ioe) {
1049: ioe.printStackTrace();
1050: return "";
1051: }
1052: }
1053: }
|