| career | drupal | java | mac | mysql | perl | scala | uml | unix  

Commons Net example source code file (

This example Commons Net source code file ( is included in the "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Java - Commons Net tags/keywords

exception, exception, ftpclient, ftpclientconfigfunctionaltest, ftpclientconfigfunctionaltest, ftpconf, ftpfile, ftpfile, gmt, io, net, network, override, socketexception, testcase, treeset, treeset, util

The Commons Net source code

 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * See the License for the specific language governing permissions and
 * limitations under the License.

import java.util.Calendar;
import java.util.Comparator;
import java.util.TreeSet;

import junit.framework.TestCase;

 * This test was contributed in a different form by W. McDonald Buck
 * of Boulder, Colorado, to help fix some bugs with the FTPClientConfig
 * in a real world setting.  It is a perfect functional test for the
 * Time Zone functionality of FTPClientConfig.
 * A publicly accessible FTP server at the US National Oceanographic and
 * Atmospheric Adminstration houses a directory which contains
 * 300 files, named sn.0000 to sn.0300. Every ten minutes or so
 * the next file in sequence is rewritten with new data. Thus the directory
 * contains observations for more than 24 hours of data.  Since the server
 * has its clock set to GMT this is an excellent functional test for any
 * machine in a different time zone.
 * Noteworthy is the fact that the ftp routines in some web browsers don't
 * work as well as this.  They can't, since they have no way of knowing the
 * server's time zone.  Depending on the local machine's position relative
 * to GMT and the time of day, the browsers may decide that a timestamp
 * would be in the  future if given the current year, so they assume the
 * year to be  last year.  This illustrates the value of FTPClientConfig's
 * time zone functionality.

public class FTPClientConfigFunctionalTest extends TestCase {

    private FTPClient FTP = new FTPClient();
    private FTPClientConfig FTPConf;

    public FTPClientConfigFunctionalTest() {


     * @throws java.lang.Exception
    protected void setUp() throws Exception {
        FTPConf = new FTPClientConfig(FTPClientConfig.SYST_UNIX);
        try {
        } catch (SocketException e) {
        } catch (IOException e) {
     * @throws java.lang.Exception
    protected void tearDown() throws Exception {
     * @param arg0
    public FTPClientConfigFunctionalTest(String arg0) {

    private TreeSet<FTPFile> getSortedList(FTPFile[] files) {
        // create a TreeSet which will sort each element
        // as it is added.
        TreeSet<FTPFile> sorted = new TreeSet(new Comparator() {

            public int compare(Object o1, Object o2) {
                FTPFile f1 = (FTPFile) o1;
                FTPFile f2 = (FTPFile) o2;
                return f1.getTimestamp().getTime().compareTo(f2.getTimestamp().getTime());


        for (int i=0; i < files.length; i++) {
            // The directory contains a few additional files at the beginning
            // which aren't in the series we want. The series we want consists
            // of files named sn.dddd. This adjusts the file list to get rid
            // of the uninteresting ones.
            if (files[i].getName().startsWith("sn")) {
        return sorted;

    public void testTimeZoneFunctionality() throws Exception {
        java.util.Date now = new java.util.Date();
        FTPFile[] files = FTP.listFiles();
        TreeSet<FTPFile> sorted = getSortedList(files);
        //SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy HH:mm z" );
        FTPFile lastfile = null;
        FTPFile firstfile = null;
        for (FTPFile thisfile : sorted) {
            if (firstfile == null) {
                firstfile = thisfile;
            //        + " " +thisfile.getName());
            if (lastfile != null) {
                // verify that the list is sorted earliest to latest.
            lastfile = thisfile;

        // test that notwithstanding any time zone differences, the newest file
        // is older than now.
        Calendar first = firstfile.getTimestamp();

        // test that the oldest is less than two days older than the newest
        // and, in particular, that no files have been considered "future"
        // by the parser and therefore been relegated to the same date a
        // year ago.
        first.add(Calendar.DATE, 2);
        assertTrue(lastfile.getTimestamp().getTime().toString()+" before "+ first.getTime().toString(),lastfile.getTimestamp().before(first));


Other Commons Net examples (source code examples)

Here is a short list of links related to this Commons Net source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller


new blog posts


Copyright 1998-2021 Alvin Alexander,
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.