FileDocCategorySizeDatePackage
MimeMessageTest.javaAPI DocApache James 2.3.115827Fri Jan 12 12:56:36 GMT 2007org.apache.james.core

MimeMessageTest

public class MimeMessageTest extends TestCase
Test the subject folding issue.

Fields Summary
Constructors Summary
Methods Summary
protected voiddebugMessage(javax.mail.internet.MimeMessage mm)

        System.out.println("-------------------");
        System.out.println(getCleanedMessageSource(mm));
        System.out.println("-------------------");
    
protected java.lang.StringgetCleanedMessageSource(javax.mail.internet.MimeMessage mm)
This test is not usable in different locale environment.

        ByteArrayOutputStream out2;
        out2 = new ByteArrayOutputStream();
        mm.writeTo(out2,new String[] {"Message-ID"});

        String res = out2.toString();

        int p = res.indexOf("\r\n\r\n");
        if (p > 0) {
            String head = res.substring(0,p);
            String[] str = head.split("\r\n");
            Arrays.sort(str);
            StringBuffer outputHead = new StringBuffer();
            for (int i = str.length-1; i >= 0; i--) {
                outputHead.append(str[i]);
                outputHead.append("\r\n");
            }
            outputHead.append(res.substring(p+2));
            res = outputHead.toString();
        }
        
        res = res.replaceAll("----=_Part_\\d_\\d+\\.\\d+","----=_Part_\\0_XXXXXXXXXXX.XXXXXXXXXXX");
        return res;
    
protected javax.mail.internet.MimeMessagegetMessageWithBadReturnPath()

        MimeMessage mmCreated = new MimeMessage(Session.getDefaultInstance(new Properties()));
        mmCreated.setSubject("test");
        mmCreated.setHeader(RFC2822Headers.RETURN_PATH, "<mybadreturn@example.com>");
        mmCreated.setText("test body");
        mmCreated.saveChanges();
        return mmCreated;
    
protected java.lang.StringgetMessageWithBadReturnPathSource()

        return "Subject: test\r\n"
            +"Return-Path: <mybadreturn@example.com>\r\n"
            +"MIME-Version: 1.0\r\n"
            +"Content-Type: text/plain; charset=us-ascii\r\n"
            +"Content-Transfer-Encoding: 7bit\r\n"
            +"\r\n"
            +"test body";
    
protected java.lang.StringgetMissingEncodingAddHeaderExpected()

        return "Subject: test\r\n"
            +"MIME-Version: 1.0\r\n"
            +"Content-Type: text/plain; charset=Cp1252\r\n"
            +"Content-Transfer-Encoding: quoted-printable\r\n"
            +"\r\n"
            +"Test=E0\r\n";
    
protected javax.mail.internet.MimeMessagegetMissingEncodingAddHeaderMessage()

        MimeMessage m = new MimeMessage(Session.getDefaultInstance(new Properties()));
        m.setText("Test\u00E0\r\n");
        m.setSubject("test");
        m.saveChanges();
        return m;
    
protected java.lang.StringgetMissingEncodingAddHeaderSource()

        return "Subject: test\r\n"+
                "\r\n"+
                "Test\u00E0\r\n";
    
protected javax.mail.internet.MimeMessagegetMissingEncodingMessage()

        MimeMessage mmCreated = new MimeMessage(Session.getDefaultInstance(new Properties()));
        mmCreated.setSubject("test");
        MimeMultipart mm = new MimeMultipart("alternative");
        mm.addBodyPart(new MimeBodyPart(new InternetHeaders(new ByteArrayInputStream("X-header: test2\r\nContent-Type: text/plain; charset=Cp1252\r\nContent-Transfer-Encoding: quoted-printable\r\n".getBytes())),"second part =E8=E8".getBytes()));
        mmCreated.setContent(mm);
        mmCreated.saveChanges();
        return mmCreated;
    
protected java.lang.StringgetMissingEncodingMessageSource()

        return "Subject: test\r\n"
        +"MIME-Version: 1.0\r\n"
        +"Content-Type: multipart/alternative; \r\n" 
        +"\tboundary=\"----=_Part_0_XXXXXXXXXXX.XXXXXXXXXXX\"\r\n"
        +"\r\n"
        +"------=_Part_0_XXXXXXXXXXX.XXXXXXXXXXX\r\n"
        +"X-header: test2\r\n"
        +"Content-Type: text/plain; charset=Cp1252\r\n"
        +"Content-Transfer-Encoding: quoted-printable\r\n"
        +"\r\n"
        +"second part =E8=E8\r\n"
        +"------=_Part_0_XXXXXXXXXXX.XXXXXXXXXXX--\r\n";
    
protected javax.mail.internet.MimeMessagegetMultipartMessage()

        MimeMessage mmCreated = new MimeMessage(Session.getDefaultInstance(new Properties()));
        mmCreated.setSubject("test");
        MimeMultipart mm = new MimeMultipart("alternative");
        mm.addBodyPart(new MimeBodyPart(new InternetHeaders(new ByteArrayInputStream("X-header: test1\r\nContent-Type: text/plain; charset=Cp1252\r\n".getBytes())),"first part тащ".getBytes()));
        mm.addBodyPart(new MimeBodyPart(new InternetHeaders(new ByteArrayInputStream("X-header: test2\r\nContent-Type: text/plain; charset=Cp1252\r\nContent-Transfer-Encoding: quoted-printable\r\n".getBytes())),"second part =E8=E8".getBytes()));
        mmCreated.setContent(mm);
        mmCreated.saveChanges();
        return mmCreated;
    
protected java.lang.StringgetMultipartMessageExpected1()

        return "Subject: test\r\n"
            +"MIME-Version: 1.0\r\n"
            +"Content-Type: multipart/alternative; \r\n" 
            +"\tboundary=\"----=_Part_0_XXXXXXXXXXX.XXXXXXXXXXX\"\r\n"
            +"\r\n"
            +"------=_Part_0_XXXXXXXXXXX.XXXXXXXXXXX\r\n"
            +"X-header: test1\r\n"
            +"Content-Type: text/plain; charset=Cp1252\r\n"
            +"Content-Transfer-Encoding: quoted-printable\r\n"
            +"\r\n"
            +"test=80\r\n"
            +"------=_Part_0_XXXXXXXXXXX.XXXXXXXXXXX\r\n"
            +"X-header: test2\r\n"
            +"Content-Type: text/plain; charset=Cp1252\r\n"
            +"Content-Transfer-Encoding: quoted-printable\r\n"
            +"\r\n"
            +"second part =E8=E8\r\n"
            +"------=_Part_0_XXXXXXXXXXX.XXXXXXXXXXX--\r\n";
    
protected java.lang.StringgetMultipartMessageExpected2()

        return "Subject: test\r\n"
            +"MIME-Version: 1.0\r\n"
            +"Content-Type: multipart/alternative; \r\n" 
            +"\tboundary=\"----=_Part_0_XXXXXXXXXXX.XXXXXXXXXXX\"\r\n"
            +"\r\n"
            +"------=_Part_0_XXXXXXXXXXX.XXXXXXXXXXX\r\n"
            +"X-header: test1\r\n"
            +"Content-Type: text/plain; charset=Cp1252\r\n"
            +"Content-Transfer-Encoding: quoted-printable\r\n"
            +"\r\n"
            +"test=80\r\n"
            +"------=_Part_0_XXXXXXXXXXX.XXXXXXXXXXX\r\n"
            +"X-header: test2\r\n"
            +"Content-Type: text/plain; charset=Cp1252\r\n"
            +"Content-Transfer-Encoding: quoted-printable\r\n"
            +"\r\n"
            +"second part =E8=E8\r\n"
            +"------=_Part_0_XXXXXXXXXXX.XXXXXXXXXXX\r\n"
            +"Subject: test3\r\n"
            +"Content-Transfer-Encoding: 7bit\r\n"
            +"Content-Type: text/plain; charset=us-ascii\r\n"
            +"\r\n"
            +"second part\r\n"
            +"------=_Part_0_XXXXXXXXXXX.XXXXXXXXXXX--\r\n";
    
protected java.lang.StringgetMultipartMessageExpected3()

        return "Subject: test\r\n"
            +"MIME-Version: 1.0\r\n"
            +"Content-Type: binary/octet-stream\r\n"
            +"Content-Transfer-Encoding: quoted-printable\r\n"
            +"\r\n"
            +"mynewco=F2=E0=F9ntent=80=E0!";
    
protected java.lang.StringgetMultipartMessageSource()

        return "Subject: test\r\n"
            +"MIME-Version: 1.0\r\n"
            +"Content-Type: multipart/alternative; \r\n" 
            +"\tboundary=\"----=_Part_0_XXXXXXXXXXX.XXXXXXXXXXX\"\r\n"
            +"\r\n"
            +"------=_Part_0_XXXXXXXXXXX.XXXXXXXXXXX\r\n"
            +"X-header: test1\r\n"
            +"Content-Type: text/plain; charset=Cp1252\r\n"
            +"Content-Transfer-Encoding: quoted-printable\r\n"
            +"\r\n"
            +"first part =E8\r\n"
            +"------=_Part_0_XXXXXXXXXXX.XXXXXXXXXXX\r\n"
            +"X-header: test2\r\n"
            +"Content-Type: text/plain; charset=Cp1252\r\n"
            +"Content-Transfer-Encoding: quoted-printable\r\n"
            +"\r\n"
            +"second part =E8=E8\r\n"
            +"------=_Part_0_XXXXXXXXXXX.XXXXXXXXXXX--\r\n";
    
protected javax.mail.internet.MimeMessagegetSimpleMessage()

        MimeMessage mmCreated = new MimeMessage(Session.getDefaultInstance(new Properties()));
        mmCreated.setSubject("test");
        mmCreated.setText("test body");
        mmCreated.saveChanges();
        return mmCreated;
    
protected java.lang.StringgetSimpleMessageCleanedSource()

        return "Subject: test\r\n"
            +"MIME-Version: 1.0\r\n"
            +"Content-Type: text/plain; charset=us-ascii\r\n"
            +"Content-Transfer-Encoding: 7bit\r\n"
            +"\r\n"
            +"test body";
    
protected java.lang.StringgetSimpleMessageCleanedSourceHeaderExpected()

        return "X-Test: foo\r\n"+getSimpleMessageCleanedSource();
    
public voidtestGetLineCount()

        MimeMessage mm = getMissingEncodingMessage();
        try {
            int count = mm.getLineCount();
            assertTrue(count == -1 || count == 7);
        } catch (Exception e) {
            fail("Unexpected exception in getLineCount");
        }
        ContainerUtil.dispose(mm);
    
public voidtestHeaderOrder()

        MimeMessage message = getSimpleMessage();
        message.setHeader(RFC2822Headers.RETURN_PATH, "<test@test.de>");
        Enumeration h =  message.getAllHeaderLines();
        
        assertEquals(h.nextElement(),"Return-Path: <test@test.de>");
    
public voidtestMessageCloningViaCoW()
This test throw a NullPointerException when the original message was created by a MimeMessageInputStreamSource.

        MimeMessage mmorig = getSimpleMessage();
        
        MimeMessage mm = new MimeMessageCopyOnWriteProxy(mmorig);

        MimeMessage mm2 = new MimeMessageCopyOnWriteProxy(mm);

        mm2.setHeader("Subject", "Modified");
        
        ContainerUtil.dispose(mm2);
        System.gc();
        Thread.sleep(200);
        //((Disposable)mail_dup.getMessage()).dispose();
        
        mm.setHeader("Subject", "Modified");
        
        ContainerUtil.dispose(mm);
        ContainerUtil.dispose(mmorig);
    
public voidtestMessageCloningViaCoW2()
This test throw a NullPointerException when the original message was created by a MimeMessageInputStreamSource.

        MimeMessage mmorig = getSimpleMessage();
        
        MimeMessage mm = new MimeMessageCopyOnWriteProxy(mmorig);
        
        MimeMessage mm2 = new MimeMessageCopyOnWriteProxy(mm);
        
        ContainerUtil.dispose(mm);
        mm = null;
        System.gc();
        Thread.sleep(200);

        try {
            mm2.writeTo(System.out);
        } catch (Exception e) {
            e.printStackTrace();
            fail("Exception while writing the message to output");
        }
        
        ContainerUtil.dispose(mm2);
        ContainerUtil.dispose(mmorig);
    
public voidtestMessageCloningViaCoWSubjectLost()
This test throw a NullPointerException when the original message was created by a MimeMessageInputStreamSource.

        MimeMessage mmorig = getSimpleMessage();
        
        MimeMessage mm = new MimeMessageCopyOnWriteProxy(mmorig);

        mm.setHeader("X-Test", "foo");
        mm.saveChanges();
        
        assertEquals(getSimpleMessageCleanedSourceHeaderExpected(),getCleanedMessageSource(mm));

        ContainerUtil.dispose(mm);
        ContainerUtil.dispose(mmorig);
    
public voidtestMultipartMessageChanges()


        MimeMessage mm = getMultipartMessage();
        
//        ByteArrayOutputStream out = new ByteArrayOutputStream();
//        mmCreated.writeTo(out,new String[] {"Message-ID"});
//        String messageSource = out.toString();
//        System.out.println(messageSource);
        
        
        MimeMultipart content1 = (MimeMultipart) mm.getContent();
        BodyPart b1 = content1.getBodyPart(0);
        b1.setContent("test\u20AC","text/plain; charset=Cp1252");
        mm.setContent(content1,mm.getContentType());
        //.setHeader(RFC2822Headers.CONTENT_TYPE,contentType);
        mm.saveChanges();

        assertEquals(getMultipartMessageExpected1(),getCleanedMessageSource(mm));

        MimeMultipart content2 = (MimeMultipart) mm.getContent();
        content2.addBodyPart(new MimeBodyPart(new InternetHeaders(new ByteArrayInputStream("Subject: test3\r\n".getBytes())),"second part".getBytes()));
        mm.setContent(content2,mm.getContentType());
        mm.saveChanges();

        assertEquals(getMultipartMessageExpected2(),getCleanedMessageSource(mm));

        mm.setContent("mynewco\u00F2\u00E0\u00F9ntent\u20AC\u00E0!","text/plain; charset=cp1252");
        mm.setHeader(RFC2822Headers.CONTENT_TYPE,"binary/octet-stream");
        //mm.setHeader("Content-Transfer-Encoding","8bit");
        mm.saveChanges();
        
        assertEquals(getMultipartMessageExpected3(),getCleanedMessageSource(mm));
        
        ContainerUtil.dispose(mm);
        
    
public voidtestReturnPath()

        MimeMessage message = getSimpleMessage();
        assertNull(message.getHeader(RFC2822Headers.RETURN_PATH));
    
public voidtestSimpleMessage()

        assertEquals(getSimpleMessageCleanedSource(), getCleanedMessageSource(getSimpleMessage()));