KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > net > pop3 > POP3ClientTest


1 /*
2  * Copyright 2001-2005 The Apache Software Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

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 JavaDoc;
23 import java.io.IOException JavaDoc;
24
25
26 /**
27  * @author <a HREF="mailto:commons-dev@apache.org">[Net]</a>
28  * @version $Id: POP3ClientTest.java 165675 2005-05-02 20:09:55Z rwinston $
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 JavaDoc user = TestSetupParameters.user;
49     String JavaDoc emptyUser = TestSetupParameters.emptyuser;
50     String JavaDoc password = TestSetupParameters.password;
51     String JavaDoc mailhost = TestSetupParameters.mailhost;
52
53     /**
54      *
55      */

56     public POP3ClientTest(String JavaDoc 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 JavaDoc
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 JavaDoc
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 JavaDoc
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 JavaDoc
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 JavaDoc
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 JavaDoc
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 JavaDoc
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 JavaDoc
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 }
181
Popular Tags