INT-3305 SFTP Connect Error Polishing

* Add code to test to wait for server to start
* Avoid NPE when trying to close a Sesion that was never open
This commit is contained in:
Gary Russell
2014-03-07 15:44:15 -05:00
parent ee73515047
commit 8933d7ff4e
2 changed files with 32 additions and 10 deletions

View File

@@ -163,7 +163,9 @@ class SftpSession implements Session<LsEntry> {
public void close() {
this.closed = true;
if (this.wrapper != null) {
this.channel.disconnect();
if (this.channel != null) {
this.channel.disconnect();
}
this.wrapper.close();
}
else {

View File

@@ -19,8 +19,11 @@ import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.instanceOf;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import java.net.ConnectException;
import org.apache.sshd.SshServer;
import org.apache.sshd.server.PasswordAuthenticator;
import org.apache.sshd.server.keyprovider.SimpleGeneratorHostKeyProvider;
@@ -29,6 +32,8 @@ import org.junit.Test;
import org.springframework.integration.test.util.SocketUtils;
import com.jcraft.jsch.JSchException;
/**
* @author Gary Russell
* @since 3.0.2
@@ -60,17 +65,32 @@ public class INT3305Tests {
f.setPort(port);
f.setUser("user");
f.setPassword("pass");
try {
f.getSession();
fail("Expected Exception");
}
catch (Exception e) {
assertThat(e, instanceOf(IllegalStateException.class));
assertThat(e.getCause(), instanceOf(IllegalStateException.class));
assertThat(e.getCause().getMessage(), equalTo("failed to connect"));
int n = 0;
while (true) {
try {
f.getSession();
fail("Expected Exception");
}
catch (Exception e) {
if (e instanceof IllegalStateException && "failed to create SFTP Session".equals(e.getMessage())) {
if (e.getCause() instanceof IllegalStateException) {
if (e.getCause().getCause() instanceof JSchException) {
if (e.getCause().getCause().getCause() instanceof ConnectException) {
assertTrue("Server failed to start in 10 seconds", n++ < 100);
Thread.sleep(100);
continue;
}
}
}
}
assertThat(e, instanceOf(IllegalStateException.class));
assertThat(e.getCause(), instanceOf(IllegalStateException.class));
assertThat(e.getCause().getMessage(), equalTo("failed to connect"));
break;
}
}
int n = 0;
n = 0;
while (n++ < 100 && server.getActiveSessions().size() > 0) {
Thread.sleep(100);
}