1 17 18 package org.apache.james.imapserver; 19 20 import org.apache.james.imapserver.store.ImapStore; 21 import org.apache.james.imapserver.store.InMemoryStore; 22 import org.apache.james.imapserver.store.ImapMailbox; 23 import org.apache.james.imapserver.store.MailboxException; 24 import org.apache.james.imapserver.store.MessageFlags; 25 import org.apache.james.imapserver.store.SimpleImapMessage; 26 import org.apache.james.core.MimeMessageSource; 27 import org.apache.james.core.MimeMessageWrapper; 28 import org.apache.james.core.MailImpl; 29 30 import junit.framework.TestCase; 31 32 import javax.mail.internet.MimeMessage ; 33 import javax.mail.Address ; 34 import java.util.Date ; 35 import java.io.InputStream ; 36 import java.io.IOException ; 37 import java.io.ByteArrayInputStream ; 38 import java.net.InetAddress ; 39 40 45 public class ImapMailboxTest extends TestCase 46 implements ImapConstants 47 { 48 public ImapMailboxTest( String s ) 49 { 50 super( s ); 51 } 52 53 public void testAppend() throws Exception 54 { 55 ImapMailbox mailbox = getMailbox(); 56 57 MessageFlags flags = new MessageFlags(); 58 flags.setFlagged( true ); 59 60 Date datetime = new Date (); 61 String message = 62 "Date: Mon, 7 Feb 1994 21:52:25 -0800 (PST)\r\n" + 63 "From: Fred Foobar <foobar@Blurdybloop.COM>\r\n" + 64 "Subject: afternoon meeting\r\n" + 65 "To: mooch@owatagu.siam.edu\r\n" + 66 "Message-Id: <B27397-0100000@Blurdybloop.COM>\r\n" + 67 "MIME-Version: 1.0\r\n" + 68 "Content-Type: TEXT/PLAIN; CHARSET=US-ASCII\r\n" + 69 "\r\n" + 70 "Hello Joe, do you think we can meet at 3:30 tomorrow?\r\n" + 71 "\r\n"; 72 long uid = appendMessage( message, flags, datetime, mailbox ); 73 74 SimpleImapMessage imapMessage = mailbox.getMessage( uid ); 75 76 assertEquals( 1, mailbox.getMessageCount() ); 77 assertTrue( imapMessage.getFlags().isFlagged() ); 78 assertTrue( ! imapMessage.getFlags().isAnswered() ); 79 80 MimeMessage mime = imapMessage.getMimeMessage(); 81 assertEquals( "TEXT/PLAIN; CHARSET=US-ASCII", mime.getContentType() ); 82 assertEquals( "afternoon meeting", mime.getSubject() ); 83 assertEquals( "Fred Foobar <foobar@Blurdybloop.COM>", 84 mime.getFrom()[0].toString() ); 85 86 } 87 88 private long appendMessage( String messageContent, MessageFlags flags, 89 Date datetime, ImapMailbox mailbox ) 90 { 91 MimeMessageSource source = 92 new MimeMessageByteArraySource( "messageContent:" + System.currentTimeMillis(), 93 messageContent.getBytes()); 94 MimeMessage message = new MimeMessageWrapper( source ); 95 SimpleImapMessage imapMessage = mailbox.createMessage( message, flags, datetime ); 96 return imapMessage.getUid(); 97 } 98 99 private ImapMailbox getMailbox() throws MailboxException 100 { 101 ImapStore store = new InMemoryStore(); 102 ImapMailbox root = store.getMailbox( ImapConstants.USER_NAMESPACE ); 103 ImapMailbox test = store.createMailbox( root, "test", true ); 104 return test; 105 } 106 107 class MimeMessageByteArraySource extends MimeMessageSource 108 { 109 private String sourceId; 110 private byte[] byteArray; 111 112 public MimeMessageByteArraySource( String sourceId, byte[] byteArray ) 113 { 114 this.sourceId = sourceId; 115 this.byteArray = byteArray; 116 } 117 118 public String getSourceId() 119 { 120 return sourceId; 121 } 122 123 public InputStream getInputStream() throws IOException 124 { 125 return new ByteArrayInputStream ( byteArray ); 126 } 127 } 128 129 130 } 131 | Popular Tags |