Package org.jboss.internal.soa.esb.util

Source Code of org.jboss.internal.soa.esb.util.FtpImplUnitTest

/*
* JBoss, Home of Professional Open Source Copyright 2006, JBoss Inc., and
* individual contributors as indicated by the @authors tag. See the
* copyright.txt in the distribution for a full listing of individual
* contributors.
*
* This is free software; you can redistribute it and/or modify it under the
* terms of the GNU Lesser General Public License as published by the Free
* Software Foundation; either version 2.1 of the License, or (at your option)
* any later version.
*
* This software is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
* details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this software; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA, or see the FSF
* site: http://www.fsf.org.
*/
package org.jboss.internal.soa.esb.util;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URISyntaxException;
import java.util.Arrays;
import java.util.List;

import junit.framework.JUnit4TestAdapter;

import org.apache.log4j.Logger;
import org.jboss.internal.soa.esb.util.embedded.EmbeddableException;
import org.jboss.internal.soa.esb.util.embedded.ftp.FtpTestUtil;
import org.jboss.internal.soa.esb.util.embedded.ftp.NoConfigFileFtpServer;
import org.jboss.soa.esb.ConfigurationException;
import org.jboss.soa.esb.addressing.EPR;
import org.jboss.soa.esb.addressing.eprs.FTPEpr;
import org.jboss.soa.esb.addressing.eprs.FileEpr;
import org.jboss.soa.esb.helpers.ConfigTree;
import org.jboss.soa.esb.util.RemoteFileSystem;
import org.jboss.soa.esb.util.RemoteFileSystemException;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

/**
* Unit test for FtpImpl that uses an embedded ftp server
*
* @author Daniel Bevenius
*
*/
public class FtpImplUnitTest
{
  private static Logger log = Logger.getLogger( FtpImplUnitTest.class );
 
  /* EmbeddedFtp Server */
  private static NoConfigFileFtpServer ftpServer;

  /* Instance of class under test */
  private static FtpImpl ftpImpl;
 
  private static String remoteInputDirName;
  private static String remoteUploadDirName;
  private static final String INPUT_SUFFIX = ".txt";
  private static final String RENAMED_SUFFIX = ".renamed";
 
  /* Content for created test files */
  private static final String TEST_FILE_CONTENT = FtpImplUnitTest.class .getName() + " junit ftp test";
  private File testFile;
  private File renamedFile;

  @BeforeClass
  public static void classSetup() throws EmbeddableException, ConfigurationException, RemoteFileSystemException, MalformedURLException, URISyntaxException
  {
    ftpServer = new NoConfigFileFtpServer();
    ftpServer.setPort( 2221 );
    ftpServer.start();

    remoteInputDirName = "/" + ftpServer.getLocalInputDir().getName();
    remoteUploadDirName = "/" + ftpServer.getLocalUploadDir().getName();
    ftpImpl = new FtpImpl( createConfigTree(), true );
  }

  @AfterClass
  public static void classTearDown() throws EmbeddableException
  {
    try
    {
      ftpServer.stop();
    }
    catch (Exception e)
    {
      log.warn( e.getMessage() );
    }
   
    if  ( !FtpTestUtil.deleteDir( ftpServer.getFtpServerDir() ) )
    {
      log.warn( "Could not delete " +  ftpServer.getFtpServerDir() ) ;
    }
  }

  @Before
  public void setUp()
  {
    testFile = FtpTestUtil.createTestFile( ftpServer.getLocalInputDir(), getClass().getName() + INPUT_SUFFIX, TEST_FILE_CONTENT );
  }
 
  @After
  public void tearDown()
  {
    FtpTestUtil.deleteFile( testFile );
    FtpTestUtil.deleteFile( renamedFile );
  }
 
  @Test
  public void construtor_FTPEpr()
  {
    try
    {
      new FtpImpl( createFTPEpr(), true );
    }
    catch ( Exception e )
    {
      fail ( e.getMessage() );
    }
  }
 
  @Test
  public void getRemoteDir() throws RemoteFileSystemException
  {
    String oldRemoteDir =  remoteInputDirName;
    try
    {
      ftpImpl.setRemoteDir( remoteUploadDirName );
      assertEquals( "The remote dir was not changed.", remoteUploadDirName, ftpImpl.getRemoteDir() );
    }
    finally
    {
      try { ftpImpl.setRemoteDir( oldRemoteDir ); } catch (Exception e) { fail( e.getMessage() ); }
    }
  }

  @Test
  public void setRemoteDir() throws RemoteFileSystemException
  {
    String oldRemoteDir = remoteInputDirName;
    try
    {
      ftpImpl.setRemoteDir( remoteUploadDirName );

      String actualRemoteDir = ftpImpl.getRemoteDir();
      assertFalse( "The setRemoteDir method did not change the directory!", oldRemoteDir.equals( actualRemoteDir ) );
    }
    finally
    {
      try { ftpImpl.setRemoteDir( oldRemoteDir ); } catch (Exception e) { fail( e.getMessage() ); }
    }
  }

  @Test
  public void getFileListFromRemoteDir() throws RemoteFileSystemException, IOException
  {
    String[] fileListFromRemoteDir = ftpImpl .getFileListFromRemoteDir( INPUT_SUFFIX );
    assertNotNull( fileListFromRemoteDir );
    List<String> fileList = Arrays.asList( fileListFromRemoteDir );
    assertTrue( "The test file was not included in the List! ", fileList.contains( testFile.getName() ) );
  }

  @Test
  public void deleteRemoteFile() throws RemoteFileSystemException
  {
    ftpImpl.deleteRemoteFile( testFile.getName() );
    assertFalse( "File was not deleted", testFile.exists() );
  }

  @Test
  public void remoteDelete() throws RemoteFileSystemException
  {
    ftpImpl.remoteDelete( testFile );
    assertFalse( testFile.exists() );
  }

  @Test
  public void renameInRemoteDir() throws RemoteFileSystemException
  {
    File from = testFile;
    String toFileName = from.getName() + RENAMED_SUFFIX;
     
    ftpImpl.renameInRemoteDir( from.getName(), toFileName );

    renamedFile = new File( ftpServer.getLocalInputDir(), toFileName );
    assertFalse( "The file was not removed from the filesystem", from .exists() );
    assertTrue( "The named file does not exist", renamedFile.exists() );
  }

  @Test
  public void remoteRename_To_Different_RemoteDir() throws RemoteFileSystemException
  {
    File from = createAbsoluteFromFile();
    File to = createAbsoluteToFileUploadDir();

    ftpImpl.remoteRename( from, to );

    renamedFile = new File( ftpServer.getLocalUploadDir(), to.getName() );
    assertFalse( "The file was not removed from the filesystem", from .exists() );
    assertTrue( renamedFile + " was not found in dir " + ftpServer.getLocalUploadDir(), renamedFile.exists() );
  }
 
  @Test
  public void remoteRename_To_Same_RemoteDir() throws RemoteFileSystemException
  {
    File from =  createAbsoluteFromFile();
    File to = createAbsoluteToFile();
     
    ftpImpl.remoteRename( from, to );

    renamedFile = new File( ftpServer.getLocalInputDir(), testFile.getName() + RENAMED_SUFFIX );
    assertFalse( testFile.exists() );
    assertTrue( renamedFile.exists() );
  }
 
  @Test
  public void downloadFile() throws RemoteFileSystemException, IOException
  {
    File localFile = null;
    File localDownloadedlFile = null;
    String testFileName = "downloadFileTestFile.txt";
    try
    {
      localFile = FtpTestUtil.createTestFile( ftpServer .getLocalInputDir(), testFileName, TEST_FILE_CONTENT );
      String remoteFileName = "/input/" + localFile.getName();
      String downloadFileName = testFileName + ".downloaded";

      ftpImpl.downloadFile( remoteFileName, downloadFileName );

      localDownloadedlFile = new File( ftpServer.getRootDir(), downloadFileName );
      assertTrue( "The remote file should not have been removed.", localFile.exists() );
      assertTrue( "File was not downloaded ", localDownloadedlFile .exists() );
    }
    finally
    {
      FtpTestUtil.deleteFile( localDownloadedlFile );
      FtpTestUtil.deleteFile( localFile );
    }
  }

  @Test
  public void uploadFile() throws RemoteFileSystemException
  {
    File localFile = null;
    File remoteFile = null;
    String testFileName = "uploadFileTestFile.txt";
    try
    {
      localFile = FtpTestUtil.createTestFileftpServer.getLocalInputDir(),  testFileName, TEST_FILE_CONTENT );
     
      String renameTo = testFileName + RENAMED_SUFFIX;
      ftpImpl.uploadFile( localFile, renameTo );
      remoteFile = new File ( ftpServer.getLocalInputDir(), renameTo );
     
      assertTrue( "The file was not uploaded succesfully." , remoteFile.exists() );
    }
    finally
    {
      FtpTestUtil.deleteFile( remoteFile );
      FtpTestUtil.deleteFile( localFile );
    }
  }

  @Test
  public void uploadUTF8File() throws RemoteFileSystemException
  {
    // Test to validate JBESB-3312
   
    File localFile = null;
    File remoteFile = null;
    String testFileName = "ﺱﻼﻣTestFile.txt";
    try
    {
      localFile = FtpTestUtil.createTestFileftpServer.getLocalInputDir(),  testFileName, TEST_FILE_CONTENT );
     
      String renameTo = testFileName + RENAMED_SUFFIX;
      ftpImpl.uploadFile( localFile, renameTo );
      remoteFile = new File ( ftpServer.getLocalInputDir(), renameTo );
     
      assertTrue( "The file was not uploaded succesfully." , remoteFile.exists() );
    }
    finally
    {
      FtpTestUtil.deleteFile( remoteFile );
      FtpTestUtil.deleteFile( localFile );
    }
  } 
 
  @Test
  public void listFilesInDirectory() throws RemoteFileSystemException
  {
    // Test to validate JBESB-3293
   
    File localFile = null;
    File localDir = null;
    String testFileName = "112233445566778899.test";
    try
    {
      String dirName = remoteUploadDirName + File.separator + "temp";
     
      localDir = FtpTestUtil.createTestDirectory(dirName);
      localFile = FtpTestUtil.createTestFile(dirName + File.separator + testFileName, TEST_FILE_CONTENT );

      String[] listone = ftpImpl.getFileListFromRemoteDir("");

      String fileName = null;
      for (int i = 0; i < listone.length; i++) {
        if ((listone[i] != null) && (listone[i].contains(testFileName))) {
          fileName = listone[i];
        }
      }
      assertTrue("The list of files should not contain " + testFileName + " but "
          + fileName + " was found.", fileName == null);
     
     
    } catch (Exception e) {
     
    }
    finally
    {
      FtpTestUtil.deleteFile( localFile );
      FtpTestUtil.deleteFile( localDir );
    }
  } 
 
  private static ConfigTree createConfigTree()
  {
    ConfigTree configTree = new ConfigTree( "junitFtpImplTest" );
    try
    {
      configTree.setAttribute( FileEpr.URL_TAG, ftpServer.getURL().toString() ) ;
    }
    catch ( MalformedURLException e )
    {
      log.error);
      fail ( e.getMessage() );
    }
    configTree.setAttribute( RemoteFileSystem.PARMS_FTP_SERVER, ftpServer .getHost() );
    configTree.setAttribute( RemoteFileSystem.PARMS_USER, ftpServer .getUserName() );
    configTree.setAttribute( RemoteFileSystem.PARMS_PASSWD, ftpServer .getPassword() );
    configTree.setAttribute( RemoteFileSystem.PARMS_REMOTE_DIR, remoteInputDirName );
    configTree.setAttribute( RemoteFileSystem.PARMS_PORT, Integer .toString( ftpServer.getPort() ) );
    configTree.setAttribute( RemoteFileSystem.PARMS_LOCAL_DIR, ftpServer .getRootDir() );
    configTree.setAttribute( RemoteFileSystem.PARMS_ASCII, Boolean .toString( false ) );
    configTree.setAttribute( RemoteFileSystem.PARMS_PASSIVE, Boolean .toString( false ) );
    configTree.setAttribute( RemoteFileSystem.PARMS_RENAME_RETRY, Integer .toString(10) );
    configTree.setAttribute( RemoteFileSystem.PARMS_TIMEOUT_DEFAULT, Integer .toString(0) );
    configTree.setAttribute( RemoteFileSystem.PARMS_TIMEOUT_DATA, Integer .toString(0) );
    configTree.setAttribute( RemoteFileSystem.PARMS_TIMEOUT_SO, Integer .toString(0) );
    configTree.setAttribute( RemoteFileSystem.PARMS_CONTROL_CHANNEL_ENCODING, "UTF-8" );

    return configTree;
  }
 
  private static FTPEpr createFTPEpr() throws MalformedURLException, URISyntaxException
  {
    FTPEpr epr = new FTPEpr ( new EPR() );
    epr.setURI( ftpServer.getURL().toURI() );
    epr.setUserName( ftpServer.getUserName() );
    epr.setErrorDirectory( ftpServer.getErrorDirName() );
    epr.setInputSuffix( INPUT_SUFFIX );
    epr.setPassword( ftpServer.getPassword() );
    epr.setPostDirectory( ftpServer.getUploadDirName() );
    epr.setWorkSuffix( ".work" );
    return epr;
  }
 
  private File createAbsoluteFromFile()
  {
    return new File ( remoteInputDirName + "/" + testFile.getName() );
  }
 
  private File createAbsoluteToFile()
  {
    return new File ( remoteInputDirName + "/" + testFile.getName() + RENAMED_SUFFIX );
  }
 
  private File createAbsoluteToFileUploadDir()
  {
    return new File ( remoteUploadDirName + "/" +  testFile.getName() + RENAMED_SUFFIX );
  }

  public static junit.framework.Test suite()
  {
    return new JUnit4TestAdapter( FtpImplUnitTest.class);
  }
 
}
TOP

Related Classes of org.jboss.internal.soa.esb.util.FtpImplUnitTest

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.