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    
022    import java.net.InetAddress;
023    import java.io.IOException;
024    
025    
026    /**
027     * @author <a href="mailto:commons-dev@apache.org">[Net]</a>
028     * @version $Id: POP3ClientTest.java 631313 2008-02-26 17:41:09Z niallp $
029     *
030     * The POP3* tests all presume the existence of the following parameters:
031     *   mailserver: localhost (running on the default port 110)
032     *   account: username=test; password=password
033     *   account: username=alwaysempty; password=password.
034     *   mail: At least four emails in the test account and zero emails
035     *         in the alwaysempty account
036     *
037     * If this won't work for you, you can change these parameters in the
038     * TestSetupParameters class.
039     *
040     * The tests were originally run on a default installation of James.
041     * Your mileage may vary based on the POP3 server you run the tests against.
042     * Some servers are more standards-compliant than others.
043     */
044    public class POP3ClientTest extends TestCase
045    {
046        POP3Client p = null;
047    
048        String user = TestSetupParameters.user;
049        String emptyUser = TestSetupParameters.emptyuser;
050        String password = TestSetupParameters.password;
051        String mailhost = TestSetupParameters.mailhost;
052    
053        /**
054         *
055         */
056        public POP3ClientTest(String name)
057        {
058            super(name);
059        }
060    
061        /**
062         * Method suite.
063         * @return TestSuite
064         */
065        public static TestSuite suite()
066        {
067            return (new TestSuite(POP3ClientTest.class));
068        }
069    
070        private void reset() throws IOException
071        {
072            //Case where this is the first time reset is called
073            if (p == null)
074            {
075                //Do nothing
076            }
077            else if (p.isConnected())
078            {
079                p.disconnect();
080            }
081            p = null;
082            p = new POP3Client();
083        }
084    
085        private void connect() throws Exception
086        {
087            p.connect(InetAddress.getByName(mailhost));
088            assertTrue(p.isConnected());
089            assertEquals(POP3.AUTHORIZATION_STATE, p.getState());
090        }
091    
092        private void login() throws Exception
093        {
094            assertTrue(p.login(user, password));
095            assertEquals(POP3.TRANSACTION_STATE, p.getState());
096        }
097    
098        /**
099         * 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    }