Skip to content

Commit 4aea880

Browse files
artembilangaryrussell
authored andcommitted
GH-2820: Close session in FtpRemFileTempl.exists
Fixes: #2820 The current `FtpRemoteFileTemplate.exists()` implementation obtains a `Session` for the `NLST_AND_DIRS` mode, but doesn't close it. * Fix the `FtpRemoteFileTemplate.exists()` method to delegate to `super` for the proper interaction with a `Session` **Cherry-pick to 5.1.x, 5.0.x & 4.3.x**
1 parent 00ca87f commit 4aea880

File tree

2 files changed

+29
-4
lines changed

2 files changed

+29
-4
lines changed

spring-integration-ftp/src/main/java/org/springframework/integration/ftp/session/FtpRemoteFileTemplate.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2014-2016 the original author or authors.
2+
* Copyright 2014-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -92,7 +92,7 @@ public boolean exists(final String path) {
9292
return !ObjectUtils.isEmpty(names);
9393

9494
case NLST_AND_DIRS:
95-
return getSession().exists(path);
95+
return FtpRemoteFileTemplate.super.exists(path);
9696

9797
default:
9898
throw new IllegalStateException("Unsupported 'existsMode': " +

spring-integration-ftp/src/test/java/org/springframework/integration/ftp/session/FtpRemoteFileTemplateTests.java

+27-2
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import org.junit.Test;
3434
import org.junit.runner.RunWith;
3535

36+
import org.springframework.beans.factory.BeanFactory;
3637
import org.springframework.beans.factory.annotation.Autowired;
3738
import org.springframework.context.annotation.Bean;
3839
import org.springframework.context.annotation.Configuration;
@@ -41,7 +42,10 @@
4142
import org.springframework.integration.file.remote.ClientCallbackWithoutResult;
4243
import org.springframework.integration.file.remote.SessionCallbackWithoutResult;
4344
import org.springframework.integration.file.remote.session.SessionFactory;
45+
import org.springframework.integration.file.support.FileExistsMode;
4446
import org.springframework.integration.ftp.FtpTestSupport;
47+
import org.springframework.integration.test.util.TestUtils;
48+
import org.springframework.integration.util.SimplePool;
4549
import org.springframework.messaging.MessagingException;
4650
import org.springframework.messaging.support.GenericMessage;
4751
import org.springframework.test.context.ContextConfiguration;
@@ -60,7 +64,7 @@ public class FtpRemoteFileTemplateTests extends FtpTestSupport {
6064
private SessionFactory<FTPFile> sessionFactory;
6165

6266
@Test
63-
public void testINT3412AppendStatRmdir() throws IOException {
67+
public void testINT3412AppendStatRmdir() {
6468
FtpRemoteFileTemplate template = new FtpRemoteFileTemplate(sessionFactory);
6569
DefaultFileNameGenerator fileNameGenerator = new DefaultFileNameGenerator();
6670
fileNameGenerator.setExpression("'foobar.txt'");
@@ -90,7 +94,7 @@ public void testINT3412AppendStatRmdir() throws IOException {
9094
assertEquals(0, files.length);
9195
assertTrue(session.rmdir("foo/"));
9296
});
93-
assertFalse(template.getSession().exists("foo"));
97+
assertFalse(template.exists("foo"));
9498
}
9599

96100
@Test
@@ -118,6 +122,27 @@ public void testFileCloseOnBadConnect() throws Exception {
118122
newFile.delete();
119123
}
120124

125+
@Test
126+
public void testConnectionClosedAfterExists() throws Exception {
127+
FtpRemoteFileTemplate template = new FtpRemoteFileTemplate(this.sessionFactory);
128+
template.setRemoteDirectoryExpression(new LiteralExpression("/"));
129+
template.setExistsMode(FtpRemoteFileTemplate.ExistsMode.NLST_AND_DIRS);
130+
template.setBeanFactory(mock(BeanFactory.class));
131+
template.afterPropertiesSet();
132+
File file = new File(System.getProperty("java.io.tmpdir"), UUID.randomUUID().toString());
133+
FileOutputStream fileOutputStream = new FileOutputStream(file);
134+
fileOutputStream.write("foo".getBytes());
135+
fileOutputStream.close();
136+
template.send(new GenericMessage<>(file), FileExistsMode.IGNORE);
137+
File newFile = new File(System.getProperty("java.io.tmpdir"), UUID.randomUUID().toString());
138+
assertTrue(file.renameTo(newFile));
139+
file.delete();
140+
newFile.delete();
141+
142+
SimplePool<?> pool = TestUtils.getPropertyValue(this.sessionFactory, "pool", SimplePool.class);
143+
assertEquals(0, pool.getActiveCount());
144+
}
145+
121146
@Configuration
122147
public static class Config {
123148

0 commit comments

Comments
 (0)