KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > columba > ristretto > pop3 > protocol > POP3Protocol2Test


1 /* ***** BEGIN LICENSE BLOCK *****
2  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
3  *
4  * The contents of this file are subject to the Mozilla Public License Version
5  * 1.1 (the "License"); you may not use this file except in compliance with
6  * the License. You may obtain a copy of the License at
7  * http://www.mozilla.org/MPL/
8  *
9  * Software distributed under the License is distributed on an "AS IS" basis,
10  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11  * for the specific language governing rights and limitations under the
12  * License.
13  *
14  * The Original Code is Ristretto Mail API.
15  *
16  * The Initial Developers of the Original Code are
17  * Timo Stich and Frederik Dietz.
18  * Portions created by the Initial Developers are Copyright (C) 2004
19  * All Rights Reserved.
20  *
21  * Contributor(s):
22  *
23  * Alternatively, the contents of this file may be used under the terms of
24  * either the GNU General Public License Version 2 or later (the "GPL"), or
25  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
26  * in which case the provisions of the GPL or the LGPL are applicable instead
27  * of those above. If you wish to allow use of your version of this file only
28  * under the terms of either the GPL or the LGPL, and not to allow others to
29  * use your version of this file under the terms of the MPL, indicate your
30  * decision by deleting the provisions above and replace them with the notice
31  * and other provisions required by the GPL or the LGPL. If you do not delete
32  * the provisions above, a recipient may use your version of this file under
33  * the terms of any one of the MPL, the GPL or the LGPL.
34  *
35  * ***** END LICENSE BLOCK ***** */

36 package org.columba.ristretto.pop3.protocol;
37
38 import java.io.IOException JavaDoc;
39 import java.util.logging.Level JavaDoc;
40 import java.util.logging.Logger JavaDoc;
41
42 import junit.framework.TestCase;
43
44 import org.columba.ristretto.io.CharSequenceSource;
45 import org.columba.ristretto.io.Source;
46 import org.columba.ristretto.io.StreamUtils;
47 import org.columba.ristretto.pop3.POP3Exception;
48 import org.columba.ristretto.pop3.POP3Protocol;
49 import org.columba.ristretto.pop3.ScanListEntry;
50 import org.columba.ristretto.pop3.UidListEntry;
51 import org.columba.ristretto.testserver.SimpleTestServerSession;
52 import org.columba.ristretto.testserver.TestServer;
53
54 public class POP3Protocol2Test extends TestCase {
55
56     public void testOpenPort() throws IOException JavaDoc, POP3Exception {
57        
58         
59         SimpleTestServerSession testSession = new SimpleTestServerSession("+OK <test@timestamp>\r\n", "QUIT\r\n");
60         testSession.addDialog("QUIT\r\n", "+OK und tschuess\r\n");
61
62         TestServer testServer = new TestServer(50110, testSession);
63
64         POP3Protocol protocol = new POP3Protocol("localhost", 50110);
65         
66          protocol.openPort();
67         assertTrue( protocol.getState() == POP3Protocol.AUTHORIZATION);
68         
69         protocol.quit();
70         
71         testServer.stop();
72     }
73     
74     public void testPlainLogin() throws IOException JavaDoc, POP3Exception {
75         SimpleTestServerSession testSession = new SimpleTestServerSession("+OK <test@timestamp>\r\n", "QUIT\r\n");
76         testSession.addDialog("USER test\r\n", "+OK\r\n");
77         testSession.addDialog("PASS star\r\n", "+OK user authenticated\r\n");
78         testSession.addDialog("QUIT\r\n", "+OK und tschuess\r\n");
79
80         TestServer testServer = new TestServer(50110, testSession);
81
82         POP3Protocol protocol = new POP3Protocol("localhost", 50110);
83         
84         protocol.openPort();
85         assertTrue( protocol.getState() == POP3Protocol.AUTHORIZATION);
86         
87         protocol.userPass("test", "star".toCharArray());
88         assertTrue( protocol.getState() == POP3Protocol.TRANSACTION);
89         
90         protocol.quit();
91         
92         testServer.stop();
93     }
94
95
96     public void testApopLogin() throws IOException JavaDoc, POP3Exception {
97         SimpleTestServerSession testSession = new SimpleTestServerSession("+OK POP3 server ready <1896.697170952@dbc.mtview.ca.us>\r\n", "QUIT\r\n");
98         testSession.addDialog("APOP mrose c4c9334bac560ecc979e58001b3e22fb\r\n", "+OK\r\n");
99         testSession.addDialog("QUIT\r\n", "+OK und tschuess\r\n");
100
101         TestServer testServer = new TestServer(50110, testSession);
102
103         POP3Protocol protocol = new POP3Protocol("localhost", 50110);
104         
105         protocol.openPort();
106         assertTrue( protocol.getState() == POP3Protocol.AUTHORIZATION);
107         
108         protocol.apop("mrose", "tanstaaf".toCharArray());
109         assertTrue( protocol.getState() == POP3Protocol.TRANSACTION);
110         
111         protocol.quit();
112         
113         testServer.stop();
114     }
115     
116     public void testLoginServerDrop() throws IOException JavaDoc, POP3Exception {
117         SimpleTestServerSession testSession = new SimpleTestServerSession("+OK <test@timestamp>\r\n", "USER test\r\n");
118         testSession.addDialog("USER test\r\n", "");
119
120         TestServer testServer = new TestServer(50110, testSession);
121
122         POP3Protocol protocol = new POP3Protocol("localhost", 50110);
123         
124         protocol.openPort();
125         assertTrue( protocol.getState() == POP3Protocol.AUTHORIZATION);
126         
127         try {
128             protocol.userPass("test", "star".toCharArray());
129             assertTrue(false);
130         } catch (IOException JavaDoc e) {
131             assertTrue(true);
132         }
133         
134         assertTrue( protocol.getState() == POP3Protocol.NOT_CONNECTED);
135         
136         testServer.stop();
137     }
138     
139     
140     public void testStat() throws IOException JavaDoc, POP3Exception {
141         SimpleTestServerSession testSession = new SimpleTestServerSession("+OK <test@timestamp>\r\n", "QUIT\r\n");
142         testSession.addDialog("USER test\r\n", "+OK\r\n");
143         testSession.addDialog("PASS star\r\n", "+OK user authenticated\r\n");
144         testSession.addDialog("STAT\r\n", "+OK 2 230\r\n");
145         testSession.addDialog("QUIT\r\n", "+OK und tschuess\r\n");
146
147         TestServer testServer = new TestServer(50110, testSession);
148
149         POP3Protocol protocol = new POP3Protocol("localhost", 50110);
150         
151         protocol.openPort();
152         assertTrue( protocol.getState() == POP3Protocol.AUTHORIZATION);
153         
154         protocol.userPass("test", "star".toCharArray());
155         assertTrue( protocol.getState() == POP3Protocol.TRANSACTION);
156         
157         int[] stat = protocol.stat();
158         assertTrue( stat[0] == 2);
159         assertTrue( stat[1] == 230);
160         assertTrue( protocol.getState() == POP3Protocol.TRANSACTION);
161         
162         protocol.quit();
163         
164         testServer.stop();
165     }
166
167
168     public void testList() throws IOException JavaDoc, POP3Exception {
169         SimpleTestServerSession testSession = new SimpleTestServerSession("+OK <test@timestamp>\r\n", "QUIT\r\n");
170         testSession.addDialog("USER test\r\n", "+OK\r\n");
171         testSession.addDialog("PASS star\r\n", "+OK user authenticated\r\n");
172         testSession.addDialog("LIST\r\n", "+OK 2 messages\r\n1 150\r\n2 230\r\n.\r\n");
173         testSession.addDialog("QUIT\r\n", "+OK und tschuess\r\n");
174
175         TestServer testServer = new TestServer(50110, testSession);
176
177         POP3Protocol protocol = new POP3Protocol("localhost", 50110);
178         
179         protocol.openPort();
180         assertTrue( protocol.getState() == POP3Protocol.AUTHORIZATION);
181         
182         protocol.userPass("test", "star".toCharArray());
183         assertTrue( protocol.getState() == POP3Protocol.TRANSACTION);
184         
185         ScanListEntry[] list = protocol.list();
186         assertTrue( list.length == 2);
187         assertTrue( list[0].getIndex() == 1);
188         assertTrue( list[0].getSize() == 150);
189         
190         assertTrue( list[1].getIndex() == 2);
191         assertTrue( list[1].getSize() == 230);
192         
193         assertTrue( protocol.getState() == POP3Protocol.TRANSACTION);
194         
195         protocol.quit();
196         
197         testServer.stop();
198     }
199
200
201     public void testListParam() throws IOException JavaDoc, POP3Exception {
202         SimpleTestServerSession testSession = new SimpleTestServerSession("+OK <test@timestamp>\r\n", "QUIT\r\n");
203         testSession.addDialog("USER test\r\n", "+OK\r\n");
204         testSession.addDialog("PASS star\r\n", "+OK user authenticated\r\n");
205         testSession.addDialog("LIST 2\r\n", "+OK 2 230\r\n");
206         testSession.addDialog("QUIT\r\n", "+OK und tschuess\r\n");
207
208         TestServer testServer = new TestServer(50110, testSession);
209
210         POP3Protocol protocol = new POP3Protocol("localhost", 50110);
211         
212         protocol.openPort();
213         assertTrue( protocol.getState() == POP3Protocol.AUTHORIZATION);
214         
215         protocol.userPass("test", "star".toCharArray());
216         assertTrue( protocol.getState() == POP3Protocol.TRANSACTION);
217         
218         ScanListEntry list = protocol.list(2);
219         assertTrue( list.getIndex() == 2);
220         assertTrue( list.getSize() == 230);
221         
222         assertTrue( protocol.getState() == POP3Protocol.TRANSACTION);
223         
224         protocol.quit();
225         
226         testServer.stop();
227     }
228     
229     public void testRetrSizeOk() throws IOException JavaDoc, POP3Exception {
230         SimpleTestServerSession testSession = new SimpleTestServerSession("+OK <test@timestamp>\r\n", "QUIT\r\n");
231         testSession.addDialog("USER test\r\n", "+OK\r\n");
232         testSession.addDialog("PASS star\r\n", "+OK user authenticated\r\n");
233         testSession.addDialog("RETR 1\r\n", "+OK 150 octets\r\nThis is a simple Test\r\nmessage\r\n.. with a dot at start\r\n.\r\n");
234         testSession.addDialog("QUIT\r\n", "+OK und tschuess\r\n");
235
236         TestServer testServer = new TestServer(50110, testSession);
237
238         POP3Protocol protocol = new POP3Protocol("localhost", 50110);
239         
240         String JavaDoc messageOriginal = "This is a simple Test\r\nmessage\r\n. with a dot at start\r\n";
241         
242         protocol.openPort();
243         assertTrue( protocol.getState() == POP3Protocol.AUTHORIZATION);
244         
245         protocol.userPass("test", "star".toCharArray());
246         assertTrue( protocol.getState() == POP3Protocol.TRANSACTION);
247         
248         
249         Source message = new CharSequenceSource( StreamUtils.readInString( protocol.retr(1 ,messageOriginal.length()) ) );
250         assertEquals( messageOriginal, message.toString());
251         assertTrue( protocol.getState() == POP3Protocol.TRANSACTION);
252         
253         protocol.quit();
254         
255         testServer.stop();
256     }
257     
258     public void testRetrSizeSmaller() throws IOException JavaDoc, POP3Exception {
259         SimpleTestServerSession testSession = new SimpleTestServerSession("+OK <test@timestamp>\r\n", "QUIT\r\n");
260         testSession.addDialog("USER test\r\n", "+OK\r\n");
261         testSession.addDialog("PASS star\r\n", "+OK user authenticated\r\n");
262         testSession.addDialog("RETR 1\r\n", "+OK 150 octets\r\nThis is a simple Test\r\nmessage\r\n.. with a dot at start\r\n.\r\n");
263         testSession.addDialog("QUIT\r\n", "+OK und tschuess\r\n");
264
265         TestServer testServer = new TestServer(50110, testSession);
266
267         POP3Protocol protocol = new POP3Protocol("localhost", 50110);
268         
269         String JavaDoc messageOriginal = "This is a simple Test\r\nmessage\r\n. with a dot at start\r\n";
270         
271         protocol.openPort();
272         assertTrue( protocol.getState() == POP3Protocol.AUTHORIZATION);
273         
274         protocol.userPass("test", "star".toCharArray());
275         assertTrue( protocol.getState() == POP3Protocol.TRANSACTION);
276         
277         
278         Source message = new CharSequenceSource( StreamUtils.readInString( protocol.retr(1 ,messageOriginal.length()-5) ) );
279         assertEquals( messageOriginal, message.toString());
280         assertTrue( protocol.getState() == POP3Protocol.TRANSACTION);
281         
282         protocol.quit();
283         
284         testServer.stop();
285     }
286
287     public void testRetrSizeBigger() throws IOException JavaDoc, POP3Exception {
288         SimpleTestServerSession testSession = new SimpleTestServerSession("+OK <test@timestamp>\r\n", "QUIT\r\n");
289         testSession.addDialog("USER test\r\n", "+OK\r\n");
290         testSession.addDialog("PASS star\r\n", "+OK user authenticated\r\n");
291         testSession.addDialog("RETR 1\r\n", "+OK 150 octets\r\nThis is a simple Test\r\nmessage\r\n.. with a dot at start\r\n.\r\n");
292         testSession.addDialog("QUIT\r\n", "+OK und tschuess\r\n");
293
294         TestServer testServer = new TestServer(50110, testSession);
295
296         POP3Protocol protocol = new POP3Protocol("localhost", 50110);
297         
298         String JavaDoc messageOriginal = "This is a simple Test\r\nmessage\r\n. with a dot at start\r\n";
299         
300         protocol.openPort();
301         assertTrue( protocol.getState() == POP3Protocol.AUTHORIZATION);
302         
303         protocol.userPass("test", "star".toCharArray());
304         assertTrue( protocol.getState() == POP3Protocol.TRANSACTION);
305         
306         
307         Source message = new CharSequenceSource( StreamUtils.readInString( protocol.retr(1 ,messageOriginal.length()+5) ) );
308         assertEquals( messageOriginal, message.toString());
309         assertTrue( protocol.getState() == POP3Protocol.TRANSACTION);
310         
311         protocol.quit();
312         
313         testServer.stop();
314     }
315     
316     
317     public void testDele() throws IOException JavaDoc, POP3Exception {
318         SimpleTestServerSession testSession = new SimpleTestServerSession("+OK <test@timestamp>\r\n", "QUIT\r\n");
319         testSession.addDialog("USER test\r\n", "+OK\r\n");
320         testSession.addDialog("PASS star\r\n", "+OK user authenticated\r\n");
321         testSession.addDialog("DELE 1\r\n", "+OK\r\n");
322         testSession.addDialog("QUIT\r\n", "+OK und tschuess\r\n");
323
324         TestServer testServer = new TestServer(50110, testSession);
325
326         POP3Protocol protocol = new POP3Protocol("localhost", 50110);
327         
328         protocol.openPort();
329         assertTrue( protocol.getState() == POP3Protocol.AUTHORIZATION);
330         
331         protocol.userPass("test", "star".toCharArray());
332         assertTrue( protocol.getState() == POP3Protocol.TRANSACTION);
333         
334         assertTrue( protocol.dele(1));
335         assertTrue( protocol.getState() == POP3Protocol.TRANSACTION);
336         
337         protocol.quit();
338         
339         testServer.stop();
340     }
341
342     public void testNoop() throws IOException JavaDoc, POP3Exception {
343         SimpleTestServerSession testSession = new SimpleTestServerSession("+OK <test@timestamp>\r\n", "QUIT\r\n");
344         testSession.addDialog("USER test\r\n", "+OK\r\n");
345         testSession.addDialog("PASS star\r\n", "+OK user authenticated\r\n");
346         testSession.addDialog("NOOP\r\n", "+OK\r\n");
347         testSession.addDialog("QUIT\r\n", "+OK und tschuess\r\n");
348
349         TestServer testServer = new TestServer(50110, testSession);
350
351         POP3Protocol protocol = new POP3Protocol("localhost", 50110);
352         
353         protocol.openPort();
354         assertTrue( protocol.getState() == POP3Protocol.AUTHORIZATION);
355         
356         protocol.userPass("test", "star".toCharArray());
357         assertTrue( protocol.getState() == POP3Protocol.TRANSACTION);
358         
359         protocol.noop();
360         assertTrue( protocol.getState() == POP3Protocol.TRANSACTION);
361         
362         protocol.quit();
363         
364         testServer.stop();
365     }
366     
367     public void testRset() throws IOException JavaDoc, POP3Exception {
368         SimpleTestServerSession testSession = new SimpleTestServerSession("+OK <test@timestamp>\r\n", "QUIT\r\n");
369         testSession.addDialog("USER test\r\n", "+OK\r\n");
370         testSession.addDialog("PASS star\r\n", "+OK user authenticated\r\n");
371         testSession.addDialog("RSET\r\n", "+OK\r\n");
372         testSession.addDialog("QUIT\r\n", "+OK und tschuess\r\n");
373
374         TestServer testServer = new TestServer(50110, testSession);
375
376         POP3Protocol protocol = new POP3Protocol("localhost", 50110);
377         
378         protocol.openPort();
379         assertTrue( protocol.getState() == POP3Protocol.AUTHORIZATION);
380         
381         protocol.userPass("test", "star".toCharArray());
382         assertTrue( protocol.getState() == POP3Protocol.TRANSACTION);
383         
384         protocol.rset();
385         assertTrue( protocol.getState() == POP3Protocol.TRANSACTION);
386         
387         protocol.quit();
388         
389         testServer.stop();
390     }
391
392     public void testTop() throws IOException JavaDoc, POP3Exception {
393         SimpleTestServerSession testSession = new SimpleTestServerSession("+OK <test@timestamp>\r\n", "QUIT\r\n");
394         testSession.addDialog("USER test\r\n", "+OK\r\n");
395         testSession.addDialog("PASS star\r\n", "+OK user authenticated\r\n");
396         testSession.addDialog("TOP 1 1\r\n", "+OK\r\nThis is the first line\r\n.\r\n");
397         testSession.addDialog("QUIT\r\n", "+OK und tschuess\r\n");
398
399         TestServer testServer = new TestServer(50110, testSession);
400
401         POP3Protocol protocol = new POP3Protocol("localhost", 50110);
402         
403         protocol.openPort();
404         assertTrue( protocol.getState() == POP3Protocol.AUTHORIZATION);
405         
406         protocol.userPass("test", "star".toCharArray());
407         assertTrue( protocol.getState() == POP3Protocol.TRANSACTION);
408         
409         Source top = protocol.top(1,1);
410         assertTrue( top.toString().equals("This is the first line\r\n"));
411         assertTrue( protocol.getState() == POP3Protocol.TRANSACTION);
412         
413         protocol.quit();
414         
415         testServer.stop();
416     }
417
418     public void testUidl() throws IOException JavaDoc, POP3Exception {
419         SimpleTestServerSession testSession = new SimpleTestServerSession("+OK <test@timestamp>\r\n", "QUIT\r\n");
420         testSession.addDialog("USER test\r\n", "+OK\r\n");
421         testSession.addDialog("PASS star\r\n", "+OK user authenticated\r\n");
422         testSession.addDialog("UIDL\r\n", "+OK 2 messages\r\n1 uid1\r\n2 uid2\r\n.\r\n");
423         testSession.addDialog("QUIT\r\n", "+OK und tschuess\r\n");
424
425         TestServer testServer = new TestServer(50110, testSession);
426
427         POP3Protocol protocol = new POP3Protocol("localhost", 50110);
428         
429         protocol.openPort();
430         assertTrue( protocol.getState() == POP3Protocol.AUTHORIZATION);
431         
432         protocol.userPass("test", "star".toCharArray());
433         assertTrue( protocol.getState() == POP3Protocol.TRANSACTION);
434         
435         UidListEntry[] list = protocol.uidl();
436         assertTrue( list.length == 2);
437         assertTrue( list[0].getIndex() == 1);
438         assertTrue( list[0].getUid().equals("uid1"));
439         
440         assertTrue( list[1].getIndex() == 2);
441         assertTrue( list[1].getUid().equals("uid2"));
442         
443         assertTrue( protocol.getState() == POP3Protocol.TRANSACTION);
444         
445         protocol.quit();
446         
447         testServer.stop();
448     }
449
450
451     public void testUidlParam() throws IOException JavaDoc, POP3Exception {
452         SimpleTestServerSession testSession = new SimpleTestServerSession("+OK <test@timestamp>\r\n", "QUIT\r\n");
453         testSession.addDialog("USER test\r\n", "+OK\r\n");
454         testSession.addDialog("PASS star\r\n", "+OK user authenticated\r\n");
455         testSession.addDialog("UIDL 2\r\n", "+OK 2 uid2\r\n");
456         testSession.addDialog("QUIT\r\n", "+OK und tschuess\r\n");
457
458         TestServer testServer = new TestServer(50110, testSession);
459
460         POP3Protocol protocol = new POP3Protocol("localhost", 50110);
461         
462         protocol.openPort();
463         assertTrue( protocol.getState() == POP3Protocol.AUTHORIZATION);
464         
465         protocol.userPass("test", "star".toCharArray());
466         assertTrue( protocol.getState() == POP3Protocol.TRANSACTION);
467         
468         UidListEntry list = protocol.uidl(2);
469         assertTrue( list.getIndex() == 2);
470         assertTrue( list.getUid().equals("uid2"));
471         
472         assertTrue( protocol.getState() == POP3Protocol.TRANSACTION);
473         
474         protocol.quit();
475         
476         testServer.stop();
477     }
478     
479     public void testCapa() throws IOException JavaDoc, POP3Exception {
480         SimpleTestServerSession testSession = new SimpleTestServerSession("+OK <test@timestamp>\r\n", "QUIT\r\n");
481         testSession.addDialog("CAPA\r\n", "+OK\r\nTOP\r\nAUTH\r\n.\r\n");
482         testSession.addDialog("QUIT\r\n", "+OK und tschuess\r\n");
483
484         TestServer testServer = new TestServer(50110, testSession);
485
486         POP3Protocol protocol = new POP3Protocol("localhost", 50110);
487         
488         protocol.openPort();
489         assertTrue( protocol.getState() == POP3Protocol.AUTHORIZATION);
490         
491         String JavaDoc[] list = protocol.capa();
492         
493         assertTrue( list.length == 2);
494         assertTrue( list[0].equals("TOP"));
495         assertTrue( list[1].equals("AUTH"));
496         
497         protocol.quit();
498         
499         testServer.stop();
500     }
501     
502     
503     /**
504      * @see junit.framework.TestCase#setUp()
505      */

506     protected void setUp() throws Exception JavaDoc {
507         Logger.getLogger("org.columba.ristretto").setLevel(Level.ALL);
508     }
509
510 }
511
Popular Tags