Search in sources :

Example 6 with Session

use of org.springframework.integration.file.remote.session.Session in project spring-integration by spring-projects.

the class RemoteFileOutboundGatewayTests method testLs_1_a_f_dirs_links.

@Test
public void testLs_1_a_f_dirs_links() throws Exception {
    SessionFactory sessionFactory = mock(SessionFactory.class);
    Session session = mock(Session.class);
    TestRemoteFileOutboundGateway gw = new TestRemoteFileOutboundGateway(sessionFactory, "ls", "payload");
    gw.setOptions("-1 -a -f -dirs -links");
    gw.afterPropertiesSet();
    when(sessionFactory.getSession()).thenReturn(session);
    TestLsEntry[] files = fileList();
    when(session.list("testremote/")).thenReturn(files);
    @SuppressWarnings("unchecked") MessageBuilder<List<String>> out = (MessageBuilder<List<String>>) gw.handleRequestMessage(new GenericMessage<>("testremote"));
    assertEquals(6, out.getPayload().size());
    assertEquals("f2", out.getPayload().get(0));
    assertEquals("f1", out.getPayload().get(1));
    assertEquals("f3", out.getPayload().get(2));
    assertEquals("f4", out.getPayload().get(3));
    assertEquals(".f5", out.getPayload().get(4));
    assertEquals(".f6", out.getPayload().get(5));
}
Also used : SessionFactory(org.springframework.integration.file.remote.session.SessionFactory) GenericMessage(org.springframework.messaging.support.GenericMessage) MessageBuilder(org.springframework.integration.support.MessageBuilder) List(java.util.List) ArrayList(java.util.ArrayList) Matchers.containsString(org.hamcrest.Matchers.containsString) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Session(org.springframework.integration.file.remote.session.Session) Test(org.junit.Test)

Example 7 with Session

use of org.springframework.integration.file.remote.session.Session in project spring-integration by spring-projects.

the class RemoteFileOutboundGatewayTests method testLs_1_f.

@Test
public void testLs_1_f() throws Exception {
    // no sort
    SessionFactory sessionFactory = mock(SessionFactory.class);
    Session session = mock(Session.class);
    TestRemoteFileOutboundGateway gw = new TestRemoteFileOutboundGateway(sessionFactory, "ls", "payload");
    gw.setOptions("-1 -f");
    gw.afterPropertiesSet();
    when(sessionFactory.getSession()).thenReturn(session);
    TestLsEntry[] files = fileList();
    when(session.list("testremote/")).thenReturn(files);
    @SuppressWarnings("unchecked") MessageBuilder<List<String>> out = (MessageBuilder<List<String>>) gw.handleRequestMessage(new GenericMessage<>("testremote"));
    assertEquals(2, out.getPayload().size());
    assertEquals("f2", out.getPayload().get(0));
    assertEquals("f1", out.getPayload().get(1));
}
Also used : SessionFactory(org.springframework.integration.file.remote.session.SessionFactory) GenericMessage(org.springframework.messaging.support.GenericMessage) MessageBuilder(org.springframework.integration.support.MessageBuilder) List(java.util.List) ArrayList(java.util.ArrayList) Matchers.containsString(org.hamcrest.Matchers.containsString) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Session(org.springframework.integration.file.remote.session.Session) Test(org.junit.Test)

Example 8 with Session

use of org.springframework.integration.file.remote.session.Session in project spring-integration by spring-projects.

the class SessionFactoryTests method testConnectionLimit.

@Test
@Ignore
public void testConnectionLimit() throws Exception {
    ExecutorService executor = Executors.newCachedThreadPool();
    DefaultFtpSessionFactory sessionFactory = new DefaultFtpSessionFactory();
    sessionFactory.setHost("192.168.28.143");
    sessionFactory.setPassword("password");
    sessionFactory.setUsername("user");
    final CachingSessionFactory factory = new CachingSessionFactory(sessionFactory, 2);
    final Random random = new Random();
    final AtomicInteger failures = new AtomicInteger();
    for (int i = 0; i < 30; i++) {
        executor.execute(() -> {
            try {
                Session session = factory.getSession();
                Thread.sleep(random.nextInt(5000));
                session.close();
            } catch (Exception e) {
                e.printStackTrace();
                failures.incrementAndGet();
            }
        });
    }
    executor.shutdown();
    executor.awaitTermination(10000, TimeUnit.SECONDS);
    assertEquals(0, failures.get());
}
Also used : CachingSessionFactory(org.springframework.integration.file.remote.session.CachingSessionFactory) Random(java.util.Random) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ExecutorService(java.util.concurrent.ExecutorService) MessagingException(org.springframework.messaging.MessagingException) Session(org.springframework.integration.file.remote.session.Session) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 9 with Session

use of org.springframework.integration.file.remote.session.Session in project spring-integration by spring-projects.

the class SftpOutboundTests method testNotSharedSession.

@Test
public void testNotSharedSession() throws Exception {
    JSch jsch = spy(new JSch());
    Constructor<com.jcraft.jsch.Session> 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, "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());
    new DirectFieldAccessor(channel1).setPropertyValue("session", jschSession1);
    new DirectFieldAccessor(channel2).setPropertyValue("session", jschSession1);
    doReturn(channel1).when(jschSession1).openChannel("sftp");
    doReturn(channel2).when(jschSession2).openChannel("sftp");
    DefaultSftpSessionFactory factory = new DefaultSftpSessionFactory(jsch, false);
    factory.setHost("host");
    factory.setUser("foo");
    factory.setPassword("bar");
    noopConnect(channel1);
    noopConnect(channel2);
    Session<LsEntry> s1 = factory.getSession();
    Session<LsEntry> s2 = factory.getSession();
    assertNotSame(TestUtils.getPropertyValue(s1, "jschSession"), TestUtils.getPropertyValue(s2, "jschSession"));
    assertSame(channel1, TestUtils.getPropertyValue(s1, "channel"));
    assertSame(channel2, TestUtils.getPropertyValue(s2, "channel"));
}
Also used : ChannelSftp(com.jcraft.jsch.ChannelSftp) DirectFieldAccessor(org.springframework.beans.DirectFieldAccessor) JSch(com.jcraft.jsch.JSch) DefaultSftpSessionFactory(org.springframework.integration.sftp.session.DefaultSftpSessionFactory) LsEntry(com.jcraft.jsch.ChannelSftp.LsEntry) Session(org.springframework.integration.file.remote.session.Session) SftpSession(org.springframework.integration.sftp.session.SftpSession) Test(org.junit.Test)

Example 10 with Session

use of org.springframework.integration.file.remote.session.Session in project spring-integration by spring-projects.

the class SftpOutboundTests method testSharedSessionCachedReset.

@Test
public void testSharedSessionCachedReset() throws Exception {
    JSch jsch = spy(new JSch());
    Constructor<com.jcraft.jsch.Session> 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, "foo", "host", 22));
    com.jcraft.jsch.Session jschSession2 = spy(ctor.newInstance(jsch, "foo", "host", 22));
    willAnswer(invocation -> {
        new DirectFieldAccessor(jschSession1).setPropertyValue("isConnected", true);
        return null;
    }).given(jschSession1).connect();
    willAnswer(invocation -> {
        new DirectFieldAccessor(jschSession2).setPropertyValue("isConnected", true);
        return null;
    }).given(jschSession2).connect();
    when(jsch.getSession("foo", "host", 22)).thenReturn(jschSession1, jschSession2);
    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);
    // Can't use when(session.open()) with a spy
    final AtomicInteger n = new AtomicInteger();
    doAnswer(invocation -> n.getAndIncrement() == 0 ? channel1 : channel2).when(jschSession1).openChannel("sftp");
    doAnswer(invocation -> n.getAndIncrement() < 3 ? channel3 : channel4).when(jschSession2).openChannel("sftp");
    DefaultSftpSessionFactory factory = new DefaultSftpSessionFactory(jsch, true);
    factory.setHost("host");
    factory.setUser("foo");
    factory.setPassword("bar");
    CachingSessionFactory<LsEntry> cachedFactory = new CachingSessionFactory<LsEntry>(factory);
    noopConnect(channel1);
    noopConnect(channel2);
    noopConnect(channel3);
    noopConnect(channel4);
    Session<LsEntry> s1 = cachedFactory.getSession();
    Session<LsEntry> 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<LsEntry> 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();
    cachedFactory.resetCache();
    verify(jschSession2).disconnect();
}
Also used : CachingSessionFactory(org.springframework.integration.file.remote.session.CachingSessionFactory) JSch(com.jcraft.jsch.JSch) DefaultSftpSessionFactory(org.springframework.integration.sftp.session.DefaultSftpSessionFactory) ChannelSftp(com.jcraft.jsch.ChannelSftp) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) DirectFieldAccessor(org.springframework.beans.DirectFieldAccessor) LsEntry(com.jcraft.jsch.ChannelSftp.LsEntry) Session(org.springframework.integration.file.remote.session.Session) SftpSession(org.springframework.integration.sftp.session.SftpSession) Test(org.junit.Test)

Aggregations

Session (org.springframework.integration.file.remote.session.Session)23 Test (org.junit.Test)22 SessionFactory (org.springframework.integration.file.remote.session.SessionFactory)16 List (java.util.List)13 GenericMessage (org.springframework.messaging.support.GenericMessage)13 ArrayList (java.util.ArrayList)12 MessageBuilder (org.springframework.integration.support.MessageBuilder)12 Matchers.containsString (org.hamcrest.Matchers.containsString)8 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)6 CachingSessionFactory (org.springframework.integration.file.remote.session.CachingSessionFactory)5 ChannelSftp (com.jcraft.jsch.ChannelSftp)4 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)4 DirectFieldAccessor (org.springframework.beans.DirectFieldAccessor)4 LsEntry (com.jcraft.jsch.ChannelSftp.LsEntry)3 JSch (com.jcraft.jsch.JSch)3 File (java.io.File)3 IOException (java.io.IOException)2 DefaultSftpSessionFactory (org.springframework.integration.sftp.session.DefaultSftpSessionFactory)2 SftpSession (org.springframework.integration.sftp.session.SftpSession)2 MessagingException (org.springframework.messaging.MessagingException)2