package org.codehaus.xfire.attachments;

import java.util.Iterator;

import javax.activation.DataHandler;

import org.codehaus.xfire.test.AbstractXFireTest;

public class StreamedAttachmentsTest
    extends AbstractXFireTest
{
    
    public void testText()
        throws Exception
    {
        StreamedAttachments atts = new StreamedAttachments(getResourceAsStream("mimedata"), 
                                                           "multipart/related; type=\"application/xop+xml\"; start=\"<soap.xml@xfire.codehaus.org>\"; start-info=\"null; charset=utf-8\"; boundary=\"----=_Part_4_701508.1145579811786\"");
        Attachment soapMessage = atts.getSoapMessage();
        assertNotNull(soapMessage);
        assertEquals("text/xml", soapMessage.getHeader("Content-Type"));
        DataHandler handler = soapMessage.getDataHandler();
        assertNotNull(handler);
        assertNotNull(handler.getInputStream());
        
        Iterator parts = atts.getParts();
        assertTrue(parts.hasNext());

        Attachment a = (Attachment) parts.next();
        assertEquals("image/jpeg", a.getHeader("Content-Type"));
        assertEquals("xfire_logo.jpg", a.getId());
        
        assertFalse(parts.hasNext());
    }
    
    public void testBoundaryWithoutQuotes()
        throws Exception
    {
        StreamedAttachments atts = new StreamedAttachments(getResourceAsStream("mimedata2"), 
                                                           "start=\"<soap.xml@xfire.codehaus.org>\"; boundary=----=_Part_0_1696092.1145592699395");
        Attachment soapMessage = atts.getSoapMessage();
        assertNotNull(soapMessage);
        DataHandler handler = soapMessage.getDataHandler();
        assertNotNull(handler);
        assertNotNull(handler.getInputStream());
        
        Iterator parts = atts.getParts();
        assertTrue(parts.hasNext());
    
        Attachment a = (Attachment) parts.next();
    
        assertFalse(parts.hasNext());
    }
        
    public void testText2()
        throws Exception
    {
        StreamedAttachments atts = new StreamedAttachments(getResourceAsStream("mimedata2"), 
                                                           "multipart/related; type=\"application/xop+xml\"; start=\"<soap.xml@xfire.codehaus.org>\"; start-info=\"null; charset=utf-8\"; boundary=\"----=_Part_0_1696092.1145592699395\"");
        Attachment soapMessage = atts.getSoapMessage();
        assertNotNull(soapMessage);
        DataHandler handler = soapMessage.getDataHandler();
        assertNotNull(handler);
        assertNotNull(handler.getInputStream());
        
        Iterator parts = atts.getParts();
        assertTrue(parts.hasNext());
    
        Attachment a = (Attachment) parts.next();

        assertFalse(parts.hasNext());
    }
}

@@ -63,16 +63,49 @@
 erator;%0D
+%0Aimport java.io.FileInputStream;%0D
 %0A%0D%0Aimpor
@@ -390,37 +390,89 @@
 chments(
-getResourceAsStream(%22
+new FileInputStream(getTestFile(%22src/test/org/codehaus/xfire/attachments/
 mimedata
@@ -473,16 +473,17 @@
 medata%22)
+)
 , %0D%0A    
@@ -1488,37 +1488,89 @@
 chments(
-getResourceAsStream(%22
+new FileInputStream(getTestFile(%22src/test/org/codehaus/xfire/attachments/
 mimedata
@@ -1564,34 +1564,34 @@
 ents/mimedata2%22)
+)
 ,
- 
 %0D%0A              
@@ -2298,29 +2298,81 @@
 nts(
-getResourceAsStream(%22
+new FileInputStream(getTestFile(%22src/test/org/codehaus/xfire/attachments/
 mime
@@ -2378,18 +2378,18 @@
 edata2%22)
+)
 ,
- 
 %0D%0A      

make test pass under reactor