1 57 58 package mime; 59 60 import java.awt.Image ; 61 import java.io.BufferedInputStream ; 62 import java.io.ByteArrayInputStream ; 63 import java.io.File ; 64 import java.io.FileInputStream ; 65 import java.io.FileNotFoundException ; 66 import java.io.IOException ; 67 import java.io.InputStream ; 68 import java.util.Arrays ; 69 70 import javax.activation.DataHandler ; 71 import javax.activation.FileDataSource ; 72 import javax.mail.internet.MimeMultipart ; 73 import javax.swing.ImageIcon ; 74 import javax.xml.namespace.QName ; 75 import javax.xml.transform.Source ; 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 108 public class MimeTest extends TestCase { 109 private final static String wsdlPath = 110 TestUtilities.getWsdlPath("java\\test\\mime"); 111 private final static String imageLocation = wsdlPath + "axis.jpg"; 112 private final static String flatfileLocation = wsdlPath + "test.txt"; 113 private final static String flatfileLocation2 = wsdlPath + "test2.txt"; 114 private final static String portName = 115 TestUtilities.getSoapServer().toUpperCase() + "Port"; 116 117 private final static String SEND_DH = "SEND-DH"; 118 private final static String RECEIVE_DH = "RECEIVE-DH"; 119 private final static String SEND_PLAINTEXT = "SEND-PLAINTEXT"; 120 private final static String RECEIVE_PLAINTEXT = "RECEIVE-PLAINTEXT"; 121 private final static String BOUNCE_IMAGE = "BOUNCE-IMAGE"; 122 private final static String BOUNCE_IMAGE2 = "BOUNCE-IMAGE2"; 123 private final static String BOUNCE_IMAGE3 = "BOUNCE-IMAGE3"; 124 private final static String BOUNCE_IMAGE4_DEFAULT = "BOUNCE-IMAGE4-DEFAULT"; 125 private final static String BOUNCE_IMAGE4_FALSE = "BOUNCE-IMAGE4-FALSE"; 126 private final static String BOUNCE_IMAGE4_NULL = "BOUNCE-IMAGE4-NULL"; 127 private final static String SEND_SOURCE = "SEND-SOURCE"; 128 private final static String RECEIVE_SOURCE = "RECEIVE-SOURCE"; 129 private final static String SEND_MIMEMULTIPART = "SEND-MIMEMULTIPART"; 130 private final static String RECEIVE_MIMEMULTIPART = "RECEIVE-MIMEMULTIPART"; 131 private final static String OR_MULTIPARTS1 = "OR-MULTIPARTS1"; 132 private final static String OR_MULTIPARTS2 = "OR-MULTIPARTS2"; 133 private final static String AND_MULTIPARTS = "AND-MULTIPARTS"; 134 private final static String MULTI_OUT_PARTS = "MULTI-OUT-PARTS"; 135 private final static String MULTI_INOUT_PARTS = "MULTI-INOUT-PARTS"; 136 private final static String NO_CONTENT = "NO-CONTENT"; 137 private final static String TYPE_STAR = "TYPE-STAR"; 138 private final static String SOAP_BODY_PARTS1 = "SOAP_BODY_PARTS1"; 139 private final static String SOAP_BODY_PARTS2 = "SOAP_BODY_PARTS2"; 140 private final static String SOAP_BODY_PARTS3 = "SOAP_BODY_PARTS3"; 141 private final static String SOAP_BODY_PARTS4 = "SOAP_BODY_PARTS4"; 142 private final static String ARRAY_OF_BINARY = "ARRAY-OF-BINARY"; 143 private final static String OPTIONAL_SOAP_BODY = "OPTIONAL_SOAP_BODY"; 144 private final static String MIX_MIME_PARTS = "MIX_MIME_PARTS"; 145 private final static String MAP_TYPE = "MAP-TYPE"; 146 private final static String BAD_NO_PART = "BAD-NO-PART"; 147 private final static String BAD_PART = "BAD-PART"; 148 private final static String BAD_NESTED = "BAD-NESTED"; 149 private final static String BAD_MIX_SOAP_MIME = "BAD-MIX-SOAP-MIME"; 150 private final static String BAD_MULTIPLE_SOAP_BODIES = "BAD-MULTIPLE-SOAP-BODIES"; 151 private final static String BAD_SOAP_BODY_TYPE = "BAD-SOAP-BODY-TYPE"; 152 153 156 private int SLEEPY_TIME = 0; 157 158 private final static String [] 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 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 [] args) { 179 junit.textui.TestRunner.run (suite()); 181 } 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 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 241 242 251 252 269 270 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 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 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 385 private void doit(String cmd, String 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 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 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 { 499 DataHandler dh = new DataHandler (new FileDataSource (flatfileLocation)); 500 String buff = stub.dataHandlerToString(dh); 501 assertTrue(compareFiles(flatfileLocation, buff)); 502 } 503 504 private void receive_dh(WSIFService service, Mime stub) throws Exception { 505 DataHandler 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 { 511 service.mapType( 512 new QName ("http://mime/", "plaintext"), 513 String .class); 514 String 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 { 520 service.mapType( 521 new QName ("http://mime/", "plaintext"), 522 String .class); 523 String buff = stub.stringToPlainText(rhyme[1]); 524 assertTrue(rhyme[1].equals(buff)); 525 } 526 527 private void bounce_image(WSIFService service, Mime stub) throws Exception { 528 service.mapType( 529 new QName ("http://mime/", "image"), 530 Image .class); 531 532 Image im1 = new ImageIcon (imageLocation).getImage(); 534 WSIFFrame.display(im1,"Original image"); 535 536 539 Image 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 551 private void bounce_image2(WSIFService service, Mime stub, String portName) 552 throws Exception { 553 DataHandler dh1 = new DataHandler (new FileDataSource (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 dh2 = (DataHandler )(out.getObjectPart("file2")); 566 567 576 assertTrue(compareFiles(dh1,dh2)); 577 } 578 579 private void bounce_image3(WSIFService service, Mime stub, String portName) 580 throws Exception { 581 DataHandler dh1 = new DataHandler (new FileDataSource (imageLocation)); 582 DataHandler dh2 = stub.bounceImage2(dh1); 583 assertTrue(compareFiles(dh1,dh2)); 584 } 585 586 private void bounce_image4_default(WSIFService service, Mime stub, String portName) 587 throws Exception { 588 DataHandler dh1 = new DataHandler (new FileDataSource (imageLocation)); 589 DataHandler dh2 = stub.bounceImage4(true, dh1); 590 assertTrue(compareFiles(dh1,dh2)); 591 } 592 593 private void bounce_image4_false(WSIFService service, Mime stub, String portName) 594 throws Exception { 595 DataHandler dh1 = new DataHandler (new FileDataSource (imageLocation)); 596 DataHandler dh2 = stub.bounceImage4(false, dh1); 597 assertTrue(dh2==null); 598 } 599 600 private void bounce_image4_null(WSIFService service, Mime stub, String portName) 601 throws Exception { 602 DataHandler dh2 = stub.bounceImage4(true, null); 603 assertTrue(dh2==null); 604 } 605 606 private void send_source(WSIFService service, Mime stub) throws Exception { 607 service.mapType( 608 new QName ("http://mime/", "source"), 609 Source .class); 610 assertTrue(false); 613 } 614 615 private void receive_source(WSIFService service, Mime stub) throws Exception { 616 service.mapType( 617 new QName ("http://mime/", "source"), 618 Source .class); 619 assertTrue(false); 622 } 623 624 private void send_mimemultipart(WSIFService service, Mime stub) throws Exception { 625 service.mapType( 626 new QName ("http://mime/", "mimemultipart"), 627 MimeMultipart .class); 628 assertTrue(false); 631 } 632 633 private void receive_mimemultipart(WSIFService service, Mime stub) throws Exception { 634 service.mapType( 635 new QName ("http://mime/", "mimemultipart"), 636 MimeMultipart .class); 637 assertTrue(false); 640 } 641 642 private void or_multiparts1(WSIFService service, Mime stub) throws Exception { 643 DataHandler dh = new DataHandler (new FileDataSource (flatfileLocation)); 644 String buff = stub.orMultiMimeParts(dh); 645 assertTrue("text/plain".equals(buff)); 646 } 647 648 private void or_multiparts2(WSIFService service, Mime stub) throws Exception { 649 DataHandler dh = new DataHandler (new FileDataSource (imageLocation)); 650 String buff = stub.orMultiMimeParts(dh); 651 assertTrue("image/jpeg".equals(buff)); 652 } 653 654 private void and_multiparts(WSIFService service, Mime stub) throws Exception { 655 DataHandler dh1 = new DataHandler (new FileDataSource (flatfileLocation)); 656 DataHandler dh2 = new DataHandler (new FileDataSource (flatfileLocation2)); 657 String 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 { 662 assertTrue(false); 663 } 664 665 private void multi_inout_parts(WSIFService service, Mime stub) throws Exception { 666 assertTrue(false); 667 } 668 669 private void no_content(WSIFService service, Mime stub) throws Exception { 670 assertTrue(rhyme[0].equals(stub.noContent(rhyme[0]))); 671 } 672 673 private void type_star(WSIFService service, Mime stub) throws Exception { 674 DataHandler dh = new DataHandler (new FileDataSource (flatfileLocation)); 675 String buff = stub.dataHandlerToString(dh); 676 assertTrue(compareFiles(flatfileLocation, buff)); 677 } 678 679 684 private void soap_body_parts1(WSIFService service, Mime stub) 685 throws Exception { 686 DataHandler dh = new DataHandler (new FileDataSource (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 s = (String )(out.getObjectPart("buff")); 699 assertTrue("1".equals(s)); 700 } 701 702 private void soap_body_parts2(WSIFService service, Mime stub) 703 throws Exception { 704 DataHandler dh = new DataHandler (new FileDataSource (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 s = (String )(out.getObjectPart("buff")); 716 assertTrue("2".equals(s)); 717 } 718 719 private void soap_body_parts3(WSIFService service, Mime stub) 720 throws Exception { 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 s = (String )(out.getObjectPart("buff")); 732 assertTrue("3".equals(s)); 733 } 734 735 private void soap_body_parts4(WSIFService service, Mime stub) 736 throws Exception { 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 s = (String )(out.getObjectPart("buff")); 747 assertTrue("4".equals(s)); 748 } 749 750 private void array_of_binary(WSIFService service, Mime stub) throws Exception { 751 DataHandler dh = new DataHandler (new FileDataSource (flatfileLocation)); 752 String buff = stub.arrayOfBinary(dh); 753 assertTrue(compareFiles(flatfileLocation, buff)); 754 } 755 756 private void map_type(WSIFService service, Mime stub) throws Exception { 757 service.mapType( 758 new QName ("http://mime/", "datahandler"), 759 DataHandler .class); 760 761 DataHandler dh = new DataHandler (new FileDataSource (flatfileLocation)); 762 String buff = stub.dataHandlerToString(dh); 763 assertTrue(compareFiles(flatfileLocation, buff)); 764 } 765 766 private void optional_soap_body(WSIFService service, Mime stub) 767 throws Exception { 768 DataHandler dh1 = new DataHandler (new FileDataSource (imageLocation)); 769 DataHandler dh2 = stub.optionalSoapBody(dh1); 770 assertTrue(compareFiles(dh1,dh2)); 771 } 772 773 private void mix_mime_parts(WSIFService service, Mime stub) 774 throws Exception 775 { 776 DataHandler dh1 = new DataHandler (new FileDataSource (flatfileLocation)); 777 DataHandler dh2 = 778 new DataHandler (new FileDataSource (flatfileLocation2)); 779 String position1 = "Position One"; 780 String position2 = "Position Two"; 781 String 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 s = (String ) (out.getObjectPart("buff")); 798 String expected = 799 position1 + readFile(dh1) + position2 + readFile(dh2) + position3; 800 assertTrue(expected.equals(s)); 801 } 802 803 804 805 private void bad_no_part(WSIFService service) throws Exception { 806 DataHandler dh = new DataHandler (new FileDataSource (flatfileLocation)); 807 boolean exceptionCaught = false; 808 try { 809 Mime stub = (Mime) service.getStub(portName, Mime.class); 810 String 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 { 820 DataHandler dh = new DataHandler (new FileDataSource (flatfileLocation)); 821 boolean exceptionCaught = false; 822 try { 823 Mime stub = (Mime) service.getStub(portName, Mime.class); 824 String 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 { 834 DataHandler dh = new DataHandler (new FileDataSource (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 { 848 DataHandler dh = new DataHandler (new FileDataSource (flatfileLocation)); 849 boolean exceptionCaught = false; 850 try { 851 Mime stub = (Mime) service.getStub(portName, Mime.class); 852 String 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 { 862 DataHandler dh = new DataHandler (new FileDataSource (flatfileLocation)); 863 boolean exceptionCaught = false; 864 try { 865 Mime stub = (Mime) service.getStub(portName, Mime.class); 866 String 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 { 876 DataHandler dh = new DataHandler (new FileDataSource (flatfileLocation)); 877 boolean exceptionCaught = false; 878 try { 879 Mime stub = (Mime) service.getStub(portName, Mime.class); 880 String 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 890 891 private boolean compareFiles(DataHandler dh1, DataHandler dh2) 892 throws FileNotFoundException , IOException { 893 InputStream is1 = dh1.getInputStream(); 894 InputStream 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 dh, String buff) 908 throws FileNotFoundException , IOException { 909 InputStream 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 one, String buff) 921 throws FileNotFoundException , IOException { 922 BufferedInputStream oneStream = null; 923 File f1 = new File (one); 924 925 boolean success = false; 926 try { 927 oneStream = 928 new BufferedInputStream ( 929 new FileInputStream (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 is, String buff) 940 throws FileNotFoundException , IOException { 941 return compareFiles(is, new ByteArrayInputStream (buff.getBytes())); 942 } 943 944 private boolean compareFiles(InputStream is1, InputStream is2) 945 throws FileNotFoundException , IOException { 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 s1 = new String (buff1); 963 String s2 = new String (buff2); 964 if (!s1.equals(s2)) 965 return false; 966 return true; 967 } 968 969 private String concat(String one, String two) 970 throws FileNotFoundException , IOException { 971 InputStream is1 = null; 972 InputStream is2 = null; 973 try { 974 is1 = (new DataHandler (new FileDataSource (one))).getInputStream(); 975 is2 = (new DataHandler (new FileDataSource (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 s1 = new String (buff1); 990 String s2 = new String (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 readFile(DataHandler dh) { 1002 try { 1003 InputStream is = dh.getInputStream(); 1004 byte[] bBuff = new byte[is.available()]; 1005 is.read(bBuff); 1006 String sBuff = new String (bBuff); 1007 return sBuff; 1008 } catch (IOException ioe) { 1009 ioe.printStackTrace(); 1010 return ""; 1011 } 1012 } 1013} 1014 | Popular Tags |