public static SessionFactory<FTPFile> sessionFactory() { return new CachingSessionFactory<FTPFile>(rawSessionFactory()); }
@Test (expected = PoolItemNotAvailableException.class) // timeout expire public void testSessionWaitExpire() throws Exception { SessionFactory sessionFactory = Mockito.mock(SessionFactory.class); Session session = Mockito.mock(Session.class); Mockito.when(sessionFactory.getSession()).thenReturn(session); CachingSessionFactory cachingFactory = new CachingSessionFactory(sessionFactory, 2); cachingFactory.setSessionWaitTimeout(3000); cachingFactory.getSession(); cachingFactory.getSession(); cachingFactory.getSession(); }
@Test public void testStaleConnection() throws Exception { SessionFactory sessionFactory = Mockito.mock(SessionFactory.class); Session sessionA = Mockito.mock(Session.class); Session sessionB = Mockito.mock(Session.class); Mockito.when(sessionA.isOpen()).thenReturn(true); Mockito.when(sessionB.isOpen()).thenReturn(false); Mockito.when(sessionFactory.getSession()).thenReturn(sessionA); Mockito.when(sessionFactory.getSession()).thenReturn(sessionB); CachingSessionFactory cachingFactory = new CachingSessionFactory(sessionFactory, 2); Session firstSession = cachingFactory.getSession(); Session secondSession = cachingFactory.getSession(); secondSession.close(); Session nonStaleSession = cachingFactory.getSession(); assertEquals(TestUtils.getPropertyValue(firstSession, "targetSession"), TestUtils.getPropertyValue(nonStaleSession, "targetSession")); }
@Test public void testSameSessionFromThePool() throws Exception { SessionFactory sessionFactory = Mockito.mock(SessionFactory.class); Session session = Mockito.mock(Session.class); Mockito.when(sessionFactory.getSession()).thenReturn(session); CachingSessionFactory cachingFactory = new CachingSessionFactory(sessionFactory, 2); Session s1 = cachingFactory.getSession(); s1.close(); Session s2 = cachingFactory.getSession(); s2.close(); assertEquals(TestUtils.getPropertyValue(s1, "targetSession"), TestUtils.getPropertyValue(s2, "targetSession")); Mockito.verify(sessionFactory, Mockito.times(2)).getSession(); }
sessionFactory.setPassword("password"); sessionFactory.setUsername("user"); final CachingSessionFactory factory = new CachingSessionFactory(sessionFactory, 2);
@Test public void testDirtySession() throws Exception { @SuppressWarnings("unchecked") SessionFactory<Object> factory = mock(SessionFactory.class); @SuppressWarnings("unchecked") Session<Object> session = mock(Session.class); when(factory.getSession()).thenReturn(session); when(session.readRaw("foo")).thenReturn(new ByteArrayInputStream("".getBytes())); when(session.finalizeRaw()).thenReturn(true); CachingSessionFactory<Object> ccf = new CachingSessionFactory<Object>(factory); RemoteFileTemplate<Object> template = new RemoteFileTemplate<Object>(ccf); template.setFileNameExpression(new LiteralExpression("foo")); template.setBeanFactory(mock(BeanFactory.class)); template.afterPropertiesSet(); try { template.get(new GenericMessage<String>("foo"), (InputStreamCallback) stream -> { throw new RuntimeException("bar"); }); fail("Expected exception"); } catch (Exception e) { assertThat(e.getCause(), instanceOf(RuntimeException.class)); assertThat(e.getCause().getMessage(), equalTo("bar")); } verify(session).close(); }
@SuppressWarnings("unchecked") @Test public <F> void testServerException() throws Exception { SessionFactory<F> sf = mock(SessionFactory.class); CachingSessionFactory<F> csf = new CachingSessionFactory<F>(sf, 2); FileTransferringMessageHandler<F> handler = new FileTransferringMessageHandler<F>(csf); handler.setBeanFactory(mock(BeanFactory.class)); Session<F> session1 = newSession(); Session<F> session2 = newSession(); Session<F> session3 = newSession(); when(sf.getSession()).thenReturn(session1, session2, session3); handler.setRemoteDirectoryExpressionString("'foo'"); handler.afterPropertiesSet(); for (int i = 0; i < 3; i++) { try { handler.handleMessage(new GenericMessage<String>("hello")); } catch (Exception e) { assertEquals("test", e.getCause().getCause().getMessage()); } } verify(session1, times(1)).write(Mockito.any(InputStream.class), Mockito.anyString()); verify(session2, times(1)).write(Mockito.any(InputStream.class), Mockito.anyString()); verify(session3, times(1)).write(Mockito.any(InputStream.class), Mockito.anyString()); SimplePool<?> pool = TestUtils.getPropertyValue(csf, "pool", SimplePool.class); assertEquals(1, pool.getAllocatedCount()); assertEquals(1, pool.getIdleCount()); assertSame(session3, TestUtils.getPropertyValue(pool, "allocated", Set.class).iterator().next()); }
public static SessionFactory<LsEntry> sessionFactory() { DefaultSftpSessionFactory factory = new DefaultSftpSessionFactory(true); factory.setHost("localhost"); factory.setPort(port); factory.setUser("foo"); factory.setPassword("foo"); factory.setAllowUnknownKeys(true); return new CachingSessionFactory<>(factory); }
factory.setUser("foo"); factory.setPassword("bar"); CachingSessionFactory<LsEntry> cachedFactory = new CachingSessionFactory<LsEntry>(factory); noopConnect(channel1); noopConnect(channel2);
@Bean @ConditionalOnMissingBean public SessionFactory<FTPFile> ftpSessionFactory(FtpSessionFactoryProperties properties) { DefaultFtpSessionFactory ftpSessionFactory = new DefaultFtpSessionFactory(); ftpSessionFactory.setHost(properties.getHost()); ftpSessionFactory.setPort(properties.getPort()); ftpSessionFactory.setUsername(properties.getUsername()); ftpSessionFactory.setPassword(properties.getPassword()); ftpSessionFactory.setClientMode(properties.getClientMode().getMode()); if (properties.getCacheSessions() != null) { CachingSessionFactory<FTPFile> csf = new CachingSessionFactory<>(ftpSessionFactory); return csf; } else { return ftpSessionFactory; } }
@Test public void testCacheAndReset() { TestSessionFactory factory = new TestSessionFactory(); CachingSessionFactory<String> cache = new CachingSessionFactory<String>(factory); cache.setTestSession(true); Session<String> sess1 = cache.getSession(); assertEquals("session:1", TestUtils.getPropertyValue(sess1, "targetSession.id")); Session<String> sess2 = cache.getSession(); assertEquals("session:2", TestUtils.getPropertyValue(sess2, "targetSession.id")); sess1.close(); // session back to pool; should be open and reused. assertTrue(sess1.isOpen()); sess1 = cache.getSession(); assertEquals("session:1", TestUtils.getPropertyValue(sess1, "targetSession.id")); assertTrue((TestUtils.getPropertyValue(sess1, "targetSession.testCalled", Boolean.class))); sess1.close(); assertTrue(sess1.isOpen()); // reset the cache; should close idle (sess1); sess2 should closed later cache.resetCache(); assertFalse(sess1.isOpen()); sess1 = cache.getSession(); assertEquals("session:3", TestUtils.getPropertyValue(sess1, "targetSession.id")); sess1.close(); assertTrue(sess1.isOpen()); // session from previous epoch is closed on return sess2.close(); assertFalse(sess2.isOpen()); cache.resetCache(); assertFalse(sess1.isOpen()); }
@Bean @ConditionalOnMissingBean public SessionFactory<FTPFile> ftpSessionFactory(FtpSessionFactoryProperties properties) { DefaultFtpSessionFactory ftpSessionFactory = new DefaultFtpSessionFactory(); ftpSessionFactory.setHost(properties.getHost()); ftpSessionFactory.setPort(properties.getPort()); ftpSessionFactory.setUsername(properties.getUsername()); ftpSessionFactory.setPassword(properties.getPassword()); ftpSessionFactory.setClientMode(properties.getClientMode().getMode()); if (properties.getCacheSessions() != null) { CachingSessionFactory<FTPFile> csf = new CachingSessionFactory<>(ftpSessionFactory); return csf; } else { return ftpSessionFactory; } }
@Bean @ConditionalOnMissingBean public SessionFactory<LsEntry> sftpSessionFactory(SftpSessionFactoryProperties properties) { DefaultSftpSessionFactory sftpSessionFactory = new DefaultSftpSessionFactory(); sftpSessionFactory.setHost(properties.getHost()); sftpSessionFactory.setPort(properties.getPort()); sftpSessionFactory.setUser(properties.getUsername()); sftpSessionFactory.setPassword(properties.getPassword()); sftpSessionFactory.setAllowUnknownKeys(properties.isAllowUnknownKeys()); if (properties.getKnownHostsExpression() != null) { sftpSessionFactory.setKnownHosts("#{" + properties.getKnownHostsExpression() + "}"); } if (properties.getCacheSessions() != null) { CachingSessionFactory<LsEntry> csf = new CachingSessionFactory<>(sftpSessionFactory); return csf; } else { return sftpSessionFactory; } }
@Bean @ConditionalOnMissingBean public SessionFactory<LsEntry> sftpSessionFactory(SftpSessionFactoryProperties properties) { DefaultSftpSessionFactory sftpSessionFactory = new DefaultSftpSessionFactory(); sftpSessionFactory.setHost(properties.getHost()); sftpSessionFactory.setPort(properties.getPort()); sftpSessionFactory.setUser(properties.getUsername()); sftpSessionFactory.setPassword(properties.getPassword()); sftpSessionFactory.setAllowUnknownKeys(properties.isAllowUnknownKeys()); if (properties.getKnownHostsExpression() != null) { sftpSessionFactory.setKnownHosts("#{" + properties.getKnownHostsExpression() + "}"); } if (properties.getCacheSessions() != null) { CachingSessionFactory<LsEntry> csf = new CachingSessionFactory<>(sftpSessionFactory); return csf; } else { return sftpSessionFactory; } }
@Bean @ConditionalOnMissingBean public SessionFactory<LsEntry> sftpSessionFactory(SftpSessionFactoryProperties properties) { DefaultSftpSessionFactory sftpSessionFactory = new DefaultSftpSessionFactory(); sftpSessionFactory.setHost(properties.getHost()); sftpSessionFactory.setPort(properties.getPort()); sftpSessionFactory.setUser(properties.getUsername()); sftpSessionFactory.setPassword(properties.getPassword()); sftpSessionFactory.setAllowUnknownKeys(properties.isAllowUnknownKeys()); if (properties.getKnownHostsExpression() != null) { sftpSessionFactory.setKnownHosts("#{" + properties.getKnownHostsExpression() + "}"); } if (properties.getCacheSessions() != null) { CachingSessionFactory<LsEntry> csf = new CachingSessionFactory<>(sftpSessionFactory); return csf; } else { return sftpSessionFactory; } }
@Bean @ConditionalOnMissingBean public SessionFactory<LsEntry> sftpSessionFactory(SftpSinkProperties properties, BeanFactory beanFactory) { DefaultSftpSessionFactory sftpSessionFactory = new DefaultSftpSessionFactory(); sftpSessionFactory.setHost(properties.getFactory().getHost()); sftpSessionFactory.setPort(properties.getFactory().getPort()); sftpSessionFactory.setUser(properties.getFactory().getUsername()); sftpSessionFactory.setPassword(properties.getFactory().getPassword()); sftpSessionFactory.setAllowUnknownKeys(properties.getFactory().isAllowUnknownKeys()); if (properties.getFactory().getKnownHostsExpression() != null) { sftpSessionFactory.setKnownHosts(properties.getFactory().getKnownHostsExpression() .getValue(IntegrationContextUtils.getEvaluationContext(beanFactory), String.class)); } if (properties.getFactory().getCacheSessions() != null) { CachingSessionFactory<LsEntry> csf = new CachingSessionFactory<>(sftpSessionFactory); return csf; } else { return sftpSessionFactory; } }