View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.net.pop3;
18  
19  import junit.framework.TestCase;
20  
21  import java.net.InetAddress;
22  import java.io.IOException;
23  import java.io.Reader;
24  
25  /**
26   * @author <a href="mailto:commons-dev@apache.org">[Net]</a>
27   * @version $Id: POP3ClientCommandsTest.java 1299238 2012-03-10 17:12:28Z sebb $
28   *
29   * The POP3* tests all presume the existence of the following parameters:
30   *   mailserver: localhost (running on the default port 110)
31   *   account: username=test; password=password
32   *   account: username=alwaysempty; password=password.
33   *   mail: At least four emails in the test account and zero emails
34   *         in the alwaysempty account
35   *
36   * If this won't work for you, you can change these parameters in the
37   * TestSetupParameters class.
38   *
39   * The tests were originally run on a default installation of James.
40   * Your mileage may vary based on the POP3 server you run the tests against.
41   * Some servers are more standards-compliant than others.
42   */
43  public class POP3ClientCommandsTest extends TestCase
44  {
45      POP3Client p = null;
46  
47      String user = TestSetupParameters.user;
48      String emptyUser = TestSetupParameters.emptyuser;
49      String password = TestSetupParameters.password;
50      String mailhost = TestSetupParameters.mailhost;
51  
52      /**
53       *
54       */
55      public POP3ClientCommandsTest(String name)
56      {
57          super(name);
58      }
59  
60      private void reset() throws IOException
61      {
62          //Case where this is the first time reset is called
63          if (p == null)
64          {
65              //Do nothing
66          }
67          else if (p.isConnected())
68          {
69              p.disconnect();
70          }
71          p = null;
72          p = new POP3Client();
73      }
74  
75      private void connect() throws Exception
76      {
77          p.connect(InetAddress.getByName(mailhost));
78          assertTrue(p.isConnected());
79          assertEquals(POP3.AUTHORIZATION_STATE, p.getState());
80      }
81  
82      private void login() throws Exception
83      {
84          assertTrue(p.login(user, password));
85          assertEquals(POP3.TRANSACTION_STATE, p.getState());
86      }
87  
88      /**
89       *
90       *
91       */
92      public void testNoopCommand() throws Exception
93      {
94          reset();
95          connect();
96  
97          //Should fail before authorization
98          assertFalse(p.noop());
99  
100         //Should pass in transaction state
101         login();
102         assertTrue(p.noop());
103 
104         //Should fail in update state
105         p.setState(POP3.UPDATE_STATE);
106         assertFalse(p.noop());
107     }
108 
109     /**
110      *
111      *
112      */
113     public void testStatus() throws Exception
114     {
115         reset();
116         connect();
117 
118         //Should fail in authorization state
119         assertNull(p.status());
120 
121         //Should pass on a mailbox with mail in it
122         login();
123         POP3MessageInfo msg = p.status();
124         assertTrue(msg.number > 0);
125         assertTrue(msg.size > 0);
126         assertNull(msg.identifier);
127         p.logout();
128 
129         //Should also pass on a mailbox with no mail in it
130         reset();
131         connect();
132         assertTrue(p.login(emptyUser, password));
133         POP3MessageInfo msg2 = p.status();
134         assertEquals(0, msg2.number);
135         assertEquals(0, msg2.size);
136         assertNull(msg2.identifier);
137         p.logout();
138 
139         //Should fail in the 'update' state
140         reset();
141         connect();
142         login();
143         p.setState(POP3.UPDATE_STATE);
144         assertNull(p.status());
145     }
146 
147     /**
148      *
149      *
150      */
151     public void testListMessagesOnFullMailbox() throws Exception
152     {
153         reset();
154         connect();
155         login();
156 
157         POP3MessageInfo[] msg = p.listMessages();
158         assertTrue(msg.length > 0);
159 
160         for(int i = 0; i < msg.length; i++)
161         {
162             assertNotNull(msg[i]);
163             assertEquals(i+1, msg[i].number);
164             assertTrue(msg[i].size > 0);
165             assertNull(msg[i].identifier);
166         }
167 
168         //Now test from the update state
169         p.setState(POP3.UPDATE_STATE);
170         msg = p.listMessages();
171         assertNull(msg);
172     }
173 
174     /**
175      *
176      *
177      */
178     public void testListMessageOnFullMailbox() throws Exception
179     {
180         reset();
181         connect();
182         login();
183 
184         //The first message is always at index 1
185         POP3MessageInfo msg = p.listMessage(1);
186         assertNotNull(msg);
187         assertEquals(1, msg.number);
188         assertTrue(msg.size > 0);
189         assertNull(msg.identifier);
190 
191         //Now retrieve a message from index 0
192         msg = p.listMessage(0);
193         assertNull(msg);
194 
195         //Now retrieve a msg that is not there
196         msg = p.listMessage(100000);
197         assertNull(msg);
198 
199         //Now retrieve a msg with a negative index
200         msg = p.listMessage(-2);
201         assertNull(msg);
202 
203         //Now try to get a valid message from the update state
204         p.setState(POP3.UPDATE_STATE);
205         msg = p.listMessage(1);
206         assertNull(msg);
207     }
208 
209     /**
210      *
211      *
212      */
213     public void testListMessagesOnEmptyMailbox() throws Exception
214     {
215         reset();
216         connect();
217         assertTrue(p.login(emptyUser, password));
218 
219         POP3MessageInfo[] msg = p.listMessages();
220         assertEquals(0, msg.length);
221 
222         //Now test from the update state
223         p.setState(POP3.UPDATE_STATE);
224         msg = p.listMessages();
225         assertNull(msg);
226     }
227 
228     /**
229      *
230      *
231      */
232     public void testListMessageOnEmptyMailbox() throws Exception
233     {
234         reset();
235         connect();
236         assertTrue(p.login(emptyUser, password));
237 
238         //The first message is always at index 1
239         POP3MessageInfo msg = p.listMessage(1);
240         assertNull(msg);
241     }
242 
243     /**
244      *
245      *
246      */
247     public void testListUniqueIDsOnFullMailbox() throws Exception
248     {
249         reset();
250         connect();
251         login();
252 
253         POP3MessageInfo[] msg = p.listUniqueIdentifiers();
254         assertTrue(msg.length > 0);
255 
256         for(int i = 0; i < msg.length; i++)
257         {
258             assertNotNull(msg[i]);
259             assertEquals(i + 1, msg[i].number);
260             assertNotNull(msg[i].identifier);
261         }
262 
263         //Now test from the update state
264         p.setState(POP3.UPDATE_STATE);
265         msg = p.listUniqueIdentifiers();
266         assertNull(msg);
267     }
268 
269     /**
270      *
271      *
272      */
273     public void testListUniqueIDOnFullMailbox() throws Exception
274     {
275         reset();
276         connect();
277         login();
278 
279         //The first message is always at index 1
280         POP3MessageInfo msg = p.listUniqueIdentifier(1);
281         assertNotNull(msg);
282         assertEquals(1, msg.number);
283         assertNotNull(msg.identifier);
284 
285         //Now retrieve a message from index 0
286         msg = p.listUniqueIdentifier(0);
287         assertNull(msg);
288 
289         //Now retrieve a msg that is not there
290         msg = p.listUniqueIdentifier(100000);
291         assertNull(msg);
292 
293         //Now retrieve a msg with a negative index
294         msg = p.listUniqueIdentifier(-2);
295         assertNull(msg);
296 
297         //Now try to get a valid message from the update state
298         p.setState(POP3.UPDATE_STATE);
299         msg = p.listUniqueIdentifier(1);
300         assertNull(msg);
301     }
302 
303     /**
304      *
305      *
306      */
307     public void testListUniqueIDsOnEmptyMailbox() throws Exception
308     {
309         reset();
310         connect();
311         assertTrue(p.login(emptyUser, password));
312 
313         POP3MessageInfo[] msg = p.listUniqueIdentifiers();
314         assertEquals(0, msg.length);
315 
316         //Now test from the update state
317         p.setState(POP3.UPDATE_STATE);
318         msg = p.listUniqueIdentifiers();
319         assertNull(msg);
320     }
321 
322     /**
323      *
324      *
325      */
326     public void testListUniqueIdentifierOnEmptyMailbox() throws Exception
327     {
328         reset();
329         connect();
330         assertTrue(p.login(emptyUser, password));
331 
332         //The first message is always at index 1
333         POP3MessageInfo msg = p.listUniqueIdentifier(1);
334         assertNull(msg);
335     }
336 
337     /**
338      *
339      *
340      */
341     public void testRetrieveMessageOnFullMailbox() throws Exception
342     {
343         reset();
344         connect();
345         login();
346         int reportedSize = 0;
347         int actualSize = 0;
348 
349         POP3MessageInfo[] msg = p.listMessages();
350         assertTrue(msg.length > 0);
351 
352         for (int i = msg.length; i > 0; i--)
353         {
354             reportedSize = msg[i - 1].size;
355             Reader r = p.retrieveMessage(i);
356             assertNotNull(r);
357 
358             int delaycount = 0;
359             if (!r.ready())
360             {
361                 //Give the reader time to get the message
362                 //from the server
363                 Thread.sleep(500);
364                 delaycount++;
365                 //but don't wait too long
366                 if (delaycount == 4)
367                 {
368                     break;
369                 }
370             }
371             while(r.ready())
372             {
373                 r.read();
374                 actualSize++;
375             }
376             //Due to variations in line termination
377             //on different platforms, the actual
378             //size may vary slightly.  On Win2KPro, the
379             //actual size is 2 bytes larger than the reported
380             //size.
381             assertTrue(actualSize >= reportedSize);
382         }
383     }
384 
385     /**
386      *
387      *
388      */
389     public void testRetrieveMessageOnEmptyMailbox() throws Exception
390     {
391         reset();
392         connect();
393         assertTrue(p.login(emptyUser, password));
394         assertNull(p.retrieveMessage(1));
395     }
396 
397     /**
398      *
399      *
400      */
401     public void testRetrieveMessageShouldFails() throws Exception
402     {
403         reset();
404         connect();
405         login();
406 
407         //Try to get message 0
408         assertNull(p.retrieveMessage(0));
409 
410         //Try to get a negative message
411         assertNull(p.retrieveMessage(-2));
412 
413         //Try to get a message that is not there
414         assertNull(p.retrieveMessage(100000));
415 
416         //Change states and try to get a valid message
417         p.setState(POP3.UPDATE_STATE);
418         assertNull(p.retrieveMessage(1));
419     }
420 
421     /**
422      *
423      *
424      */
425     public void testRetrieveMessageTopOnFullMailbox() throws Exception
426     {
427         reset();
428         connect();
429         login();
430         int numLines = 10;
431 
432         POP3MessageInfo[] msg = p.listMessages();
433         assertTrue(msg.length > 0);
434 
435         for (int i = 0; i < msg.length; i++)
436         {
437             Reader r = p.retrieveMessageTop(i + 1, numLines);
438             assertNotNull(r);
439             r.close();
440             r = null;
441         }
442     }
443 
444     /**
445      *
446      *
447      */
448     public void testRetrieveOverSizedMessageTopOnFullMailbox() throws Exception
449     {
450         reset();
451         connect();
452         login();
453         int reportedSize = 0;
454         int actualSize = 0;
455 
456         POP3MessageInfo msg = p.listMessage(1);
457         reportedSize = msg.size;
458 
459         //Now try to retrieve more lines than exist in the message
460         Reader r = p.retrieveMessageTop(1, 100000);
461         assertNotNull(r);
462 
463         int delaycount = 0;
464         while(!r.ready())
465         {
466             //Give the reader time to get the message
467             //from the server
468             Thread.sleep(500);
469             delaycount++;
470             //but don't wait too long
471             if (delaycount == 4)
472             {
473                 break;
474             }
475         }
476         while(r.ready())
477         {
478             r.read();
479             actualSize++;
480         }
481         //Due to variations in line termination
482         //on different platforms, the actual
483         //size may vary slightly.  On Win2KPro, the
484         //actual size is 2 bytes larger than the reported
485         //size.
486         assertTrue(actualSize >= reportedSize);
487     }
488 
489     /**
490      *
491      *
492      */
493     public void testRetrieveMessageTopOnEmptyMailbox() throws Exception
494     {
495         reset();
496         connect();
497         assertTrue(p.login(emptyUser, password));
498         assertNull(p.retrieveMessageTop(1, 10));
499     }
500 
501     /**
502      *
503      *
504      */
505     public void testRetrieveMessageTopShouldFails() throws Exception
506     {
507         reset();
508         connect();
509         login();
510 
511         //Try to get message 0
512         assertNull(p.retrieveMessageTop(0, 10));
513 
514         //Try to get a negative message
515         assertNull(p.retrieveMessageTop(-2, 10));
516 
517         //Try to get a message that is not there
518         assertNull(p.retrieveMessageTop(100000, 10));
519 
520         //Change states and try to get a valid message
521         p.setState(POP3.UPDATE_STATE);
522         assertNull(p.retrieveMessageTop(1, 10));
523     }
524 
525     public void testDeleteWithReset() throws Exception
526     {
527         reset();
528         connect();
529         login();
530         //Get the original number of messages
531         POP3MessageInfo[] msg = p.listMessages();
532         int numMessages = msg.length;
533         int numDeleted = 0;
534 
535         //Now delete some and logout
536         for (int i = 0; i < numMessages - 1; i ++)
537         {
538             p.deleteMessage(i + 1);
539             numDeleted++;
540         }
541         //Check to see that they are marked as deleted
542         assertEquals(numMessages, (numDeleted + 1));
543 
544         //Now reset to unmark the messages as deleted
545         p.reset();
546 
547         //Logout and come back in
548         p.logout();
549         reset();
550         connect();
551         login();
552 
553         //Get the new number of messages, because of
554         //reset, new number should match old number
555         msg = p.listMessages();
556         assertEquals(numMessages, msg.length);
557     }
558 
559     public void testDelete() throws Exception
560     {
561         reset();
562         connect();
563         login();
564         //Get the original number of messages
565         POP3MessageInfo[] msg = p.listMessages();
566         int numMessages = msg.length;
567         int numDeleted = 0;
568 
569         //Now delete some and logout
570         for (int i = 0; i < numMessages - 3; i ++)
571         {
572             p.deleteMessage(i + 1);
573             numDeleted++;
574         }
575         //Check to see that they are marked as deleted
576         assertEquals(numMessages, (numDeleted + 3));
577 
578         //Logout and come back in
579         p.logout();
580         reset();
581         connect();
582         login();
583 
584         //Get the new number of messages, because of
585         //reset, new number should match old number
586         msg = p.listMessages();
587         assertEquals(numMessages - numDeleted, msg.length);
588     }
589 
590     public void testResetAndDeleteShouldFails() throws Exception
591     {
592         reset();
593         connect();
594         login();
595 
596         p.setState(POP3.UPDATE_STATE);
597         assertFalse(p.reset());
598 
599         assertFalse(p.deleteMessage(1));
600     }
601 }