KickJava   Java API By Example, From Geeks To Geeks.

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


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.IOException JavaDoc;
24 import java.util.Arrays JavaDoc;
25 import java.util.HashSet JavaDoc;
26 import java.util.Set JavaDoc;
27
28 import javax.mail.Message JavaDoc;
29 import javax.mail.MessagingException JavaDoc;
30 import javax.mail.Flags.Flag;
31 import javax.mail.internet.MimeMessage JavaDoc;
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 JavaDoc[] 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 JavaDoc existingSet = null;
46
47     public void setUp() throws MailboxException, MessagingException JavaDoc, IOException JavaDoc, MailboxManagerException
48     {
49         super.setUp();
50         existingSet=new HashSet JavaDoc(Arrays.asList(existing));
51         createFolders((String JavaDoc[]) existingSet.toArray(new String JavaDoc[0]));
52     }
53
54
55
56     
57     public void testLogin() throws ProtocolException, IOException JavaDoc, MessagingException JavaDoc
58     {
59         verifyCommand(new LoginCommand(USER_NAME,USER_PASSWORD));
60     }
61     
62     public void testCreate() throws ProtocolException, MessagingException JavaDoc, IOException JavaDoc, MailboxManagerException{
63         testLogin();
64         assertFalse(folderExists(USER_MAILBOX_ROOT+".Trash"));
65         String JavaDoc 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 JavaDoc, IOException JavaDoc {
71         testLogin();
72         String JavaDoc 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 JavaDoc, IOException JavaDoc {
77         testLogin();
78         String JavaDoc 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 JavaDoc, IOException JavaDoc {
83         testLogin();
84         BufferedReader JavaDoc br = handleRequestReader((++counter)+" LSUB \""+USER_MAILBOX_ROOT+"\" \"*\"\n");
85         String JavaDoc response=null;
86         String JavaDoc 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 JavaDoc 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 JavaDoc, IOException JavaDoc {
105         testLogin();
106         BufferedReader JavaDoc br = handleRequestReader((++counter)+" LIST \""+USER_MAILBOX_ROOT+"\" \"*\"\n");
107         String JavaDoc response=null;
108         String JavaDoc 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 JavaDoc 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 JavaDoc, IOException JavaDoc, MailboxManagerException {
127         long uidv=getUidValidity(USER_MAILBOX_ROOT+".test");
128         MimeMessage JavaDoc[] 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 JavaDoc 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 JavaDoc statusResponse="OK [READ-WRITE] SELECT completed.";
146     
147         verify(command,rg.getResponseSet(),statusResponse);
148     }
149     
150
151
152     public void testAppendToSelectedInbox() throws ProtocolException, MessagingException JavaDoc, IOException JavaDoc, MailboxManagerException {
153         testLogin();
154         
155         long uidv=getUidValidity(USER_MAILBOX_ROOT+".test");
156         
157         String JavaDoc 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 JavaDoc statusResponse="OK [READ-WRITE] SELECT completed.";
168     
169         verify(command,rg.getResponseSet(),statusResponse);
170         
171         
172         
173         MimeMessage JavaDoc mm=MessageGenerator.generateSimpleMessage();
174         String JavaDoc content=MessageGenerator.messageContentToString(mm);
175         String JavaDoc 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 JavaDoc[] 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