SourcePollingChannelAdapter fileInbound = context.getBean(SourcePollingChannelAdapter.class); when(sessionFactory.getSession()).thenThrow(new RuntimeException("Force Failure")); fileInbound.start();
when(sessionFactory.getSession()).thenReturn(session); fileInbound.start();
private SessionFactory<FTPFile> mockSf() { @SuppressWarnings("unchecked") SessionFactory<FTPFile> mocksf = mock(SessionFactory.class); given(mocksf.getSession()).willReturn(mockSession()); return mocksf; }
@Override public Session<F> getSession() { if (this.activeTemplateCallbacks.get() > 0) { Session<F> session = this.contextSessions.get(); // If no session in the ThreadLocal, no {@code invoke()} in this call stack if (session != null) { return session; } } return this.sessionFactory.getSession(); }
public Session<F> getSession(Object key) { SessionFactory<F> sessionFactory = this.factoryLocator.getSessionFactory(key); Assert.notNull(sessionFactory, "No default SessionFactory configured"); return sessionFactory.getSession(); }
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; }
@SuppressWarnings("unchecked") @Test(expected = IllegalArgumentException.class) public <F> void testEmptyTemporaryFileSuffixCannotBeNull() throws Exception { SessionFactory<F> sf = mock(SessionFactory.class); Session<F> session = mock(Session.class); when(sf.getSession()).thenReturn(session); FileTransferringMessageHandler<F> handler = new FileTransferringMessageHandler<F>(sf); handler.setBeanFactory(mock(BeanFactory.class)); handler.setRemoteDirectoryExpressionString("headers['path']"); handler.setTemporaryFileSuffix(null); handler.onInit(); }
@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 (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")); }
@SuppressWarnings("unchecked") @Before public void setUp() throws Exception { SessionFactory<Object> sessionFactory = mock(SessionFactory.class); this.template = new RemoteFileTemplate<Object>(sessionFactory); this.template.setRemoteDirectoryExpression(new LiteralExpression("/foo")); this.template.setBeanFactory(mock(BeanFactory.class)); this.template.afterPropertiesSet(); this.session = mock(Session.class); when(sessionFactory.getSession()).thenReturn(this.session); this.file = this.folder.newFile(); }
@Test public void testLs_None() throws Exception { SessionFactory sessionFactory = mock(SessionFactory.class); Session session = mock(Session.class); TestRemoteFileOutboundGateway gw = new TestRemoteFileOutboundGateway(sessionFactory, "ls", "payload"); gw.afterPropertiesSet(); when(sessionFactory.getSession()).thenReturn(session); TestLsEntry[] files = new TestLsEntry[0]; when(session.list("testremote/")).thenReturn(files); @SuppressWarnings("unchecked") MessageBuilder<List<TestLsEntry>> out = (MessageBuilder<List<TestLsEntry>>) gw .handleRequestMessage(new GenericMessage<>("testremote")); assertEquals(0, out.getPayload().size()); }
@SuppressWarnings("unchecked") @Test public <F> void testUseTemporaryFileNameFalse() throws Exception { SessionFactory<F> sf = mock(SessionFactory.class); Session<F> session = mock(Session.class); when(sf.getSession()).thenReturn(session); ExpressionParser parser = new SpelExpressionParser(); FileTransferringMessageHandler<F> handler = new FileTransferringMessageHandler<F>(sf); handler.setRemoteDirectoryExpression(parser.parseExpression("headers['path']")); handler.setUseTemporaryFileName(false); handler.setBeanFactory(mock(BeanFactory.class)); handler.afterPropertiesSet(); Message<?> message = MessageBuilder.withPayload("hello").setHeader("path", null).build(); handler.handleMessage(message); verify(session, times(1)).write(Mockito.any(InputStream.class), Mockito.anyString()); verify(session, times(0)).rename(Mockito.anyString(), Mockito.anyString()); }
@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 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)); }
@Test public void testLs_1() throws Exception { SessionFactory sessionFactory = mock(SessionFactory.class); Session session = mock(Session.class); TestRemoteFileOutboundGateway gw = new TestRemoteFileOutboundGateway(sessionFactory, "ls", "payload"); gw.setOptions("-1"); 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("f1", out.getPayload().get(0)); assertEquals("f2", out.getPayload().get(1)); }
@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"))); } }
@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(); }
/** * 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 @SuppressWarnings("unchecked") public void testNlstAndWorkingDirExpression() throws IOException { this.inboundNlst.send(new GenericMessage<>("foo")); Message<?> receive = this.output.receive(10000); assertNotNull(receive); assertThat(receive.getPayload(), instanceOf(List.class)); List<String> files = (List<String>) receive.getPayload(); assertEquals(3, files.size()); assertThat(files, containsInAnyOrder("subFtpSource", " ftpSource1.txt", "ftpSource2.txt")); FTPFile[] ftpFiles = ftpSessionFactory.getSession().list(null); for (FTPFile ftpFile : ftpFiles) { if (!ftpFile.isDirectory()) { assertTrue(files.contains(ftpFile.getName())); } } }