📄 testtransfer.java
字号:
public void testTransferUnixText() throws Exception {
log.info("testTransferUnixText()");
try {
connect();
// monitor transfer progress
ftp.setProgressMonitor(new TestProgressMonitor(), 250000);
// move to test directory
ftp.chdir(testdir);
ftp.setType(FTPTransferType.ASCII);
// put to a random filename
String filename = generateRandomFilename();
ftp.put(localDataDir + localUnixTextFile, filename);
// get it back
ftp.get(localDataDir + filename, filename);
// check equality of local files - against the equivalent local text file
// not the transferred unix one
assertIdentical(localDataDir + localTextFile, localDataDir + filename);
// delete remote file
ftp.delete(filename);
try {
ftp.modtime(filename);
fail(filename + " should not be found");
}
catch (IOException ex) {
log.info("Expected exception: " + ex.getMessage());
}
catch (FTPException ex) {
log.info("Expected exception: " + ex.getMessage());
}
// and delete local file
File local = new File(localDataDir + filename);
local.delete();
ftp.quit();
}
finally {
ftp.quitImmediately();
}
}
/**
* Test getting a byte array
*/
public void testGetBytes() throws Exception {
log.info("testGetBytes()");
try {
connect();
// monitor transfer progress
ftp.setProgressMonitor(new TestProgressMonitor(), 250000);
// move to test directory
ftp.chdir(testdir);
ftp.setType(FTPTransferType.BINARY);
// get the file and work out its size
String filename1 = generateRandomFilename();
ftp.get(localDataDir + filename1, remoteBinaryFile);
File file1 = new File(localDataDir + filename1);
long len = file1.length();
// now get to a buffer and check the length
byte[] result = ftp.get(remoteBinaryFile);
assertTrue(result.length == len);
// put the buffer
String filename2 = generateRandomFilename();
ftp.put(result, filename2);
// get it back as a file
ftp.get(localDataDir + filename2, filename2);
// remove it remotely
ftp.delete(filename2);
// and now check files are identical
File file2 = new File(localDataDir + filename2);
assertIdentical(file1, file2);
// and finally delete them
file1.delete();
file2.delete();
ftp.quit();
}
finally {
ftp.quitImmediately();
}
}
/**
* Test the stream functionality
*/
public void testTransferStream() throws Exception {
log.info("testTransferStream()");
try {
connect();
// monitor transfer progress
ftp.setProgressMonitor(new TestProgressMonitor(), 250000);
// move to test directory
ftp.chdir(testdir);
ftp.setType(FTPTransferType.BINARY);
// get file as output stream
ByteArrayOutputStream out = new ByteArrayOutputStream();
ftp.get(out, remoteBinaryFile);
// convert to byte array
byte[] result1 = out.toByteArray();
// put this
String filename = generateRandomFilename();
ftp.put(new ByteArrayInputStream(result1), filename);
// get it back
byte[] result2 = ftp.get(filename);
// delete remote file
ftp.delete(filename);
// and compare the buffers
assertIdentical(result1, result2);
ftp.quit();
}
finally {
ftp.quitImmediately();
}
}
/**
* Test the append functionality in put()
*/
public void testPutAppend() throws Exception {
log.info("testPutAppend()");
try {
connect();
// monitor transfer progress
ftp.setProgressMonitor(new TestProgressMonitor(), 250000);
// move to test directory
ftp.chdir(testdir);
ftp.setType(FTPTransferType.BINARY);
// put to a random filename
String filename = generateRandomFilename();
ftp.put(localDataDir + localBinaryFile, filename);
// second time, append if possible
int count = 1;
ftp.put(localDataDir + localBinaryFile, filename, true);
count++;
// get it back & delete remotely
ftp.get(localDataDir + filename, filename);
ftp.delete(filename);
// check it is the right size
File file1 = new File(localDataDir + localBinaryFile);
File file2 = new File(localDataDir + filename);
assertTrue(file1.length()*count == file2.length());
log.info(localBinaryFile + " length=" + file1.length() + ", " + filename + " length=" + file2.length());
// and finally delete it
file2.delete();
ftp.quit();
}
finally {
ftp.quitImmediately();
}
}
/**
* Test transferring empty files
*/
public void testTransferEmpty() throws Exception {
log.info("testTransferEmpty()");
try {
connect();
// move to test directory
ftp.chdir(testdir);
// get an empty file
ftp.get(localDataDir + remoteEmptyFile, remoteEmptyFile);
File empty = new File(localDataDir + remoteEmptyFile);
assertTrue(empty.exists());
assertTrue(empty.length() == 0);
// delete it
empty.delete();
// put an empty file
ftp.put(localDataDir + localEmptyFile, localEmptyFile);
// get it back as a different filename
String filename = generateRandomFilename();
ftp.get(localDataDir + filename, localEmptyFile);
empty = new File(localDataDir + filename);
assertTrue(empty.exists());
assertTrue(empty.length() == 0);
// delete file we got back (copy of our local empty file)
empty.delete();
// and delete the remote empty file we
// put there
ftp.delete(localEmptyFile);
ftp.quit();
}
finally {
ftp.quitImmediately();
}
}
/**
* Test transferring non-existent files
*/
public void testTransferNonExistent() throws Exception {
log.info("testTransferNonExistent()");
try {
connect();
// move to test directory
ftp.chdir(testdir);
// generate a name & try to get it
String filename = generateRandomFilename();
log.info("Getting non-existent file: " + filename);
try {
ftp.get(localDataDir + filename, filename);
fail(filename + " should not be found");
}
catch (IOException ex) {
log.info("Expected exception: " + ex.getMessage());
}
catch (FTPException ex) {
log.info("Expected exception: " + ex.getMessage());
}
// ensure we don't have a local file of that name produced
File file = new File(localDataDir + filename);
assertFalse(file.exists());
// generate name & try to put
filename = generateRandomFilename();
try {
ftp.put(localDataDir + filename, filename);
fail(filename + " should not be found");
}
catch (FileNotFoundException ex) {
log.info("Expected exception: " + ex.getMessage());
}
ftp.quit();
}
finally {
ftp.quitImmediately();
}
}
/**
* Test of progress monitor functionality
*/
public class TestProgressMonitor implements FTPProgressMonitor {
/* (non-Javadoc)
* @see com.enterprisedt.net.ftp.FTPProgressMonitor#bytesTransferred(long)
*/
public void bytesTransferred(long count) {
log.info(count + " bytes transferred");
}
}
/**
* Automatic test suite construction
*
* @return suite of tests for this class
*/
public static Test suite() {
return new TestSuite(TestTransfer.class);
}
/**
* Enable our class to be run, doing the
* tests
*/
public static void main(String[] args) {
junit.textui.TestRunner.run(suite());
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -