1 19 20 package org.apache.james.imapserver.handler.session; 21 22 import java.io.IOException ; 23 24 import javax.mail.MessagingException ; 25 import javax.mail.Flags.Flag; 26 import javax.mail.internet.MimeMessage ; 27 28 import org.apache.james.imapserver.ProtocolException; 29 import org.apache.james.imapserver.client.CopyClientCommand; 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.store.MailboxException; 35 import org.apache.james.imapserver.util.MessageGenerator; 36 import org.apache.james.mailboxmanager.MailboxManagerException; 37 38 public class CopySessionTest extends AbstractSessionTest { 39 40 41 String [] folders = {USER_MAILBOX_ROOT+".INBOX",USER_MAILBOX_ROOT+".test",USER_MAILBOX_ROOT+".test2"}; 42 MimeMessage [] msgs= null; 43 long[] uids = null; 44 45 public void setUp() throws MailboxException, MessagingException , IOException , MailboxManagerException { 46 super.setUp(); 47 msgs=MessageGenerator.generateSimpleMessages(4); 48 createFolders(folders); 49 appendMessagesClosed(USER_MAILBOX_ROOT+".INBOX",msgs); 51 deleteAll(USER_MAILBOX_ROOT+".INBOX"); 52 msgs[0].setFlag(Flag.SEEN,true); 53 msgs[0].setFlag(Flag.ANSWERED,true); 54 msgs[1].setFlag(Flag.SEEN,true); 55 msgs[2].setFlag(Flag.ANSWERED,true); 56 uids=addUIDMessagesOpen(USER_MAILBOX_ROOT+".INBOX",msgs); 57 } 58 59 60 public void doTestCopyAll(boolean uid) throws ProtocolException, IOException , MessagingException , MailboxManagerException { 61 verifyCommand(new LoginCommand(USER_NAME,USER_PASSWORD)); 62 verifyCommand(new SelectCommand("INBOX", msgs, getUidValidity(USER_MAILBOX_ROOT+".INBOX"))); 63 64 final MessageSet set; 65 if (uid) { 66 set = new MessageSet(msgs,uids,1,-1); 67 } else { 68 set = new MessageSet(msgs,1,-1); 69 } 70 verifyCommand(new CopyClientCommand(set,"test")); 71 72 useFolder(USER_MAILBOX_ROOT+".test"); 73 MimeMessage [] testMsgs=getMessages(USER_MAILBOX_ROOT+".test"); 74 assertEquals(4,testMsgs.length); 75 for (int i = 0; i < msgs.length; i++) { 76 assertEquals("Message content differs "+i, MessageGenerator.messageContentToString(msgs[i]), 77 MessageGenerator.messageContentToString(testMsgs[i])); 78 assertEquals("Flags differ "+i,msgs[i].getFlags(),testMsgs[i].getFlags()); 79 } 80 freeFolder(USER_MAILBOX_ROOT+".test"); 81 82 verifyCommand(new LogoutClientCommand()); 83 assertFalse(isOpen(USER_MAILBOX_ROOT+".INBOX")); 84 assertFalse(isOpen(USER_MAILBOX_ROOT+".test")); 85 } 86 87 88 public void doTestCopyOne(boolean uid) throws ProtocolException, IOException , MessagingException , MailboxManagerException { 89 verifyCommand(new LoginCommand(USER_NAME,USER_PASSWORD)); 90 verifyCommand(new SelectCommand("INBOX", msgs, getUidValidity(USER_MAILBOX_ROOT+".INBOX"))); 91 92 final MessageSet set; 93 if (uid) { 94 set = new MessageSet(msgs,uids,6); 95 } else { 96 set = new MessageSet(msgs,2); 97 } 98 verifyCommand(new CopyClientCommand(set,"test")); 99 100 useFolder("test"); 101 MimeMessage [] testMsgs=getMessages(USER_MAILBOX_ROOT+".test"); 102 assertEquals(1,testMsgs.length); 103 104 assertEquals("Message content differs ", MessageGenerator.messageContentToString(msgs[1]), 105 MessageGenerator.messageContentToString(testMsgs[0])); 106 assertEquals("Flags differ ",msgs[1].getFlags(),testMsgs[0].getFlags()); 107 108 freeFolder(USER_MAILBOX_ROOT+".test"); 109 110 verifyCommand(new LogoutClientCommand()); 111 assertFalse(isOpen(USER_MAILBOX_ROOT+".INBOX")); 112 assertFalse(isOpen(USER_MAILBOX_ROOT+".test")); 113 } 114 public void doTestCopyThree(boolean uid) throws ProtocolException, IOException , MessagingException , MailboxManagerException { 115 verifyCommand(new LoginCommand(USER_NAME,USER_PASSWORD)); 116 verifyCommand(new SelectCommand("INBOX", msgs, getUidValidity(USER_MAILBOX_ROOT+".INBOX"))); 117 118 final MessageSet set; 119 if (uid) { 120 set = new MessageSet(msgs,uids,6,8); 121 } else { 122 set = new MessageSet(msgs,2,4); 123 } 124 verifyCommand(new CopyClientCommand(set,"test")); 125 126 useFolder(USER_MAILBOX_ROOT+".test"); 127 MimeMessage [] testMsgs=getMessages(USER_MAILBOX_ROOT+".test"); 128 assertEquals(3,testMsgs.length); 129 for (int i=0; i<3; i++) { 130 assertEquals("Message content differs ", MessageGenerator.messageContentToString(msgs[i+1]), 131 MessageGenerator.messageContentToString(testMsgs[i])); 132 assertEquals("Flags differ ",msgs[i+1].getFlags(),testMsgs[i].getFlags()); 133 } 134 135 freeFolder(USER_MAILBOX_ROOT+".test"); 136 137 verifyCommand(new LogoutClientCommand()); 138 assertFalse(isOpen(USER_MAILBOX_ROOT+".INBOX")); 139 assertFalse(isOpen(USER_MAILBOX_ROOT+".test")); 140 } 141 142 143 public void testCopyAll() throws ProtocolException, IOException , MessagingException , MailboxManagerException { 144 doTestCopyAll(false); 145 } 146 147 public void testUidCopyAll() throws ProtocolException, IOException , MessagingException , MailboxManagerException { 148 doTestCopyAll(true); 149 } 150 151 public void testCopyOne() throws ProtocolException, IOException , MessagingException , MailboxManagerException { 152 doTestCopyOne(false); 153 } 154 155 public void testUidCopyOne() throws ProtocolException, IOException , MessagingException , MailboxManagerException { 156 doTestCopyOne(true); 157 } 158 159 public void testCopyThree() throws ProtocolException, IOException , MessagingException , MailboxManagerException { 160 doTestCopyThree(false); 161 } 162 163 public void testUidCopyThree() throws ProtocolException, IOException , MessagingException , MailboxManagerException { 164 doTestCopyThree(true); 165 } 166 } 167 | Popular Tags |