commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jbre...@brekke.org (Jeffrey D. Brekke)
Subject Re: [Net] FTPClient and finally questions...
Date Wed, 23 Jul 2003 04:27:17 GMT
package org.apache.commons.net.ftp;

import junit.framework.TestCase;

import java.io.IOException;

import java.util.Arrays;

/**
 * @author jbrekke
 */
public class TryCatchFinallyTest extends TestCase
{
    private FTPClient ftpClient;

    /**
     * Constructor for TryCatchFinallyTest.
     *
     * @param arg0
     */
    public TryCatchFinallyTest(String arg0)
    {
        super(arg0);
    }

	public void testFileDownloadFailure()
		throws Exception
	{
		try
		{
			String[] files = listFiles("myfile", ".");

			fail("Found files and shouldn't have: " + Arrays.asList(files));
		}
		catch (FTPServerException e)
		{
			// do nothing, we expect this to happen
		}
	}

    private String[] listFiles(String mask,
                              String dir)
                       throws FTPServerException
    {
        String[] _files = null;

        try
        {
            ftpClient.changeWorkingDirectory(dir);
            _files = ftpClient.listNames(mask);

            if ((_files == null) || (_files.length == 0))
            {
                warn("No files to download for mask " + mask);
                throw new FTPServerException("No files found!");
            }
        }
        catch (IOException e)
        {
            String msg = "FTP Server Error Occurred : " + e.getMessage();

            warn(msg);
            throw new FTPServerException(msg);
        }
        finally
        {
            if (ftpClient.isConnected())
            {
                try
                {
                    ftpClient.disconnect();
                }
                catch (IOException f)
                {
                    // do nothing
                }
            }
        }

        return _files;
    }

    /* (non-Javadoc)
     * @see junit.framework.TestCase#setUp()
     */
    protected void setUp() throws Exception
    {
        super.setUp();
        ftpClient = new FTPClient();
        ftpClient.connect("ftp.ibiblio.org");
        ftpClient.login("anonymous", "joe@apache.org");
    }

    /* (non-Javadoc)
     * @see junit.framework.TestCase#tearDown()
     */
    protected void tearDown()
                     throws Exception
    {
        super.tearDown();

        try
        {
            ftpClient.logout();
            ftpClient.disconnect();
        }
        catch (NullPointerException npe)
        {
        }
    }

    /**
     * @param string
     */
    private void warn(String string)
    {
        System.out.println(string);
    }
}



>>>>> On Tue, 22 Jul 2003 18:12:09 -0400, Jeff Duska <Jeff.Duska@noaa.gov>
said:

> _files = ftpClient.listNames(mask); if (_files == null ||
> _files.length ==0) { log.warn("No files to download for mask " +
> mask); throw new FTPServerException("No files found!");
>             }

>         // code to download the files

>         } catch (IOException e) { String msg = "FTP Server Error
> Occurred" + e.getMessage(); log.warn(msg); throw new
> FTPServerException(msg); } finally { if (ftpClient.isConnected()) {
> try { ftpClient.disconnect(); } catch (IOException f) { // do
> nothing
>                 }
>             }
>             return _files;
>         }


> I throw a FTPServerException when listNames returns a zero length
> array or null. The problem is my FTPServerException is getting lost.
> I can follow the exception into the finally cause. Once I return
> from this method, java forgets about my exception that I threw. In
> my example unit test, shown below, I don't get a FTPServerException
> thus my test fails.

>   public void testFileDownloadFailure() throws
> FTPServerTimeoutException { try { // setup will creates an instance
> of FTPServer // FTP server should cause a FTPServerException
> _ftp.getFiles("myfile", "."); fail(); } catch (FTPServerException e)
> { // do nothing, we expect this to happen
>         }
>     }

-- 
=====================================================================
Jeffrey D. Brekke                                   jbrekke@wi.rr.com
Wisconsin,  USA                                     brekke@apache.org
                                                    ekkerbj@yahoo.com


Mime
  • Unnamed multipart/mixed (inline, None, 0 bytes)
View raw message