/** * Test the session is still alive, e.g. when checking out from a pool. * The default implementation simply delegates to {@link #isOpen()}. * @return true if the test is successful. * @since 5.1 */ default boolean test() { return this.isOpen(); }
private <F> Session<F> newSession() throws IOException { @SuppressWarnings("unchecked") Session<F> session = mock(Session.class); doThrow(new IOException("test")).when(session).write(any(InputStream.class), anyString()); when(session.isOpen()).thenReturn(false); return session; }
@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 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()); }
@Test public void testStream() { String dir = "ftpSource/"; this.inboundGetStream.send(new GenericMessage<Object>(dir + " ftpSource1.txt")); Message<?> result = this.output.receive(1000); assertNotNull(result); assertEquals("source1", result.getPayload()); assertEquals("ftpSource/", result.getHeaders().get(FileHeaders.REMOTE_DIRECTORY)); assertEquals(" ftpSource1.txt", result.getHeaders().get(FileHeaders.REMOTE_FILE)); Session<?> session = (Session<?>) result.getHeaders().get(IntegrationMessageHeaderAccessor.CLOSEABLE_RESOURCE); // Returned to cache assertTrue(session.isOpen()); // Raw reading is finished assertFalse(TestUtils.getPropertyValue(session, "targetSession.readingRaw", AtomicBoolean.class).get()); // Check that we can use the same session from cache to read another remote InputStream this.inboundGetStream.send(new GenericMessage<Object>(dir + "ftpSource2.txt")); result = this.output.receive(1000); assertNotNull(result); assertEquals("source2", result.getPayload()); assertEquals("ftpSource/", result.getHeaders().get(FileHeaders.REMOTE_DIRECTORY)); assertEquals("ftpSource2.txt", result.getHeaders().get(FileHeaders.REMOTE_FILE)); assertSame(TestUtils.getPropertyValue(session, "targetSession"), TestUtils.getPropertyValue(result.getHeaders().get(IntegrationMessageHeaderAccessor.CLOSEABLE_RESOURCE), "targetSession")); }