Skip to content

GH-2820: Close session in FtpRemFileTempl.exists #2821

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 19, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2014-2016 the original author or authors.
* Copyright 2014-2019 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.
Expand Down Expand Up @@ -92,7 +92,7 @@ public boolean exists(final String path) {
return !ObjectUtils.isEmpty(names);

case NLST_AND_DIRS:
return getSession().exists(path);
return FtpRemoteFileTemplate.super.exists(path);

default:
throw new IllegalStateException("Unsupported 'existsMode': " +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,10 @@
import org.springframework.integration.file.remote.ClientCallbackWithoutResult;
import org.springframework.integration.file.remote.SessionCallbackWithoutResult;
import org.springframework.integration.file.remote.session.SessionFactory;
import org.springframework.integration.file.support.FileExistsMode;
import org.springframework.integration.ftp.FtpTestSupport;
import org.springframework.integration.test.util.TestUtils;
import org.springframework.integration.util.SimplePool;
import org.springframework.messaging.MessagingException;
import org.springframework.messaging.support.GenericMessage;
import org.springframework.test.context.ContextConfiguration;
Expand All @@ -61,7 +64,7 @@ public class FtpRemoteFileTemplateTests extends FtpTestSupport {
private SessionFactory<FTPFile> sessionFactory;

@Test
public void testINT3412AppendStatRmdir() throws IOException {
public void testINT3412AppendStatRmdir() {
FtpRemoteFileTemplate template = new FtpRemoteFileTemplate(sessionFactory);
DefaultFileNameGenerator fileNameGenerator = new DefaultFileNameGenerator();
fileNameGenerator.setBeanFactory(mock(BeanFactory.class));
Expand Down Expand Up @@ -94,7 +97,7 @@ public void testINT3412AppendStatRmdir() throws IOException {
assertThat(files.length).isEqualTo(0);
assertThat(session.rmdir("foo/")).isTrue();
});
assertThat(template.getSession().exists("foo")).isFalse();
assertThat(template.exists("foo")).isFalse();
}

@Test
Expand Down Expand Up @@ -122,6 +125,27 @@ public void testFileCloseOnBadConnect() throws Exception {
newFile.delete();
}

@Test
public void testConnectionClosedAfterExists() throws Exception {
FtpRemoteFileTemplate template = new FtpRemoteFileTemplate(this.sessionFactory);
template.setRemoteDirectoryExpression(new LiteralExpression("/"));
template.setExistsMode(FtpRemoteFileTemplate.ExistsMode.NLST_AND_DIRS);
template.setBeanFactory(mock(BeanFactory.class));
template.afterPropertiesSet();
File file = new File(System.getProperty("java.io.tmpdir"), UUID.randomUUID().toString());
FileOutputStream fileOutputStream = new FileOutputStream(file);
fileOutputStream.write("foo".getBytes());
fileOutputStream.close();
template.send(new GenericMessage<>(file), FileExistsMode.IGNORE);
File newFile = new File(System.getProperty("java.io.tmpdir"), UUID.randomUUID().toString());
assertThat(file.renameTo(newFile)).isTrue();
file.delete();
newFile.delete();

SimplePool<?> pool = TestUtils.getPropertyValue(this.sessionFactory, "pool", SimplePool.class);
assertThat(pool.getActiveCount()).isEqualTo(0);
}

@Configuration
public static class Config {

Expand Down