FileDocCategorySizeDatePackage
TestAttachment.javaAPI DocApache Axis 1.44993Sat Apr 22 18:57:28 BST 2006test.saaj

TestAttachment

public class TestAttachment extends TestCase

Fields Summary
Constructors Summary
public TestAttachment(String name)

        super(name);
    
Methods Summary
public static voidmain(java.lang.String[] args)

        test.saaj.TestAttachment tester = new test.saaj.TestAttachment("TestSAAJ");
        tester.testMultipleAttachments();
        tester.testStringAttachment();
        tester.testBadAttSize();
    
public voidtestBadAttSize()

        MessageFactory factory = MessageFactory.newInstance();
        SOAPMessage message = factory.createMessage();

        ByteArrayInputStream ins=new ByteArrayInputStream(new byte[5]);
        DataHandler dh=new DataHandler(new Src(ins,"text/plain"));
        AttachmentPart part = message.createAttachmentPart(dh);
        assertEquals("Size should match",5,part.getSize());
    
public voidtestMultipleAttachments()

        SOAPConnectionFactory scFactory = SOAPConnectionFactory.newInstance();
        SOAPConnection con = scFactory.createConnection();

        MessageFactory factory = MessageFactory.newInstance();
        SOAPMessage msg = factory.createMessage();
        java.net.URL url1 = new java.net.URL("http://slashdot.org/slashdot.xml");
        java.net.URL url2 = new java.net.URL("http://www.apache.org/LICENSE.txt");

        AttachmentPart a1 = msg.createAttachmentPart(new javax.activation.DataHandler(url1));
        a1.setContentType("text/xml");
        msg.addAttachmentPart(a1);
        AttachmentPart a2 = msg.createAttachmentPart(new javax.activation.DataHandler(url1));
        a2.setContentType("text/xml");
        msg.addAttachmentPart(a2);
        AttachmentPart a3 = msg.createAttachmentPart(new javax.activation.DataHandler(url2));
        a3.setContentType("text/plain");
        msg.addAttachmentPart(a3);

        assertTrue(msg.countAttachments()==3);

        javax.xml.soap.MimeHeaders mimeHeaders = new javax.xml.soap.MimeHeaders();
        mimeHeaders.addHeader("Content-Type", "text/xml");

        int nAttachments = 0;
        java.util.Iterator iterator = msg.getAttachments(mimeHeaders);
	    while (iterator.hasNext()) {
            nAttachments++;
	        AttachmentPart ap = (AttachmentPart)iterator.next();
	        assertTrue(ap.equals(a1) || ap.equals(a2));
	    }
        assertTrue(nAttachments==2);
    
public voidtestStringAttachment()

        SOAPConnectionFactory scFactory = SOAPConnectionFactory.newInstance();
        SOAPConnection con = scFactory.createConnection();

        MessageFactory factory = MessageFactory.newInstance();
        SOAPMessage message = factory.createMessage();
        AttachmentPart attachment = message.createAttachmentPart();
        String stringContent = "Update address for Sunny Skies " +
                "Inc., to 10 Upbeat Street, Pleasant Grove, CA 95439";

        attachment.setContent(stringContent, "text/plain");
        attachment.setContentId("update_address");
        message.addAttachmentPart(attachment);

        assertTrue(message.countAttachments()==1);

        java.util.Iterator it = message.getAttachments();
        while (it.hasNext()) {
            attachment = (AttachmentPart) it.next();
            Object content = attachment.getContent();
            String id = attachment.getContentId();
            System.out.println("Attachment " + id + " contains: " + content);
            assertEquals(content,stringContent);
        }
        System.out.println("Here is what the XML message looks like:");
        message.writeTo(System.out);

        message.removeAllAttachments();
        assertTrue(message.countAttachments()==0);