Finishing up INT-293, INT-154. Parametrized DefaultMessageMapper, refactored FtpSource to use a pool, added namespace support for FtpTarget.

This commit is contained in:
Iwein Fuld
2008-08-17 05:37:03 +00:00
parent 8272a7d94c
commit d6aed95948
13 changed files with 211 additions and 241 deletions

View File

@@ -16,18 +16,10 @@
package org.springframework.integration.adapter.ftp;
import static org.easymock.EasyMock.*;
import static org.easymock.EasyMock.eq;
import static org.easymock.EasyMock.expect;
import static org.easymock.EasyMock.getCurrentArguments;
import static org.easymock.EasyMock.isA;
import static org.easymock.classextension.EasyMock.createMock;
import static org.easymock.classextension.EasyMock.replay;
import static org.easymock.classextension.EasyMock.reset;
import static org.easymock.classextension.EasyMock.verify;
import static org.junit.Assert.*;
import static org.easymock.classextension.EasyMock.*;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import java.io.File;
import java.io.FilenameFilter;
@@ -59,7 +51,14 @@ public class FtpSourceTests {
private FTPFile ftpFile = createMock(FTPFile.class);
private Object[] globalMocks = new Object[] { messageCreator, ftpClient, ftpFile };
private FTPClientPool ftpClientPool = createNiceMock(FTPClientPool.class);
@Before
public void liberalPool() throws Exception {
expect(ftpClientPool.getClient()).andReturn(ftpClient).anyTimes();
}
private Object[] globalMocks = new Object[] { messageCreator, ftpClient, ftpFile, ftpClientPool };
private static final String HOST = "testHost";
@@ -73,10 +72,7 @@ public class FtpSourceTests {
@Before
public void initializeFtpSource() {
ftpSource = new FtpSource(messageCreator, ftpClient);
ftpSource.setHost(HOST);
ftpSource.setUsername(USER);
ftpSource.setPassword(PASS);
ftpSource = new FtpSource(messageCreator, ftpClientPool);
}
@Before
@@ -86,20 +82,12 @@ public class FtpSourceTests {
@Test
public void retrieveSingleFile() throws Exception {
// connect client and get file
expect(ftpClient.isConnected()).andReturn(false);
expect(ftpClient.isConnected()).andReturn(true).anyTimes();
ftpClient.connect(HOST, 21);
expect(ftpClient.login(USER, PASS)).andReturn(true);
expect(ftpClient.setFileType(anyInt())).andReturn(true);
expect(ftpClient.printWorkingDirectory()).andReturn("/").anyTimes();
expect(ftpClient.listFiles()).andReturn(mockedFTPFilesNamed("test1"));
expect(ftpClient.retrieveFile(eq("test1"), isA(OutputStream.class))).andReturn(true);
// create message
expect(messageCreator.createMessage(isA(List.class))).andReturn(
new GenericMessage(Arrays.asList(new File("test1"))));
ftpClient.disconnect();
replay(globalMocks);
Message<List<File>> received = ftpSource.receive();
ftpSource.onSend(received);
@@ -124,15 +112,6 @@ public class FtpSourceTests {
@Test
public void retrieveMultipleFiles() throws Exception {
// connect client
expect(ftpClient.isConnected()).andReturn(false);
expect(ftpClient.isConnected()).andReturn(true).anyTimes();
ftpClient.connect(HOST, 21);
expect(ftpClient.login(USER, PASS)).andReturn(true);
expect(ftpClient.setFileType(anyInt())).andReturn(true);
expect(ftpClient.printWorkingDirectory()).andReturn("/").anyTimes();
// get files
expect(ftpClient.listFiles()).andReturn(mockedFTPFilesNamed("test1", "test2")).times(2);
@@ -141,7 +120,6 @@ public class FtpSourceTests {
// create message
List<File> files = Arrays.asList(new File("test1"), new File("test2"));
expect(messageCreator.createMessage(isA(List.class))).andReturn(new GenericMessage(files));
ftpClient.disconnect();
replay(globalMocks);
Message receivedFiles = ftpSource.receive();
@@ -154,16 +132,12 @@ public class FtpSourceTests {
@Test
public void retrieveMultipleChangingFiles() throws Exception {
// assume client already connected
expect(ftpClient.isConnected()).andReturn(true).anyTimes();
// first run
FTPFile[] mockedFTPFiles = mockedFTPFilesNamed("test1", "test2");
expect(ftpClient.listFiles()).andReturn(mockedFTPFiles);
expect(ftpClient.retrieveFile(eq("test1"), isA(OutputStream.class))).andReturn(true);
expect(ftpClient.retrieveFile(eq("test2"), isA(OutputStream.class))).andReturn(true);
ftpClient.disconnect();
// second run, change the date so the messages should be retrieved again
// expect(ftpClient.isConnected()).andReturn(true);
FTPFile[] mockedFTPFiles2 = mockedFTPFilesNamed("test1", "test2");
@@ -173,7 +147,6 @@ public class FtpSourceTests {
// create message
List<File> files = Arrays.asList(new File("test1"), new File("test2"));
expect(messageCreator.createMessage(isA(List.class))).andReturn(new GenericMessage(files)).times(2);
ftpClient.disconnect();
replay(globalMocks);
Message receivedFiles = ftpSource.receive();
@@ -188,13 +161,10 @@ public class FtpSourceTests {
this.ftpSource.setMaxMessagesPerPayload(2);
// assume client already connected
expect(ftpClient.isConnected()).andReturn(true).anyTimes();
FTPFile[] mockedFTPFiles = mockedFTPFilesNamed("test1", "test2", "test3");
// expect two receive runs
expect(ftpClient.listFiles()).andReturn(mockedFTPFiles).times(2);
ftpClient.disconnect();
expectLastCall().times(2);
// first run
expect(ftpClient.retrieveFile(eq("test1"), isA(OutputStream.class))).andReturn(true);
@@ -227,8 +197,6 @@ public class FtpSourceTests {
public void concurrentPollingSunnyDay() throws Exception {
this.ftpSource.setMaxMessagesPerPayload(2);
// assume client already connected
expect(ftpClient.isConnected()).andReturn(true).anyTimes();
// first run
FTPFile[] mockedFTPFiles = mockedFTPFilesNamed("test1", "test2", "test3", "test4", "test5");
expect(ftpClient.listFiles()).andReturn(mockedFTPFiles);
@@ -249,9 +217,6 @@ public class FtpSourceTests {
}
}).times(3);
ftpClient.disconnect();
expectLastCall().times(3);
replay(globalMocks);
final CountDownLatch receivesDone = new CountDownLatch(3);
@@ -275,24 +240,20 @@ public class FtpSourceTests {
receivesDone.await();
verify(globalMocks);
}
@Test public void onFailure() throws Exception{
// connect client and get file
expect(ftpClient.isConnected()).andReturn(true).anyTimes();
expect(ftpClient.listFiles()).andReturn(mockedFTPFilesNamed("test1")).times(2);
expect(ftpClient.retrieveFile(eq("test1"), isA(OutputStream.class))).andReturn(true).times(2);
// create message
expect(messageCreator.createMessage(isA(List.class))).andReturn(
new GenericMessage(Arrays.asList(new File("test1")))).times(2);
ftpClient.disconnect();
ftpClient.disconnect();
replay(globalMocks);
Message<List<File>> received = ftpSource.receive();
ftpSource.onFailure(new MessagingException(received));
assertEquals(received, ftpSource.receive());
verify(globalMocks);
}
@Test
public void onFailure() throws Exception {
expect(ftpClient.listFiles()).andReturn(mockedFTPFilesNamed("test1")).times(2);
expect(ftpClient.retrieveFile(eq("test1"), isA(OutputStream.class))).andReturn(true).times(2);
// create message
expect(messageCreator.createMessage(isA(List.class))).andReturn(
new GenericMessage(Arrays.asList(new File("test1")))).times(2);
replay(globalMocks);
Message<List<File>> received = ftpSource.receive();
ftpSource.onFailure(new MessagingException(received));
assertEquals(received, ftpSource.receive());
verify(globalMocks);
}
@AfterClass
public static void deleteFiles() {

View File

@@ -64,8 +64,7 @@ public class FtpTargetTest {
@Before
public void intitializeSubject() {
this.ftpTarget = new FtpTarget(messageMapper);
ftpTarget.setFtpClientPool(ftpClientPool);
this.ftpTarget = new FtpTarget(messageMapper, ftpClientPool);
}
// Tests

View File

@@ -0,0 +1,16 @@
package org.springframework.integration.adapter.ftp.config;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import org.springframework.integration.annotation.Splitter;
import org.springframework.integration.message.Message;
public class CollectionSplitter {
@SuppressWarnings("unchecked")
@Splitter public List split(Message<? extends Collection> message) {
return new ArrayList(message.getPayload());
}
}

View File

@@ -14,6 +14,7 @@ import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.integration.adapter.ftp.FtpSource;
import org.springframework.integration.adapter.ftp.QueuedFTPClientPool;
import org.springframework.integration.channel.ChannelRegistry;
import org.springframework.integration.channel.PollableChannel;
import org.springframework.integration.config.MessageBusParser;
@@ -57,12 +58,13 @@ public class FtpSourceIntegrationTests {
@Before
public void initializeFtpSource() throws Exception {
ftpSource = new FtpSource(messageCreator);
ftpSource.setHost("localhost");
ftpSource.setUsername("ftp-user");
ftpSource.setPassword("kaas");
QueuedFTPClientPool queuedFTPClientPool = new QueuedFTPClientPool();
ftpSource = new FtpSource(messageCreator, queuedFTPClientPool);
queuedFTPClientPool.setHost("localhost");
queuedFTPClientPool.setUsername("ftp-user");
queuedFTPClientPool.setPassword("kaas");
ftpSource.setLocalWorkingDirectory(localWorkDir);
ftpSource.setRemoteWorkingDirectory("ftp-test");
queuedFTPClientPool.setRemoteWorkingDirectory("ftp-test");
}
@SuppressWarnings("unchecked")

View File

@@ -38,6 +38,7 @@ import org.springframework.integration.adapter.ftp.FtpSource;
/**
* @author Mark Fisher
* @author Marius Bogoevici
* @author Iwein Fuld
*/
public class FtpSourceParserTests {
@@ -45,14 +46,15 @@ public class FtpSourceParserTests {
public void testFtpSourceAdapterParser() {
ApplicationContext context = new ClassPathXmlApplicationContext("ftpSourceParserTests.xml", this.getClass());
FtpSource ftpSource = (FtpSource) context.getBean("ftpSourceDefault");
DirectFieldAccessor accessor = new DirectFieldAccessor(ftpSource);
DirectFieldAccessor sourceAccessor = new DirectFieldAccessor(ftpSource);
DirectFieldAccessor accessor = new DirectFieldAccessor(sourceAccessor.getPropertyValue("clientPool"));
assertEquals("testHost", accessor.getPropertyValue("host"));
assertEquals(2121, accessor.getPropertyValue("port"));
assertEquals(new File("/local"), accessor.getPropertyValue("localWorkingDirectory"));
assertEquals(new File("/local"), sourceAccessor.getPropertyValue("localWorkingDirectory"));
assertEquals("/remote", accessor.getPropertyValue("remoteWorkingDirectory"));
assertEquals("testUser", accessor.getPropertyValue("username"));
assertEquals("testPassword", accessor.getPropertyValue("password"));
Object messageCreator = accessor.getPropertyValue("messageCreator");
Object messageCreator = sourceAccessor.getPropertyValue("messageCreator");
assertTrue(messageCreator instanceof FileMessageCreator);
DirectFieldAccessor messageCreatorAccessor = new DirectFieldAccessor(messageCreator);
assertEquals(messageCreatorAccessor.getPropertyValue("deleteFileAfterCreation"), false);
@@ -62,14 +64,15 @@ public class FtpSourceParserTests {
public void testFtpSourceTextType() {
ApplicationContext context = new ClassPathXmlApplicationContext("ftpSourceParserTests.xml", this.getClass());
FtpSource ftpSource = (FtpSource) context.getBean("ftpSourceText");
DirectFieldAccessor accessor = new DirectFieldAccessor(ftpSource);
DirectFieldAccessor sourceAccessor = new DirectFieldAccessor(ftpSource);
DirectFieldAccessor accessor = new DirectFieldAccessor(sourceAccessor.getPropertyValue("clientPool"));
assertEquals("testHost", accessor.getPropertyValue("host"));
assertEquals(2121, accessor.getPropertyValue("port"));
assertEquals(new File("/local"), accessor.getPropertyValue("localWorkingDirectory"));
assertEquals(new File("/local"), sourceAccessor.getPropertyValue("localWorkingDirectory"));
assertEquals("/remote", accessor.getPropertyValue("remoteWorkingDirectory"));
assertEquals("testUser", accessor.getPropertyValue("username"));
assertEquals("testPassword", accessor.getPropertyValue("password"));
Object messageCreator = accessor.getPropertyValue("messageCreator");
Object messageCreator = sourceAccessor.getPropertyValue("messageCreator");
assertTrue(messageCreator instanceof TextFileMessageCreator);
DirectFieldAccessor messageCreatorAccessor = new DirectFieldAccessor(messageCreator);
assertEquals(messageCreatorAccessor.getPropertyValue("deleteFileAfterCreation"), true);
@@ -79,14 +82,15 @@ public class FtpSourceParserTests {
public void testFtpSourceBinaryType() {
ApplicationContext context = new ClassPathXmlApplicationContext("ftpSourceParserTests.xml", this.getClass());
FtpSource ftpSource = (FtpSource) context.getBean("ftpSourceBinary");
DirectFieldAccessor accessor = new DirectFieldAccessor(ftpSource);
DirectFieldAccessor sourceAccessor = new DirectFieldAccessor(ftpSource);
DirectFieldAccessor accessor = new DirectFieldAccessor(sourceAccessor.getPropertyValue("clientPool"));
assertEquals("testHost", accessor.getPropertyValue("host"));
assertEquals(2121, accessor.getPropertyValue("port"));
assertEquals(new File("/local"), accessor.getPropertyValue("localWorkingDirectory"));
assertEquals(new File("/local"), sourceAccessor.getPropertyValue("localWorkingDirectory"));
assertEquals("/remote", accessor.getPropertyValue("remoteWorkingDirectory"));
assertEquals("testUser", accessor.getPropertyValue("username"));
assertEquals("testPassword", accessor.getPropertyValue("password"));
Object messageCreator = accessor.getPropertyValue("messageCreator");
Object messageCreator = sourceAccessor.getPropertyValue("messageCreator");
assertTrue(messageCreator instanceof ByteArrayFileMessageCreator);
DirectFieldAccessor messageCreatorAccessor = new DirectFieldAccessor(messageCreator);
assertEquals(messageCreatorAccessor.getPropertyValue("deleteFileAfterCreation"), true);
@@ -96,14 +100,15 @@ public class FtpSourceParserTests {
public void testFtpSourceFileType() {
ApplicationContext context = new ClassPathXmlApplicationContext("ftpSourceParserTests.xml", this.getClass());
FtpSource ftpSource = (FtpSource) context.getBean("ftpSourceFile");
DirectFieldAccessor accessor = new DirectFieldAccessor(ftpSource);
DirectFieldAccessor sourceAccessor = new DirectFieldAccessor(ftpSource);
DirectFieldAccessor accessor = new DirectFieldAccessor(sourceAccessor.getPropertyValue("clientPool"));
assertEquals("testHost", accessor.getPropertyValue("host"));
assertEquals(2121, accessor.getPropertyValue("port"));
assertEquals(new File("/local"), accessor.getPropertyValue("localWorkingDirectory"));
assertEquals(new File("/local"), sourceAccessor.getPropertyValue("localWorkingDirectory"));
assertEquals("/remote", accessor.getPropertyValue("remoteWorkingDirectory"));
assertEquals("testUser", accessor.getPropertyValue("username"));
assertEquals("testPassword", accessor.getPropertyValue("password"));
Object messageCreator = accessor.getPropertyValue("messageCreator");
Object messageCreator = sourceAccessor.getPropertyValue("messageCreator");
assertTrue(messageCreator instanceof FileMessageCreator);
DirectFieldAccessor messageCreatorAccessor = new DirectFieldAccessor(messageCreator);
assertEquals(messageCreatorAccessor.getPropertyValue("deleteFileAfterCreation"), false);
@@ -113,14 +118,15 @@ public class FtpSourceParserTests {
public void testFtpSourceCustomType() {
ApplicationContext context = new ClassPathXmlApplicationContext("ftpSourceParserTests.xml", this.getClass());
FtpSource ftpSource = (FtpSource) context.getBean("ftpSourceCustom");
DirectFieldAccessor accessor = new DirectFieldAccessor(ftpSource);
DirectFieldAccessor sourceAccessor = new DirectFieldAccessor(ftpSource);
DirectFieldAccessor accessor = new DirectFieldAccessor(sourceAccessor.getPropertyValue("clientPool"));
assertEquals("testHost", accessor.getPropertyValue("host"));
assertEquals(2121, accessor.getPropertyValue("port"));
assertEquals(new File("/local"), accessor.getPropertyValue("localWorkingDirectory"));
assertEquals(new File("/local"), sourceAccessor.getPropertyValue("localWorkingDirectory"));
assertEquals("/remote", accessor.getPropertyValue("remoteWorkingDirectory"));
assertEquals("testUser", accessor.getPropertyValue("username"));
assertEquals("testPassword", accessor.getPropertyValue("password"));
Object messageCreator = accessor.getPropertyValue("messageCreator");
Object messageCreator = sourceAccessor.getPropertyValue("messageCreator");
assertTrue(messageCreator instanceof CustomMessageCreator);
// not testing for deleteFileAfterCreation - this is completely left up
// to the implementation

View File

@@ -25,32 +25,27 @@ import org.junit.AfterClass;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
import org.springframework.integration.adapter.ftp.QueuedFTPClientPool;
import org.springframework.integration.adapter.ftp.FtpTarget;
import org.springframework.integration.adapter.ftp.QueuedFTPClientPool;
import org.springframework.integration.message.DefaultMessageMapper;
import org.springframework.integration.message.GenericMessage;
import org.springframework.integration.message.Message;
import org.springframework.integration.message.MessageMapper;
/**
* @author Iwein Fuld
*/
@Ignore
//@Ignore
public class FtpTargetIntegrationTest {
private FtpTarget ftpTarget;
@Before
public void initFtpTarget() {
ftpTarget = new FtpTarget(new MessageMapper<File, File>() {
public File mapMessage(Message<File> message) {
return message.getPayload();
}
});
QueuedFTPClientPool clientPool = new QueuedFTPClientPool();
clientPool.setHost("localhost");
clientPool.setUser("ftp-user");
clientPool.setPass("kaas");
ftpTarget.setFtpClientPool(clientPool);
clientPool.setUsername("ftp-user");
clientPool.setPassword("kaas");
clientPool.setRemoteWorkingDirectory("ftp-test");
ftpTarget = new FtpTarget(new DefaultMessageMapper<File>(), clientPool);
}
@Test

View File

@@ -12,21 +12,25 @@
<si:message-bus />
<bean id="ftpSource"
class="org.springframework.integration.adapter.ftp.FtpSource"
p:host="localhost" p:password="kaas" p:username="ftp-user"
p:remoteWorkingDirectory="ftp-test">
class="org.springframework.integration.adapter.ftp.FtpSource">
<constructor-arg>
<bean
class="org.springframework.integration.message.DefaultMessageCreator" />
</constructor-arg>
<constructor-arg>
<bean
class="org.springframework.integration.adapter.ftp.QueuedFTPClientPool"
p:host="localhost" p:password="kaas" p:username="ftp-user"
p:remoteWorkingDirectory="ftp-test" />
</constructor-arg>
</bean>
<si:channel-adapter id="input" source="ftpSource" />
<si:splitter output-channel="output" input-channel="input" ref="defaultSplitter" method="split"/>
<bean id="splitter"
class="org.springframework.integration.adapter.ftp.config.CollectionSplitter" />
<si:splitter output-channel="output" input-channel="input"
ref="splitter" method="split" />
<bean id="defaultSplitter"
class="org.springframework.integration.util.CollectionSplitter" />
<si:channel id="output"/>
<si:channel id="output" />
</beans>