KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > net > ftp > FTPClientConfigFunctionalTest


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 JavaDoc;
21 import java.net.SocketException JavaDoc;
22 import java.text.SimpleDateFormat JavaDoc;
23 import java.util.Calendar JavaDoc;
24 import java.util.Comparator JavaDoc;
25 import java.util.Iterator JavaDoc;
26 import java.util.TreeSet JavaDoc;
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 JavaDoc {
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 JavaDoc e) {
79             e.printStackTrace();
80         } catch (IOException JavaDoc e) {
81             e.printStackTrace();
82         }
83     }
84     /*
85      * @throws java.lang.Exception
86      */

87     protected void tearDown() throws Exception JavaDoc {
88         FTP.disconnect();
89         super.tearDown();
90     }
91     /**
92      * @param arg0
93      */

94     public FTPClientConfigFunctionalTest(String JavaDoc arg0) {
95         super(arg0);
96     }
97
98     
99     private TreeSet JavaDoc getSortedList(FTPFile[] files) {
100         // create a TreeSet which will sort each element
101
// as it is added.
102
TreeSet JavaDoc sorted = new TreeSet JavaDoc(new Comparator JavaDoc() {
103
104             public int compare(Object JavaDoc o1, Object JavaDoc 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 JavaDoc[] args) {
127         FTPClientConfigFunctionalTest F = new FTPClientConfigFunctionalTest();
128     }
129     
130     public void testTimeZoneFunctionality() throws Exception JavaDoc {
131         java.util.Date JavaDoc now = new java.util.Date JavaDoc();
132         FTPFile[] files = FTP.listFiles();
133         TreeSet JavaDoc 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 JavaDoc 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 JavaDoc 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
170
Popular Tags