MkDirs no longer tries to create folders twice when path has trailing slash.

This commit is contained in:
Urs Reupke
2012-03-23 10:58:42 +01:00
parent 1c4781a65d
commit c627fabebd
2 changed files with 37 additions and 0 deletions

View File

@@ -90,6 +90,7 @@ public class SFTPClient
public void mkdirs(String path) public void mkdirs(String path)
throws IOException { throws IOException {
final Deque<String> dirsToMake = new LinkedList<String>(); final Deque<String> dirsToMake = new LinkedList<String>();
path = PathComponents.trimTrailingSeparator(path, engine.getPathHelper().getPathSeparator());
for (PathComponents current = engine.getPathHelper().getComponents(path); ; for (PathComponents current = engine.getPathHelper().getComponents(path); ;
current = engine.getPathHelper().getComponents(current.getParent())) { current = engine.getPathHelper().getComponents(current.getParent())) {
final FileAttributes attrs = statExistence(current.getPath()); final FileAttributes attrs = statExistence(current.getPath());

View File

@@ -0,0 +1,36 @@
package net.schmizz.sshj.sftp;
import org.junit.Before;
import org.junit.Test;
import java.io.IOException;
import static net.schmizz.sshj.sftp.PathHelper.DEFAULT_PATH_SEPARATOR;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
public class SFTPClientTest {
private final SFTPEngine sftpEngine = mock(SFTPEngine.class);
private final SFTPClient client = new SFTPClient(sftpEngine);
@Before
public void setPathHelper() throws Exception {
PathHelper helper = new PathHelper(sftpEngine, DEFAULT_PATH_SEPARATOR);
when(sftpEngine.getPathHelper()).thenReturn(helper);
}
@Before
public void setRemoteWorkingDirectory() throws IOException {
when(sftpEngine.canonicalize(".")).thenReturn("/workingdirectory");
FileAttributes isADirectory = new FileAttributes.Builder().withType(FileMode.Type.DIRECTORY).build();
when(sftpEngine.stat("/workingdirectory")).thenReturn(isADirectory);
}
@Test
public void doesNotTryToCreateDirectoryTwiceWhenPathHasTrailingSeparator() throws Exception {
client.mkdirs("/folder/directory/");
verify(sftpEngine, times(1)).makeDir("/folder/directory");
}
}