diff --git a/.gitignore b/.gitignore index d5f7190a01..9da248c4fd 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ +*.msg *.iml *.ipr *.iws diff --git a/spring-integration-ftp/src/main/java/org/springframework/integration/ftp/inbound/FtpInboundRemoteFileSystemSynchronizer.java b/spring-integration-ftp/src/main/java/org/springframework/integration/ftp/inbound/FtpInboundRemoteFileSystemSynchronizer.java index e6f11290f2..60f4e98ad9 100644 --- a/spring-integration-ftp/src/main/java/org/springframework/integration/ftp/inbound/FtpInboundRemoteFileSystemSynchronizer.java +++ b/spring-integration-ftp/src/main/java/org/springframework/integration/ftp/inbound/FtpInboundRemoteFileSystemSynchronizer.java @@ -85,7 +85,12 @@ public class FtpInboundRemoteFileSystemSynchronizer extends AbstractInboundRemot acknowledge(client, ftpFile); } catch (Throwable th) { - throw new RuntimeException(th); + if (th instanceof RuntimeException){ + throw (RuntimeException)th; + } + else { + throw new MessagingException("Failed to compy file", th); + } } finally { fos.close(); 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 new file mode 100644 index 0000000000..839ea4065c --- /dev/null +++ b/spring-integration-ftp/src/test/java/org/springframework/integration/ftp/inbound/FtpInboundRemoteFileSystemSynchronizerTest.java @@ -0,0 +1,76 @@ +/* + * 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.inbound; + +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import java.io.File; +import java.io.OutputStream; +import java.util.Arrays; + +import org.apache.commons.net.ftp.FTPClient; +import org.apache.commons.net.ftp.FTPFile; +import org.junit.Test; +import org.mockito.Mockito; + +import org.springframework.core.io.FileSystemResource; +import org.springframework.integration.file.filters.FileListFilter; +import org.springframework.integration.ftp.client.FtpClientPool; + +/** + * @author Oleg Zhurakousky + * + */ +public class FtpInboundRemoteFileSystemSynchronizerTest { + + @SuppressWarnings({ "rawtypes", "unchecked" }) + @Test + public void testCopyFileToLocalDir() throws Exception { + File file = new File(System.getProperty("java.io.tmpdir") + "/foo.txt"); + if (file.exists()){ + file.delete(); + } + FtpInboundRemoteFileSystemSynchronizer syncronizer = new FtpInboundRemoteFileSystemSynchronizer(); + syncronizer.setLocalDirectory(new FileSystemResource(System.getProperty("java.io.tmpdir"))); + FileListFilter filter = mock(FileListFilter.class); + // + syncronizer.setFilter(filter); + + FtpClientPool clientPoll = mock(FtpClientPool.class); + FTPClient ftpClient = mock(FTPClient.class); + 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(); + + syncronizer.syncRemoteToLocalFileSystem(); + + 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/FtpSendingMessageHandlerTest.java b/spring-integration-ftp/src/test/java/org/springframework/integration/ftp/outbound/FtpSendingMessageHandlerTest.java new file mode 100644 index 0000000000..b8e7a7cbf6 --- /dev/null +++ b/spring-integration-ftp/src/test/java/org/springframework/integration/ftp/outbound/FtpSendingMessageHandlerTest.java @@ -0,0 +1,84 @@ +/* + * 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.outbound; + +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import java.io.File; +import java.io.InputStream; + +import org.apache.commons.net.ftp.FTPClient; +import org.junit.Test; +import org.mockito.Mockito; + +import org.springframework.integration.ftp.client.FtpClientPool; +import org.springframework.integration.message.GenericMessage; + +/** + * @author Oleg Zhurakousky + * + */ +public class FtpSendingMessageHandlerTest { + + @SuppressWarnings({ "unchecked", "rawtypes" }) + @Test + public void testHandleFileNameMessage() throws Exception { + FtpSendingMessageHandler handler = new FtpSendingMessageHandler(); + FtpClientPool clientPoll = mock(FtpClientPool.class); + FTPClient client = mock(FTPClient.class); + when(client.storeFile(Mockito.anyString(), Mockito.any(InputStream.class))).thenReturn(true); + when(clientPoll.getClient()).thenReturn(client); + + handler.setFtpClientPool(clientPoll); + handler.handleMessage(new GenericMessage("hello")); + verify(clientPoll, times(1)).getClient(); + verify(client, times(1)).storeFile(Mockito.anyString(), Mockito.any(InputStream.class)); + } + @SuppressWarnings({ "unchecked", "rawtypes" }) + @Test + public void testHandleFileAsByte() throws Exception { + FtpSendingMessageHandler handler = new FtpSendingMessageHandler(); + FtpClientPool clientPoll = mock(FtpClientPool.class); + FTPClient client = mock(FTPClient.class); + when(client.storeFile(Mockito.anyString(), Mockito.any(InputStream.class))).thenReturn(true); + when(clientPoll.getClient()).thenReturn(client); + + handler.setFtpClientPool(clientPoll); + handler.handleMessage(new GenericMessage("hello".getBytes())); + verify(clientPoll, times(1)).getClient(); + verify(client, times(1)).storeFile(Mockito.anyString(), Mockito.any(InputStream.class)); + } + + @SuppressWarnings({ "unchecked", "rawtypes" }) + @Test + public void testHandleFileMessage() throws Exception { + FtpSendingMessageHandler handler = new FtpSendingMessageHandler(); + FtpClientPool clientPoll = mock(FtpClientPool.class); + FTPClient client = mock(FTPClient.class); + when(client.storeFile(Mockito.anyString(), Mockito.any(InputStream.class))).thenReturn(true); + when(clientPoll.getClient()).thenReturn(client); + + handler.setFtpClientPool(clientPoll); + + File file = File.createTempFile("foo", ".txt"); + handler.handleMessage(new GenericMessage(file)); + verify(clientPoll, times(1)).getClient(); + verify(client, times(1)).storeFile(Mockito.anyString(), Mockito.any(InputStream.class)); + } +} diff --git a/spring-integration-sftp/src/main/java/org/springframework/integration/sftp/inbound/SftpInboundRemoteFileSystemSynchronizer.java b/spring-integration-sftp/src/main/java/org/springframework/integration/sftp/inbound/SftpInboundRemoteFileSystemSynchronizer.java index 4c8f77622c..e873450111 100644 --- a/spring-integration-sftp/src/main/java/org/springframework/integration/sftp/inbound/SftpInboundRemoteFileSystemSynchronizer.java +++ b/spring-integration-sftp/src/main/java/org/springframework/integration/sftp/inbound/SftpInboundRemoteFileSystemSynchronizer.java @@ -101,13 +101,12 @@ public class SftpInboundRemoteFileSystemSynchronizer extends AbstractInboundRemo return true; } catch (Throwable th) { - logger.error("failure occurred while copying from remote to local directory", th); + throw new MessagingException("Failure occurred while copying from remote to local directory", th); } } else { return true; } - return false; } @Override @@ -119,7 +118,8 @@ public class SftpInboundRemoteFileSystemSynchronizer extends AbstractInboundRemo session.start(); ChannelSftp channelSftp = session.getChannel(); Collection beforeFilter = channelSftp.ls(remotePath); - ChannelSftp.LsEntry[] entries = (beforeFilter == null) ? new ChannelSftp.LsEntry[0] : beforeFilter.toArray(new ChannelSftp.LsEntry[beforeFilter.size()]); + ChannelSftp.LsEntry[] entries = (beforeFilter == null) ? new ChannelSftp.LsEntry[0] : + beforeFilter.toArray(new ChannelSftp.LsEntry[beforeFilter.size()]); Collection files = this.filter.filterFiles(entries); for (ChannelSftp.LsEntry lsEntry : files) { if ((lsEntry != null) && !lsEntry.getAttrs().isDir() && !lsEntry.getAttrs().isLink()) { diff --git a/spring-integration-sftp/src/test/java/org/springframework/integration/sftp/inbound/SftpInboundRemoteFileSystemSynchronizerTests.java b/spring-integration-sftp/src/test/java/org/springframework/integration/sftp/inbound/SftpInboundRemoteFileSystemSynchronizerTests.java new file mode 100644 index 0000000000..7de5d9f219 --- /dev/null +++ b/spring-integration-sftp/src/test/java/org/springframework/integration/sftp/inbound/SftpInboundRemoteFileSystemSynchronizerTests.java @@ -0,0 +1,88 @@ +/* + * 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.sftp.inbound; + +import static org.mockito.Mockito.atLeast; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import java.io.File; +import java.io.FileInputStream; +import java.util.Vector; + +import org.junit.Test; +import org.mockito.Mockito; + +import org.springframework.core.io.FileSystemResource; +import org.springframework.integration.file.filters.FileListFilter; +import org.springframework.integration.sftp.session.SftpSession; +import org.springframework.integration.sftp.session.SftpSessionPool; + +import com.jcraft.jsch.ChannelSftp; +import com.jcraft.jsch.ChannelSftp.LsEntry; +import com.jcraft.jsch.SftpATTRS; + +/** + * @author Oleg Zhurakousky + * + */ +public class SftpInboundRemoteFileSystemSynchronizerTests { + @SuppressWarnings({ "rawtypes", "unchecked" }) + @Test + public void testCopyFileToLocalDir() throws Exception { + File file = new File(System.getProperty("java.io.tmpdir") + "/foo.txt"); + if (file.exists()){ + file.delete(); + } + SftpInboundRemoteFileSystemSynchronizer syncronizer = new SftpInboundRemoteFileSystemSynchronizer(); + syncronizer.setLocalDirectory(new FileSystemResource(System.getProperty("java.io.tmpdir"))); + syncronizer.setRemotePath("foo/bar"); + + FileListFilter filter = mock(FileListFilter.class); + + syncronizer.setFilter(filter); + + SftpSessionPool sessionPoll = mock(SftpSessionPool.class); + SftpSession sftpSession = mock(SftpSession.class); + + when(sessionPoll.getSession()).thenReturn(sftpSession); + ChannelSftp channel = mock(ChannelSftp.class); + when(channel.get((String) Mockito.any())).thenReturn(new FileInputStream(new File(".classpath"))); + when(sftpSession.getChannel()).thenReturn(channel); + Vector entries = new Vector(); + LsEntry entry = mock(LsEntry.class); + SftpATTRS attr = mock(SftpATTRS.class); + when(attr.isDir()).thenReturn(false); + when(attr.isLink()).thenReturn(false); + when(entry.getFilename()).thenReturn("foo.txt"); + when(entry.getAttrs()).thenReturn(attr); + entries.add(entry); + when(channel.ls("foo/bar")).thenReturn(entries); + when(filter.filterFiles((Object[]) Mockito.any())).thenReturn(entries); + + syncronizer.setClientPool(sessionPoll); + syncronizer.setShouldDeleteSourceFile(true); + syncronizer.afterPropertiesSet(); + + syncronizer.syncRemoteToLocalFileSystem(); + + verify(sessionPoll, times(1)).getSession(); + verify(sftpSession, atLeast(1)).getChannel(); + // will add more validation, but for now this test is mainly to get the test coverage up + } +} diff --git a/spring-integration-sftp/src/test/java/org/springframework/integration/sftp/outbound/SftpSendingMessageHandlerTest.java b/spring-integration-sftp/src/test/java/org/springframework/integration/sftp/outbound/SftpSendingMessageHandlerTest.java new file mode 100644 index 0000000000..7318b31248 --- /dev/null +++ b/spring-integration-sftp/src/test/java/org/springframework/integration/sftp/outbound/SftpSendingMessageHandlerTest.java @@ -0,0 +1,92 @@ +/* + * 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.sftp.outbound; + +import static org.mockito.Mockito.atLeast; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import java.io.File; + +import org.junit.Test; + +import org.springframework.expression.spel.standard.SpelExpressionParser; +import org.springframework.integration.message.GenericMessage; +import org.springframework.integration.sftp.session.SftpSession; +import org.springframework.integration.sftp.session.SftpSessionPool; + +import com.jcraft.jsch.ChannelSftp; + +/** + * + * @author Oleg Zhurakousky + * + */ +// there are few validations in this tests, but it is mainly to increase code coverage during CI +public class SftpSendingMessageHandlerTest { + @SuppressWarnings({ "unchecked", "rawtypes" }) + @Test + public void testHandleFileNameMessage() throws Exception { + SftpSessionPool sessionPoll = mock(SftpSessionPool.class); + SftpSession session = mock(SftpSession.class); + ChannelSftp channel = mock(ChannelSftp.class); + when(session.getChannel()).thenReturn(channel); + when(sessionPoll.getSession()).thenReturn(session); + SftpSendingMessageHandler handler = new SftpSendingMessageHandler(sessionPoll); + handler.setRemoteDirectoryExpression(new SpelExpressionParser().parseExpression("'foo.txt'")); + + handler.handleMessage(new GenericMessage("hello")); + verify(session, atLeast(1)).getChannel(); + verify(sessionPoll, times(1)).getSession(); + } + @SuppressWarnings({ "unchecked", "rawtypes" }) + @Test + public void testHandleFileAsByte() throws Exception { + SftpSessionPool sessionPoll = mock(SftpSessionPool.class); + SftpSession session = mock(SftpSession.class); + ChannelSftp channel = mock(ChannelSftp.class); + when(session.getChannel()).thenReturn(channel); + when(sessionPoll.getSession()).thenReturn(session); + SftpSendingMessageHandler handler = new SftpSendingMessageHandler(sessionPoll); + handler.setRemoteDirectoryExpression(new SpelExpressionParser().parseExpression("'foo.txt'")); + + handler.handleMessage(new GenericMessage("hello".getBytes())); + verify(session, atLeast(1)).getChannel(); + verify(sessionPoll, times(1)).getSession(); + } + + @SuppressWarnings({ "unchecked", "rawtypes" }) + @Test + public void testHandleFileMessage() throws Exception { + SftpSessionPool sessionPoll = mock(SftpSessionPool.class); + SftpSession session = mock(SftpSession.class); + ChannelSftp channel = mock(ChannelSftp.class); + when(session.getChannel()).thenReturn(channel); + when(sessionPoll.getSession()).thenReturn(session); + SftpSendingMessageHandler handler = new SftpSendingMessageHandler(sessionPoll); + handler.setRemoteDirectoryExpression(new SpelExpressionParser().parseExpression("'foo.txt'")); + + handler.handleMessage(new GenericMessage("hello".getBytes())); + + + File file = File.createTempFile("foo", ".txt"); + handler.handleMessage(new GenericMessage(file)); + verify(session, atLeast(1)).getChannel(); + verify(sessionPoll, atLeast(1)).getSession(); + } +}