001: /*
002: * Copyright 2001-2005 The Apache Software Foundation
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.apache.commons.net.pop3;
018:
019: import junit.framework.TestCase;
020: import junit.framework.TestSuite;
021:
022: import java.net.InetAddress;
023: import java.io.IOException;
024:
025: /**
026: * @author <a href="mailto:commons-dev@apache.org">[Net]</a>
027: * @version $Id: POP3ClientTest.java 165675 2005-05-02 20:09:55Z rwinston $
028: *
029: * The POP3* tests all presume the existence of the following parameters:
030: * mailserver: localhost (running on the default port 110)
031: * account: username=test; password=password
032: * account: username=alwaysempty; password=password.
033: * mail: At least four emails in the test account and zero emails
034: * in the alwaysempty account
035: *
036: * If this won't work for you, you can change these parameters in the
037: * TestSetupParameters class.
038: *
039: * The tests were originally run on a default installation of James.
040: * Your mileage may vary based on the POP3 server you run the tests against.
041: * Some servers are more standards-compliant than others.
042: */
043: public class POP3ClientTest extends TestCase {
044: POP3Client p = null;
045:
046: String user = TestSetupParameters.user;
047: String emptyUser = TestSetupParameters.emptyuser;
048: String password = TestSetupParameters.password;
049: String mailhost = TestSetupParameters.mailhost;
050:
051: /**
052: *
053: */
054: public POP3ClientTest(String name) {
055: super (name);
056: }
057:
058: /**
059: * Method suite.
060: * @return TestSuite
061: */
062: public static TestSuite suite() {
063: return (new TestSuite(POP3ClientTest.class));
064: }
065:
066: private void reset() throws IOException {
067: //Case where this is the first time reset is called
068: if (p == null) {
069: //Do nothing
070: } else if (p.isConnected()) {
071: p.disconnect();
072: }
073: p = null;
074: p = new POP3Client();
075: }
076:
077: private void connect() throws Exception {
078: p.connect(InetAddress.getByName(mailhost));
079: assertTrue(p.isConnected());
080: assertEquals(POP3.AUTHORIZATION_STATE, p.getState());
081: }
082:
083: private void login() throws Exception {
084: assertTrue(p.login(user, password));
085: assertEquals(POP3.TRANSACTION_STATE, p.getState());
086: }
087:
088: /**
089: * Simple test to logon to a valid server using a valid
090: * user name and password.
091: */
092: public void testValidLoginWithNameAndPassword() throws Exception {
093: reset();
094: connect();
095:
096: //Try with a valid user
097: login();
098: }
099:
100: /**
101: *
102: */
103: public void testInvalidLoginWithBadName() throws Exception {
104: reset();
105: connect();
106:
107: //Try with an invalid user that doesn't exist
108: assertFalse(p.login("badusername", password));
109: }
110:
111: /**
112: *
113: */
114: public void testInvalidLoginWithBadPassword() throws Exception {
115: reset();
116: connect();
117:
118: //Try with a bad password
119: assertFalse(p.login(user, "badpassword"));
120: }
121:
122: /**
123: * Test to try to run the login method from the
124: * disconnected, transaction and update states
125: */
126: public void testLoginFromWrongState() throws Exception {
127: reset();
128:
129: //Not currently connected, not in authorization state
130: //Try to login with good name/password
131: assertFalse(p.login(user, password));
132:
133: //Now connect and set the state to 'transaction' and try again
134: connect();
135: p.setState(POP3.TRANSACTION_STATE);
136: assertFalse(p.login(user, password));
137: p.disconnect();
138:
139: //Now connect and set the state to 'update' and try again
140: connect();
141: p.setState(POP3.UPDATE_STATE);
142: assertFalse(p.login(user, password));
143: p.disconnect();
144: }
145:
146: /**
147: *
148: *
149: */
150: public void testLogoutFromAllStates() throws Exception {
151: //From 'transaction' state
152: reset();
153: connect();
154: login();
155: assertTrue(p.logout());
156: assertEquals(POP3.UPDATE_STATE, p.getState());
157:
158: //From 'update' state
159: reset();
160: connect();
161: login();
162: p.setState(POP3.UPDATE_STATE);
163: assertTrue(p.logout());
164: }
165: }
|