@Override public <T> T invoke(OperationsCallback<F, T> action) { Session<F> contextSession = this.contextSessions.get(); if (contextSession == null) { this.contextSessions.set(this.sessionFactory.getSession()); } this.activeTemplateCallbacks.incrementAndGet(); try { return action.doInOperations(this); } finally { this.activeTemplateCallbacks.decrementAndGet(); if (contextSession == null) { Session<F> session = this.contextSessions.get(); if (session != null) { session.close(); } this.contextSessions.remove(); } } }
if (!invokeScope && session != null) { try { session.close();
private Session<FTPFile> spyOnSession() { Session<FTPFile> session = spy(this.ftpSessionFactory.getSession()); session.close(); @SuppressWarnings("unchecked") BlockingQueue<Session<FTPFile>> cache = TestUtils.getPropertyValue(ftpSessionFactory, "pool.available", BlockingQueue.class); assertNotNull(cache.poll()); cache.offer(session); @SuppressWarnings("unchecked") Set<Session<FTPFile>> allocated = TestUtils.getPropertyValue(ftpSessionFactory, "pool.allocated", Set.class); allocated.clear(); allocated.add(session); return session; }
@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(); }
@Test public void testStream() { Session<?> session = spy(this.sessionFactory.getSession()); session.close(); String dir = "sftpSource/"; this.inboundGetStream.send(new GenericMessage<Object>(dir + " sftpSource1.txt")); Message<?> result = this.output.receive(1000); assertNotNull(result); assertEquals("source1", result.getPayload()); assertEquals("sftpSource/", result.getHeaders().get(FileHeaders.REMOTE_DIRECTORY)); assertEquals(" sftpSource1.txt", result.getHeaders().get(FileHeaders.REMOTE_FILE)); verify(session).close(); }
@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")); }
Session session = factory.getSession(); Thread.sleep(random.nextInt(5000)); session.close();
@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(); }
protected void doTest(SshServer server, Session<LsEntry> session) throws IOException { assertEquals(1, server.getActiveSessions().size()); LsEntry[] list = session.list("."); if (list.length > 0) { session.remove("*"); } session.write(new ByteArrayInputStream("foo".getBytes()), "bar"); list = session.list("."); assertEquals("bar", list[1].getFilename()); ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); session.read("bar", outputStream); assertEquals("foo", new String(outputStream.toByteArray())); session.remove("bar"); session.close(); }
@Test public void testInt3100RawGET() throws Exception { Session<?> session = this.ftpSessionFactory.getSession(); ByteArrayOutputStream baos = new ByteArrayOutputStream(); FileCopyUtils.copy(session.readRaw("ftpSource/ ftpSource1.txt"), baos); assertTrue(session.finalizeRaw()); assertEquals("source1", new String(baos.toByteArray())); baos = new ByteArrayOutputStream(); FileCopyUtils.copy(session.readRaw("ftpSource/ftpSource2.txt"), baos); assertTrue(session.finalizeRaw()); assertEquals("source2", new String(baos.toByteArray())); session.close(); }
@Test @SuppressWarnings("unchecked") public void testMGETOnNullDir() throws IOException { Session<FTPFile> session = ftpSessionFactory.getSession(); ((FTPClient) session.getClientInstance()).changeWorkingDirectory("ftpSource"); session.close(); this.inboundMGet.send(new GenericMessage<Object>("")); Message<?> result = this.output.receive(1000); assertNotNull(result); List<File> localFiles = (List<File>) result.getPayload(); assertThat(localFiles.size(), Matchers.greaterThan(0)); for (File file : localFiles) { assertThat(file.getName(), isOneOf(" localTarget1.txt", "localTarget2.txt")); assertThat(file.getName(), not(containsString("null"))); } }
/** * Only runs with a real server (see class javadocs). */ @Test public void testInt3100RawGET() throws Exception { Session<?> session = this.sessionFactory.getSession(); ByteArrayOutputStream baos = new ByteArrayOutputStream(); FileCopyUtils.copy(session.readRaw("sftpSource/ sftpSource1.txt"), baos); assertTrue(session.finalizeRaw()); assertEquals("source1", new String(baos.toByteArray())); baos = new ByteArrayOutputStream(); FileCopyUtils.copy(session.readRaw("sftpSource/sftpSource2.txt"), baos); assertTrue(session.finalizeRaw()); assertEquals("source2", new String(baos.toByteArray())); session.close(); }
@Test public void testInt3088MPutNotRecursive() throws Exception { Session<?> session = sessionFactory.getSession(); session.close(); session = TestUtils.getPropertyValue(session, "targetSession", Session.class); ChannelSftp channel = spy(TestUtils.getPropertyValue(session, "channel", ChannelSftp.class)); new DirectFieldAccessor(session).setPropertyValue("channel", channel); String dir = "sftpSource/"; this.inboundMGetRecursive.send(new GenericMessage<Object>(dir + "*")); while (output.receive(0) != null) { // drain } this.inboundMPut.send(new GenericMessage<File>(getSourceLocalDirectory())); @SuppressWarnings("unchecked") Message<List<String>> out = (Message<List<String>>) this.output.receive(1000); assertNotNull(out); assertEquals(2, out.getPayload().size()); assertThat(out.getPayload().get(0), not(equalTo(out.getPayload().get(1)))); assertThat( out.getPayload().get(0), anyOf(equalTo("sftpTarget/localSource1.txt"), equalTo("sftpTarget/localSource2.txt"))); assertThat( out.getPayload().get(1), anyOf(equalTo("sftpTarget/localSource1.txt"), equalTo("sftpTarget/localSource2.txt"))); verify(channel).chmod(384, "sftpTarget/localSource1.txt"); // 384 = 600 octal verify(channel).chmod(384, "sftpTarget/localSource2.txt"); }
@Test public void testInboundChannelAdapterWithNullDir() throws IOException { Session<FTPFile> session = ftpSessionFactory.getSession(); ((FTPClient) session.getClientInstance()).changeWorkingDirectory("ftpSource"); session.close(); this.ftpInbound.start(); Message<?> message = this.output.receive(10000); assertNotNull(message); assertThat(message.getPayload(), instanceOf(File.class)); assertEquals(" ftpSource1.txt", ((File) message.getPayload()).getName()); message = this.output.receive(10000); assertNotNull(message); assertThat(message.getPayload(), instanceOf(File.class)); assertEquals("ftpSource2.txt", ((File) message.getPayload()).getName()); assertNull(this.output.receive(10)); this.ftpInbound.stop(); }
@Test @SuppressWarnings("unchecked") public void testLsForNullDir() throws IOException { Session<FTPFile> session = ftpSessionFactory.getSession(); ((FTPClient) session.getClientInstance()).changeWorkingDirectory("ftpSource"); session.close(); this.inboundLs.send(new GenericMessage<String>("foo")); Message<?> receive = this.output.receive(10000); assertNotNull(receive); assertThat(receive.getPayload(), instanceOf(List.class)); List<String> files = (List<String>) receive.getPayload(); assertEquals(2, files.size()); assertThat(files, containsInAnyOrder(" ftpSource1.txt", "ftpSource2.txt")); FTPFile[] ftpFiles = ftpSessionFactory.getSession().list(null); for (FTPFile ftpFile : ftpFiles) { if (!ftpFile.isDirectory()) { assertTrue(files.contains(ftpFile.getName())); } } }
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(); assertNotSame(TestUtils.getPropertyValue(s1, "targetSession"), TestUtils.getPropertyValue(s3, "targetSession")); assertSame(channel3, TestUtils.getPropertyValue(s3, "targetSession.channel")); s2.close(); verify(jschSession1).disconnect(); s2 = cachedFactory.getSession(); 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();
@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()); }