KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > james > imapserver > handler > session > UidStoreSessionTest


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.imapserver.handler.session;
21
22 import java.io.IOException JavaDoc;
23
24 import javax.mail.Flags JavaDoc;
25 import javax.mail.MessagingException JavaDoc;
26 import javax.mail.Flags.Flag;
27 import javax.mail.internet.MimeMessage JavaDoc;
28
29 import org.apache.james.imapserver.ProtocolException;
30 import org.apache.james.imapserver.client.LoginCommand;
31 import org.apache.james.imapserver.client.LogoutClientCommand;
32 import org.apache.james.imapserver.client.MessageSet;
33 import org.apache.james.imapserver.client.SelectCommand;
34 import org.apache.james.imapserver.client.StoreClientCommand;
35 import org.apache.james.imapserver.store.MailboxException;
36 import org.apache.james.imapserver.util.MessageGenerator;
37 import org.apache.james.mailboxmanager.MailboxManagerException;
38
39
40
41 public class UidStoreSessionTest extends AbstractSessionTest {
42     
43     
44     String JavaDoc[] onlyInbox = {USER_MAILBOX_ROOT+".INBOX"};
45     MimeMessage JavaDoc[] msgs= null;
46     long[] uids = null;
47     
48     public void setUp() throws MailboxException, MessagingException JavaDoc, IOException JavaDoc, MailboxManagerException {
49         super.setUp();
50         msgs=MessageGenerator.generateSimpleMessages(4);
51         createFolders(onlyInbox);
52         // increase the uid
53
appendMessagesClosed(USER_MAILBOX_ROOT+".INBOX",msgs);
54         deleteAll(USER_MAILBOX_ROOT+".INBOX");
55         uids=addUIDMessagesOpen(USER_MAILBOX_ROOT+".INBOX",msgs);
56     }
57     
58     public void doTestSetFlags(boolean uid, boolean silent) throws ProtocolException, IOException JavaDoc, MessagingException JavaDoc, MailboxManagerException {
59         verifyCommand(new LoginCommand(USER_NAME,USER_PASSWORD));
60         Flags JavaDoc flagsMany= new Flags JavaDoc();
61         flagsMany.add(Flag.SEEN);
62         flagsMany.add(Flag.FLAGGED);
63         flagsMany.add(Flag.ANSWERED);
64         setFlags(USER_MAILBOX_ROOT+".INBOX", 5, 6, flagsMany, true, false);
65         setFlags(USER_MAILBOX_ROOT+".INBOX", 8, 8, flagsMany, true, false);
66         msgs=getMessages(USER_MAILBOX_ROOT+".INBOX");
67         uids=getUids(USER_MAILBOX_ROOT+".INBOX");
68         
69         verifyCommand(new SelectCommand(USER_MAILBOX_ROOT+".INBOX", msgs, getUidValidity(USER_MAILBOX_ROOT+".INBOX")));
70
71
72
73         MessageSet set;
74         StoreClientCommand storeCommand;
75
76         
77         Flags JavaDoc flagsSeen=new Flags JavaDoc();
78         flagsSeen.add(Flag.SEEN);
79         
80         
81         if (uid) {
82             set=new MessageSet(msgs,uids,7,8);
83         } else {
84             set=new MessageSet(msgs,3,4);
85         }
86         
87         msgs[2].setFlag(Flag.SEEN, true);
88         msgs[3].setFlags(flagsMany,false);
89         msgs[3].setFlag(Flag.SEEN, true);
90              
91         storeCommand=new StoreClientCommand(StoreClientCommand.MODE_SET,silent,flagsSeen,set);
92         verifyCommand(storeCommand);
93         
94         if (uid) {
95             set=new MessageSet(msgs,uids,5);
96         }else {
97             set=new MessageSet(msgs,1);
98         }
99         
100         msgs[0].setFlags(flagsMany,false);
101         msgs[0].setFlag(Flag.SEEN, true);
102         
103         storeCommand=new StoreClientCommand(StoreClientCommand.MODE_SET,silent,flagsSeen,set);
104         verifyCommand(storeCommand);
105         
106         msgs=getMessages(USER_MAILBOX_ROOT+".INBOX");
107         
108         assertEquals(flagsSeen,msgs[0].getFlags());
109         assertEquals(flagsMany,msgs[1].getFlags());
110         assertEquals(flagsSeen,msgs[2].getFlags());
111         assertEquals(flagsSeen,msgs[3].getFlags());
112         verifyCommand(new LogoutClientCommand());
113         assertFalse(isOpen(USER_MAILBOX_ROOT+".INBOX"));
114     }
115     
116     public void doTestAddFlags(boolean uid, boolean silent) throws ProtocolException, IOException JavaDoc, MessagingException JavaDoc, MailboxManagerException {
117         setFlags(USER_MAILBOX_ROOT+".INBOX", 5, 8, new Flags JavaDoc(Flags.Flag.DRAFT), true, false);
118         verifyCommand(new LoginCommand(USER_NAME,USER_PASSWORD));
119         verifyCommand(new SelectCommand(USER_MAILBOX_ROOT+".INBOX", msgs, getUidValidity(USER_MAILBOX_ROOT+".INBOX")));
120         msgs=getMessages(USER_MAILBOX_ROOT+".INBOX");
121         uids=getUids(USER_MAILBOX_ROOT+".INBOX");
122
123         MessageSet set;
124         StoreClientCommand storeCommand;
125         
126         Flags JavaDoc flagsSeen=new Flags JavaDoc();
127         flagsSeen.add(Flag.SEEN);
128         if (uid) {
129             set=new MessageSet(msgs,uids,5);
130         } else {
131             set=new MessageSet(msgs,1);
132         }
133         msgs[0].setFlags(flagsSeen, true);
134         
135         storeCommand=new StoreClientCommand(StoreClientCommand.MODE_ADD,silent,flagsSeen,set);
136         verifyCommand(storeCommand);
137         
138         Flags JavaDoc flagsMany= new Flags JavaDoc();
139         flagsMany.add(Flag.SEEN);
140         flagsMany.add(Flag.FLAGGED);
141         flagsMany.add(Flag.ANSWERED);
142         
143         if (uid) {
144             set=new MessageSet(msgs,uids,6,8);
145         }else {
146             set=new MessageSet(msgs,2,4);
147         }
148         msgs[1].setFlags(flagsMany, true);
149         msgs[2].setFlags(flagsMany, true);
150         msgs[3].setFlags(flagsMany, true);
151
152         storeCommand=new StoreClientCommand(StoreClientCommand.MODE_ADD,silent,flagsMany,set);
153         verifyCommand(storeCommand);
154         
155         msgs=getMessages(USER_MAILBOX_ROOT+".INBOX");
156         
157         flagsSeen.add(Flags.Flag.DRAFT);
158         flagsMany.add(Flags.Flag.DRAFT);
159         
160         assertEquals(flagsSeen,msgs[0].getFlags());
161         assertEquals(flagsMany,msgs[1].getFlags());
162         assertEquals(flagsMany,msgs[2].getFlags());
163         assertEquals(flagsMany,msgs[3].getFlags());
164         verifyCommand(new LogoutClientCommand());
165         assertFalse(isOpen(USER_MAILBOX_ROOT+".INBOX"));
166     }
167     
168     public void doTestRemoveFlags(boolean uid, boolean silent) throws ProtocolException, IOException JavaDoc, MessagingException JavaDoc, MailboxManagerException {
169
170         Flags JavaDoc flagsEmpty=new Flags JavaDoc();
171         
172         Flags JavaDoc flagsSeen=new Flags JavaDoc();
173         flagsSeen.add(Flag.SEEN);
174         
175         Flags JavaDoc flagsAnswered=new Flags JavaDoc();
176         flagsAnswered.add(Flag.ANSWERED);
177         
178         Flags JavaDoc flagsSeenFlagged= new Flags JavaDoc();
179         flagsSeenFlagged.add(Flag.SEEN);
180         flagsSeenFlagged.add(Flag.FLAGGED);
181         
182         Flags JavaDoc flagsSeenFlaggedAnswered= new Flags JavaDoc();
183         flagsSeenFlaggedAnswered.add(Flag.SEEN);
184         flagsSeenFlaggedAnswered.add(Flag.FLAGGED);
185         flagsSeenFlaggedAnswered.add(Flag.ANSWERED);
186         
187         setFlags(USER_MAILBOX_ROOT+".INBOX", 5, 5, flagsSeenFlaggedAnswered, true, false);
188         setFlags(USER_MAILBOX_ROOT+".INBOX", 6, 6, flagsSeen, true, false);
189         setFlags(USER_MAILBOX_ROOT+".INBOX", 7, 8, flagsSeenFlaggedAnswered, true, false);
190
191         msgs=getMessages(USER_MAILBOX_ROOT+".INBOX");
192         uids=getUids(USER_MAILBOX_ROOT+".INBOX");
193         
194         verifyCommand(new LoginCommand(USER_NAME,USER_PASSWORD));
195         verifyCommand(new SelectCommand(USER_MAILBOX_ROOT+".INBOX", msgs, getUidValidity(USER_MAILBOX_ROOT+".INBOX")));
196         
197         MessageSet set;
198         StoreClientCommand storeCommand;
199
200         set=new MessageSet(msgs,1,3,new int[] {1,3},uids,uid);
201         
202         msgs[0].setFlag(Flags.Flag.ANSWERED,false);
203         msgs[2].setFlag(Flags.Flag.ANSWERED,false);
204        
205         storeCommand=new StoreClientCommand(StoreClientCommand.MODE_REMOVE,silent,flagsAnswered,set);
206         verifyCommand(storeCommand);
207
208         if (uid) {
209             set=new MessageSet(msgs,uids,8);
210         } else {
211             set=new MessageSet(msgs,4);
212         }
213         msgs[3].setFlags(flagsSeenFlaggedAnswered,false);
214         
215         storeCommand=new StoreClientCommand(StoreClientCommand.MODE_REMOVE,silent,flagsSeenFlaggedAnswered,set);
216         verifyCommand(storeCommand);
217         
218         msgs=getMessages(USER_MAILBOX_ROOT+".INBOX");
219         
220         assertEquals(flagsSeenFlagged,msgs[0].getFlags());
221         assertEquals(flagsSeen,msgs[1].getFlags());
222         assertEquals(flagsSeenFlagged,msgs[2].getFlags());
223         assertEquals(flagsEmpty,msgs[3].getFlags());
224         verifyCommand(new LogoutClientCommand());
225         assertFalse(isOpen(USER_MAILBOX_ROOT+".INBOX"));
226     }
227     
228     public void testSetFlags() throws ProtocolException, IOException JavaDoc, MessagingException JavaDoc, MailboxManagerException {
229         doTestSetFlags(false, false);
230     }
231     
232     public void testAddFlags() throws ProtocolException, IOException JavaDoc, MessagingException JavaDoc, MailboxManagerException {
233         doTestAddFlags(false, false);
234     }
235     
236     public void testRemoveFlags() throws ProtocolException, IOException JavaDoc, MessagingException JavaDoc, MailboxManagerException {
237         doTestRemoveFlags(false, false);
238     }
239     
240     public void testUidSetFlags() throws ProtocolException, IOException JavaDoc, MessagingException JavaDoc, MailboxManagerException {
241         doTestSetFlags(true, false);
242     }
243     
244     public void testUidAddFlags() throws ProtocolException, IOException JavaDoc, MessagingException JavaDoc, MailboxManagerException {
245         doTestAddFlags(true, false);
246     }
247     
248     public void testUidRemoveFlags() throws ProtocolException, IOException JavaDoc, MessagingException JavaDoc, MailboxManagerException {
249         doTestRemoveFlags(true, false);
250     }
251     
252     
253     public void testSilentSetFlags() throws ProtocolException, IOException JavaDoc, MessagingException JavaDoc, MailboxManagerException {
254         doTestSetFlags(false, true);
255     }
256     
257     public void testSilentAddFlags() throws ProtocolException, IOException JavaDoc, MessagingException JavaDoc, MailboxManagerException {
258         doTestAddFlags(false, true);
259     }
260     
261     public void testSilentRemoveFlags() throws ProtocolException, IOException JavaDoc, MessagingException JavaDoc, MailboxManagerException {
262         doTestRemoveFlags(false, true);
263     }
264     
265     public void testSilentUidSetFlags() throws ProtocolException, IOException JavaDoc, MessagingException JavaDoc, MailboxManagerException {
266         doTestSetFlags(true, true);
267     }
268     
269     public void testSilentUidAddFlags() throws ProtocolException, IOException JavaDoc, MessagingException JavaDoc, MailboxManagerException {
270         doTestAddFlags(true, true);
271     }
272     
273     public void testSilentUidRemoveFlags() throws ProtocolException, IOException JavaDoc, MessagingException JavaDoc, MailboxManagerException {
274         doTestRemoveFlags(true, true);
275     }
276 }
277
Popular Tags