Skip to content

Commit 1e93d51

Browse files
committed
(S)FTP tests: Add missed @DirtiesContext
Related to: https://build.spring.io/browse/INT-MASTERSPRING40-826/ If we don't close application context on the embedded (S)FTP server, we my still have some session opened in other tests, which would cause some unexpected behavior like extra `SessionClosedEvent` in the test for FTP events
1 parent 87f5409 commit 1e93d51

File tree

2 files changed

+27
-40
lines changed

2 files changed

+27
-40
lines changed

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

+8-12
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
package org.springframework.integration.ftp.session;
1818

1919
import static org.assertj.core.api.Assertions.assertThat;
20-
import static org.assertj.core.api.Assertions.fail;
20+
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
2121
import static org.mockito.Mockito.mock;
2222
import static org.mockito.Mockito.when;
2323

@@ -46,8 +46,8 @@
4646
import org.springframework.integration.util.SimplePool;
4747
import org.springframework.messaging.MessagingException;
4848
import org.springframework.messaging.support.GenericMessage;
49-
import org.springframework.test.context.ContextConfiguration;
50-
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
49+
import org.springframework.test.annotation.DirtiesContext;
50+
import org.springframework.test.context.junit4.SpringRunner;
5151

5252
/**
5353
* @author Gary Russell
@@ -56,8 +56,8 @@
5656
* @since 4.1
5757
*
5858
*/
59-
@ContextConfiguration
60-
@RunWith(SpringJUnit4ClassRunner.class)
59+
@RunWith(SpringRunner.class)
60+
@DirtiesContext
6161
public class FtpRemoteFileTemplateTests extends FtpTestSupport {
6262

6363
@Autowired
@@ -112,13 +112,9 @@ public void testFileCloseOnBadConnect() throws Exception {
112112
FileOutputStream fileOutputStream = new FileOutputStream(file);
113113
fileOutputStream.write("foo".getBytes());
114114
fileOutputStream.close();
115-
try {
116-
template.send(new GenericMessage<>(file));
117-
fail("exception expected");
118-
}
119-
catch (MessagingException e) {
120-
assertThat(e.getCause().getMessage()).isEqualTo("bar");
121-
}
115+
assertThatExceptionOfType(MessagingException.class)
116+
.isThrownBy(() -> template.send(new GenericMessage<>(file)))
117+
.withStackTraceContaining("bar");
122118
File newFile = new File(System.getProperty("java.io.tmpdir"), UUID.randomUUID().toString());
123119
assertThat(file.renameTo(newFile)).isTrue();
124120
file.delete();

spring-integration-sftp/src/test/java/org/springframework/integration/sftp/outbound/SftpServerOutboundTests.java

+19-28
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package org.springframework.integration.sftp.outbound;
1818

1919
import static org.assertj.core.api.Assertions.assertThat;
20+
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
2021
import static org.assertj.core.api.Assertions.fail;
2122
import static org.mockito.Mockito.spy;
2223
import static org.mockito.Mockito.verify;
@@ -66,8 +67,8 @@
6667
import org.springframework.messaging.MessagingException;
6768
import org.springframework.messaging.PollableChannel;
6869
import org.springframework.messaging.support.GenericMessage;
69-
import org.springframework.test.context.ContextConfiguration;
70-
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
70+
import org.springframework.test.annotation.DirtiesContext;
71+
import org.springframework.test.context.junit4.SpringRunner;
7172
import org.springframework.util.FileCopyUtils;
7273

7374
import com.jcraft.jsch.ChannelSftp;
@@ -76,10 +77,11 @@
7677
/**
7778
* @author Artem Bilan
7879
* @author Gary Russell
80+
*
7981
* @since 3.0
8082
*/
81-
@ContextConfiguration
82-
@RunWith(SpringJUnit4ClassRunner.class)
83+
@RunWith(SpringRunner.class)
84+
@DirtiesContext
8385
public class SftpServerOutboundTests extends SftpTestSupport {
8486

8587
@Autowired
@@ -165,17 +167,11 @@ public void testInt2866LocalDirectoryExpressionGET() {
165167

166168
@Test
167169
public void testInt2866InvalidLocalDirectoryExpression() {
168-
try {
169-
this.invalidDirExpression.send(new GenericMessage<Object>("sftpSource/ sftpSource1.txt"));
170-
fail("Exception expected.");
171-
}
172-
catch (Exception e) {
173-
Throwable cause = e.getCause();
174-
assertThat(cause).isNotNull();
175-
cause = cause.getCause();
176-
assertThat(cause).isInstanceOf(IllegalArgumentException.class);
177-
assertThat(cause.getMessage()).startsWith("Failed to make local directory");
178-
}
170+
assertThatExceptionOfType(Exception.class)
171+
.isThrownBy(() ->
172+
this.invalidDirExpression.send(new GenericMessage<Object>("sftpSource/ sftpSource1.txt")))
173+
.withRootCauseInstanceOf(IllegalArgumentException.class)
174+
.withMessageContaining("Failed to make local directory");
179175
}
180176

181177
@Test
@@ -190,15 +186,12 @@ public void testInt2866LocalDirectoryExpressionMGET() {
190186

191187
assertThat(localFiles).hasSizeGreaterThan(0);
192188

193-
boolean assertedModified = false;
194189
for (File file : localFiles) {
195190
assertThat(file.getPath().replaceAll(Matcher.quoteReplacement(File.separator), "/")).contains(dir);
196191
if (file.getPath().contains("localTarget1")) {
197-
assertedModified = assertPreserved(modified, file);
192+
assertPreserved(modified, file);
198193
}
199194
}
200-
assertThat(assertedModified).isTrue();
201-
202195
dir = "sftpSource/subSftpSource/";
203196
this.inboundMGet.send(new GenericMessage<Object>(dir + "*.txt"));
204197
result = this.output.receive(1000);
@@ -225,14 +218,12 @@ public void testInt3172LocalDirectoryExpressionMGETRecursive() throws IOExceptio
225218
List<File> localFiles = (List<File>) result.getPayload();
226219
assertThat(localFiles).hasSize(3);
227220

228-
boolean assertedModified = false;
229221
for (File file : localFiles) {
230222
assertThat(file.getPath().replaceAll(Matcher.quoteReplacement(File.separator), "/")).contains(dir);
231223
if (file.getPath().contains("localTarget1")) {
232-
assertedModified = assertPreserved(modified, file);
224+
assertPreserved(modified, file);
233225
}
234226
}
235-
assertThat(assertedModified).isTrue();
236227
assertThat(localFiles.get(2).getPath().replaceAll(Matcher.quoteReplacement(File.separator), "/"))
237228
.contains(dir + "subSftpSource");
238229

@@ -270,10 +261,9 @@ private long setModifiedOnSource1() {
270261
return modified;
271262
}
272263

273-
private boolean assertPreserved(long modified, File file) {
264+
private void assertPreserved(long modified, File file) {
274265
assertThat(Math.abs(file.lastModified() - modified))
275266
.as("lastModified wrong by " + (modified - file.lastModified())).isLessThan(1_000);
276-
return true;
277267
}
278268

279269
@Test
@@ -480,7 +470,7 @@ public void testInt3088MPutRecursive() {
480470
while (output.receive(0) != null) {
481471
// drain
482472
}
483-
this.inboundMPutRecursive.send(new GenericMessage<File>(getSourceLocalDirectory()));
473+
this.inboundMPutRecursive.send(new GenericMessage<>(getSourceLocalDirectory()));
484474
@SuppressWarnings("unchecked")
485475
Message<List<String>> out = (Message<List<String>>) this.output.receive(1000);
486476
assertThat(out).isNotNull();
@@ -504,7 +494,7 @@ public void testInt3088MPutRecursiveFiltered() {
504494
while (output.receive(0) != null) {
505495
// drain
506496
}
507-
this.inboundMPutRecursiveFiltered.send(new GenericMessage<File>(getSourceLocalDirectory()));
497+
this.inboundMPutRecursiveFiltered.send(new GenericMessage<>(getSourceLocalDirectory()));
508498
@SuppressWarnings("unchecked")
509499
Message<List<String>> out = (Message<List<String>>) this.output.receive(1000);
510500
assertThat(out).isNotNull();
@@ -559,7 +549,7 @@ public void testStream() {
559549

560550
@Test
561551
public void testMessageSessionCallback() {
562-
this.inboundCallback.send(new GenericMessage<String>("foo"));
552+
this.inboundCallback.send(new GenericMessage<>("foo"));
563553
Message<?> receive = this.output.receive(10000);
564554
assertThat(receive).isNotNull();
565555
assertThat(receive.getPayload()).isEqualTo("FOO");
@@ -576,7 +566,7 @@ private static final class TestMessageSessionCallback
576566
implements MessageSessionCallback<LsEntry, Object> {
577567

578568
@Override
579-
public Object doInSession(Session<ChannelSftp.LsEntry> session, Message<?> requestMessage) throws IOException {
569+
public Object doInSession(Session<ChannelSftp.LsEntry> session, Message<?> requestMessage) {
580570
return ((String) requestMessage.getPayload()).toUpperCase();
581571
}
582572

@@ -627,6 +617,7 @@ public MessageChannel eventChannel() {
627617
return true;
628618
};
629619
}
620+
630621
}
631622

632623
}

0 commit comments

Comments
 (0)