diff --git a/org.springframework.integration.adapter/src/main/java/org/springframework/integration/adapter/ftp/QueuedFTPClientPool.java b/org.springframework.integration.adapter/src/main/java/org/springframework/integration/adapter/ftp/QueuedFTPClientPool.java index cd1a4c8219..0482618d50 100644 --- a/org.springframework.integration.adapter/src/main/java/org/springframework/integration/adapter/ftp/QueuedFTPClientPool.java +++ b/org.springframework.integration.adapter/src/main/java/org/springframework/integration/adapter/ftp/QueuedFTPClientPool.java @@ -41,26 +41,24 @@ public class QueuedFTPClientPool implements FTPClientPool { private final Queue pool; - private FTPClientConfig config; + private volatile FTPClientConfig config; - private String host; + private volatile String host; - private int port = FTP.DEFAULT_PORT; + private volatile int port = FTP.DEFAULT_PORT; - private String user; + private volatile String user; - private String pass; + private volatile String pass; - private FTPClientFactory factory = new DefaultFactory(); + private volatile FTPClientFactory factory = new DefaultFactory(); private final Log log = LogFactory.getLog(this.getClass()); - public QueuedFTPClientPool() { this(DEFAULT_POOL_SIZE); } - /** * @param maxPoolSize the maximum size of the pool */ @@ -73,7 +71,7 @@ public class QueuedFTPClientPool implements FTPClientPool { } public synchronized void releaseClient(FTPClient client) { - if (client != null) { + if (client != null && client.isConnected()) { if (!pool.offer(client)) { try { client.disconnect(); @@ -115,7 +113,6 @@ public class QueuedFTPClientPool implements FTPClientPool { this.factory = factory; } - private class DefaultFactory implements FTPClientFactory { public FTPClient getClient() throws SocketException, IOException { diff --git a/org.springframework.integration.adapter/src/test/java/org/springframework/integration/adapter/ftp/FtpTargetTest.java b/org.springframework.integration.adapter/src/test/java/org/springframework/integration/adapter/ftp/FtpTargetTest.java new file mode 100644 index 0000000000..ad19349382 --- /dev/null +++ b/org.springframework.integration.adapter/src/test/java/org/springframework/integration/adapter/ftp/FtpTargetTest.java @@ -0,0 +1,96 @@ +/* + * Copyright 2002-2008 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.integration.adapter.ftp; + +import static org.easymock.classextension.EasyMock.*; +import static org.junit.Assert.*; + +import java.io.File; +import java.io.FileInputStream; + +import org.apache.commons.net.ftp.FTPClient; +import org.junit.Before; +import org.junit.Test; +import org.springframework.integration.message.Message; +import org.springframework.integration.message.MessageMapper; + +/** + * + * @author Iwein Fuld + * + */ +@SuppressWarnings("unchecked") +public class FtpTargetTest { + + private FtpTarget ftpTarget; + + // Mocks and initialization + private MessageMapper messageMapper = createMock(MessageMapper.class); + + private Message message = createMock(Message.class); + + private FTPClient ftpClient = createMock(FTPClient.class); + + /* + * We don't want tests to worry about interaction with the pool (with the + * exception of one dedicated test), so let's make the pool as transparent + * as possible. + */ + private FTPClientPool ftpClientPool = createNiceMock(FTPClientPool.class); + + @Before + public void liberalPool() throws Exception { + expect(ftpClientPool.getClient()).andReturn(ftpClient).anyTimes(); + } + + /* + * Handle to all mocks in this test so you can't forget to include one in a + * replay, verify or reset call. + */ + private Object[] allMocks = new Object[] { messageMapper, message, ftpClient, ftpClientPool }; + + @Before + public void intitializeSubject() { + this.ftpTarget = new FtpTarget(messageMapper); + ftpTarget.setFtpClientPool(ftpClientPool); + } + + // Tests + @Test + public void send() throws Exception { + expect(messageMapper.mapMessage(message)).andReturn(File.createTempFile("test", ".tmp")); + expect(ftpClient.storeFile(isA(String.class), isA(FileInputStream.class))).andReturn(true); + replay(allMocks); + boolean sent = ftpTarget.send(message); + assertTrue(sent); + verify(allMocks); + } + + @Test + public void sendFailed_negative() throws Exception { + expect(messageMapper.mapMessage(message)).andReturn(File.createTempFile("test", ".tmp")); + expect(ftpClient.storeFile(isA(String.class), isA(FileInputStream.class))).andReturn(false); + replay(allMocks); + boolean sent = ftpTarget.send(message); + assertFalse(sent); + verify(allMocks); + } + + @Test + public void sendFailed_() throws Exception { + + } +} diff --git a/org.springframework.integration.adapter/src/test/java/org/springframework/integration/adapter/ftp/QueuedFTPClientPoolTest.java b/org.springframework.integration.adapter/src/test/java/org/springframework/integration/adapter/ftp/QueuedFTPClientPoolTest.java index c13085d3d6..a6f3783e8b 100644 --- a/org.springframework.integration.adapter/src/test/java/org/springframework/integration/adapter/ftp/QueuedFTPClientPoolTest.java +++ b/org.springframework.integration.adapter/src/test/java/org/springframework/integration/adapter/ftp/QueuedFTPClientPoolTest.java @@ -18,10 +18,7 @@ package org.springframework.integration.adapter.ftp; import static junit.framework.Assert.assertEquals; import static junit.framework.Assert.assertSame; import static junit.framework.Assert.assertTrue; -import static org.easymock.EasyMock.createMock; -import static org.easymock.EasyMock.expect; -import static org.easymock.EasyMock.replay; -import static org.easymock.EasyMock.verify; +import static org.easymock.EasyMock.*; import java.util.ArrayList; import java.util.List; @@ -61,8 +58,8 @@ public class QueuedFTPClientPoolTest { @Test public void getMultipleGet() throws Exception { - FTPClient[] expectedClients = new FTPClient[] { new FTPClient(), new FTPClient(), new FTPClient(), - new FTPClient(), new FTPClient(), new FTPClient() }; + FTPClient[] expectedClients = new FTPClient[] { connectedFTPClient(), connectedFTPClient(), + connectedFTPClient(), connectedFTPClient(), connectedFTPClient(), connectedFTPClient() }; for (FTPClient client : expectedClients) { expect(factoryMock.getClient()).andReturn(client); } @@ -75,8 +72,8 @@ public class QueuedFTPClientPoolTest { @Test public void getMultipleGetReleaseGet() throws Exception { - FTPClient[] expectedClients = new FTPClient[] { new FTPClient(), new FTPClient(), new FTPClient(), - new FTPClient(), new FTPClient() }; + FTPClient[] expectedClients = new FTPClient[] { connectedFTPClient(), connectedFTPClient(), + connectedFTPClient(), connectedFTPClient(), connectedFTPClient() }; for (FTPClient client : expectedClients) { expect(factoryMock.getClient()).andReturn(client); } @@ -95,4 +92,13 @@ public class QueuedFTPClientPoolTest { } verify(allMocks); } + + private FTPClient connectedFTPClient() { + return new FTPClient() { + @Override + public boolean isConnected() { + return true; + } + }; + } }