1 19 20 package org.apache.james.imapserver.handler.session; 21 22 import java.io.BufferedReader ; 23 import java.io.IOException ; 24 import java.util.Arrays ; 25 import java.util.HashSet ; 26 import java.util.Set ; 27 28 import javax.mail.Message ; 29 import javax.mail.MessagingException ; 30 import javax.mail.Flags.Flag; 31 import javax.mail.internet.MimeMessage ; 32 33 import org.apache.james.imapserver.ProtocolException; 34 import org.apache.james.imapserver.client.LoginCommand; 35 import org.apache.james.imapserver.store.MailboxException; 36 import org.apache.james.imapserver.util.MessageGenerator; 37 import org.apache.james.imapserver.util.UnsolicitedResponseGenerator; 38 import org.apache.james.mailboxmanager.MailboxManagerException; 39 40 41 public class SessionTest extends AbstractSessionTest 42 { 43 44 String [] existing = {USER_MAILBOX_ROOT+".INBOX",USER_MAILBOX_ROOT+".test",USER_MAILBOX_ROOT+".test1",USER_MAILBOX_ROOT+".test1.test1a",USER_MAILBOX_ROOT+".test1.test1b",USER_MAILBOX_ROOT+".test2.test2a",USER_MAILBOX_ROOT+".test2.test2b"}; 45 Set existingSet = null; 46 47 public void setUp() throws MailboxException, MessagingException , IOException , MailboxManagerException 48 { 49 super.setUp(); 50 existingSet=new HashSet (Arrays.asList(existing)); 51 createFolders((String []) existingSet.toArray(new String [0])); 52 } 53 54 55 56 57 public void testLogin() throws ProtocolException, IOException , MessagingException 58 { 59 verifyCommand(new LoginCommand(USER_NAME,USER_PASSWORD)); 60 } 61 62 public void testCreate() throws ProtocolException, MessagingException , IOException , MailboxManagerException{ 63 testLogin(); 64 assertFalse(folderExists(USER_MAILBOX_ROOT+".Trash")); 65 String response = handleRequest((++counter)+" create \"Trash\"\n"); 66 assertEquals(counter+" OK CREATE completed.\r\n",response); 67 assertTrue(folderExists(USER_MAILBOX_ROOT+".Trash")); 68 } 69 70 public void testListInbox() throws ProtocolException, MessagingException , IOException { 71 testLogin(); 72 String response = handleRequest((++counter)+" LIST \"\" \"INBOX\"\n"); 73 assertEquals("* LIST () \".\" INBOX\r\n"+counter+" OK LIST completed.\r\n",response); 74 } 75 76 public void testListInboxFullName() throws ProtocolException, MessagingException , IOException { 77 testLogin(); 78 String response = handleRequest((++counter)+" LIST \"\" \""+USER_MAILBOX_ROOT+".INBOX\"\n"); 79 assertEquals("* LIST () \".\" "+USER_MAILBOX_ROOT+".INBOX\r\n"+counter+" OK LIST completed.\r\n",response); 80 } 81 82 public void testLsubAll() throws ProtocolException, MessagingException , IOException { 83 testLogin(); 84 BufferedReader br = handleRequestReader((++counter)+" LSUB \""+USER_MAILBOX_ROOT+"\" \"*\"\n"); 85 String response=null; 86 String start="* LSUB () \".\" "; 87 while ((response=br.readLine())!=null) { 88 System.out.println("Parsing "+response); 89 if (response.charAt(0)=='*') { 90 assertTrue(response.startsWith(start)); 91 String name=response.substring(start.length()); 92 assertTrue(existingSet.remove(name)); 93 94 } else { 95 break; 96 } 97 } 98 assertEquals(0,existingSet.size()); 99 assertEquals(counter+" OK LSUB completed.",response); 100 assertNull(br.readLine()); 101 102 103 } 104 public void testListAll() throws ProtocolException, MessagingException , IOException { 105 testLogin(); 106 BufferedReader br = handleRequestReader((++counter)+" LIST \""+USER_MAILBOX_ROOT+"\" \"*\"\n"); 107 String response=null; 108 String start="* LIST () \".\" "; 109 while ((response=br.readLine())!=null) { 110 System.out.println("Parsing "+response); 111 if (response.charAt(0)=='*') { 112 assertTrue(response.startsWith(start)); 113 String name=response.substring(start.length()); 114 assertTrue(existingSet.remove(name)); 115 116 } else { 117 break; 118 } 119 } 120 assertEquals(0,existingSet.size()); 121 assertEquals(counter+" OK LIST completed.",response); 122 assertNull(br.readLine()); 123 } 124 125 126 public void testSelectNonEmpty() throws ProtocolException, MessagingException , IOException , MailboxManagerException { 127 long uidv=getUidValidity(USER_MAILBOX_ROOT+".test"); 128 MimeMessage [] msgs=MessageGenerator.generateSimpleMessages(5); 129 msgs[0].setFlag(Flag.SEEN,true); 130 msgs[1].setFlag(Flag.SEEN,true); 131 addUIDMessagesOpen(USER_MAILBOX_ROOT+".test",msgs); 132 msgs=MessageGenerator.generateSimpleMessages(5); 133 appendMessagesClosed(USER_MAILBOX_ROOT+".test",msgs); 134 testLogin(); 135 String command=" SELECT \"test\""; 136 137 UnsolicitedResponseGenerator rg=new UnsolicitedResponseGenerator(); 138 rg.addExists(10); 139 rg.addRecent(5); 140 rg.addFlags(); 141 rg.addUidValidity(uidv); 142 rg.addFirstUnseen(3); 143 rg.addPermanentFlags(); 144 145 String statusResponse="OK [READ-WRITE] SELECT completed."; 146 147 verify(command,rg.getResponseSet(),statusResponse); 148 } 149 150 151 152 public void testAppendToSelectedInbox() throws ProtocolException, MessagingException , IOException , MailboxManagerException { 153 testLogin(); 154 155 long uidv=getUidValidity(USER_MAILBOX_ROOT+".test"); 156 157 String command=" SELECT \"test\""; 158 159 UnsolicitedResponseGenerator rg=new UnsolicitedResponseGenerator(); 160 rg.addExists(0); 161 rg.addRecent(0); 162 rg.addFlags(); 163 rg.addUidValidity(uidv); 164 rg.addFirstUnseen(0); 165 rg.addPermanentFlags(); 166 167 String statusResponse="OK [READ-WRITE] SELECT completed."; 168 169 verify(command,rg.getResponseSet(),statusResponse); 170 171 172 173 MimeMessage mm=MessageGenerator.generateSimpleMessage(); 174 String content=MessageGenerator.messageContentToString(mm); 175 String request=" append \"test\" (\\Seen) {"+content.length()+"+}\n"; 176 request += content; 177 178 rg=new UnsolicitedResponseGenerator(); 179 rg.addRecent(0); 180 rg.addExists(1); 181 182 verify(request,rg.getResponseSet(),"OK APPEND completed."); 183 184 Message [] msgs=getMessages(USER_MAILBOX_ROOT+".test"); 185 assertEquals(1,msgs.length); 186 assertEquals(content,MessageGenerator.messageContentToString(msgs[0])); 187 } 188 } 189 190 191 | Popular Tags |