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: import java.io.Reader;
022:
023: /**
024: * @author <a href="mailto:commons-dev@apache.org">[Net]</a>
025: * @version $Id: POP3ConstructorTest.java 165675 2005-05-02 20:09:55Z rwinston $
026: *
027: * The POP3* tests all presume the existence of the following parameters:
028: * mailserver: localhost (running on the default port 110)
029: * account: username=test; password=password
030: * account: username=alwaysempty; password=password.
031: * mail: At least four emails in the test account and zero emails
032: * in the alwaysempty account
033: *
034: * If this won't work for you, you can change these parameters in the
035: * TestSetupParameters class.
036: *
037: * The tests were originally run on a default installation of James.
038: * Your mileage may vary based on the POP3 server you run the tests against.
039: * Some servers are more standards-compliant than others.
040: */
041: public class POP3ConstructorTest extends TestCase {
042: String user = TestSetupParameters.user;
043: String emptyUser = TestSetupParameters.emptyuser;
044: String password = TestSetupParameters.password;
045: String mailhost = TestSetupParameters.mailhost;
046:
047: /**
048: *
049: */
050: public POP3ConstructorTest(String name) {
051: super (name);
052: }
053:
054: /**
055: * Method suite.
056: * @return TestSuite
057: */
058: public static TestSuite suite() {
059: return (new TestSuite(POP3ConstructorTest.class));
060: }
061:
062: /**
063: * This test will ensure that the constants are not inadvertently changed.
064: * If the constants are changed in org.apache.commons.net.pop3 for some
065: * reason, this test will have to be updated.
066: */
067: public void testConstants() {
068: //From POP3
069: assertEquals(110, POP3.DEFAULT_PORT);
070: assertEquals(-1, POP3.DISCONNECTED_STATE);
071: assertEquals(0, POP3.AUTHORIZATION_STATE);
072: assertEquals(1, POP3.TRANSACTION_STATE);
073: assertEquals(2, POP3.UPDATE_STATE);
074:
075: //From POP3Command
076: assertEquals(0, POP3Command.USER);
077: assertEquals(1, POP3Command.PASS);
078: assertEquals(2, POP3Command.QUIT);
079: assertEquals(3, POP3Command.STAT);
080: assertEquals(4, POP3Command.LIST);
081: assertEquals(5, POP3Command.RETR);
082: assertEquals(6, POP3Command.DELE);
083: assertEquals(7, POP3Command.NOOP);
084: assertEquals(8, POP3Command.RSET);
085: assertEquals(9, POP3Command.APOP);
086: assertEquals(10, POP3Command.TOP);
087: assertEquals(11, POP3Command.UIDL);
088: }
089:
090: /**
091: * Test the default constructor
092: *
093: */
094: public void testPOP3DefaultConstructor() {
095: POP3 pop = new POP3();
096:
097: assertEquals(110, pop.getDefaultPort());
098: assertEquals(POP3.DISCONNECTED_STATE, pop.getState());
099: assertNull(pop._reader);
100: assertNotNull(pop._replyLines);
101: }
102:
103: /**
104: * Test the default constructor
105: *
106: */
107: public void testPOP3ClientStateTransition() throws Exception {
108: POP3Client pop = new POP3Client();
109:
110: //Initial state
111: assertEquals(110, pop.getDefaultPort());
112: assertEquals(POP3.DISCONNECTED_STATE, pop.getState());
113: assertNull(pop._reader);
114: assertNotNull(pop._replyLines);
115:
116: //Now connect
117: pop.connect(mailhost);
118: assertEquals(POP3.AUTHORIZATION_STATE, pop.getState());
119:
120: //Now authenticate
121: pop.login(user, password);
122: assertEquals(POP3.TRANSACTION_STATE, pop.getState());
123:
124: //Now do a series of commands and make sure the state stays as it should
125: pop.noop();
126: assertEquals(POP3.TRANSACTION_STATE, pop.getState());
127: pop.status();
128: assertEquals(POP3.TRANSACTION_STATE, pop.getState());
129:
130: //Make sure we have at least one message to test
131: POP3MessageInfo[] msg = pop.listMessages();
132:
133: if (msg.length > 0) {
134: pop.deleteMessage(1);
135: assertEquals(POP3.TRANSACTION_STATE, pop.getState());
136:
137: pop.reset();
138: assertEquals(POP3.TRANSACTION_STATE, pop.getState());
139:
140: pop.listMessage(1);
141: assertEquals(POP3.TRANSACTION_STATE, pop.getState());
142:
143: pop.listMessages();
144: assertEquals(POP3.TRANSACTION_STATE, pop.getState());
145:
146: pop.listUniqueIdentifier(1);
147: assertEquals(POP3.TRANSACTION_STATE, pop.getState());
148:
149: pop.listUniqueIdentifiers();
150: assertEquals(POP3.TRANSACTION_STATE, pop.getState());
151:
152: Reader r = pop.retrieveMessage(1);
153: assertEquals(POP3.TRANSACTION_STATE, pop.getState());
154:
155: //Add some sleep here to handle network latency
156: while (!r.ready()) {
157: Thread.sleep(10);
158: }
159: r.close();
160: r = null;
161:
162: r = pop.retrieveMessageTop(1, 10);
163: assertEquals(POP3.TRANSACTION_STATE, pop.getState());
164:
165: //Add some sleep here to handle network latency
166: while (!r.ready()) {
167: Thread.sleep(10);
168: }
169: r.close();
170: r = null;
171:
172: }
173:
174: //Now logout
175: pop.logout();
176: assertEquals(POP3.UPDATE_STATE, pop.getState());
177: }
178: }
|