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  
24  
25  /**
26   * @author <a href="mailto:commons-dev@apache.org">[Net]</a>
27   * @version $Id: POP3ClientTest.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 POP3ClientTest 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 POP3ClientTest(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       * Simple test to logon to a valid server using a valid
90       * user name and password.
91       */
92      public void testValidLoginWithNameAndPassword() throws Exception
93      {
94          reset();
95          connect();
96  
97          //Try with a valid user
98          login();
99      }
100 
101     /**
102      *
103      */
104     public void testInvalidLoginWithBadName() throws Exception
105     {
106         reset();
107         connect();
108 
109         //Try with an invalid user that doesn't exist
110         assertFalse(p.login("badusername", password));
111     }
112 
113     /**
114      *
115      */
116     public void testInvalidLoginWithBadPassword() throws Exception
117     {
118         reset();
119         connect();
120 
121         //Try with a bad password
122         assertFalse(p.login(user, "badpassword"));
123     }
124 
125     /**
126      * Test to try to run the login method from the
127      * disconnected, transaction and update states
128      */
129     public void testLoginFromWrongState() throws Exception
130     {
131         reset();
132 
133         //Not currently connected, not in authorization state
134         //Try to login with good name/password
135         assertFalse(p.login(user, password));
136 
137         //Now connect and set the state to 'transaction' and try again
138         connect();
139         p.setState(POP3.TRANSACTION_STATE);
140         assertFalse(p.login(user, password));
141         p.disconnect();
142 
143         //Now connect and set the state to 'update' and try again
144         connect();
145         p.setState(POP3.UPDATE_STATE);
146         assertFalse(p.login(user, password));
147         p.disconnect();
148     }
149 
150     /**
151      *
152      *
153      */
154     public void testLogoutFromAllStates() throws Exception
155     {
156         //From 'transaction' state
157         reset();
158         connect();
159         login();
160         assertTrue(p.logout());
161         assertEquals(POP3.UPDATE_STATE, p.getState());
162 
163         //From 'update' state
164         reset();
165         connect();
166         login();
167         p.setState(POP3.UPDATE_STATE);
168         assertTrue(p.logout());
169     }
170 }