From 9390f595dd64561d5b0845d7a17f48a54f7159c4 Mon Sep 17 00:00:00 2001 From: Oleg Zhurakousky Date: Tue, 16 Nov 2010 11:46:57 -0500 Subject: [PATCH] INT-1614 more polishing, added tests, 78% code coverege --- ...stractFtpOutboundChannelAdapterParser.java | 2 - ...SynchronizingMessageSourceFactoryBean.java | 56 ------------------ .../ftp/config/FtpNamespaceHandler.java | 25 +------- .../ftp/config/FtpNamespaceParserSupport.java | 52 ----------------- .../FtpSendingMessageHandlerFactoryBean.java | 57 +------------------ .../ftp/FtpParserOutboundTests-context.xml | 16 ++++-- .../ftp/FtpParserOutboundTests.java | 1 - .../ftp/InboundFtpFileServiceActivator.java | 34 ----------- .../integration/ftp/OutboundFtpExample.java | 34 ----------- ...boundChannelAdapterParserTests-context.xml | 22 ++++--- .../FtpInboundChannelAdapterParserTests.java | 27 +++++++++ ...boundRemoteFileSystemSynchronizerTest.java | 20 ++++--- .../integration/ftp/outbound-ftp-context.xml | 39 ------------- 13 files changed, 67 insertions(+), 318 deletions(-) delete mode 100644 spring-integration-ftp/src/main/java/org/springframework/integration/ftp/config/FtpNamespaceParserSupport.java delete mode 100644 spring-integration-ftp/src/test/java/org/springframework/integration/ftp/InboundFtpFileServiceActivator.java delete mode 100644 spring-integration-ftp/src/test/java/org/springframework/integration/ftp/OutboundFtpExample.java delete mode 100644 spring-integration-ftp/src/test/java/org/springframework/integration/ftp/outbound-ftp-context.xml diff --git a/spring-integration-ftp/src/main/java/org/springframework/integration/ftp/config/AbstractFtpOutboundChannelAdapterParser.java b/spring-integration-ftp/src/main/java/org/springframework/integration/ftp/config/AbstractFtpOutboundChannelAdapterParser.java index 9902949779..7781e5af24 100644 --- a/spring-integration-ftp/src/main/java/org/springframework/integration/ftp/config/AbstractFtpOutboundChannelAdapterParser.java +++ b/spring-integration-ftp/src/main/java/org/springframework/integration/ftp/config/AbstractFtpOutboundChannelAdapterParser.java @@ -35,8 +35,6 @@ public abstract class AbstractFtpOutboundChannelAdapterParser extends AbstractOu IntegrationNamespaceUtils.setValueIfAttributeDefined(builder,element,"charset"); IntegrationNamespaceUtils.setReferenceIfAttributeDefined(builder,element,"filename-generator", "fileNameGenerator"); IntegrationNamespaceUtils.setReferenceIfAttributeDefined(builder,element,"client-factory"); - - FtpNamespaceParserSupport.configureCoreFtpClient(builder, element, parserContext); return builder.getBeanDefinition(); } diff --git a/spring-integration-ftp/src/main/java/org/springframework/integration/ftp/config/FtpInboundSynchronizingMessageSourceFactoryBean.java b/spring-integration-ftp/src/main/java/org/springframework/integration/ftp/config/FtpInboundSynchronizingMessageSourceFactoryBean.java index 47280e45c7..ed5e6d085b 100644 --- a/spring-integration-ftp/src/main/java/org/springframework/integration/ftp/config/FtpInboundSynchronizingMessageSourceFactoryBean.java +++ b/spring-integration-ftp/src/main/java/org/springframework/integration/ftp/config/FtpInboundSynchronizingMessageSourceFactoryBean.java @@ -53,20 +53,6 @@ class FtpInboundSynchronizingMessageSourceFactoryBean private volatile AbstractFtpClientFactory clientFactory; - //volatile String host; - - //volatile String port; - - //volatile String username; - - //volatile String password; - - //volatile String remoteDirectory; - - //volatile int clientMode = FTPClient.ACTIVE_LOCAL_DATA_CONNECTION_MODE; - - //volatile int fileType = FTP.BINARY_FILE_TYPE; - volatile String defaultFtpInboundFolderName = "ftpInbound"; private volatile String localWorkingDirectory; @@ -79,27 +65,6 @@ class FtpInboundSynchronizingMessageSourceFactoryBean private volatile String autoDeleteRemoteFilesOnSync; - -// public void setHost(String host) { -// this.host = host; -// } -// -// public void setPort(String port) { -// this.port = port; -// } -// -// public void setUsername(String username) { -// this.username = username; -// } -// -// public void setPassword(String password) { -// this.password = password; -// } -// -// public void setFileType(int fileType) { -// this.fileType = fileType; -// } - public void setClientFactory(AbstractFtpClientFactory clientFactory) { this.clientFactory = clientFactory; } @@ -112,18 +77,10 @@ class FtpInboundSynchronizingMessageSourceFactoryBean this.autoDeleteRemoteFilesOnSync = autoDeleteRemoteFilesOnSync; } -// public void setRemoteDirectory(String remoteDirectory) { -// this.remoteDirectory = remoteDirectory; -// } - public void setLocalWorkingDirectory(String localWorkingDirectory) { this.localWorkingDirectory = localWorkingDirectory; } -// public void setClientMode(int clientMode) { -// this.clientMode = clientMode; -// } - public void setFilter(FileListFilter filter) { this.filter = filter; } @@ -147,19 +104,6 @@ class FtpInboundSynchronizingMessageSourceFactoryBean return (Resource) resourceEditor.getValue(); } -// protected AbstractFtpClientFactory initializeFactory(AbstractFtpClientFactory factory) throws Exception { -// factory.setHost(this.host); -// if (StringUtils.hasText(this.port)) { -// factory.setPort(Integer.parseInt(this.port)); -// } -// factory.setUsername(this.username); -// factory.setPassword(this.password); -// factory.setRemoteWorkingDirectory(this.remoteDirectory); -// factory.setClientMode(this.clientMode); -// factory.setFileType(this.fileType); -// return factory; -// } - @Override protected FtpInboundRemoteFileSystemSynchronizingMessageSource createInstance() throws Exception { boolean autoCreatDirs = Boolean.parseBoolean(this.autoCreateDirectories); diff --git a/spring-integration-ftp/src/main/java/org/springframework/integration/ftp/config/FtpNamespaceHandler.java b/spring-integration-ftp/src/main/java/org/springframework/integration/ftp/config/FtpNamespaceHandler.java index a43d8fc701..7b63e25088 100644 --- a/spring-integration-ftp/src/main/java/org/springframework/integration/ftp/config/FtpNamespaceHandler.java +++ b/spring-integration-ftp/src/main/java/org/springframework/integration/ftp/config/FtpNamespaceHandler.java @@ -16,40 +16,19 @@ package org.springframework.integration.ftp.config; -import org.apache.commons.net.ftp.FTP; - import org.springframework.integration.config.xml.AbstractIntegrationNamespaceHandler; -import java.util.HashMap; -import java.util.Map; - /** * Provides namespace support for using FTP *

* This is *heavily* influenced by the good work done by Iwein before. * * @author Josh Long + * @author Oleg Zhurakousky + * @since 2.0 */ public class FtpNamespaceHandler extends AbstractIntegrationNamespaceHandler { - static public Map FILE_TYPES = new HashMap(); - - static public Map CLIENT_MODES = new HashMap(); - - static { - // file types - FILE_TYPES.put("ebcdic-file-type", FTP.EBCDIC_FILE_TYPE); - FILE_TYPES.put("ascii-file-type", FTP.ASCII_FILE_TYPE); - FILE_TYPES.put("binary-file-type", FTP.BINARY_FILE_TYPE); - - // client modes - CLIENT_MODES.put("active-local-data-connection-mode", 0); - CLIENT_MODES.put("active-remote-data-connection-mode", 1); - CLIENT_MODES.put("passive-local-data-connection-mode", 2); - CLIENT_MODES.put("passive-remote-data-connection-mode", 3); - } - - public void init() { registerBeanDefinitionParser("inbound-channel-adapter", new FtpInboundChannelAdapterParser()); registerBeanDefinitionParser("outbound-channel-adapter", new FtpOutboundChannelAdapterParser()); diff --git a/spring-integration-ftp/src/main/java/org/springframework/integration/ftp/config/FtpNamespaceParserSupport.java b/spring-integration-ftp/src/main/java/org/springframework/integration/ftp/config/FtpNamespaceParserSupport.java deleted file mode 100644 index 8c1628c63c..0000000000 --- a/spring-integration-ftp/src/main/java/org/springframework/integration/ftp/config/FtpNamespaceParserSupport.java +++ /dev/null @@ -1,52 +0,0 @@ -/* - * Copyright 2002-2010 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.ftp.config; - -import org.w3c.dom.Element; - -import org.springframework.beans.factory.support.BeanDefinitionBuilder; -import org.springframework.beans.factory.xml.ParserContext; -import org.springframework.integration.config.xml.IntegrationNamespaceUtils; - -/** - * General support for parsers in the FTP namespace. - * - * @author Josh Long - */ -public class FtpNamespaceParserSupport { - - /** - * Handles values that are supported across all adapters. - * @param builder a builder - * @param element an element - * @param parserContext a parser context - */ - public static void configureCoreFtpClient(BeanDefinitionBuilder builder, Element element, ParserContext parserContext) { - for (String p : "auto-create-directories,username,port,password,host,remote-directory".split(",")) { - IntegrationNamespaceUtils.setValueIfAttributeDefined(builder, element, p); - } - if (element.hasAttribute("file-type")) { - int fileType = FtpNamespaceHandler.FILE_TYPES.get(element.getAttribute("file-type")); - builder.addPropertyValue("fileType", fileType); - } - if (element.hasAttribute("client-mode")) { - int clientMode = FtpNamespaceHandler.CLIENT_MODES.get(element.getAttribute("client-mode")); - builder.addPropertyValue("clientMode", clientMode); - } - } - -} diff --git a/spring-integration-ftp/src/main/java/org/springframework/integration/ftp/config/FtpSendingMessageHandlerFactoryBean.java b/spring-integration-ftp/src/main/java/org/springframework/integration/ftp/config/FtpSendingMessageHandlerFactoryBean.java index 3919467702..626183e6e8 100644 --- a/spring-integration-ftp/src/main/java/org/springframework/integration/ftp/config/FtpSendingMessageHandlerFactoryBean.java +++ b/spring-integration-ftp/src/main/java/org/springframework/integration/ftp/config/FtpSendingMessageHandlerFactoryBean.java @@ -17,7 +17,6 @@ package org.springframework.integration.ftp.config; import org.springframework.beans.factory.config.AbstractFactoryBean; - import org.springframework.integration.file.FileNameGenerator; import org.springframework.integration.ftp.client.AbstractFtpClientFactory; import org.springframework.integration.ftp.client.DefaultFtpClientFactory; @@ -35,23 +34,9 @@ import org.springframework.integration.ftp.outbound.FtpSendingMessageHandler; */ class FtpSendingMessageHandlerFactoryBean extends AbstractFactoryBean { -// protected volatile int port; -// -// protected volatile String username; -// -// protected volatile String password; -// -// protected volatile String host; -// -// protected volatile String remoteDirectory; - private volatile String charset; -// protected volatile int clientMode; -// -// private volatile int fileType; - - private FileNameGenerator fileNameGenerator; + private volatile FileNameGenerator fileNameGenerator; private volatile AbstractFtpClientFactory clientFactory; @@ -67,57 +52,17 @@ class FtpSendingMessageHandlerFactoryBean extends AbstractFactoryBean getObjectType() { return FtpSendingMessageHandler.class; } -// protected AbstractFtpClientFactory initializeClientFactory(AbstractFtpClientFactory factory) { -// factory.setHost(this.host); -// factory.setPort(this.port); -// factory.setUsername(this.username); -// factory.setPassword(this.password); -// factory.setRemoteWorkingDirectory(this.remoteDirectory); -// factory.setClientMode(this.clientMode); -// factory.setFileType(this.fileType); -// return factory; -// } - protected AbstractFtpClientFactory createClientFactory(){ return new DefaultFtpClientFactory(); } @Override protected FtpSendingMessageHandler createInstance() throws Exception { - //AbstractFtpClientFactory defaultFtpClientFactory = this.initializeClientFactory(this.createClientFactory()); QueuedFtpClientPool queuedFtpClientPool = new QueuedFtpClientPool(15, this.clientFactory); FtpSendingMessageHandler ftpSendingMessageHandler = new FtpSendingMessageHandler( queuedFtpClientPool); diff --git a/spring-integration-ftp/src/test/java/org/springframework/integration/ftp/FtpParserOutboundTests-context.xml b/spring-integration-ftp/src/test/java/org/springframework/integration/ftp/FtpParserOutboundTests-context.xml index aacfa1abc2..af5ba9c2b4 100644 --- a/spring-integration-ftp/src/test/java/org/springframework/integration/ftp/FtpParserOutboundTests-context.xml +++ b/spring-integration-ftp/src/test/java/org/springframework/integration/ftp/FtpParserOutboundTests-context.xml @@ -8,13 +8,21 @@ http://www.springframework.org/schema/integration/ftp http://www.springframework.org/schema/integration/ftp/spring-integration-ftp-2.0.xsd"> + + + + + + + + + + + diff --git a/spring-integration-ftp/src/test/java/org/springframework/integration/ftp/FtpParserOutboundTests.java b/spring-integration-ftp/src/test/java/org/springframework/integration/ftp/FtpParserOutboundTests.java index 81d42a7af7..c4cee5f7fc 100644 --- a/spring-integration-ftp/src/test/java/org/springframework/integration/ftp/FtpParserOutboundTests.java +++ b/spring-integration-ftp/src/test/java/org/springframework/integration/ftp/FtpParserOutboundTests.java @@ -39,7 +39,6 @@ import org.springframework.integration.test.util.TestUtils; public class FtpParserOutboundTests { @Test - @Ignore public void testFtpOutboundWithFileGenerator() throws Exception{ ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("FtpParserOutboundTests-context.xml", this.getClass()); diff --git a/spring-integration-ftp/src/test/java/org/springframework/integration/ftp/InboundFtpFileServiceActivator.java b/spring-integration-ftp/src/test/java/org/springframework/integration/ftp/InboundFtpFileServiceActivator.java deleted file mode 100644 index 1d494fec74..0000000000 --- a/spring-integration-ftp/src/test/java/org/springframework/integration/ftp/InboundFtpFileServiceActivator.java +++ /dev/null @@ -1,34 +0,0 @@ -package org.springframework.integration.ftp; - -import org.apache.commons.lang.StringUtils; -import org.springframework.context.support.ClassPathXmlApplicationContext; -import org.springframework.integration.Message; -import org.springframework.integration.annotation.ServiceActivator; - -import java.io.File; - - -/** - * Simple component to test the inbound integration - * - * @author Josh Long - */ - -public class InboundFtpFileServiceActivator { - - @ServiceActivator - public void onNewRemoteFTPFile(Message file) - throws Throwable { - System.out.println(StringUtils.repeat("=", 100)); - System.out.println("A new file has appeared: " + file.getPayload().getAbsolutePath()); - - for (String h : file.getHeaders().keySet()) - System.out.println(String.format("%s = %s", h, file.getHeaders().get(h))); - } - - public static void main(String[] args) throws Throwable { - ClassPathXmlApplicationContext classPathXmlApplicationContext = - new ClassPathXmlApplicationContext("inbound-ftp-context.xml"); - classPathXmlApplicationContext.start(); - } -} diff --git a/spring-integration-ftp/src/test/java/org/springframework/integration/ftp/OutboundFtpExample.java b/spring-integration-ftp/src/test/java/org/springframework/integration/ftp/OutboundFtpExample.java deleted file mode 100644 index 7eaf49f808..0000000000 --- a/spring-integration-ftp/src/test/java/org/springframework/integration/ftp/OutboundFtpExample.java +++ /dev/null @@ -1,34 +0,0 @@ -/* - * Copyright 2002-2010 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.ftp; - -import org.springframework.context.support.ClassPathXmlApplicationContext; - -/** - * This simple example demonstrates sending a file to a remote FTP server using the ftp:outbound-channel-adapter - *

- * It reads files from a directory on your computer and systematically puts them on the remote FTP server, - * - * @author Josh Long - */ -public class OutboundFtpExample { - - public static void main(String[] args) throws Throwable { - new ClassPathXmlApplicationContext("outbound-ftp-context.xml"); - } - -} diff --git a/spring-integration-ftp/src/test/java/org/springframework/integration/ftp/config/FtpInboundChannelAdapterParserTests-context.xml b/spring-integration-ftp/src/test/java/org/springframework/integration/ftp/config/FtpInboundChannelAdapterParserTests-context.xml index ac5eb9e4f5..db5f7ccf34 100644 --- a/spring-integration-ftp/src/test/java/org/springframework/integration/ftp/config/FtpInboundChannelAdapterParserTests-context.xml +++ b/spring-integration-ftp/src/test/java/org/springframework/integration/ftp/config/FtpInboundChannelAdapterParserTests-context.xml @@ -8,15 +8,19 @@ http://www.springframework.org/schema/integration/ftp http://www.springframework.org/schema/integration/ftp/spring-integration-ftp-2.0.xsd"> - - - - - - - - - + + + + + + + + + + + + { + + @Override + public DefaultFtpClientFactory getObject() throws Exception { + DefaultFtpClientFactory factory = mock(DefaultFtpClientFactory.class); + FTPClient client = mock(FTPClient.class); + when(factory.getClient()).thenReturn(client); + return factory; + } + + @Override + public Class getObjectType() { + return DefaultFtpClientFactory.class; + } + + @Override + public boolean isSingleton() { + return true; + } + + } } diff --git a/spring-integration-ftp/src/test/java/org/springframework/integration/ftp/inbound/FtpInboundRemoteFileSystemSynchronizerTest.java b/spring-integration-ftp/src/test/java/org/springframework/integration/ftp/inbound/FtpInboundRemoteFileSystemSynchronizerTest.java index 839ea4065c..a43e0b95ee 100644 --- a/spring-integration-ftp/src/test/java/org/springframework/integration/ftp/inbound/FtpInboundRemoteFileSystemSynchronizerTest.java +++ b/spring-integration-ftp/src/test/java/org/springframework/integration/ftp/inbound/FtpInboundRemoteFileSystemSynchronizerTest.java @@ -31,7 +31,9 @@ import org.mockito.Mockito; import org.springframework.core.io.FileSystemResource; import org.springframework.integration.file.filters.FileListFilter; -import org.springframework.integration.ftp.client.FtpClientPool; +import org.springframework.integration.ftp.client.DefaultFtpClientFactory; +import org.springframework.integration.ftp.client.QueuedFtpClientPool; +import org.springframework.integration.ftp.filters.FtpPatternMatchingFileListFilter; /** * @author Oleg Zhurakousky @@ -48,21 +50,24 @@ public class FtpInboundRemoteFileSystemSynchronizerTest { } FtpInboundRemoteFileSystemSynchronizer syncronizer = new FtpInboundRemoteFileSystemSynchronizer(); syncronizer.setLocalDirectory(new FileSystemResource(System.getProperty("java.io.tmpdir"))); - FileListFilter filter = mock(FileListFilter.class); + FileListFilter filter = new FtpPatternMatchingFileListFilter("foo.txt"); // syncronizer.setFilter(filter); - FtpClientPool clientPoll = mock(FtpClientPool.class); + DefaultFtpClientFactory factory = mock(DefaultFtpClientFactory.class); FTPClient ftpClient = mock(FTPClient.class); + when(ftpClient.sendNoOp()).thenReturn(true); + when(factory.getClient()).thenReturn(ftpClient); + + QueuedFtpClientPool clientPoll = new QueuedFtpClientPool(factory); + FTPFile f1 = mock(FTPFile.class); when(f1.isFile()).thenReturn(true); when(f1.getName()).thenReturn("foo.txt"); - + FTPFile[] files = new FTPFile[]{f1}; when(ftpClient.listFiles()).thenReturn(files); - when(clientPoll.getClient()).thenReturn(ftpClient); - when(filter.filterFiles((Object[]) Mockito.any())).thenReturn(Arrays.asList(files)); - + syncronizer.setClientPool(clientPoll); syncronizer.setShouldDeleteSourceFile(true); syncronizer.afterPropertiesSet(); @@ -71,6 +76,5 @@ public class FtpInboundRemoteFileSystemSynchronizerTest { verify(ftpClient, times(1)).retrieveFile(Mockito.anyString(), Mockito.any(OutputStream.class)); verify(ftpClient, times(1)).deleteFile(Mockito.anyString()); - verify(clientPoll, times(1)).releaseClient(ftpClient); } } diff --git a/spring-integration-ftp/src/test/java/org/springframework/integration/ftp/outbound-ftp-context.xml b/spring-integration-ftp/src/test/java/org/springframework/integration/ftp/outbound-ftp-context.xml deleted file mode 100644 index d5002bcabb..0000000000 --- a/spring-integration-ftp/src/test/java/org/springframework/integration/ftp/outbound-ftp-context.xml +++ /dev/null @@ -1,39 +0,0 @@ - - - - - - - - - - - - - - - - - -