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 junit.framework.TestSuite;
21
22 import java.net.InetAddress;
23 import java.io.IOException;
24
25
26 /**
27 * @author <a href="mailto:commons-dev@apache.org">[Net]</a>
28 * @version $Id: POP3ClientTest.java 631313 2008-02-26 17:41:09Z niallp $
29 *
30 * The POP3* tests all presume the existence of the following parameters:
31 * mailserver: localhost (running on the default port 110)
32 * account: username=test; password=password
33 * account: username=alwaysempty; password=password.
34 * mail: At least four emails in the test account and zero emails
35 * in the alwaysempty account
36 *
37 * If this won't work for you, you can change these parameters in the
38 * TestSetupParameters class.
39 *
40 * The tests were originally run on a default installation of James.
41 * Your mileage may vary based on the POP3 server you run the tests against.
42 * Some servers are more standards-compliant than others.
43 */
44 public class POP3ClientTest extends TestCase
45 {
46 POP3Client p = null;
47
48 String user = TestSetupParameters.user;
49 String emptyUser = TestSetupParameters.emptyuser;
50 String password = TestSetupParameters.password;
51 String mailhost = TestSetupParameters.mailhost;
52
53 /**
54 *
55 */
56 public POP3ClientTest(String name)
57 {
58 super(name);
59 }
60
61 /**
62 * Method suite.
63 * @return TestSuite
64 */
65 public static TestSuite suite()
66 {
67 return (new TestSuite(POP3ClientTest.class));
68 }
69
70 private void reset() throws IOException
71 {
72 //Case where this is the first time reset is called
73 if (p == null)
74 {
75 //Do nothing
76 }
77 else if (p.isConnected())
78 {
79 p.disconnect();
80 }
81 p = null;
82 p = new POP3Client();
83 }
84
85 private void connect() throws Exception
86 {
87 p.connect(InetAddress.getByName(mailhost));
88 assertTrue(p.isConnected());
89 assertEquals(POP3.AUTHORIZATION_STATE, p.getState());
90 }
91
92 private void login() throws Exception
93 {
94 assertTrue(p.login(user, password));
95 assertEquals(POP3.TRANSACTION_STATE, p.getState());
96 }
97
98 /**
99 * Simple test to logon to a valid server using a valid
100 * user name and password.
101 */
102 public void testValidLoginWithNameAndPassword() throws Exception
103 {
104 reset();
105 connect();
106
107 //Try with a valid user
108 login();
109 }
110
111 /**
112 *
113 */
114 public void testInvalidLoginWithBadName() throws Exception
115 {
116 reset();
117 connect();
118
119 //Try with an invalid user that doesn't exist
120 assertFalse(p.login("badusername", password));
121 }
122
123 /**
124 *
125 */
126 public void testInvalidLoginWithBadPassword() throws Exception
127 {
128 reset();
129 connect();
130
131 //Try with a bad password
132 assertFalse(p.login(user, "badpassword"));
133 }
134
135 /**
136 * Test to try to run the login method from the
137 * disconnected, transaction and update states
138 */
139 public void testLoginFromWrongState() throws Exception
140 {
141 reset();
142
143 //Not currently connected, not in authorization state
144 //Try to login with good name/password
145 assertFalse(p.login(user, password));
146
147 //Now connect and set the state to 'transaction' and try again
148 connect();
149 p.setState(POP3.TRANSACTION_STATE);
150 assertFalse(p.login(user, password));
151 p.disconnect();
152
153 //Now connect and set the state to 'update' and try again
154 connect();
155 p.setState(POP3.UPDATE_STATE);
156 assertFalse(p.login(user, password));
157 p.disconnect();
158 }
159
160 /**
161 *
162 *
163 */
164 public void testLogoutFromAllStates() throws Exception
165 {
166 //From 'transaction' state
167 reset();
168 connect();
169 login();
170 assertTrue(p.logout());
171 assertEquals(POP3.UPDATE_STATE, p.getState());
172
173 //From 'update' state
174 reset();
175 connect();
176 login();
177 p.setState(POP3.UPDATE_STATE);
178 assertTrue(p.logout());
179 }
180 }