001 /*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
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 631313 2008-02-26 17:41:09Z niallp $
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 {
043 String user = TestSetupParameters.user;
044 String emptyUser = TestSetupParameters.emptyuser;
045 String password = TestSetupParameters.password;
046 String mailhost = TestSetupParameters.mailhost;
047
048 /**
049 *
050 */
051 public POP3ConstructorTest(String name)
052 {
053 super(name);
054 }
055
056 /**
057 * Method suite.
058 * @return TestSuite
059 */
060 public static TestSuite suite()
061 {
062 return (new TestSuite(POP3ConstructorTest.class));
063 }
064
065
066 /**
067 * This test will ensure that the constants are not inadvertently changed.
068 * If the constants are changed in org.apache.commons.net.pop3 for some
069 * reason, this test will have to be updated.
070 */
071 public void testConstants()
072 {
073 //From POP3
074 assertEquals(110, POP3.DEFAULT_PORT);
075 assertEquals(-1, POP3.DISCONNECTED_STATE);
076 assertEquals(0, POP3.AUTHORIZATION_STATE);
077 assertEquals(1, POP3.TRANSACTION_STATE);
078 assertEquals(2, POP3.UPDATE_STATE);
079
080 //From POP3Command
081 assertEquals(0, POP3Command.USER);
082 assertEquals(1, POP3Command.PASS);
083 assertEquals(2, POP3Command.QUIT);
084 assertEquals(3, POP3Command.STAT);
085 assertEquals(4, POP3Command.LIST);
086 assertEquals(5, POP3Command.RETR);
087 assertEquals(6, POP3Command.DELE);
088 assertEquals(7, POP3Command.NOOP);
089 assertEquals(8, POP3Command.RSET);
090 assertEquals(9, POP3Command.APOP);
091 assertEquals(10, POP3Command.TOP);
092 assertEquals(11, POP3Command.UIDL);
093 }
094
095 /**
096 * Test the default constructor
097 *
098 */
099 public void testPOP3DefaultConstructor()
100 {
101 POP3 pop = new POP3();
102
103 assertEquals(110, pop.getDefaultPort());
104 assertEquals(POP3.DISCONNECTED_STATE, pop.getState());
105 assertNull(pop._reader);
106 assertNotNull(pop._replyLines);
107 }
108
109 /**
110 * Test the default constructor
111 *
112 */
113 public void testPOP3ClientStateTransition() throws Exception
114 {
115 POP3Client pop = new POP3Client();
116
117 //Initial state
118 assertEquals(110, pop.getDefaultPort());
119 assertEquals(POP3.DISCONNECTED_STATE, pop.getState());
120 assertNull(pop._reader);
121 assertNotNull(pop._replyLines);
122
123 //Now connect
124 pop.connect(mailhost);
125 assertEquals(POP3.AUTHORIZATION_STATE, pop.getState());
126
127 //Now authenticate
128 pop.login(user, password);
129 assertEquals(POP3.TRANSACTION_STATE, pop.getState());
130
131 //Now do a series of commands and make sure the state stays as it should
132 pop.noop();
133 assertEquals(POP3.TRANSACTION_STATE, pop.getState());
134 pop.status();
135 assertEquals(POP3.TRANSACTION_STATE, pop.getState());
136
137 //Make sure we have at least one message to test
138 POP3MessageInfo[] msg = pop.listMessages();
139
140 if (msg.length > 0)
141 {
142 pop.deleteMessage(1);
143 assertEquals(POP3.TRANSACTION_STATE, pop.getState());
144
145 pop.reset();
146 assertEquals(POP3.TRANSACTION_STATE, pop.getState());
147
148 pop.listMessage(1);
149 assertEquals(POP3.TRANSACTION_STATE, pop.getState());
150
151 pop.listMessages();
152 assertEquals(POP3.TRANSACTION_STATE, pop.getState());
153
154 pop.listUniqueIdentifier(1);
155 assertEquals(POP3.TRANSACTION_STATE, pop.getState());
156
157 pop.listUniqueIdentifiers();
158 assertEquals(POP3.TRANSACTION_STATE, pop.getState());
159
160 Reader r = pop.retrieveMessage(1);
161 assertEquals(POP3.TRANSACTION_STATE, pop.getState());
162
163 //Add some sleep here to handle network latency
164 while(!r.ready())
165 {
166 Thread.sleep(10);
167 }
168 r.close();
169 r = null;
170
171 r = pop.retrieveMessageTop(1, 10);
172 assertEquals(POP3.TRANSACTION_STATE, pop.getState());
173
174 //Add some sleep here to handle network latency
175 while(!r.ready())
176 {
177 Thread.sleep(10);
178 }
179 r.close();
180 r = null;
181
182 }
183
184 //Now logout
185 pop.logout();
186 assertEquals(POP3.UPDATE_STATE, pop.getState());
187 }
188 }