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

TestAttachmentSerialization

public class TestAttachmentSerialization extends TestCase
Test the attachments load/save sample code.

Fields Summary
static Log
log
public static final String
MIME_MULTIPART_RELATED
public static final String
MIME_APPLICATION_DIME
public static final String
NS_PREFIX
public static final String
NS_URI
Constructors Summary
public TestAttachmentSerialization(String name)


       
        super(name);
    
Methods Summary
public intloadMsgWithAttachments(java.io.InputStream is)

        MimeHeaders headers = new MimeHeaders();
        headers.setHeader("Content-Type", MIME_MULTIPART_RELATED);
        MessageFactory mf = MessageFactory.newInstance();
        SOAPMessage msg = mf.createMessage(headers, is);
        SOAPPart sp = msg.getSOAPPart();
        SOAPEnvelope envelope = sp.getEnvelope();
        assertTrue(sp != null);
        assertTrue(envelope != null);
        return msg.countAttachments();
    
public static voidmain(java.lang.String[] args)

        TestAttachmentSerialization tester = new TestAttachmentSerialization("tester");
        tester.testAttachments();
    
public intsaveMsgWithAttachments(java.io.OutputStream os)


          
        MessageFactory mf = MessageFactory.newInstance();
        SOAPMessage msg = mf.createMessage();

        SOAPPart sp = msg.getSOAPPart();
        SOAPEnvelope envelope = sp.getEnvelope();
        SOAPHeader header = envelope.getHeader();
        SOAPBody body = envelope.getBody();

        SOAPElement el = header.addHeaderElement(envelope.createName("field4", NS_PREFIX, NS_URI));
        SOAPElement el2 = el.addChildElement("field4b", NS_PREFIX);
        SOAPElement el3 = el2.addTextNode("field4value");

        el = body.addBodyElement(envelope.createName("bodyfield3", NS_PREFIX, NS_URI));
        el2 = el.addChildElement("bodyfield3a", NS_PREFIX);
        el2.addTextNode("bodyvalue3a");
        el2 = el.addChildElement("bodyfield3b", NS_PREFIX);
        el2.addTextNode("bodyvalue3b");
        el2 = el.addChildElement("datefield", NS_PREFIX);

        AttachmentPart ap = msg.createAttachmentPart();
        ap.setContent("some attachment text...", "text/plain");
        msg.addAttachmentPart(ap);

        String jpgfilename = "docs/images/axis.jpg";
        File myfile = new File(jpgfilename);
        FileDataSource fds = new FileDataSource(myfile);
        DataHandler dh = new DataHandler(fds);
        AttachmentPart ap2 = msg.createAttachmentPart(dh);
        ap2.setContentType("image/jpg");
        msg.addAttachmentPart(ap2);

        // Test for Bug #17664
        if(msg.saveRequired()) {
            msg.saveChanges();
        }
        MimeHeaders headers = msg.getMimeHeaders();
        assertTrue(headers != null);
        String [] contentType = headers.getHeader("Content-Type");  
        assertTrue(contentType != null);
        
        msg.writeTo(os);
        os.flush();
        return msg.countAttachments();
    
public voidtestAttachments()

        try {
            ByteArrayOutputStream bais = new ByteArrayOutputStream();
            int count1 = saveMsgWithAttachments(bais);
            int count2 = loadMsgWithAttachments(new ByteArrayInputStream(bais.toByteArray()));
            assertEquals(count1, count2);
        } catch (Exception e) {
            e.printStackTrace();
            throw new Exception("Fault returned from test: " + e);
        }