001: /*
002: * Copyright 2005 The Apache Software Foundation
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.apache.commons.net.ftp;
017:
018: import junit.framework.TestCase;
019: import java.io.IOException;
020: import java.net.SocketException;
021: import java.text.SimpleDateFormat;
022: import java.util.Calendar;
023: import java.util.Comparator;
024: import java.util.Iterator;
025: import java.util.TreeSet;
026:
027: /*
028: * This test was contributed in a different form by W. McDonald Buck
029: * of Boulder, Colorado, to help fix some bugs with the FTPClientConfig
030: * in a real world setting. It is a perfect functional test for the
031: * Time Zone functionality of FTPClientConfig.
032: *
033: * A publicly accessible FTP server at the US National Oceanographic and
034: * Atmospheric Adminstration houses a directory which contains
035: * 300 files, named sn.0000 to sn.0300. Every ten minutes or so
036: * the next file in sequence is rewritten with new data. Thus the directory
037: * contains observations for more than 24 hours of data. Since the server
038: * has its clock set to GMT this is an excellent functional test for any
039: * machine in a different time zone.
040: *
041: * Noteworthy is the fact that the ftp routines in some web browsers don't
042: * work as well as this. They can't, since they have no way of knowing the
043: * server's time zone. Depending on the local machine's position relative
044: * to GMT and the time of day, the browsers may decide that a timestamp
045: * would be in the future if given the current year, so they assume the
046: * year to be last year. This illustrates the value of FTPClientConfig's
047: * time zone functionality.
048: */
049:
050: public class FTPClientConfigFunctionalTest extends TestCase {
051:
052: private FTPClient FTP = new FTPClient();
053: private FTPClientConfig FTPConf;
054:
055: /**
056: *
057: */
058: public FTPClientConfigFunctionalTest() {
059: super ();
060:
061: }
062:
063: /*
064: * @throws java.lang.Exception
065: */
066: protected void setUp() throws Exception {
067: super .setUp();
068: FTPConf = new FTPClientConfig(FTPClientConfig.SYST_UNIX);
069: FTPConf.setServerTimeZoneId("GMT");
070: FTP.configure(FTPConf);
071: try {
072: FTP.connect("tgftp.nws.noaa.gov");
073: FTP.login("anonymous", "testing@apache.org");
074: FTP
075: .changeWorkingDirectory("SL.us008001/DF.an/DC.sflnd/DS.metar");
076: FTP.enterLocalPassiveMode();
077: } catch (SocketException e) {
078: e.printStackTrace();
079: } catch (IOException e) {
080: e.printStackTrace();
081: }
082: }
083:
084: /*
085: * @throws java.lang.Exception
086: */
087: protected void tearDown() throws Exception {
088: FTP.disconnect();
089: super .tearDown();
090: }
091:
092: /**
093: * @param arg0
094: */
095: public FTPClientConfigFunctionalTest(String arg0) {
096: super (arg0);
097: }
098:
099: 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(
108: f2.getTimestamp().getTime());
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: public static void main(String[] args) {
126: FTPClientConfigFunctionalTest F = new FTPClientConfigFunctionalTest();
127: }
128:
129: public void testTimeZoneFunctionality() throws Exception {
130: java.util.Date now = new java.util.Date();
131: FTPFile[] files = FTP.listFiles();
132: TreeSet sorted = getSortedList(files);
133: //SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy HH:mm z" );
134: FTPFile lastfile = null;
135: FTPFile firstfile = null;
136: for (Iterator it = sorted.iterator(); it.hasNext();) {
137: FTPFile this file = (FTPFile) it.next();
138: if (firstfile == null) {
139: firstfile = this file;
140: }
141: //System.out.println(sdf.format(thisfile.getTimestamp().getTime())
142: // + " " +thisfile.getName());
143: if (lastfile != null) {
144: // verify that the list is sorted earliest to latest.
145: assertTrue(lastfile.getTimestamp().before(
146: this file.getTimestamp()));
147: }
148: lastfile = this file;
149: }
150:
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().before(first));
162:
163: }
164: }
|