1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  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 java.io.IOException;
20  import java.net.SocketException;
21  import java.util.Calendar;
22  import java.util.Comparator;
23  import java.util.TreeSet;
24  
25  import junit.framework.TestCase;
26  
27  /*
28   * This test was contributed in a different form by W. McDonald Buck
29   * of Boulder, Colorado, to help fix some bugs with the FTPClientConfig
30   * in a real world setting.  It is a perfect functional test for the
31   * Time Zone functionality of FTPClientConfig.
32   * 
33   * A publicly accessible FTP server at the US National Oceanographic and
34   * Atmospheric Adminstration houses a directory which contains 
35   * 300 files, named sn.0000 to sn.0300. Every ten minutes or so 
36   * the next file in sequence is rewritten with new data. Thus the directory 
37   * contains observations for more than 24 hours of data.  Since the server
38   * has its clock set to GMT this is an excellent functional test for any
39   * machine in a different time zone. 
40   * 
41   * Noteworthy is the fact that the ftp routines in some web browsers don't 
42   * work as well as this.  They can't, since they have no way of knowing the 
43   * server's time zone.  Depending on the local machine's position relative 
44   * to GMT and the time of day, the browsers may decide that a timestamp 
45   * would be in the  future if given the current year, so they assume the 
46   * year to be  last year.  This illustrates the value of FTPClientConfig's 
47   * time zone functionality.
48   */
49  
50  public class FTPClientConfigFunctionalTest extends TestCase {
51  
52      private FTPClient FTP = new FTPClient(); 
53      private FTPClientConfig FTPConf; 
54  
55  
56      /**
57       * 
58       */
59      public FTPClientConfigFunctionalTest() {
60          super();
61  
62      }
63  
64      /* 
65       * @throws java.lang.Exception
66       */
67      @Override
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      @Override
88      protected void tearDown() throws Exception {
89          FTP.disconnect();
90          super.tearDown();
91      }
92      /**
93       * @param arg0
94       */
95      public FTPClientConfigFunctionalTest(String arg0) {
96          super(arg0);
97      }
98  
99      
100     @SuppressWarnings("unchecked")
101     private TreeSet<FTPFile> getSortedList(FTPFile[] files) {
102         // create a TreeSet which will sort each element
103         // as it is added.
104         TreeSet<FTPFile> sorted = new TreeSet<FTPFile>(new Comparator() {
105 
106             public int compare(Object o1, Object o2) {
107                 FTPFile f1 = (FTPFile) o1;
108                 FTPFile f2 = (FTPFile) o2;
109                 return f1.getTimestamp().getTime().compareTo(f2.getTimestamp().getTime());
110             }
111             
112         });
113         
114          
115         for (int i=0; i < files.length; i++) {
116             // The directory contains a few additional files at the beginning
117             // which aren't in the series we want. The series we want consists
118             // of files named sn.dddd. This adjusts the file list to get rid 
119             // of the uninteresting ones. 
120             if (files[i].getName().startsWith("sn")) {
121                 sorted.add(files[i]);
122             }    
123         }
124         return sorted;
125     }
126 
127     
128     public static void main(String[] args) {
129         FTPClientConfigFunctionalTest F = new FTPClientConfigFunctionalTest();
130     }
131     
132     public void testTimeZoneFunctionality() throws Exception {
133         java.util.Date now = new java.util.Date();
134         FTPFile[] files = FTP.listFiles();
135         TreeSet<FTPFile> sorted = getSortedList(files);
136         //SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy HH:mm z" );
137         FTPFile lastfile = null;
138         FTPFile firstfile = null;
139         for (FTPFile thisfile : sorted) {
140             if (firstfile == null) {
141                 firstfile = thisfile;
142             }
143             //System.out.println(sdf.format(thisfile.getTimestamp().getTime()) 
144             //        + " " +thisfile.getName());
145             if (lastfile != null) {
146                 // verify that the list is sorted earliest to latest.
147                 assertTrue(lastfile.getTimestamp()
148                         .before(thisfile.getTimestamp()));
149             }
150             lastfile = thisfile;
151         }
152         
153         // test that notwithstanding any time zone differences, the newest file
154         // is older than now.
155         assertTrue(lastfile.getTimestamp().getTime().before(now));
156         Calendar first = firstfile.getTimestamp();
157 
158         // test that the oldest is less than two days older than the newest 
159         // and, in particular, that no files have been considered "future" 
160         // by the parser and therefore been relegated to the same date a 
161         // year ago.
162         first.add(Calendar.DATE, 2);
163         assertTrue(lastfile.getTimestamp().before(first));
164 
165     }
166 }
167 
168 
169 
170