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