KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > net > pop3 > POP3ClientCommandsTest


1 /**
2  * Copyright 2001-2005 The Apache Software Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.apache.commons.net.pop3;
18
19 import junit.framework.TestCase;
20 import junit.framework.TestSuite;
21
22 import java.net.InetAddress JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.io.Reader JavaDoc;
25
26 /**
27  * @author <a HREF="mailto:commons-dev@apache.org">[Net]</a>
28  * @version $Id: POP3ClientCommandsTest.java 165675 2005-05-02 20:09:55Z rwinston $
29  *
30  * The POP3* tests all presume the existence of the following parameters:
31  * mailserver: localhost (running on the default port 110)
32  * account: username=test; password=password
33  * account: username=alwaysempty; password=password.
34  * mail: At least four emails in the test account and zero emails
35  * in the alwaysempty account
36  *
37  * If this won't work for you, you can change these parameters in the
38  * TestSetupParameters class.
39  *
40  * The tests were originally run on a default installation of James.
41  * Your mileage may vary based on the POP3 server you run the tests against.
42  * Some servers are more standards-compliant than others.
43  */

44 public class POP3ClientCommandsTest extends TestCase
45 {
46     POP3Client p = null;
47
48     String JavaDoc user = TestSetupParameters.user;
49     String JavaDoc emptyUser = TestSetupParameters.emptyuser;
50     String JavaDoc password = TestSetupParameters.password;
51     String JavaDoc mailhost = TestSetupParameters.mailhost;
52
53     /**
54      *
55      */

56     public POP3ClientCommandsTest(String JavaDoc name)
57     {
58         super(name);
59     }
60
61     /**
62      * Method suite.
63      * @return TestSuite
64      */

65     public static TestSuite suite()
66     {
67         return (new TestSuite(POP3ClientCommandsTest.class));
68     }
69
70     private void reset() throws IOException JavaDoc
71     {
72         //Case where this is the first time reset is called
73
if (p == null)
74         {
75             //Do nothing
76
}
77         else if (p.isConnected())
78         {
79             p.disconnect();
80         }
81         p = null;
82         p = new POP3Client();
83     }
84
85     private void connect() throws Exception JavaDoc
86     {
87         p.connect(InetAddress.getByName(mailhost));
88         assertTrue(p.isConnected());
89         assertEquals(POP3.AUTHORIZATION_STATE, p.getState());
90     }
91
92     private void login() throws Exception JavaDoc
93     {
94         assertTrue(p.login(user, password));
95         assertEquals(POP3.TRANSACTION_STATE, p.getState());
96     }
97
98     /**
99      *
100      *
101      */

102     public void testNoopCommand() throws Exception JavaDoc
103     {
104         reset();
105         connect();
106
107         //Should fail before authorization
108
assertFalse(p.noop());
109
110         //Should pass in transaction state
111
login();
112         assertTrue(p.noop());
113
114         //Should fail in update state
115
p.setState(POP3.UPDATE_STATE);
116         assertFalse(p.noop());
117     }
118
119     /**
120      *
121      *
122      */

123     public void testStatus() throws Exception JavaDoc
124     {
125         reset();
126         connect();
127
128         //Should fail in authorization state
129
assertNull(p.status());
130
131         //Should pass on a mailbox with mail in it
132
login();
133         POP3MessageInfo msg = p.status();
134         assertTrue(msg.number > 0);
135         assertTrue(msg.size > 0);
136         assertNull(msg.identifier);
137         p.logout();
138
139         //Should also pass on a mailbox with no mail in it
140
reset();
141         connect();
142         assertTrue(p.login(emptyUser, password));
143         POP3MessageInfo msg2 = p.status();
144         assertTrue(msg2.number == 0);
145         assertTrue(msg2.size == 0);
146         assertNull(msg2.identifier);
147         p.logout();
148
149         //Should fail in the 'update' state
150
reset();
151         connect();
152         login();
153         p.setState(POP3.UPDATE_STATE);
154         assertNull(p.status());
155     }
156
157     /**
158      *
159      *
160      */

161     public void testListMessagesOnFullMailbox() throws Exception JavaDoc
162     {
163         reset();
164         connect();
165         login();
166
167         POP3MessageInfo[] msg = p.listMessages();
168         assertTrue(msg.length > 0);
169
170         for(int i = 0; i < msg.length; i++)
171         {
172             assertNotNull(msg[i]);
173             assertTrue(msg[i].number == i + 1);
174             assertTrue(msg[i].size > 0);
175             assertNull(msg[i].identifier);
176         }
177
178         //Now test from the update state
179
p.setState(POP3.UPDATE_STATE);
180         msg = p.listMessages();
181         assertNull(msg);
182     }
183
184     /**
185      *
186      *
187      */

188     public void testListMessageOnFullMailbox() throws Exception JavaDoc
189     {
190         reset();
191         connect();
192         login();
193
194         //The first message is always at index 1
195
POP3MessageInfo msg = p.listMessage(1);
196         assertNotNull(msg);
197         assertTrue(msg.number == 1);
198         assertTrue(msg.size > 0);
199         assertNull(msg.identifier);
200
201         //Now retrieve a message from index 0
202
msg = p.listMessage(0);
203         assertNull(msg);
204
205         //Now retrieve a msg that is not there
206
msg = p.listMessage(100000);
207         assertNull(msg);
208
209         //Now retrieve a msg with a negative index
210
msg = p.listMessage(-2);
211         assertNull(msg);
212
213         //Now try to get a valid message from the update state
214
p.setState(POP3.UPDATE_STATE);
215         msg = p.listMessage(1);
216         assertNull(msg);
217     }
218
219     /**
220      *
221      *
222      */

223     public void testListMessagesOnEmptyMailbox() throws Exception JavaDoc
224     {
225         reset();
226         connect();
227         assertTrue(p.login(emptyUser, password));
228
229         POP3MessageInfo[] msg = p.listMessages();
230         assertTrue(msg.length == 0);
231
232         //Now test from the update state
233
p.setState(POP3.UPDATE_STATE);
234         msg = p.listMessages();
235         assertNull(msg);
236     }
237
238     /**
239      *
240      *
241      */

242     public void testListMessageOnEmptyMailbox() throws Exception JavaDoc
243     {
244         reset();
245         connect();
246         assertTrue(p.login(emptyUser, password));
247
248         //The first message is always at index 1
249
POP3MessageInfo msg = p.listMessage(1);
250         assertNull(msg);
251     }
252
253     /**
254      *
255      *
256      */

257     public void testListUniqueIDsOnFullMailbox() throws Exception JavaDoc
258     {
259         reset();
260         connect();
261         login();
262
263         POP3MessageInfo[] msg = p.listUniqueIdentifiers();
264         assertTrue(msg.length > 0);
265
266         for(int i = 0; i < msg.length; i++)
267         {
268             assertNotNull(msg[i]);
269             assertTrue(msg[i].number == i + 1);
270             assertNotNull(msg[i].identifier);
271         }
272
273         //Now test from the update state
274
p.setState(POP3.UPDATE_STATE);
275         msg = p.listUniqueIdentifiers();
276         assertNull(msg);
277     }
278
279     /**
280      *
281      *
282      */

283     public void testListUniqueIDOnFullMailbox() throws Exception JavaDoc
284     {
285         reset();
286         connect();
287         login();
288
289         //The first message is always at index 1
290
POP3MessageInfo msg = p.listUniqueIdentifier(1);
291         assertNotNull(msg);
292         assertTrue(msg.number == 1);
293         assertNotNull(msg.identifier);
294
295         //Now retrieve a message from index 0
296
msg = p.listUniqueIdentifier(0);
297         assertNull(msg);
298
299         //Now retrieve a msg that is not there
300
msg = p.listUniqueIdentifier(100000);
301         assertNull(msg);
302
303         //Now retrieve a msg with a negative index
304
msg = p.listUniqueIdentifier(-2);
305         assertNull(msg);
306
307         //Now try to get a valid message from the update state
308
p.setState(POP3.UPDATE_STATE);
309         msg = p.listUniqueIdentifier(1);
310         assertNull(msg);
311     }
312
313     /**
314      *
315      *
316      */

317     public void testListUniqueIDsOnEmptyMailbox() throws Exception JavaDoc
318     {
319         reset();
320         connect();
321         assertTrue(p.login(emptyUser, password));
322
323         POP3MessageInfo[] msg = p.listUniqueIdentifiers();
324         assertTrue(msg.length == 0);
325
326         //Now test from the update state
327
p.setState(POP3.UPDATE_STATE);
328         msg = p.listUniqueIdentifiers();
329         assertNull(msg);
330     }
331
332     /**
333      *
334      *
335      */

336     public void testListUniqueIdentifierOnEmptyMailbox() throws Exception JavaDoc
337     {
338         reset();
339         connect();
340         assertTrue(p.login(emptyUser, password));
341
342         //The first message is always at index 1
343
POP3MessageInfo msg = p.listUniqueIdentifier(1);
344         assertNull(msg);
345     }
346
347     /**
348      *
349      *
350      */

351     public void testRetrieveMessageOnFullMailbox() throws Exception JavaDoc
352     {
353         reset();
354         connect();
355         login();
356         int reportedSize = 0;
357         int actualSize = 0;
358
359         POP3MessageInfo[] msg = p.listMessages();
360         assertTrue(msg.length > 0);
361
362         for (int i = msg.length; i > 0; i--)
363         {
364             reportedSize = msg[i - 1].size;
365             Reader JavaDoc r = p.retrieveMessage(i);
366             assertNotNull(r);
367
368             int delaycount = 0;
369             if (!r.ready())
370             {
371                 //Give the reader time to get the message
372
//from the server
373
Thread.sleep(500);
374                 delaycount++;
375                 //but don't wait too long
376
if (delaycount == 4)
377                 {
378                     break;
379                 }
380             }
381             while(r.ready())
382             {
383                 r.read();
384                 actualSize++;
385             }
386             //Due to variations in line termination
387
//on different platforms, the actual
388
//size may vary slightly. On Win2KPro, the
389
//actual size is 2 bytes larger than the reported
390
//size.
391
assertTrue(actualSize >= reportedSize);
392         }
393     }
394
395     /**
396      *
397      *
398      */

399     public void testRetrieveMessageOnEmptyMailbox() throws Exception JavaDoc
400     {
401         reset();
402         connect();
403         assertTrue(p.login(emptyUser, password));
404         assertNull(p.retrieveMessage(1));
405     }
406
407     /**
408      *
409      *
410      */

411     public void testRetrieveMessageShouldFails() throws Exception JavaDoc
412     {
413         reset();
414         connect();
415         login();
416
417         //Try to get message 0
418
assertNull(p.retrieveMessage(0));
419
420         //Try to get a negative message
421
assertNull(p.retrieveMessage(-2));
422
423         //Try to get a message that is not there
424
assertNull(p.retrieveMessage(100000));
425
426         //Change states and try to get a valid message
427
p.setState(POP3.UPDATE_STATE);
428         assertNull(p.retrieveMessage(1));
429     }
430
431     /**
432      *
433      *
434      */

435     public void testRetrieveMessageTopOnFullMailbox() throws Exception JavaDoc
436     {
437         reset();
438         connect();
439         login();
440         int numLines = 10;
441
442         POP3MessageInfo[] msg = p.listMessages();
443         assertTrue(msg.length > 0);
444
445         for (int i = 0; i < msg.length; i++)
446         {
447             Reader JavaDoc r = p.retrieveMessageTop(i + 1, numLines);
448             assertNotNull(r);
449             r.close();
450             r = null;
451         }
452     }
453
454     /**
455      *
456      *
457      */

458     public void testRetrieveOverSizedMessageTopOnFullMailbox() throws Exception JavaDoc
459     {
460         reset();
461         connect();
462         login();
463         int reportedSize = 0;
464         int actualSize = 0;
465
466         POP3MessageInfo msg = p.listMessage(1);
467         reportedSize = msg.size;
468
469         //Now try to retrieve more lines than exist in the message
470
Reader JavaDoc r = p.retrieveMessageTop(1, 100000);
471         assertNotNull(r);
472
473         int delaycount = 0;
474         while(!r.ready())
475         {
476             //Give the reader time to get the message
477
//from the server
478
Thread.sleep(500);
479             delaycount++;
480             //but don't wait too long
481
if (delaycount == 4)
482             {
483                 break;
484             }
485         }
486         while(r.ready())
487         {
488             r.read();
489             actualSize++;
490         }
491         //Due to variations in line termination
492
//on different platforms, the actual
493
//size may vary slightly. On Win2KPro, the
494
//actual size is 2 bytes larger than the reported
495
//size.
496
assertTrue(actualSize >= reportedSize);
497     }
498
499     /**
500      *
501      *
502      */

503     public void testRetrieveMessageTopOnEmptyMailbox() throws Exception JavaDoc
504     {
505         reset();
506         connect();
507         assertTrue(p.login(emptyUser, password));
508         assertNull(p.retrieveMessageTop(1, 10));
509     }
510
511     /**
512      *
513      *
514      */

515     public void testRetrieveMessageTopShouldFails() throws Exception JavaDoc
516     {
517         reset();
518         connect();
519         login();
520
521         //Try to get message 0
522
assertNull(p.retrieveMessageTop(0, 10));
523
524         //Try to get a negative message
525
assertNull(p.retrieveMessageTop(-2, 10));
526
527         //Try to get a message that is not there
528
assertNull(p.retrieveMessageTop(100000, 10));
529
530         //Change states and try to get a valid message
531
p.setState(POP3.UPDATE_STATE);
532         assertNull(p.retrieveMessageTop(1, 10));
533     }
534
535     public void testDeleteWithReset() throws Exception JavaDoc
536     {
537         reset();
538         connect();
539         login();
540         //Get the original number of messages
541
POP3MessageInfo[] msg = p.listMessages();
542         int numMessages = msg.length;
543         int numDeleted = 0;
544
545         //Now delete some and logout
546
for (int i = 0; i < numMessages - 1; i ++)
547         {
548             p.deleteMessage(i + 1);
549             numDeleted++;
550         }
551         //Check to see that they are marked as deleted
552
assertEquals(numMessages, (numDeleted + 1));
553
554         //Now reset to unmark the messages as deleted
555
p.reset();
556
557         //Logout and come back in
558
p.logout();
559         reset();
560         connect();
561         login();
562
563         //Get the new number of messages, because of
564
//reset, new number should match old number
565
msg = p.listMessages();
566         assertEquals(numMessages, msg.length);
567     }
568
569     public void testDelete() throws Exception JavaDoc
570     {
571         reset();
572         connect();
573         login();
574         //Get the original number of messages
575
POP3MessageInfo[] msg = p.listMessages();
576         int numMessages = msg.length;
577         int numDeleted = 0;
578
579         //Now delete some and logout
580
for (int i = 0; i < numMessages - 3; i ++)
581         {
582             p.deleteMessage(i + 1);
583             numDeleted++;
584         }
585         //Check to see that they are marked as deleted
586
assertEquals(numMessages, (numDeleted + 3));
587
588         //Logout and come back in
589
p.logout();
590         reset();
591         connect();
592         login();
593
594         //Get the new number of messages, because of
595
//reset, new number should match old number
596
msg = p.listMessages();
597         assertEquals(numMessages - numDeleted, msg.length);
598     }
599
600     public void testResetAndDeleteShouldFails() throws Exception JavaDoc
601     {
602         reset();
603         connect();
604         login();
605
606         p.setState(POP3.UPDATE_STATE);
607         assertFalse(p.reset());
608
609         assertFalse(p.deleteMessage(1));
610     }
611 }
612
Popular Tags