A
Andrea Francia
Recently during the write of a specialized FTP Client in Java I found
some problem in writing automated tests for testing the main
functionalities.
So I used a sort of semi automated tests that required me to set up the
a local FTP Server with the particular configuration needed for the
specific test.
The project was finished without using a Ftp Server Stub, but I think
that a configurable Ftp Server Stub configurable via code could be
useful, may be for others developers or for me in the future.
So I investigated the feasibility creating an incomplete prototype of
Ftp Server Stub. Now, before commit myself in this work I would hear
some opinions on the possible usefullness of this project.
Do you think could be useful?
Do you know some other alternative?
The following example shows how the FtpServerStub could be used in a
JUnit test which test the downloadLatestFile() method which should
download the latest file matching the URL glob pattern.
public void testDownloadLatestFile() {
// prepare ftp server
FtpServerStub server = new FtpServerStub();
server.setPort(2121);
server.setFileSystemRoot(new Directory() {{
mkdir("data");
touch("data/list-1-jan-1970.txt")
.withModificationTime(0)
.withUtf8Content("BAD: this is not the latest!");
touch("data/list-1-jan-2000.txt")
.withModificationTime(946684800L)
.withUtf8Content("You downloaded the right one!");
touch("data/another-file.dat")
.withModificationTime(1199145600L) // newer that 1-jan-2000
.withUtf8Content("BAD: this does not matches the pattern!");
}});
server.start(); // start in a new thread
// execute the method under test
String result = Downloader.downloadLatestFile(
"ftp://127.0.0.1:2121/data/*.txt");
// verify the result
assertEquals(result, "You downloaded the right one!");
}
some problem in writing automated tests for testing the main
functionalities.
So I used a sort of semi automated tests that required me to set up the
a local FTP Server with the particular configuration needed for the
specific test.
The project was finished without using a Ftp Server Stub, but I think
that a configurable Ftp Server Stub configurable via code could be
useful, may be for others developers or for me in the future.
So I investigated the feasibility creating an incomplete prototype of
Ftp Server Stub. Now, before commit myself in this work I would hear
some opinions on the possible usefullness of this project.
Do you think could be useful?
Do you know some other alternative?
The following example shows how the FtpServerStub could be used in a
JUnit test which test the downloadLatestFile() method which should
download the latest file matching the URL glob pattern.
public void testDownloadLatestFile() {
// prepare ftp server
FtpServerStub server = new FtpServerStub();
server.setPort(2121);
server.setFileSystemRoot(new Directory() {{
mkdir("data");
touch("data/list-1-jan-1970.txt")
.withModificationTime(0)
.withUtf8Content("BAD: this is not the latest!");
touch("data/list-1-jan-2000.txt")
.withModificationTime(946684800L)
.withUtf8Content("You downloaded the right one!");
touch("data/another-file.dat")
.withModificationTime(1199145600L) // newer that 1-jan-2000
.withUtf8Content("BAD: this does not matches the pattern!");
}});
server.start(); // start in a new thread
// execute the method under test
String result = Downloader.downloadLatestFile(
"ftp://127.0.0.1:2121/data/*.txt");
// verify the result
assertEquals(result, "You downloaded the right one!");
}