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(); }
@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(); } } }
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(); }
@SuppressWarnings("unchecked") @Test public <F> void testRemoteDirWithEmptyString() throws Exception { SessionFactory<F> sf = mock(SessionFactory.class); Session<F> session = mock(Session.class); when(sf.getSession()).thenReturn(session); doAnswer(invocation -> { String path = invocation.getArgument(1); assertFalse(path.startsWith("/")); return null; }).when(session).rename(Mockito.anyString(), Mockito.anyString()); ExpressionParser parser = new SpelExpressionParser(); FileTransferringMessageHandler<F> handler = new FileTransferringMessageHandler<F>(sf); handler.setRemoteDirectoryExpression(parser.parseExpression("''")); handler.setBeanFactory(mock(BeanFactory.class)); handler.afterPropertiesSet(); handler.handleMessage(new GenericMessage<String>("hello")); verify(session, times(1)).write(Mockito.any(InputStream.class), Mockito.anyString()); }
@Test public void testFileCloseOnBadConnect() throws Exception { @SuppressWarnings("unchecked") SessionFactory<FTPFile> sessionFactory = mock(SessionFactory.class); when(sessionFactory.getSession()).thenThrow(new RuntimeException("bar")); FtpRemoteFileTemplate template = new FtpRemoteFileTemplate(sessionFactory); template.setRemoteDirectoryExpression(new LiteralExpression("foo")); template.afterPropertiesSet(); File file = new File(System.getProperty("java.io.tmpdir"), UUID.randomUUID().toString()); FileOutputStream fileOutputStream = new FileOutputStream(file); fileOutputStream.write("foo".getBytes()); fileOutputStream.close(); try { template.send(new GenericMessage<>(file)); fail("exception expected"); } catch (MessagingException e) { assertEquals("bar", e.getCause().getMessage()); } File newFile = new File(System.getProperty("java.io.tmpdir"), UUID.randomUUID().toString()); assertTrue(file.renameTo(newFile)); file.delete(); newFile.delete(); }
@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()); }
@Test(expected = MessagingException.class) public void testMGetEmpty() throws Exception { SessionFactory sessionFactory = mock(SessionFactory.class); TestRemoteFileOutboundGateway gw = new TestRemoteFileOutboundGateway(sessionFactory, "mget", "payload"); gw.setLocalDirectory(new File(this.tmpDir)); gw.setOptions(" -x "); gw.afterPropertiesSet(); new File(this.tmpDir + "/f1").delete(); new File(this.tmpDir + "/f2").delete(); when(sessionFactory.getSession()).thenReturn(new TestSession() { @Override public void read(String source, OutputStream outputStream) throws IOException { outputStream.write("testData".getBytes()); } }); gw.handleRequestMessage(new GenericMessage<String>("testremote/*")); }
@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()); }
@SuppressWarnings("unchecked") @Test public <F> void testRemoteDirWithNull() throws Exception { SessionFactory<F> sf = mock(SessionFactory.class); Session<F> session = mock(Session.class); when(sf.getSession()).thenReturn(session); doAnswer(invocation -> { String path = invocation.getArgument(1); assertFalse(path.startsWith("/")); return null; }).when(session).rename(Mockito.anyString(), Mockito.anyString()); ExpressionParser parser = new SpelExpressionParser(); FileTransferringMessageHandler<F> handler = new FileTransferringMessageHandler<F>(sf); handler.setRemoteDirectoryExpression(parser.parseExpression("headers['path']")); 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()); }
@SuppressWarnings("unchecked") @Test public <F> void testTemporaryRemoteDir() throws Exception { SessionFactory<F> sf = mock(SessionFactory.class); Session<F> session = mock(Session.class); final AtomicReference<String> temporaryPath = new AtomicReference<String>(); final AtomicReference<String> finalPath = new AtomicReference<String>(); when(sf.getSession()).thenReturn(session); doAnswer(invocation -> { temporaryPath.set(invocation.getArgument(0)); finalPath.set(invocation.getArgument(1)); return null; }).when(session).rename(Mockito.anyString(), Mockito.anyString()); FileTransferringMessageHandler<F> handler = new FileTransferringMessageHandler<F>(sf); handler.setRemoteDirectoryExpression(new LiteralExpression("foo")); handler.setTemporaryRemoteDirectoryExpression(new LiteralExpression("bar")); handler.setBeanFactory(mock(BeanFactory.class)); handler.afterPropertiesSet(); handler.handleMessage(new GenericMessage<String>("hello")); verify(session, times(1)).write(Mockito.any(InputStream.class), Mockito.anyString()); assertEquals("bar", temporaryPath.get().substring(0, 3)); assertEquals("foo", finalPath.get().substring(0, 3)); }
@Test public void testMove() throws Exception { SessionFactory sessionFactory = mock(SessionFactory.class); TestRemoteFileOutboundGateway gw = new TestRemoteFileOutboundGateway(sessionFactory, "mv", "payload"); gw.afterPropertiesSet(); Session<?> session = mock(Session.class); final AtomicReference<String> args = new AtomicReference<String>(); doAnswer(invocation -> { Object[] arguments = invocation.getArguments(); args.set((String) arguments[0] + (String) arguments[1]); return null; }).when(session).rename(anyString(), anyString()); when(sessionFactory.getSession()).thenReturn(session); Message<String> requestMessage = MessageBuilder.withPayload("foo") .setHeader(FileHeaders.RENAME_TO, "bar") .build(); MessageBuilder<?> out = (MessageBuilder<?>) gw.handleRequestMessage(requestMessage); assertEquals("foo", out.getHeaders().get(FileHeaders.REMOTE_FILE)); assertEquals("foobar", args.get()); assertEquals(Boolean.TRUE, out.getPayload()); }