Code Search for Developers
 
 
  

FtpClientTest.java from Kneobase at Krugle


Show FtpClientTest.java syntax highlighted

package com.kneobase.driver.ftp;

import java.io.IOException;
import java.io.InputStream;

import junit.framework.TestCase;

import com.kneobase.driver.ftp.client.I_FtpClient;
import com.kneobase.driver.ftp.client.I_FtpFile;
import com.kneobase.driver.ftp.client.JCNFtpClient;

/**
 * @author patricio.keilty@colaborativa.net
 *
 */
public class FtpClientTest extends TestCase {

	private I_FtpClient client;
	
	/**
	 * Constructor for FtpClientTest.
	 * @param arg0
	 */
	public FtpClientTest(String arg0) {
		super(arg0);
	}

	/*
	 * @see TestCase#setUp()
	 */
	protected void setUp() throws Exception {
		super.setUp();
		//client = new JvftpClient();
		client = new JCNFtpClient();
		client.setHostname( System.getProperty( "host" ));
		client.setPort( Integer.parseInt( System.getProperty( "port" )));
		client.setUsername( System.getProperty( "username" ));
		client.setPassword( System.getProperty( "password" ));
		client.connectAndLogin();
	}

	/*
	 * @see TestCase#tearDown()
	 */
	protected void tearDown() throws Exception {
		super.tearDown();
		client.logoutAndDisconnect();
	}

	public void testGetFile(){
	    InputStream is = null;
		try {
			I_FtpFile newFile = client.getFile( System.getProperty( "filename" ));
			is = newFile.getInputStream();
			while( is.skip( 255 ) > 0 );
			assertTrue( true );
		} catch( IOException e ){
			fail( e.getMessage() );
		} finally {
		    if( is != null ){
		        try {
                    is.close();
                } catch (IOException e1) {
                    // intentionally left blank
                }
		    }
		}
	}

	public void testGetDirectory(){
		try {
			I_FtpFile newFile = client.getFile( System.getProperty( "directory" ));
			assertTrue( newFile.isDirectory() );
		} catch( IOException e ){
			fail( e.getMessage() );
		}
	}

	public void testList() {
		try {
			String cwd = client.getCurrentDirectory();
			I_FtpFile[] fileInfos = client.list();
			assertTrue( fileInfos.length > 0 );
			for( int j=0;j<fileInfos.length;j++ ){
				assertFalse( "".equals( fileInfos[j].getName() ));
			}
		} catch( IOException e ){
			fail( e.getMessage() );
		}
	}
	
	public void testGetAbsolutePath(){
		try {
			String cwd = client.getCurrentDirectory();
			I_FtpFile[] fileInfos = client.list();
			assertTrue( fileInfos.length > 0 );
			boolean dirPresent = false;
			int dirIndex = 0;
			for( int j=0;j<fileInfos.length;j++ ){
				if( !dirPresent && fileInfos[j].isDirectory() ){
					dirPresent = true;
					dirIndex = j;
				}
				assertTrue( fileInfos[j].getAbsolutePath().startsWith( cwd ) );
			}

			// test one level up in path
			if( dirPresent ){
				client.changeDirectory( fileInfos[ dirIndex ].getAbsolutePath() );
				cwd = client.getCurrentDirectory();
				fileInfos = client.list();
				for( int j=0;j<fileInfos.length;j++ ){
					assertTrue( fileInfos[j].getAbsolutePath().startsWith( cwd ) );
				}
			}
		} catch( IOException e ){
			fail( e.getMessage() );
		}
	}
	
	public void testRetrieveWholeTree() {
		try {
		    client.changeDirectory( "bibliografia" );
			retrieveDirectoryFiles( client.getCurrentDirectory() );
		} catch( Exception e ){
			fail( e.getMessage() );
		}
	}

	/**
	 * @param string
	 */
	private void retrieveDirectoryFiles( String dirName ){
	    InputStream is = null;
	    try {
			client.changeDirectory( dirName );
			I_FtpFile[] filesInfo = client.list();
			for( int k=0;k<filesInfo.length;k++ ){
				if( filesInfo[k].isDirectory() ) {
					retrieveDirectoryFiles( filesInfo[k].getName() );
				} else {
				    try {
				        System.out.println( filesInfo[k].getAbsolutePath() );
				        is = filesInfo[k].getInputStream();
				        while( is.skip( 255 ) > 0 );
				    } finally {
					    if( is != null ){
					        try {
			                    is.close();
			                } catch (IOException e1) {
			                    // intentionally left blank
			                }
					    }
				    }
				}
			}
			client.changeDirectory( ".." );
		} catch( Exception e ){
			fail( e.getMessage() );
		}
	}
}




See more files for this project here

Kneobase

Kneobase is an enterprise search engine, based upon the Lucene search engine and the Spring framework. It allows to perform full-text search across many different content sources. It is highly adaptable out-of-the-box and has a pluggable architecture.

Project homepage: http://sourceforge.net/projects/kneobase
Programming language(s): Java,XML
License: other

  FtpClientTest.java
  FtpContentSourceTest.java
  TestPathComparison.java