From 305472043f8d13a101c1ca76df3f5d082562bc5b Mon Sep 17 00:00:00 2001 From: Gary Russell Date: Wed, 26 Mar 2014 14:20:03 -0400 Subject: [PATCH] INT-3343 Bump JSch Version to 0.1.51 JIRA: https://jira.spring.io/browse/INT-3343 --- build.gradle | 2 +- .../sftp/outbound/SftpOutboundTests.java | 81 ++++++++++++++----- 2 files changed, 61 insertions(+), 22 deletions(-) diff --git a/build.gradle b/build.gradle index 674e7dc053..27ee51c020 100644 --- a/build.gradle +++ b/build.gradle @@ -499,7 +499,7 @@ project('spring-integration-sftp') { compile project(":spring-integration-core") compile project(":spring-integration-file") compile project(":spring-integration-stream") - compile "com.jcraft:jsch:0.1.49" + compile "com.jcraft:jsch:0.1.51" compile "org.springframework:spring-context-support:$springVersion" compile("javax.activation:activation:$javaxActivationVersion", optional) testCompile project(":spring-integration-test") diff --git a/spring-integration-sftp/src/test/java/org/springframework/integration/sftp/outbound/SftpOutboundTests.java b/spring-integration-sftp/src/test/java/org/springframework/integration/sftp/outbound/SftpOutboundTests.java index 76950c77d3..f5f49aa0b9 100644 --- a/spring-integration-sftp/src/test/java/org/springframework/integration/sftp/outbound/SftpOutboundTests.java +++ b/spring-integration-sftp/src/test/java/org/springframework/integration/sftp/outbound/SftpOutboundTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2013 the original author or authors. + * Copyright 2002-2014 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. @@ -22,6 +22,7 @@ import static org.junit.Assert.assertSame; import static org.junit.Assert.assertTrue; import static org.mockito.Matchers.anyString; import static org.mockito.Mockito.doAnswer; +import static org.mockito.Mockito.doReturn; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.never; import static org.mockito.Mockito.spy; @@ -36,6 +37,7 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.Vector; +import java.util.concurrent.atomic.AtomicInteger; import org.junit.Test; import org.mockito.Mockito; @@ -221,18 +223,28 @@ public class SftpOutboundTests { @Test public void testSharedSession() throws Exception { JSch jsch = spy(new JSch()); - Constructor ctor = com.jcraft.jsch.Session.class.getDeclaredConstructor(JSch.class); + Constructor ctor = com.jcraft.jsch.Session.class.getDeclaredConstructor(JSch.class, String.class, String.class, int.class); ctor.setAccessible(true); - com.jcraft.jsch.Session jschSession1 = spy(ctor.newInstance(jsch)); - com.jcraft.jsch.Session jschSession2 = spy(ctor.newInstance(jsch)); + com.jcraft.jsch.Session jschSession1 = spy(ctor.newInstance(jsch, "foo", "host", 22)); + com.jcraft.jsch.Session jschSession2 = spy(ctor.newInstance(jsch, "foo", "host", 22)); new DirectFieldAccessor(jschSession1).setPropertyValue("isConnected", true); new DirectFieldAccessor(jschSession2).setPropertyValue("isConnected", true); when(jsch.getSession("foo", "host", 22)).thenReturn(jschSession1, jschSession2); - ChannelSftp channel1 = spy(new ChannelSftp()); - ChannelSftp channel2 = spy(new ChannelSftp()); + final ChannelSftp channel1 = spy(new ChannelSftp()); + doReturn("channel1").when(channel1).toString(); + final ChannelSftp channel2 = spy(new ChannelSftp()); + doReturn("channel2").when(channel2).toString(); new DirectFieldAccessor(channel1).setPropertyValue("session", jschSession1); new DirectFieldAccessor(channel2).setPropertyValue("session", jschSession1); - when(jschSession1.openChannel("sftp")).thenReturn(channel1, channel2); + // Can't use when(session.open()) with a spy + final AtomicInteger n = new AtomicInteger(); + doAnswer(new Answer() { + + @Override + public ChannelSftp answer(InvocationOnMock invocation) throws Throwable { + return n.getAndIncrement() == 0 ? channel1 : channel2; + } + }).when(jschSession1).openChannel("sftp"); DefaultSftpSessionFactory factory = new DefaultSftpSessionFactory(jsch, true); factory.setHost("host"); factory.setUser("foo"); @@ -242,15 +254,17 @@ public class SftpOutboundTests { Session s1 = factory.getSession(); Session s2 = factory.getSession(); assertSame(TestUtils.getPropertyValue(s1, "jschSession"), TestUtils.getPropertyValue(s2, "jschSession")); + assertSame(channel1, TestUtils.getPropertyValue(s1, "channel")); + assertSame(channel2, TestUtils.getPropertyValue(s2, "channel")); } @Test public void testNotSharedSession() throws Exception { JSch jsch = spy(new JSch()); - Constructor ctor = com.jcraft.jsch.Session.class.getDeclaredConstructor(JSch.class); + Constructor ctor = com.jcraft.jsch.Session.class.getDeclaredConstructor(JSch.class, String.class, String.class, int.class); ctor.setAccessible(true); - com.jcraft.jsch.Session jschSession1 = spy(ctor.newInstance(jsch)); - com.jcraft.jsch.Session jschSession2 = spy(ctor.newInstance(jsch)); + com.jcraft.jsch.Session jschSession1 = spy(ctor.newInstance(jsch, "foo", "host", 22)); + com.jcraft.jsch.Session jschSession2 = spy(ctor.newInstance(jsch, "foo", "host", 22)); new DirectFieldAccessor(jschSession1).setPropertyValue("isConnected", true); new DirectFieldAccessor(jschSession2).setPropertyValue("isConnected", true); when(jsch.getSession("foo", "host", 22)).thenReturn(jschSession1, jschSession2); @@ -258,8 +272,8 @@ public class SftpOutboundTests { ChannelSftp channel2 = spy(new ChannelSftp()); new DirectFieldAccessor(channel1).setPropertyValue("session", jschSession1); new DirectFieldAccessor(channel2).setPropertyValue("session", jschSession1); - when(jschSession1.openChannel("sftp")).thenReturn(channel1); - when(jschSession2.openChannel("sftp")).thenReturn(channel2); + doReturn(channel1).when(jschSession1).openChannel("sftp"); + doReturn(channel2).when(jschSession2).openChannel("sftp"); DefaultSftpSessionFactory factory = new DefaultSftpSessionFactory(jsch, false); factory.setHost("host"); factory.setUser("foo"); @@ -269,26 +283,46 @@ public class SftpOutboundTests { Session s1 = factory.getSession(); Session s2 = factory.getSession(); assertNotSame(TestUtils.getPropertyValue(s1, "jschSession"), TestUtils.getPropertyValue(s2, "jschSession")); + assertSame(channel1, TestUtils.getPropertyValue(s1, "channel")); + assertSame(channel2, TestUtils.getPropertyValue(s2, "channel")); } @Test public void testSharedSessionCachedReset() throws Exception { JSch jsch = spy(new JSch()); - Constructor ctor = com.jcraft.jsch.Session.class.getDeclaredConstructor(JSch.class); + Constructor ctor = com.jcraft.jsch.Session.class.getDeclaredConstructor(JSch.class, String.class, String.class, int.class); ctor.setAccessible(true); - com.jcraft.jsch.Session jschSession1 = spy(ctor.newInstance(jsch)); - com.jcraft.jsch.Session jschSession2 = spy(ctor.newInstance(jsch)); + com.jcraft.jsch.Session jschSession1 = spy(ctor.newInstance(jsch, "foo", "host", 22)); + com.jcraft.jsch.Session jschSession2 = spy(ctor.newInstance(jsch, "foo", "host", 22)); new DirectFieldAccessor(jschSession1).setPropertyValue("isConnected", true); new DirectFieldAccessor(jschSession2).setPropertyValue("isConnected", true); when(jsch.getSession("foo", "host", 22)).thenReturn(jschSession1, jschSession2); - ChannelSftp channel1 = spy(new ChannelSftp()); - ChannelSftp channel2 = spy(new ChannelSftp()); - ChannelSftp channel3 = spy(new ChannelSftp()); - ChannelSftp channel4 = spy(new ChannelSftp()); + final ChannelSftp channel1 = spy(new ChannelSftp()); + doReturn("channel1").when(channel1).toString(); + final ChannelSftp channel2 = spy(new ChannelSftp()); + doReturn("channel2").when(channel2).toString(); + final ChannelSftp channel3 = spy(new ChannelSftp()); + doReturn("channel3").when(channel3).toString(); + final ChannelSftp channel4 = spy(new ChannelSftp()); + doReturn("channel4").when(channel4).toString(); new DirectFieldAccessor(channel1).setPropertyValue("session", jschSession1); new DirectFieldAccessor(channel2).setPropertyValue("session", jschSession1); - when(jschSession1.openChannel("sftp")).thenReturn(channel1, channel2); - when(jschSession2.openChannel("sftp")).thenReturn(channel3, channel4); + // Can't use when(session.open()) with a spy + final AtomicInteger n = new AtomicInteger(); + doAnswer(new Answer() { + + @Override + public ChannelSftp answer(InvocationOnMock invocation) throws Throwable { + return n.getAndIncrement() == 0 ? channel1 : channel2; + } + }).when(jschSession1).openChannel("sftp"); + doAnswer(new Answer() { + + @Override + public ChannelSftp answer(InvocationOnMock invocation) throws Throwable { + return n.getAndIncrement() < 3 ? channel3 : channel4; + } + }).when(jschSession2).openChannel("sftp"); DefaultSftpSessionFactory factory = new DefaultSftpSessionFactory(jsch, true); factory.setHost("host"); factory.setUser("foo"); @@ -301,21 +335,26 @@ public class SftpOutboundTests { Session s1 = cachedFactory.getSession(); Session s2 = cachedFactory.getSession(); assertSame(jschSession1, TestUtils.getPropertyValue(s2, "targetSession.jschSession")); + assertSame(channel1, TestUtils.getPropertyValue(s1, "targetSession.channel")); + assertSame(channel2, TestUtils.getPropertyValue(s2, "targetSession.channel")); assertSame(TestUtils.getPropertyValue(s1, "targetSession.jschSession"), TestUtils.getPropertyValue(s2, "targetSession.jschSession")); s1.close(); Session s3 = cachedFactory.getSession(); assertSame(TestUtils.getPropertyValue(s1, "targetSession"), TestUtils.getPropertyValue(s3, "targetSession")); + assertSame(channel1, TestUtils.getPropertyValue(s3, "targetSession.channel")); s3.close(); cachedFactory.resetCache(); verify(jschSession1, never()).disconnect(); s3 = cachedFactory.getSession(); assertSame(jschSession2, TestUtils.getPropertyValue(s3, "targetSession.jschSession")); assertNotSame(TestUtils.getPropertyValue(s1, "targetSession"), TestUtils.getPropertyValue(s3, "targetSession")); + assertSame(channel3, TestUtils.getPropertyValue(s3, "targetSession.channel")); s2.close(); verify(jschSession1).disconnect(); s2 = cachedFactory.getSession(); assertSame(jschSession2, TestUtils.getPropertyValue(s2, "targetSession.jschSession")); assertNotSame(TestUtils.getPropertyValue(s3, "targetSession"), TestUtils.getPropertyValue(s2, "targetSession")); + assertSame(channel4, TestUtils.getPropertyValue(s2, "targetSession.channel")); s2.close(); s3.close(); verify(jschSession2, never()).disconnect();