package org.openslx.satellitedaemon.ftp; import java.io.File; import java.io.IOException; import java.util.Arrays; import junit.framework.Assert; import org.apache.ftpserver.FtpServer; import org.apache.ftpserver.ftplet.FtpException; import org.junit.AfterClass; import org.junit.BeforeClass; import org.junit.Test; public class FtpUpDownUtilTest { private static final int FTP_PORT = 2121; private static final String FTP_HOST = "localhost"; private static final String FTP_HOME_DIR = "target/FtpHome"; private static final String FTPUSERSPROPS_FILE = "target/FtpUsers.properties"; private static final String READ_USER_NAME = "ReadUserName"; private static final String READ_USER_PWD = "ReadUserPwd"; private static final String WRITE_USER_NAME = "WriteUserName"; private static final String WRITE_USER_PWD = "WriteUserPwd"; private static FtpServer ftpServer; @BeforeClass public static void startFtpServer() throws FtpException, IOException { ftpServer = FtpTestUtil.createFtpServer( FTP_PORT, FTP_HOME_DIR, READ_USER_NAME, READ_USER_PWD, WRITE_USER_NAME, WRITE_USER_PWD, FTPUSERSPROPS_FILE, 0 ); ftpServer.start(); } @AfterClass public static void stoppFtpServer() { // Um den FTP-Server von ausserhalb des Tests eine Zeit lang ueber // ftp://WriteUserName:WriteUserPwd@localhost:2121 // anzusprechen, kann folgende Zeile aktiviert werden: // try { Thread.sleep( 55000 ); } catch( InterruptedException e ) {/*ok*/} ftpServer.stop(); ftpServer = null; } @Test public void testFtp() throws IOException { final String LOCAL_SRC_FILE = "TestSrc.txt"; final String LOCAL_DST_FILE = "target/TestDst.txt"; final String REMOTE_FILE = "Test.txt"; File testFile = new File( LOCAL_SRC_FILE ); if( !testFile.exists() ) testFile.createNewFile(); // Upload-Versuch ohne Schreibberechtigung muss fehlschlagen: Assert.assertFalse( "READ_USER", FtpUpDownUtil.upload( LOCAL_SRC_FILE, REMOTE_FILE, FTP_HOST, FTP_PORT, READ_USER_NAME, READ_USER_PWD, false ) ); // Teste Upload mit Schreibberechtigung: Assert.assertTrue( "WRITE_USER", FtpUpDownUtil.upload( LOCAL_SRC_FILE, REMOTE_FILE, FTP_HOST, FTP_PORT, WRITE_USER_NAME, WRITE_USER_PWD, false ) ); Assert.assertTrue( "REMOTE_FILE.exists", new File( FTP_HOME_DIR, REMOTE_FILE ).exists() ); // Teste Download: Assert.assertTrue( "Download", FtpUpDownUtil.download( LOCAL_DST_FILE, REMOTE_FILE, FTP_HOST, FTP_PORT, READ_USER_NAME, READ_USER_PWD, false ) ); Assert.assertTrue( "LOCAL_DST_FILE.exists", new File( LOCAL_DST_FILE ).exists() ); // Teste Auflistung: String[] remoteFilenameList = FtpUpDownUtil.list( FTP_HOST, FTP_PORT, READ_USER_NAME, READ_USER_PWD ); Assert.assertTrue( "remoteFilenameList", Arrays.asList( remoteFilenameList ).contains( REMOTE_FILE ) ); } }