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

MimeMessageWrapperTest

public class MimeMessageWrapperTest extends MimeMessageFromStreamTest
Test the subject folding issue.

Fields Summary
TestableMimeMessageWrapper
mw
String
content
String
sep
String
body
Constructors Summary
Methods Summary
protected javax.mail.internet.MimeMessagegetMessageFromSources(java.lang.String sources)


          
        MimeMessageInputStreamSource mmis = null;
        try {
            mmis = new MimeMessageInputStreamSource("test", new SharedByteArrayInputStream(sources.getBytes()));
        } catch (MessagingException e) {
        }
        return new TestableMimeMessageWrapper(mmis);
    
protected voidsetUp()

        mw = (TestableMimeMessageWrapper) getMessageFromSources(content+sep+body);
    
protected voidtearDown()

        ContainerUtil.dispose(mw);
    
public voidtestAddHeaderAndSave()

        try {
            mw.addHeader("X-Test", "X-Value");
            
            assertEquals("X-Value", mw.getHeader("X-Test")[0]);
            
            mw.saveChanges();

            ByteArrayOutputStream rawMessage = new ByteArrayOutputStream();
            mw.writeTo(rawMessage);
            
            assertEquals("X-Value", mw.getHeader("X-Test")[0]);

            String res = rawMessage.toString();
            
            boolean found = res.indexOf("X-Test: X-Value") > 0;
            assertEquals(true,found);

        } catch (MessagingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    
public voidtestAddReturnPathOnBadMessage()

        MimeMessage message = getMessageWithBadReturnPath();
        message.addHeader(RFC2822Headers.RETURN_PATH, "<test@test.de>");
        // test that we have now 2 return-paths
        Enumeration e = message.getMatchingHeaderLines(new String[] {"Return-Path"});
        assertEquals("Return-Path: <test@test.de>",e.nextElement());
        assertEquals("Return-Path: <mybadreturn@example.com>",e.nextElement());
        // test that return-path is the first line
        Enumeration h =  message.getAllHeaderLines();
        assertEquals("Return-Path: <test@test.de>",h.nextElement());
    
public voidtestDeferredHeaderLoading()
this is commented out due optimisation reverts (JAMES-559) public void testDeferredMessageLoadingWhileWriting() throws MessagingException, IOException { mw.setMessageLoadable(false); assertEquals("foo",mw.getSubject()); assertFalse(mw.isModified()); mw.setSubject("newSubject"); assertEquals("newSubject",mw.getSubject()); assertFalse(mw.messageParsed()); assertTrue(mw.isModified()); mw.setMessageLoadable(true); }

        mw.setHeadersLoadable(false);
        try {
            assertEquals("foo",mw.getSubject());
            fail("subject should not be loadable here, headers loading is disabled");
        } catch (IllegalStateException e) {
            
        }
    
public voidtestDeferredMessageLoading()

        assertEquals("foo",mw.getSubject());
        assertFalse(mw.messageParsed());
        assertEquals("bar\r\n",mw.getContent());
        assertTrue(mw.messageParsed());
        assertFalse(mw.isModified());
    
public voidtestGetSubjectFolding()

        try {
            StringBuffer res = new StringBuffer();
            BufferedReader r = new BufferedReader(new InputStreamReader(mw.getInputStream()));
            String line;
            while (r.ready()) {
                line = r.readLine();
                res.append(line+"\r\n");
            }
            r.close();
            assertEquals(body,res.toString());
        } catch (MessagingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    
public voidtestMessageCloned()
See JAMES-474 MimeMessageWrapper(MimeMessage) should clone the original message.

        MimeMessageWrapper mmw = new MimeMessageWrapper(mw);
        ContainerUtil.dispose(mw);
        mw = null;
        System.gc();
        Thread.sleep(200);
        mmw.writeTo(System.out);
    
public voidtestReplaceReturnPathOnBadMessage()

        MimeMessage message = getMessageWithBadReturnPath();
        message.setHeader(RFC2822Headers.RETURN_PATH, "<test@test.de>");
        Enumeration e =  message.getMatchingHeaderLines(new String[] {"Return-Path"});
        assertEquals("Return-Path: <test@test.de>",e.nextElement());
        assertFalse(e.hasMoreElements());
        Enumeration h =  message.getAllHeaderLines();
        assertEquals("Return-Path: <test@test.de>",h.nextElement());
        assertFalse(h.nextElement().toString().startsWith("Return-Path:"));