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