    1
    2   /*
    3    * Copyright 2005 The Apache Software Foundation
    4    *
    5    * Licensed under the Apache License, Version 2.0 (the "License");
    6    * you may not use this file except in compliance with the License.
    7    * You may obtain a copy of the License at
    8    *
    9    *     http://www.apache.org/licenses/LICENSE-2.0
    10   *
    11   * Unless required by applicable law or agreed to in writing, software
    12   * distributed under the License is distributed on an "AS IS" BASIS,
    13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14   * See the License for the specific language governing permissions and
    15   * limitations under the License.
    16   */
    17  package org.apache.commons.net.ftp;
    18
    19  import junit.framework.TestCase;
    20  import java.io.IOException;
    21  import java.net.SocketException;
    22  import java.text.SimpleDateFormat;
    23  import java.util.Calendar;
    24  import java.util.Comparator;
    25  import java.util.Iterator;
    26  import java.util.TreeSet;
    27
    28  /*
    29   * This test was contributed in a different form by W. McDonald Buck
    30   * of Boulder, Colorado, to help fix some bugs with the FTPClientConfig
    31   * in a real world setting.  It is a perfect functional test for the
    32   * Time Zone functionality of FTPClientConfig.
    33   *
    34   * A publicly accessible FTP server at the US National Oceanographic and
    35   * Atmospheric Adminstration houses a directory which contains
    36   * 300 files, named sn.0000 to sn.0300. Every ten minutes or so
    37   * the next file in sequence is rewritten with new data. Thus the directory
    38   * contains observations for more than 24 hours of data.  Since the server
    39   * has its clock set to GMT this is an excellent functional test for any
    40   * machine in a different time zone.
    41   *
    42   * Noteworthy is the fact that the ftp routines in some web browsers don't
    43   * work as well as this.  They can't, since they have no way of knowing the
    44   * server's time zone.  Depending on the local machine's position relative
    45   * to GMT and the time of day, the browsers may decide that a timestamp
    46   * would be in the  future if given the current year, so they assume the
    47   * year to be  last year.  This illustrates the value of FTPClientConfig's
    48   * time zone functionality.
    49   */
    50
    51  public class FTPClientConfigFunctionalTest extends TestCase {
    52
    53      private FTPClient FTP = new FTPClient();
    54      private FTPClientConfig FTPConf;
    55
    56
    57      /***
    58       *
    59       */
    60      public FTPClientConfigFunctionalTest() {
    61          super();
    62
    63  	}
    64
    65      /*
    66       * @throws java.lang.Exception
    67       */
    68      protected void setUp() throws Exception {
    69          super.setUp();
    70  	    FTPConf = new FTPClientConfig(FTPClientConfig.SYST_UNIX);
    71  	    FTPConf.setServerTimeZoneId("GMT");
    72  	    FTP.configure(FTPConf);
    73          try {
    74              FTP.connect("tgftp.nws.noaa.gov");
    75              FTP.login("anonymous","testing@apache.org");
    76              FTP.changeWorkingDirectory("SL.us008001/DF.an/DC.sflnd/DS.metar");
    77              FTP.enterLocalPassiveMode();
    78          } catch (SocketException e) {
    79              e.printStackTrace();
    80          } catch (IOException e) {
    81              e.printStackTrace();
    82          }
    83      }
    84      /*
    85       * @throws java.lang.Exception
    86       */
    87      protected void tearDown() throws Exception {
    88          FTP.disconnect();
    89          super.tearDown();
    90      }
    91      /***
    92       * @param arg0
    93       */
    94      public FTPClientConfigFunctionalTest(String arg0) {
    95          super(arg0);
    96      }
    97
    98
    99      private TreeSet getSortedList(FTPFile[] files) {
    100         // create a TreeSet which will sort each element
    101         // as it is added.
    102         TreeSet sorted = new TreeSet(new Comparator() {
    103
    104             public int compare(Object o1, Object o2) {
    105                 FTPFile f1 = (FTPFile) o1;
    106                 FTPFile f2 = (FTPFile) o2;
    107                 return f1.getTimestamp().getTime().compareTo(f2.getTimestamp().getTime());
    108             }
    109
    110         });
    111
    112
    113         for (int i=0; i < files.length; i++) {
    114 			// The directory contains a few additional files at the beginning
    115 			// which aren't in the series we want. The series we want consists
    116 			// of files named sn.dddd. This adjusts the file list to get rid
    117 			// of the uninteresting ones.
    118             if (files[i].getName().startsWith("sn")) {
    119                 sorted.add(files[i]);
    120             }
    121         }
    122         return sorted;
    123     }
    124
    125
    126     public static void main(String[] args) {
    127         FTPClientConfigFunctionalTest F = new FTPClientConfigFunctionalTest();
    128     }
    129
    130     public void testTimeZoneFunctionality() throws Exception {
    131         java.util.Date now = new java.util.Date();
    132         FTPFile[] files = FTP.listFiles();
    133         TreeSet sorted = getSortedList(files);
    134         //SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy HH:mm z" );
    135         FTPFile lastfile = null;
    136         FTPFile firstfile = null;
    137         for (Iterator it = sorted.iterator(); it.hasNext();) {
    138             FTPFile thisfile = (FTPFile) it.next();
    139             if (firstfile == null) {
    140                 firstfile = thisfile;
    141             }
    142             //System.out.println(sdf.format(thisfile.getTimestamp().getTime())
    143             //        + " " +thisfile.getName());
    144             if (lastfile != null) {
    145                 // verify that the list is sorted earliest to latest.
    146                 assertTrue(lastfile.getTimestamp()
    147                         .before(thisfile.getTimestamp()));
    148             }
    149             lastfile = thisfile;
    150         }
    151
    152         // test that notwithstanding any time zone differences, the newest file
    153         // is older than now.
    154         assertTrue(lastfile.getTimestamp().getTime().before(now));
    155         Calendar first = firstfile.getTimestamp();
    156
    157         // test that the oldest is less than two days older than the newest
    158         // and, in particular, that no files have been considered "future"
    159         // by the parser and therefore been relegated to the same date a
    160         // year ago.
    161         first.add(Calendar.DATE, 2);
    162         assertTrue(lastfile.getTimestamp().before(first));
    163
    164     }
    165 }
    166
    167
    168
    169