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 final 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 private TreeSet<FTPFile> getSortedList(FTPFile[] files) { 100 // create a TreeSet which will sort each element 101 // as it is added. 102 TreeSet<FTPFile> sorted = new TreeSet<FTPFile>(new Comparator<Object>() { 103 104 // @Override 105 public int compare(Object o1, Object o2) { 106 FTPFile f1 = (FTPFile) o1; 107 FTPFile f2 = (FTPFile) o2; 108 return f1.getTimestamp().getTime().compareTo(f2.getTimestamp().getTime()); 109 } 110 111 }); 112 113 114 for (FTPFile file : files) 115 { 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 (file.getName().startsWith("sn")) { 121 sorted.add(file); 122 } 123 } 124 return sorted; 125 } 126 127 public void testTimeZoneFunctionality() throws Exception { 128 java.util.Date now = new java.util.Date(); 129 FTPFile[] files = FTP.listFiles(); 130 TreeSet<FTPFile> sorted = getSortedList(files); 131 //SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy HH:mm z" ); 132 FTPFile lastfile = null; 133 FTPFile firstfile = null; 134 for (FTPFile thisfile : sorted) { 135 if (firstfile == null) { 136 firstfile = thisfile; 137 } 138 //System.out.println(sdf.format(thisfile.getTimestamp().getTime()) 139 // + " " +thisfile.getName()); 140 if (lastfile != null) { 141 // verify that the list is sorted earliest to latest. 142 assertTrue(lastfile.getTimestamp() 143 .before(thisfile.getTimestamp())); 144 } 145 lastfile = thisfile; 146 } 147 148 if (firstfile == null || lastfile == null) { 149 fail("No files found"); 150 } else { 151 // test that notwithstanding any time zone differences, the newest file 152 // is older than now. 153 assertTrue(lastfile.getTimestamp().getTime().before(now)); 154 Calendar first = firstfile.getTimestamp(); 155 156 // test that the oldest is less than two days older than the newest 157 // and, in particular, that no files have been considered "future" 158 // by the parser and therefore been relegated to the same date a 159 // year ago. 160 first.add(Calendar.DATE, 2); 161 assertTrue(lastfile.getTimestamp().getTime().toString()+" before "+ first.getTime().toString(),lastfile.getTimestamp().before(first)); 162 } 163 } 164 } 165 166 167 168