1 19 20 package org.apache.james.imapserver.handler.session; 21 22 import java.io.BufferedReader ; 23 import java.io.ByteArrayInputStream ; 24 import java.io.ByteArrayOutputStream ; 25 import java.io.IOException ; 26 import java.io.StringReader ; 27 import java.util.ArrayList ; 28 import java.util.Arrays ; 29 import java.util.Date ; 30 import java.util.HashSet ; 31 import java.util.Iterator ; 32 import java.util.LinkedList ; 33 import java.util.List ; 34 import java.util.Set ; 35 36 import javax.mail.Flags ; 37 import javax.mail.MessagingException ; 38 import javax.mail.Flags.Flag; 39 import javax.mail.internet.MimeMessage ; 40 41 import org.apache.avalon.framework.logger.LogEnabled; 42 import org.apache.james.imapserver.ImapRequestHandler; 43 import org.apache.james.imapserver.ImapSession; 44 import org.apache.james.imapserver.ImapSessionImpl; 45 import org.apache.james.imapserver.ProtocolException; 46 import org.apache.james.imapserver.TestConstants; 47 import org.apache.james.imapserver.client.Command; 48 import org.apache.james.imapserver.mock.MockImapHandler; 49 import org.apache.james.imapserver.mock.MockImapHandlerConfigurationData; 50 import org.apache.james.imapserver.mock.MockUser; 51 import org.apache.james.imapserver.store.MailboxException; 52 import org.apache.james.mailboxmanager.GeneralMessageSet; 53 import org.apache.james.mailboxmanager.ListResult; 54 import org.apache.james.mailboxmanager.MailboxManagerException; 55 import org.apache.james.mailboxmanager.MessageResult; 56 import org.apache.james.mailboxmanager.impl.GeneralMessageSetImpl; 57 import org.apache.james.mailboxmanager.mailbox.GeneralMailboxSession; 58 import org.apache.james.mailboxmanager.mailbox.ImapMailboxSession; 59 import org.apache.james.mailboxmanager.manager.MailboxManager; 60 import org.apache.james.test.mock.avalon.MockLogger; 61 import org.jmock.MockObjectTestCase; 62 63 public abstract class AbstractSessionTest extends MockObjectTestCase implements TestConstants { 64 65 MailboxManager mailboxManager; 66 67 private ImapRequestHandler handler; 68 private ImapSession session; 69 70 int counter=0; 71 72 public AbstractSessionTest() { 73 } 74 75 public void setUp() throws MailboxException, MessagingException , IOException , MailboxManagerException 76 { 77 78 MockImapHandlerConfigurationData theConfigData = new MockImapHandlerConfigurationData(); 79 theConfigData.getMailboxManagerProvider().deleteEverything(); 80 session = new ImapSessionImpl(theConfigData.getMailboxManagerProvider(), 81 theConfigData.getUsersRepository(), new MockImapHandler(), 82 HOST_NAME, HOST_ADDRESS); 83 ((LogEnabled)session).enableLogging(new MockLogger()); 84 handler = new ImapRequestHandler(); 85 handler.enableLogging(new MockLogger()); 86 mailboxManager=theConfigData.getMailboxManagerProvider().getMailboxManagerInstance(new MockUser()); 87 88 } 89 90 void createFolders(String [] folders) throws MailboxManagerException { 91 for (int i=0; i<folders.length; i++) { 92 mailboxManager.createMailbox(folders[i]); 93 } 94 } 95 96 public void appendMessagesClosed(String folder,MimeMessage [] msgs) throws MailboxManagerException, MessagingException { 97 GeneralMailboxSession mailbox=getImapMailboxSession(folder); 98 for (int i = 0; i < msgs.length; i++) { 99 msgs[i].setFlags(new Flags (Flags.Flag.RECENT), true); 100 mailbox.appendMessage(msgs[i],new Date (),MessageResult.NOTHING); 101 102 } 103 mailbox.close(); 104 } 105 106 public long[] addUIDMessagesOpen(String folder,MimeMessage [] msgs) throws MailboxManagerException, MessagingException { 107 GeneralMailboxSession mailbox=getImapMailboxSession(folder); 108 long[] uids=new long[msgs.length]; 109 for (int i = 0; i < msgs.length; i++) { 110 msgs[i].setFlags(new Flags (Flags.Flag.RECENT), false); 111 uids[i]=mailbox.appendMessage(msgs[i],new Date (),MessageResult.UID).getUid(); 112 } 113 mailbox.close(); 114 return uids; 115 } 116 public long getUidValidity(String folder) throws MailboxManagerException { 117 ImapMailboxSession mailbox=getImapMailboxSession(folder); 118 long uidv=mailbox.getUidValidity(); 119 mailbox.close(); 120 return uidv; 121 } 122 123 124 public long getUidNext(String folder) throws MailboxManagerException { 125 ImapMailboxSession mailbox=getImapMailboxSession(folder); 126 long uidNext=mailbox.getUidNext(); 127 mailbox.close(); 128 return uidNext; 129 } 130 131 public MimeMessage [] getMessages(String folder) throws MailboxManagerException { 132 GeneralMailboxSession mailbox=getImapMailboxSession(folder); 133 MessageResult[] messageResults=mailbox.getMessages(GeneralMessageSetImpl.all(),MessageResult.MIME_MESSAGE); 134 MimeMessage [] mms=new MimeMessage [messageResults.length]; 135 for (int i = 0; i < messageResults.length; i++) { 136 mms[i]=messageResults[i].getMimeMessage(); 137 } 138 mailbox.close(); 139 return mms; 140 } 141 142 public long[] getUids(String folder) throws MailboxManagerException { 143 GeneralMailboxSession mailbox=getImapMailboxSession(folder); 144 MessageResult[] messageResults=mailbox.getMessages(GeneralMessageSetImpl.all(),MessageResult.UID); 145 146 long[] uids=new long[messageResults.length]; 147 for (int i = 0; i < messageResults.length; i++) { 148 uids[i]=messageResults[i].getUid(); 149 } 150 mailbox.close(); 151 return uids; 152 } 153 154 public boolean folderExists(String folder) throws MailboxManagerException { 155 return mailboxManager.existsMailbox(folder); 156 } 157 158 public String [] getFolderNames() throws MailboxManagerException { 159 ListResult[] listResults=mailboxManager.list("","*",false); 160 String [] names=new String [listResults.length]; 161 for (int i = 0; i < listResults.length; i++) { 162 names[i]=listResults[i].getName(); 163 } 164 return names; 165 } 166 167 public void verifyFolderList(String [] expected,String [] found) { 168 Set expectedSet=new HashSet (Arrays.asList(expected)); 169 Set foundSet=new HashSet (Arrays.asList(found)); 170 Set missing=new HashSet (expectedSet); 171 missing.removeAll(foundSet); 172 Set notExpected=new HashSet (foundSet); 173 notExpected.removeAll(expectedSet); 174 assertEquals("Missing folders :"+missing,0,missing.size()); 175 assertEquals("Not expected folders :"+notExpected,0,notExpected.size()); 176 } 177 178 public boolean isOpen(String folder) throws MessagingException { 179 return false; 181 } 182 public void useFolder(String folder) { 183 184 } 185 public void freeFolder(String folder) { 186 187 } 188 189 public void deleteAll(String folder) throws MailboxManagerException { 190 ImapMailboxSession mailbox=getImapMailboxSession(folder); 191 mailbox.setFlags(new Flags (Flag.DELETED),true,false,GeneralMessageSetImpl.all(),null); 192 mailbox.expunge(GeneralMessageSetImpl.all(),MessageResult.NOTHING); 193 mailbox.close(); 194 } 195 public BufferedReader handleRequestReader(String s) throws ProtocolException 196 { 197 return new BufferedReader (new StringReader (handleRequest(s))); 198 } 199 200 public String handleRequest(String s) throws ProtocolException 201 { 202 ByteArrayInputStream is = new ByteArrayInputStream (s.getBytes()); 203 ByteArrayOutputStream os = new ByteArrayOutputStream (); 204 System.out.println("IN :" + s); 205 handler.handleRequest(is, os, session); 206 String out = os.toString(); 207 System.out.println("OUT:" + out); 208 return out; 209 210 } 211 protected void verify(final String command, final Set requiredResponseSet, final String requiredStatusResponse) throws ProtocolException, IOException , MessagingException { 212 Command c=new Command() { 213 public List getExpectedResponseList() throws MessagingException , IOException { 214 return new LinkedList (requiredResponseSet); 215 } 216 public String getExpectedStatusResponse() { 217 return requiredStatusResponse; 218 } 219 public String getCommand() { 220 return command; 221 } 222 }; 223 verifyCommand(c); 224 } 225 226 protected void verifyCommand(Command command) throws ProtocolException, IOException , MessagingException { 227 228 BufferedReader br=handleRequestReader((++counter)+" "+command.getCommand()+"\n"); 229 Set requiredResponseSet=new HashSet (command.getExpectedResponseList()); 230 List rec=new ArrayList (); 231 String line; 232 while ((line=br.readLine())!=null) { 233 rec.add(line); 234 } 235 assertFalse("nothing received",rec.isEmpty()); 236 String statusResponse=(String )rec.get(rec.size()-1); 237 rec.remove(rec.size()-1); 238 Set responseSet=new HashSet (rec); 239 responseSet.removeAll(requiredResponseSet); 240 requiredResponseSet.removeAll(new HashSet (rec)); 241 if (responseSet.size()>0) { 242 System.out.println("Not awaitet responses: "+responseSet); 243 } 244 if (requiredResponseSet.size()>0) { 245 System.out.println("Missed responses: "+requiredResponseSet); 246 } 247 assertEquals("Missed responses: "+requiredResponseSet,0,requiredResponseSet.size()); 248 assertEquals("Not awaitet responses: "+responseSet,0,responseSet.size()); 249 assertEquals("Status respons differs",counter+" "+command.getExpectedStatusResponse(),statusResponse); 250 } 251 252 protected void verifyCommandOrdered(Command command) throws ProtocolException, IOException , MessagingException 253 { 254 BufferedReader br=handleRequestReader((++counter)+" "+command.getCommand()+"\n"); 255 256 int c=0; 257 for (Iterator it = command.getExpectedResponseList().iterator(); it.hasNext();) { 258 c++; 259 String expectedResponse = (String ) it.next(); 260 String [] expectedLines=expectedResponse.split("\r\n"); 262 for (int i = 0; i < expectedLines.length; i++) { 263 String readLine=br.readLine(); 264 assertNotNull("Unexpected end of response (response "+c+" line "+i+")",readLine); 265 assertEquals("Unexpected response line (response "+c+" line "+i+")",expectedLines[i],readLine); 266 } 267 } 268 269 String statusResponse=(String )br.readLine();; 270 assertEquals("Status response differs",counter+" "+command.getExpectedStatusResponse(),statusResponse); 271 String notExpected=br.readLine(); 272 assertNull("did expect eof",notExpected); 273 } 274 275 public void setFlags(String mailboxName,long fromUid,long toUid,Flags flags, boolean value, boolean replace) throws MailboxManagerException { 276 ImapMailboxSession mailbox=getImapMailboxSession(mailboxName); 277 mailbox.setFlags(flags, value, replace, GeneralMessageSetImpl.uidRange(fromUid, toUid), null); 278 mailbox.close(); 279 } 280 private ImapMailboxSession getImapMailboxSession(String mailboxName) throws MailboxManagerException { 281 int[] neededSets = new int[] {GeneralMessageSet.TYPE_UID}; 282 int neededResults= MessageResult.UID + MessageResult.MIME_MESSAGE + MessageResult.FLAGS; 283 ImapMailboxSession mailboxSession= mailboxManager.getImapMailboxSession(mailboxName); 284 return mailboxSession; 285 } 286 } 287 | Popular Tags |