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 java.io.Reader;
20  
21  import junit.framework.TestCase;
22  
23  /**
24   * The POP3* tests all presume the existence of the following parameters: mailserver: localhost (running on the default port 110) account: username=test;
25   * password=password account: username=alwaysempty; password=password. mail: At least four emails in the test account and zero emails in the alwaysempty account
26   *
27   * If this won't work for you, you can change these parameters in the TestSetupParameters class.
28   *
29   * The tests were originally run on a default installation of James. Your mileage may vary based on the POP3 server you run the tests against. Some servers are
30   * more standards-compliant than others.
31   */
32  public class POP3ConstructorTest extends TestCase {
33      String user = POP3Constants.user;
34      String emptyUser = POP3Constants.emptyuser;
35      String password = POP3Constants.password;
36      String mailhost = POP3Constants.mailhost;
37  
38      public POP3ConstructorTest(final String name) {
39          super(name);
40      }
41  
42      /*
43       * This test will ensure that the constants are not inadvertently changed. If the constants are changed in org.apache.commons.net.pop3 for some reason, this
44       * test will have to be updated.
45       */
46      public void testConstants() {
47          // From POP3
48          assertEquals(110, POP3.DEFAULT_PORT);
49          assertEquals(-1, POP3.DISCONNECTED_STATE);
50          assertEquals(0, POP3.AUTHORIZATION_STATE);
51          assertEquals(1, POP3.TRANSACTION_STATE);
52          assertEquals(2, POP3.UPDATE_STATE);
53  
54          // From POP3Command
55          assertEquals(0, POP3Command.USER);
56          assertEquals(1, POP3Command.PASS);
57          assertEquals(2, POP3Command.QUIT);
58          assertEquals(3, POP3Command.STAT);
59          assertEquals(4, POP3Command.LIST);
60          assertEquals(5, POP3Command.RETR);
61          assertEquals(6, POP3Command.DELE);
62          assertEquals(7, POP3Command.NOOP);
63          assertEquals(8, POP3Command.RSET);
64          assertEquals(9, POP3Command.APOP);
65          assertEquals(10, POP3Command.TOP);
66          assertEquals(11, POP3Command.UIDL);
67      }
68  
69      public void testPOP3ClientStateTransition() throws Exception {
70          final POP3Client pop = new POP3Client();
71  
72          // Initial state
73          assertEquals(110, pop.getDefaultPort());
74          assertEquals(POP3.DISCONNECTED_STATE, pop.getState());
75          assertNull(pop.reader);
76          assertNotNull(pop.replyLines);
77  
78          // Now connect
79          pop.connect(mailhost);
80          assertEquals(POP3.AUTHORIZATION_STATE, pop.getState());
81  
82          // Now authenticate
83          pop.login(user, password);
84          assertEquals(POP3.TRANSACTION_STATE, pop.getState());
85  
86          // Now do a series of commands and make sure the state stays as it should
87          pop.noop();
88          assertEquals(POP3.TRANSACTION_STATE, pop.getState());
89          pop.status();
90          assertEquals(POP3.TRANSACTION_STATE, pop.getState());
91  
92          // Make sure we have at least one message to test
93          final POP3MessageInfo[] msg = pop.listMessages();
94  
95          if (msg.length > 0) {
96              pop.deleteMessage(1);
97              assertEquals(POP3.TRANSACTION_STATE, pop.getState());
98  
99              pop.reset();
100             assertEquals(POP3.TRANSACTION_STATE, pop.getState());
101 
102             pop.listMessage(1);
103             assertEquals(POP3.TRANSACTION_STATE, pop.getState());
104 
105             pop.listMessages();
106             assertEquals(POP3.TRANSACTION_STATE, pop.getState());
107 
108             pop.listUniqueIdentifier(1);
109             assertEquals(POP3.TRANSACTION_STATE, pop.getState());
110 
111             pop.listUniqueIdentifiers();
112             assertEquals(POP3.TRANSACTION_STATE, pop.getState());
113 
114             Reader r = pop.retrieveMessage(1);
115             assertEquals(POP3.TRANSACTION_STATE, pop.getState());
116 
117             // Add some sleep here to handle network latency
118             while (!r.ready()) {
119                 Thread.sleep(10);
120             }
121             r.close();
122             r = null;
123 
124             r = pop.retrieveMessageTop(1, 10);
125             assertEquals(POP3.TRANSACTION_STATE, pop.getState());
126 
127             // Add some sleep here to handle network latency
128             while (!r.ready()) {
129                 Thread.sleep(10);
130             }
131             r.close();
132             r = null;
133 
134         }
135 
136         // Now logout
137         pop.logout();
138         assertEquals(POP3.UPDATE_STATE, pop.getState());
139     }
140 
141     public void testPOP3DefaultConstructor() {
142         final POP3 pop = new POP3();
143 
144         assertEquals(110, pop.getDefaultPort());
145         assertEquals(POP3.DISCONNECTED_STATE, pop.getState());
146         assertNull(pop.reader);
147         assertNotNull(pop.replyLines);
148     }
149 }