    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  import java.io.Reader;
    22
    23  /***
    24   * @author <a href="mailto:commons-dev@apache.org">[Net]</a>
    25   * @version $Id: POP3ConstructorTest.java 165675 2005-05-02 20:09:55Z rwinston $
    26   *
    27   * The POP3* tests all presume the existence of the following parameters:
    28   *   mailserver: localhost (running on the default port 110)
    29   *   account: username=test; password=password
    30   *   account: username=alwaysempty; password=password.
    31   *   mail: At least four emails in the test account and zero emails
    32   *         in the alwaysempty account
    33   *
    34   * If this won't work for you, you can change these parameters in the
    35   * TestSetupParameters class.
    36   *
    37   * The tests were originally run on a default installation of James.
    38   * Your mileage may vary based on the POP3 server you run the tests against.
    39   * Some servers are more standards-compliant than others.
    40   */
    41  public class POP3ConstructorTest extends TestCase
    42  {
    43      String user = TestSetupParameters.user;
    44      String emptyUser = TestSetupParameters.emptyuser;
    45      String password = TestSetupParameters.password;
    46      String mailhost = TestSetupParameters.mailhost;
    47
    48      /***
    49       *
    50       */
    51      public POP3ConstructorTest(String name)
    52      {
    53          super(name);
    54      }
    55
    56      /***
    57       * Method suite.
    58       * @return TestSuite
    59       */
    60      public static TestSuite suite()
    61      {
    62          return (new TestSuite(POP3ConstructorTest.class));
    63      }
    64
    65
    66      /***
    67       * This test will ensure that the constants are not inadvertently changed.
    68       * If the constants are changed in org.apache.commons.net.pop3 for some
    69       * reason, this test will have to be updated.
    70       */
    71      public void testConstants()
    72      {
    73          //From POP3
    74          assertEquals(110, POP3.DEFAULT_PORT);
    75          assertEquals(-1, POP3.DISCONNECTED_STATE);
    76          assertEquals(0, POP3.AUTHORIZATION_STATE);
    77          assertEquals(1, POP3.TRANSACTION_STATE);
    78          assertEquals(2, POP3.UPDATE_STATE);
    79
    80          //From POP3Command
    81          assertEquals(0, POP3Command.USER);
    82          assertEquals(1, POP3Command.PASS);
    83          assertEquals(2, POP3Command.QUIT);
    84          assertEquals(3, POP3Command.STAT);
    85          assertEquals(4, POP3Command.LIST);
    86          assertEquals(5, POP3Command.RETR);
    87          assertEquals(6, POP3Command.DELE);
    88          assertEquals(7, POP3Command.NOOP);
    89          assertEquals(8, POP3Command.RSET);
    90          assertEquals(9, POP3Command.APOP);
    91          assertEquals(10, POP3Command.TOP);
    92          assertEquals(11, POP3Command.UIDL);
    93      }
    94
    95      /***
    96       * Test the default constructor
    97       *
    98       */
    99      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 }