KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > james > mailboxmanager > AbstractImapMailboxSelfTestCase


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

19
20 package org.apache.james.mailboxmanager;
21
22 import java.util.Date JavaDoc;
23
24 import javax.mail.Flags JavaDoc;
25 import javax.mail.MessagingException JavaDoc;
26 import javax.mail.internet.MimeMessage JavaDoc;
27
28 import junit.framework.TestCase;
29
30 import org.apache.james.mailboxmanager.impl.GeneralMessageSetImpl;
31 import org.apache.james.mailboxmanager.impl.MailboxListenerCollector;
32 import org.apache.james.mailboxmanager.mailbox.ImapMailboxSession;
33 import org.apache.james.mailboxmanager.manager.MailboxManager;
34
35 public abstract class AbstractImapMailboxSelfTestCase extends TestCase {
36     
37     protected MailboxManager mailboxManager;
38     
39     protected ImapMailboxSession mailbox;
40     
41     protected MailboxListenerCollector collector;
42     
43     public static final String JavaDoc INBOX="#users.tuser.INBOX";
44     
45     public void setUp() throws Exception JavaDoc {
46         super.setUp();
47         mailboxManager.createMailbox(INBOX);
48         mailbox=mailboxManager.getImapMailboxSession(INBOX);
49         collector=new MailboxListenerCollector();
50         mailbox.addListener(collector, 0);
51         assertNotNull(mailbox);
52     }
53     
54     public void testGetFirstUnseen() throws MailboxManagerException, MessagingException JavaDoc {
55         assertNotNull(mailbox);
56         for (int i = 0; i < 5; i++) {
57             MimeMessage JavaDoc mm=TestUtil.createMessage();
58             mm.setFlags(new Flags JavaDoc(Flags.Flag.SEEN), true);
59             MessageResult mr=mailbox.appendMessage(mm, new Date JavaDoc(), MessageResult.UID);
60             assertEquals(i+1, mr.getUid());
61         }
62         for (int i = 0; i < 3; i++) {
63             MessageResult mr=mailbox.appendMessage(TestUtil.createMessage(), new Date JavaDoc(), MessageResult.UID);
64             assertEquals(i+6, mr.getUid());
65         }
66         for (int i = 0; i < 3; i++) {
67             MimeMessage JavaDoc mm=TestUtil.createMessage();
68             mm.setFlags(new Flags JavaDoc(Flags.Flag.SEEN), true);
69             MessageResult mr=mailbox.appendMessage(mm, new Date JavaDoc(), MessageResult.UID);
70             assertEquals(i+9, mr.getUid());
71         }
72         MessageResult mr;
73         mr=mailbox.getFirstUnseen(MessageResult.UID | MessageResult.MSN);
74         assertNotNull(mr);
75         assertEquals(6, mr.getUid());
76         assertEquals(6, mr.getMsn());
77         mailbox.setFlags(new Flags JavaDoc(Flags.Flag.DELETED), true, false, GeneralMessageSetImpl.uidRange(1,3), null);
78         mailbox.expunge(GeneralMessageSetImpl.all(), 0);
79         mailbox.getExpungedEvents(true);
80         mr=mailbox.getFirstUnseen(MessageResult.UID | MessageResult.MSN);
81         assertNotNull(mr);
82         assertEquals(6, mr.getUid());
83         assertEquals(3, mr.getMsn());
84     }
85     
86     public void testGetExpungedEvents() throws MessagingException JavaDoc, MailboxManagerException {
87         for (int i = 0; i < 5; i++) {
88             MessageResult mr=mailbox.appendMessage(TestUtil.createMessage(), new Date JavaDoc(), MessageResult.UID | MessageResult.MSN);
89             assertEquals(i+1, mr.getUid());
90             assertEquals(i+1, mr.getMsn());
91         }
92         mailbox.setFlags(new Flags JavaDoc(Flags.Flag.DELETED), true, false, GeneralMessageSetImpl.uidRange(2, 4), null);
93         final MessageResult[] expungeResult1=mailbox.expunge(GeneralMessageSetImpl.all(), MessageResult.UID | MessageResult.MSN);
94         checkMessageResults(new long[] {2,3,4},new int[] {2,3,4},expungeResult1);
95         
96         final MessageResult[] getResult1 = mailbox.getMessages(GeneralMessageSetImpl.all(), MessageResult.UID | MessageResult.MSN);
97         checkMessageResults(new long[] {1,5},new int[] {1,5},getResult1);
98         
99         
100         final MessageResult[] expungeEventResult1 = mailbox.getExpungedEvents(false);
101         checkMessageResults(new long[] {2,3,4},new int[] {2,3,4},expungeEventResult1);
102         final MessageResult[] expungeEventResult2 = mailbox.getExpungedEvents(true);
103         checkMessageResults(new long[] {2,3,4},new int[] {2,2,2},expungeEventResult2);
104         
105         
106         final MessageResult[] getResult2 = mailbox.getMessages(GeneralMessageSetImpl.all(), MessageResult.UID | MessageResult.MSN);
107         checkMessageResults(new long[] {1,5},new int[] {1,2},getResult2);
108         
109         for (int i = 0; i < 5; i++) {
110             MessageResult mr=mailbox.appendMessage(TestUtil.createMessage(), new Date JavaDoc(), MessageResult.UID | MessageResult.MSN);
111             assertEquals(6+i, mr.getUid());
112             assertEquals(3+i, mr.getMsn());
113         }
114
115         final MessageResult[] getResult3 = mailbox.getMessages(GeneralMessageSetImpl.all(), MessageResult.UID | MessageResult.MSN);
116         checkMessageResults(new long[] {1,5,6,7,8,9,10},new int[] {1,2,3,4,5,6,7},getResult3);
117         
118         mailbox.setFlags(new Flags JavaDoc(Flags.Flag.DELETED), true, false, GeneralMessageSetImpl.msnRange(2,4), null);
119         mailbox.setFlags(new Flags JavaDoc(Flags.Flag.DELETED), true, false, GeneralMessageSetImpl.oneMsn(6), null);
120         
121         final MessageResult[] expungeResult2=mailbox.expunge(GeneralMessageSetImpl.all(), MessageResult.UID | MessageResult.MSN);
122         checkMessageResults(new long[] {5,6,7,9},new int[] {2,3,4,6},expungeResult2);
123         
124         final MessageResult[] getResult4 = mailbox.getMessages(GeneralMessageSetImpl.all(), MessageResult.UID | MessageResult.MSN);
125         checkMessageResults(new long[] {1,8,10},new int[] {1,5,7},getResult4);
126         
127         final MessageResult[] expungeEventResult3 = mailbox.getExpungedEvents(false);
128         checkMessageResults(new long[] {5,6,7,9},new int[] {2,3,4,6},expungeEventResult3);
129         final MessageResult[] expungeEventResult4 = mailbox.getExpungedEvents(true);
130         checkMessageResults(new long[] {5,6,7,9},new int[] {2,2,2,3},expungeEventResult4);
131         
132         final MessageResult[] getResult5 = mailbox.getMessages(GeneralMessageSetImpl.all(), MessageResult.UID | MessageResult.MSN);
133         checkMessageResults(new long[] {1,8,10},new int[] {1,2,3},getResult5);
134     }
135     
136     public void testGetUidNext() throws MessagingException JavaDoc {
137         assertEquals(1, mailbox.getUidNext());
138         MessageResult mr=mailbox.appendMessage(TestUtil.createMessage(), new Date JavaDoc(), MessageResult.UID | MessageResult.MSN);
139         assertEquals(1,mr.getUid());
140         assertEquals(2, mailbox.getUidNext());
141     }
142     
143     public void testAddedEvents() throws MailboxManagerException, MessagingException JavaDoc {
144         assertEquals(0,mailbox.getFlagEvents(false).length);
145         MessageResult mr=mailbox.appendMessage(TestUtil.createMessage(), new Date JavaDoc(), MessageResult.UID | MessageResult.MSN);
146         assertEquals(0,mailbox.getFlagEvents(false).length);
147         assertEquals(0,mailbox.getFlagEvents(true).length);
148     }
149     
150     protected void assertEventCount(MailboxListenerCollector collector,long[] added, long[] flags, long[] expunged) {
151         assertEquals("added size", added.length, collector.getAddedList(true).size());
152         assertEquals("flags size", added.length, collector.getFlaggedList(true).size());
153         assertEquals("deleted size", added.length, collector.getExpungedList(true).size());
154         
155     }
156     
157     protected void checkMessageResults(long[] uids,int[] msns,MessageResult[] messageResults) {
158         assertEquals(uids.length, msns.length);
159         assertEquals(uids.length, messageResults.length);
160         for (int i = 0; i < messageResults.length; i++) {
161             assertEquals("Uid at pos "+i,uids[i], messageResults[i].getUid());
162             assertEquals("Msn at pos "+i,msns[i], messageResults[i].getMsn());
163         }
164     }
165
166 }
167
Popular Tags