improving FtpSource tests

This commit is contained in:
Iwein Fuld
2008-07-30 10:14:32 +00:00
parent a2d43b1d80
commit 37ca37d2c4
2 changed files with 119 additions and 6 deletions

View File

@@ -8,7 +8,6 @@ import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Collections;
import java.util.List;
import org.apache.commons.net.ftp.FTPClient;
@@ -38,6 +37,8 @@ public class FtpSourceTests {
private FtpSource ftpSource;
private Long size = 100l;
@Before
public void initializeFtpSource() {
ftpSource = new FtpSource(messageCreator, ftpClient);
@@ -61,7 +62,7 @@ public class FtpSourceTests {
expect(ftpClient.login(USER, PASS)).andReturn(true);
expect(ftpClient.setFileType(anyInt())).andReturn(true);
expect(ftpClient.printWorkingDirectory()).andReturn("/");
expect(ftpClient.listFiles()).andReturn(mockedFTPFilesNamed(Calendar.getInstance(), "test"));
expect(ftpClient.listFiles()).andReturn(mockedFTPFilesNamed("test"));
expect(ftpClient.retrieveFile(eq("test"), isA(OutputStream.class))).andReturn(true);
// create message
expect(messageCreator.createMessage(isA(List.class))).andReturn(
@@ -72,13 +73,16 @@ public class FtpSourceTests {
verify(globalMocks);
}
private FTPFile[] mockedFTPFilesNamed(Calendar timestamp, String... names) {
private FTPFile[] mockedFTPFilesNamed(String... names) {
List<FTPFile> files = new ArrayList<FTPFile>();
// ensure difference by increasing size
Calendar timestamp = Calendar.getInstance();
size++;
for (String name : names) {
FTPFile ftpFile = createMock(FTPFile.class);
expect(ftpFile.getName()).andReturn(name).anyTimes();
expect(ftpFile.getTimestamp()).andReturn(timestamp).anyTimes();
expect(ftpFile.getSize()).andReturn(100l).anyTimes();
expect(ftpFile.getSize()).andReturn(size).anyTimes();
files.add(ftpFile);
replay(ftpFile);
}
@@ -97,8 +101,7 @@ public class FtpSourceTests {
expect(ftpClient.printWorkingDirectory()).andReturn("/");
// get files
Calendar timestamp = Calendar.getInstance();
expect(ftpClient.listFiles()).andReturn(mockedFTPFilesNamed(timestamp, "test", "test2")).anyTimes();
expect(ftpClient.listFiles()).andReturn(mockedFTPFilesNamed("test", "test2")).times(2);
expect(ftpClient.retrieveFile(eq("test"), isA(OutputStream.class))).andReturn(true);
expect(ftpClient.retrieveFile(eq("test2"), isA(OutputStream.class))).andReturn(true);
@@ -116,5 +119,46 @@ public class FtpSourceTests {
assertNull(secondReceived);
}
@Test
public void retrieveMultipleChangingFiles() throws Exception {
// assume client already connected
expect(ftpClient.isConnected()).andReturn(true).anyTimes();
// first run
checkOrder(ftpClient, true);
FTPFile[] mockedFTPFiles = mockedFTPFilesNamed("test", "test2");
expect(ftpClient.listFiles()).andReturn(mockedFTPFiles);
expect(ftpClient.isConnected()).andReturn(true).anyTimes();
expect(ftpClient.retrieveFile(eq("test"), isA(OutputStream.class))).andReturn(true);
expect(ftpClient.isConnected()).andReturn(true).anyTimes();
expect(ftpClient.retrieveFile(eq("test2"), isA(OutputStream.class))).andReturn(true);
checkOrder(ftpClient, false);
ftpClient.disconnect();
expect(ftpClient.isConnected()).andReturn(true).anyTimes();
// second run, change the date so the messages should be retrieved again
// expect(ftpClient.isConnected()).andReturn(true);
checkOrder(ftpClient, true);
FTPFile[] mockedFTPFiles2 = mockedFTPFilesNamed("test", "test2");
expect(ftpClient.listFiles()).andReturn(mockedFTPFiles2);
expect(ftpClient.isConnected()).andReturn(true).anyTimes();
expect(ftpClient.retrieveFile(eq("test"), isA(OutputStream.class))).andReturn(true);
expect(ftpClient.isConnected()).andReturn(true).anyTimes();
expect(ftpClient.retrieveFile(eq("test2"), isA(OutputStream.class))).andReturn(true);
checkOrder(ftpClient, false);
expect(ftpClient.isConnected()).andReturn(true).anyTimes();
// create message
List<File> files = Arrays.asList(new File("test"), new File("test2"));
expect(messageCreator.createMessage(isA(List.class))).andReturn(new GenericMessage(files));
expect(messageCreator.createMessage(isA(List.class))).andReturn(new GenericMessage(files));
ftpClient.disconnect();
replay(globalMocks);
Message receivedFiles = ftpSource.receive();
ftpSource.onSend(receivedFiles);
ftpSource.onSend(ftpSource.receive());
verify(globalMocks);
assertEquals(files, receivedFiles.getPayload());
}
}

View File

@@ -0,0 +1,69 @@
package org.springframework.integration.adapter.ftp.config;
import static org.junit.Assert.assertTrue;
import java.io.File;
import java.util.List;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
import org.springframework.integration.adapter.ftp.FtpSource;
import org.springframework.integration.message.GenericMessage;
import org.springframework.integration.message.Message;
import org.springframework.integration.message.MessageCreator;
/**
* @author Iwein Fuld
*/
/*
* These tests assume you have a local ftp server running. The whole class
* should be disabled and only run when you have started your ftp server and are
* in need of experimenting.
*
* To pass the test you should have an ftp server running at localhost that
* accepts a login for ftp-user/kaas and has a remote directory ftp-test with at
* least one file in it. Nothing is stopping you from changing the code to your
* needs of course, this is just a starting point for local testing.
*/
// ftp server dependency. comment away Ignore if you want to run this
@Ignore
public class FtpSourceIntegrationTests {
private FtpSource ftpSource;
private MessageCreator<List<File>, List<File>> messageCreator = new MessageCreator<List<File>, List<File>>() {
public Message<List<File>> createMessage(List<File> object) {
return new GenericMessage<List<File>>(object);
}
};
private static File localWorkDir;
@BeforeClass
public static void initializeEnvironment() {
localWorkDir = new File(System.getProperty("java.io.tmpdir") + "/" + FtpSourceIntegrationTests.class.getName());
localWorkDir.mkdir();
}
@Before
public void initializeFtpSource() throws Exception {
ftpSource = new FtpSource(messageCreator);
ftpSource.setHost("localhost");
ftpSource.setUsername("ftp-user");
ftpSource.setPassword("kaas");
ftpSource.setLocalWorkingDirectory(localWorkDir);
ftpSource.setRemoteWorkingDirectory("ftp-test");
}
@SuppressWarnings("unchecked")
@Test
public void receive() {
Message<List<File>> received = ftpSource.receive();
assertTrue(received.getPayload().iterator().next().exists());
}
}