KickJava   Java API By Example, From Geeks To Geeks.

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


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.BufferedReader JavaDoc;
23 import java.io.ByteArrayInputStream JavaDoc;
24 import java.io.ByteArrayOutputStream JavaDoc;
25 import java.io.IOException JavaDoc;
26 import java.io.StringReader JavaDoc;
27 import java.util.ArrayList JavaDoc;
28 import java.util.Arrays JavaDoc;
29 import java.util.Date JavaDoc;
30 import java.util.HashSet JavaDoc;
31 import java.util.Iterator JavaDoc;
32 import java.util.LinkedList JavaDoc;
33 import java.util.List JavaDoc;
34 import java.util.Set JavaDoc;
35
36 import javax.mail.Flags JavaDoc;
37 import javax.mail.MessagingException JavaDoc;
38 import javax.mail.Flags.Flag;
39 import javax.mail.internet.MimeMessage JavaDoc;
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 JavaDoc, IOException JavaDoc, 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 JavaDoc[] 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 JavaDoc folder,MimeMessage JavaDoc[] msgs) throws MailboxManagerException, MessagingException JavaDoc {
97         GeneralMailboxSession mailbox=getImapMailboxSession(folder);
98         for (int i = 0; i < msgs.length; i++) {
99             msgs[i].setFlags(new Flags JavaDoc(Flags.Flag.RECENT), true);
100             mailbox.appendMessage(msgs[i],new Date JavaDoc(),MessageResult.NOTHING);
101             
102         }
103         mailbox.close();
104     }
105
106     public long[] addUIDMessagesOpen(String JavaDoc folder,MimeMessage JavaDoc[] msgs) throws MailboxManagerException, MessagingException JavaDoc {
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 JavaDoc(Flags.Flag.RECENT), false);
111             uids[i]=mailbox.appendMessage(msgs[i],new Date JavaDoc(),MessageResult.UID).getUid();
112         }
113         mailbox.close();
114         return uids;
115     }
116     public long getUidValidity(String JavaDoc 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 JavaDoc folder) throws MailboxManagerException {
125         ImapMailboxSession mailbox=getImapMailboxSession(folder);
126         long uidNext=mailbox.getUidNext();
127         mailbox.close();
128         return uidNext;
129     }
130     
131     public MimeMessage JavaDoc[] getMessages(String JavaDoc folder) throws MailboxManagerException {
132         GeneralMailboxSession mailbox=getImapMailboxSession(folder);
133         MessageResult[] messageResults=mailbox.getMessages(GeneralMessageSetImpl.all(),MessageResult.MIME_MESSAGE);
134         MimeMessage JavaDoc[] mms=new MimeMessage JavaDoc[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 JavaDoc 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 JavaDoc folder) throws MailboxManagerException {
155         return mailboxManager.existsMailbox(folder);
156     }
157     
158     public String JavaDoc[] getFolderNames() throws MailboxManagerException {
159         ListResult[] listResults=mailboxManager.list("","*",false);
160         String JavaDoc[] names=new String JavaDoc[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 JavaDoc[] expected,String JavaDoc[] found) {
168         Set JavaDoc expectedSet=new HashSet JavaDoc(Arrays.asList(expected));
169         Set JavaDoc foundSet=new HashSet JavaDoc(Arrays.asList(found));
170         Set JavaDoc missing=new HashSet JavaDoc(expectedSet);
171         missing.removeAll(foundSet);
172         Set JavaDoc notExpected=new HashSet JavaDoc(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 JavaDoc folder) throws MessagingException JavaDoc {
179         // TODO implement this
180
return false;
181     }
182     public void useFolder(String JavaDoc folder) {
183         
184     }
185     public void freeFolder(String JavaDoc folder) {
186         
187     }
188     
189     public void deleteAll(String JavaDoc folder) throws MailboxManagerException {
190         ImapMailboxSession mailbox=getImapMailboxSession(folder);
191         mailbox.setFlags(new Flags JavaDoc(Flag.DELETED),true,false,GeneralMessageSetImpl.all(),null);
192         mailbox.expunge(GeneralMessageSetImpl.all(),MessageResult.NOTHING);
193         mailbox.close();
194     }
195     public BufferedReader JavaDoc handleRequestReader(String JavaDoc s) throws ProtocolException
196     {
197         return new BufferedReader JavaDoc(new StringReader JavaDoc(handleRequest(s)));
198     }
199
200     public String JavaDoc handleRequest(String JavaDoc s) throws ProtocolException
201     {
202         ByteArrayInputStream JavaDoc is = new ByteArrayInputStream JavaDoc(s.getBytes());
203         ByteArrayOutputStream JavaDoc os = new ByteArrayOutputStream JavaDoc();
204         System.out.println("IN :" + s);
205         handler.handleRequest(is, os, session);
206         String JavaDoc out = os.toString();
207         System.out.println("OUT:" + out);
208         return out;
209         
210     }
211     protected void verify(final String JavaDoc command, final Set JavaDoc requiredResponseSet, final String JavaDoc requiredStatusResponse) throws ProtocolException, IOException JavaDoc, MessagingException JavaDoc {
212         Command c=new Command() {
213             public List JavaDoc getExpectedResponseList() throws MessagingException JavaDoc, IOException JavaDoc {
214                 return new LinkedList JavaDoc(requiredResponseSet);
215             }
216             public String JavaDoc getExpectedStatusResponse() {
217                 return requiredStatusResponse;
218             }
219             public String JavaDoc getCommand() {
220                 return command;
221             }
222         };
223         verifyCommand(c);
224     }
225     
226     protected void verifyCommand(Command command) throws ProtocolException, IOException JavaDoc, MessagingException JavaDoc {
227         
228         BufferedReader JavaDoc br=handleRequestReader((++counter)+" "+command.getCommand()+"\n");
229         Set JavaDoc requiredResponseSet=new HashSet JavaDoc(command.getExpectedResponseList());
230         List JavaDoc rec=new ArrayList JavaDoc();
231         String JavaDoc line;
232         while ((line=br.readLine())!=null) {
233              rec.add(line);
234         }
235         assertFalse("nothing received",rec.isEmpty());
236         String JavaDoc statusResponse=(String JavaDoc)rec.get(rec.size()-1);
237         rec.remove(rec.size()-1);
238         Set JavaDoc responseSet=new HashSet JavaDoc(rec);
239         responseSet.removeAll(requiredResponseSet);
240         requiredResponseSet.removeAll(new HashSet JavaDoc(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 JavaDoc, MessagingException JavaDoc
253     {
254         BufferedReader JavaDoc br=handleRequestReader((++counter)+" "+command.getCommand()+"\n");
255         
256         int c=0;
257         for (Iterator JavaDoc it = command.getExpectedResponseList().iterator(); it.hasNext();) {
258             c++;
259             String JavaDoc expectedResponse = (String JavaDoc) it.next();
260 // System.out.println("Expected: "+expectedResponse);
261
String JavaDoc[] expectedLines=expectedResponse.split("\r\n");
262             for (int i = 0; i < expectedLines.length; i++) {
263                 String JavaDoc 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 JavaDoc statusResponse=(String JavaDoc)br.readLine();;
270         assertEquals("Status response differs",counter+" "+command.getExpectedStatusResponse(),statusResponse);
271         String JavaDoc notExpected=br.readLine();
272         assertNull("did expect eof",notExpected);
273     }
274     
275     public void setFlags(String JavaDoc mailboxName,long fromUid,long toUid,Flags JavaDoc 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 JavaDoc 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