1 19 20 package org.apache.james.imapserver.handler.session; 21 22 import java.io.IOException ; 23 24 import javax.mail.Flags ; 25 import javax.mail.MessagingException ; 26 import javax.mail.Flags.Flag; 27 import javax.mail.internet.MimeMessage ; 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 [] onlyInbox = {USER_MAILBOX_ROOT+".INBOX"}; 45 MimeMessage [] msgs= null; 46 long[] uids = null; 47 48 public void setUp() throws MailboxException, MessagingException , IOException , MailboxManagerException { 49 super.setUp(); 50 msgs=MessageGenerator.generateSimpleMessages(4); 51 createFolders(onlyInbox); 52 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 , MessagingException , MailboxManagerException { 59 verifyCommand(new LoginCommand(USER_NAME,USER_PASSWORD)); 60 Flags flagsMany= new Flags (); 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 flagsSeen=new Flags (); 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 , MessagingException , MailboxManagerException { 117 setFlags(USER_MAILBOX_ROOT+".INBOX", 5, 8, new Flags (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 flagsSeen=new Flags (); 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 flagsMany= new Flags (); 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 , MessagingException , MailboxManagerException { 169 170 Flags flagsEmpty=new Flags (); 171 172 Flags flagsSeen=new Flags (); 173 flagsSeen.add(Flag.SEEN); 174 175 Flags flagsAnswered=new Flags (); 176 flagsAnswered.add(Flag.ANSWERED); 177 178 Flags flagsSeenFlagged= new Flags (); 179 flagsSeenFlagged.add(Flag.SEEN); 180 flagsSeenFlagged.add(Flag.FLAGGED); 181 182 Flags flagsSeenFlaggedAnswered= new Flags (); 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 , MessagingException , MailboxManagerException { 229 doTestSetFlags(false, false); 230 } 231 232 public void testAddFlags() throws ProtocolException, IOException , MessagingException , MailboxManagerException { 233 doTestAddFlags(false, false); 234 } 235 236 public void testRemoveFlags() throws ProtocolException, IOException , MessagingException , MailboxManagerException { 237 doTestRemoveFlags(false, false); 238 } 239 240 public void testUidSetFlags() throws ProtocolException, IOException , MessagingException , MailboxManagerException { 241 doTestSetFlags(true, false); 242 } 243 244 public void testUidAddFlags() throws ProtocolException, IOException , MessagingException , MailboxManagerException { 245 doTestAddFlags(true, false); 246 } 247 248 public void testUidRemoveFlags() throws ProtocolException, IOException , MessagingException , MailboxManagerException { 249 doTestRemoveFlags(true, false); 250 } 251 252 253 public void testSilentSetFlags() throws ProtocolException, IOException , MessagingException , MailboxManagerException { 254 doTestSetFlags(false, true); 255 } 256 257 public void testSilentAddFlags() throws ProtocolException, IOException , MessagingException , MailboxManagerException { 258 doTestAddFlags(false, true); 259 } 260 261 public void testSilentRemoveFlags() throws ProtocolException, IOException , MessagingException , MailboxManagerException { 262 doTestRemoveFlags(false, true); 263 } 264 265 public void testSilentUidSetFlags() throws ProtocolException, IOException , MessagingException , MailboxManagerException { 266 doTestSetFlags(true, true); 267 } 268 269 public void testSilentUidAddFlags() throws ProtocolException, IOException , MessagingException , MailboxManagerException { 270 doTestAddFlags(true, true); 271 } 272 273 public void testSilentUidRemoveFlags() throws ProtocolException, IOException , MessagingException , MailboxManagerException { 274 doTestRemoveFlags(true, true); 275 } 276 } 277 | Popular Tags |