verify(this.session).write(any(InputStream.class), eq("foo/006.txt.writing")); verify(this.session).write(any(InputStream.class), eq("foo/009.txt.writing")); verify(this.session).rename("foo/002.txt.writing", "foo/002.txt"); verify(this.session).rename("foo/006.txt.writing", "foo/006.txt"); verify(this.session).rename("foo/009.txt.writing", "foo/009.txt");
@Override public void rename(final String fromPath, final String toPath) { Assert.hasText(fromPath, "Old filename cannot be null or empty"); Assert.hasText(toPath, "New filename cannot be null or empty"); this.execute((SessionCallbackWithoutResult<F>) session -> { int lastSeparator = toPath.lastIndexOf(RemoteFileTemplate.this.remoteFileSeparator); if (lastSeparator > 0) { String remoteFileDirectory = toPath.substring(0, lastSeparator + 1); RemoteFileUtils.makeDirectories(remoteFileDirectory, session, RemoteFileTemplate.this.remoteFileSeparator, RemoteFileTemplate.this.logger); } session.rename(fromPath, toPath); }); }
/** * Move one remote path to another. * The message can be consulted to determine some context; * isn't used in the default implementation. * @param message the request message related to this move command * @param session the remote protocol session to perform move command * @param remoteFilePath the source remote path * @param remoteFileNewPath the target remote path * @return true or false as a result of the operation * @throws IOException the IO exception during performing remote command * @since 5.0 */ protected boolean mv(Message<?> message, Session<F> session, String remoteFilePath, String remoteFileNewPath) throws IOException { int lastSeparator = remoteFileNewPath.lastIndexOf(this.remoteFileTemplate.getRemoteFileSeparator()); if (lastSeparator > 0) { String remoteFileDirectory = remoteFileNewPath.substring(0, lastSeparator + 1); RemoteFileUtils.makeDirectories(remoteFileDirectory, session, this.remoteFileTemplate.getRemoteFileSeparator(), this.logger); } session.rename(remoteFilePath, remoteFileNewPath); return true; }
@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)); }
@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()); }
@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()); }
@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()); }
@Test public void testMoveWithExpression() throws Exception { SessionFactory sessionFactory = mock(SessionFactory.class); TestRemoteFileOutboundGateway gw = new TestRemoteFileOutboundGateway(sessionFactory, "mv", "payload"); gw.setRenameExpression(PARSER.parseExpression("payload.substring(1)")); 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] + arguments[1]); return null; }).when(session).rename(anyString(), anyString()); when(sessionFactory.getSession()).thenReturn(session); MessageBuilder<?> out = (MessageBuilder<?>) gw.handleRequestMessage(new GenericMessage<>("foo")); assertEquals("oo", out.getHeaders().get(FileHeaders.RENAME_TO)); assertEquals("foo", out.getHeaders().get(FileHeaders.REMOTE_FILE)); assertEquals("foooo", args.get()); assertEquals(Boolean.TRUE, out.getPayload()); }
@Test public void testMoveWithMkDirs() throws Exception { SessionFactory sessionFactory = mock(SessionFactory.class); TestRemoteFileOutboundGateway gw = new TestRemoteFileOutboundGateway(sessionFactory, "mv", "payload"); gw.setRenameExpression(PARSER.parseExpression("'foo/bar/baz'")); 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()); final List<String> madeDirs = new ArrayList<String>(); doAnswer(invocation -> { madeDirs.add(invocation.getArgument(0)); return null; }).when(session).mkdir(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("foofoo/bar/baz", args.get()); assertEquals(Boolean.TRUE, out.getPayload()); assertEquals(2, madeDirs.size()); assertEquals("foo", madeDirs.get(0)); assertEquals("foo/bar", madeDirs.get(1)); }
assertThat(out.get(1), equalTo("foo/buz.txt")); assertThat(written.get(), equalTo("foo/buz.txt.writing")); verify(session).rename("foo/buz.txt.writing", "foo/buz.txt");
verify(session).write(any(InputStream.class), captor.capture()); assertEquals("foo/bar.txt.writing", captor.getValue()); verify(session).rename("foo/bar.txt.writing", "foo/bar.txt");
verify(session).write(any(InputStream.class), captor.capture()); assertEquals("foo/bar.txt.writing", captor.getValue()); verify(session).rename("foo/bar.txt.writing", "foo/bar.txt"); verify(session, times(2)).write(any(InputStream.class), captor.capture()); assertEquals("foo/bar.txt.writing", captor.getValue()); verify(session, times(2)).rename("foo/bar.txt.writing", "foo/bar.txt");
session.rename(tempFilePath, remoteFilePath);