KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > james > imapserver > ImapMailboxTest


1 /***********************************************************************
2  * Copyright (c) 2000-2004 The Apache Software Foundation. *
3  * All rights reserved. *
4  * ------------------------------------------------------------------- *
5  * Licensed under the Apache License, Version 2.0 (the "License"); you *
6  * may not use this file except in compliance with the License. You *
7  * may obtain a copy of the License at: *
8  * *
9  * http://www.apache.org/licenses/LICENSE-2.0 *
10  * *
11  * Unless required by applicable law or agreed to in writing, software *
12  * distributed under the License is distributed on an "AS IS" BASIS, *
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or *
14  * implied. See the License for the specific language governing *
15  * permissions and limitations under the License. *
16  ***********************************************************************/

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 JavaDoc;
33 import javax.mail.Address JavaDoc;
34 import java.util.Date JavaDoc;
35 import java.io.InputStream JavaDoc;
36 import java.io.IOException JavaDoc;
37 import java.io.ByteArrayInputStream JavaDoc;
38 import java.net.InetAddress JavaDoc;
39
40 /**
41  *
42  *
43  * @version $Revision: 1.2.2.3 $
44  */

45 public class ImapMailboxTest extends TestCase
46         implements ImapConstants
47 {
48     public ImapMailboxTest( String JavaDoc s )
49     {
50         super( s );
51     }
52
53     public void testAppend() throws Exception JavaDoc
54     {
55         ImapMailbox mailbox = getMailbox();
56
57         MessageFlags flags = new MessageFlags();
58         flags.setFlagged( true );
59
60         Date JavaDoc datetime = new Date JavaDoc();
61         String JavaDoc 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 JavaDoc 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 JavaDoc messageContent, MessageFlags flags,
89                                 Date JavaDoc datetime, ImapMailbox mailbox )
90     {
91         MimeMessageSource source =
92                 new MimeMessageByteArraySource( "messageContent:" + System.currentTimeMillis(),
93                                                 messageContent.getBytes());
94         MimeMessage JavaDoc 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 JavaDoc sourceId;
110         private byte[] byteArray;
111
112         public MimeMessageByteArraySource( String JavaDoc sourceId, byte[] byteArray )
113         {
114             this.sourceId = sourceId;
115             this.byteArray = byteArray;
116         }
117
118         public String JavaDoc getSourceId()
119         {
120             return sourceId;
121         }
122
123         public InputStream JavaDoc getInputStream() throws IOException JavaDoc
124         {
125             return new ByteArrayInputStream JavaDoc( byteArray );
126         }
127     }
128
129
130 }
131
Popular Tags