diff --git a/spring-integration-file/src/main/java/org/springframework/integration/file/remote/handler/FileTransferringMessageHandler.java b/spring-integration-file/src/main/java/org/springframework/integration/file/remote/handler/FileTransferringMessageHandler.java index 06a947f01f..75a426f1f0 100644 --- a/spring-integration-file/src/main/java/org/springframework/integration/file/remote/handler/FileTransferringMessageHandler.java +++ b/spring-integration-file/src/main/java/org/springframework/integration/file/remote/handler/FileTransferringMessageHandler.java @@ -283,19 +283,19 @@ public class FileTransferringMessageHandler extends AbstractMessageHandler { private void makeDirectories(String path, Session session) throws IOException { if (!session.exists(path)){ - int nextSeparatorIndex = path.lastIndexOf(remoteFileSeparator); + int nextSeparatorIndex = path.lastIndexOf(this.remoteFileSeparator); if (nextSeparatorIndex > -1){ List pathsToCreate = new LinkedList(); while (nextSeparatorIndex > -1){ String pathSegment = path.substring(0, nextSeparatorIndex); - if (session.exists(pathSegment)){ + if (pathSegment.length() == 0 || session.exists(pathSegment)) { // no more paths to create break; } else { pathsToCreate.add(0, pathSegment); - nextSeparatorIndex = pathSegment.lastIndexOf(remoteFileSeparator); + nextSeparatorIndex = pathSegment.lastIndexOf(this.remoteFileSeparator); } } diff --git a/spring-integration-sftp/src/test/java/org/springframework/integration/sftp/outbound/SftpOutboundTests.java b/spring-integration-sftp/src/test/java/org/springframework/integration/sftp/outbound/SftpOutboundTests.java index 74b32357ae..5575ac3650 100644 --- a/spring-integration-sftp/src/test/java/org/springframework/integration/sftp/outbound/SftpOutboundTests.java +++ b/spring-integration-sftp/src/test/java/org/springframework/integration/sftp/outbound/SftpOutboundTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2012 the original author or authors. + * Copyright 2002-2013 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. @@ -18,19 +18,23 @@ package org.springframework.integration.sftp.outbound; import static junit.framework.Assert.assertEquals; import static junit.framework.Assert.assertTrue; +import static org.mockito.Matchers.anyString; import static org.mockito.Mockito.doAnswer; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; -import java.io.*; -import java.util.*; +import java.io.File; +import java.io.FileOutputStream; +import java.io.InputStream; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.Vector; -import com.jcraft.jsch.SftpATTRS; import org.junit.Test; import org.mockito.Mockito; import org.mockito.invocation.InvocationOnMock; import org.mockito.stubbing.Answer; - import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.expression.common.LiteralExpression; @@ -50,9 +54,11 @@ import org.springframework.util.FileCopyUtils; import com.jcraft.jsch.ChannelSftp; import com.jcraft.jsch.ChannelSftp.LsEntry; +import com.jcraft.jsch.SftpATTRS; /** * @author Oleg Zhurakousky + * @author Gary Russell */ public class SftpOutboundTests { @@ -147,14 +153,39 @@ public class SftpOutboundTests { Object payload = result.getPayload(); assertTrue(payload instanceof List); @SuppressWarnings("unchecked") - List remoteFiles = (List) payload; + List> remoteFiles = (List>) payload; assertEquals(3, remoteFiles.size()); List files = Arrays.asList(new File("remote-test-dir").list()); - for (FileInfo remoteFile : remoteFiles) { + for (FileInfo remoteFile : remoteFiles) { assertTrue(files.contains(remoteFile.getFilename())); } } + @Test //INT-2954 + public void testMkDir() throws Exception { + @SuppressWarnings("unchecked") + Session session = mock(Session.class); + when(session.exists(anyString())).thenReturn(Boolean.FALSE); + @SuppressWarnings("unchecked") + SessionFactory sessionFactory = mock(SessionFactory.class); + when(sessionFactory.getSession()).thenReturn(session); + FileTransferringMessageHandler handler = new FileTransferringMessageHandler(sessionFactory); + handler.setAutoCreateDirectory(true); + handler.setRemoteDirectoryExpression(new LiteralExpression("/foo/bar/baz")); + final List madeDirs = new ArrayList(); + doAnswer(new Answer() { + public Object answer(InvocationOnMock invocation) throws Throwable { + madeDirs.add((String) invocation.getArguments()[0]); + return null; + } + }).when(session).mkdir(anyString()); + handler.handleMessage(new GenericMessage("qux")); + assertEquals(3, madeDirs.size()); + assertEquals("/foo", madeDirs.get(0)); + assertEquals("/foo/bar", madeDirs.get(1)); + assertEquals("/foo/bar/baz", madeDirs.get(2)); + } + public static class TestSftpSessionFactory extends DefaultSftpSessionFactory { @Override