### Question:
UploadProcessor implements FileTransferProcessor { @Override public void run(final ChannelSftp channel) { final EtmPoint point = MONITOR.createPoint(getClass().getName() + ": run(ChannelSftp)"); try { final File sourceFile = new File(request.getSourceFile()); final String remotePath = request.getTargetFile().getPath(); try { channel.put(sourceFile.getAbsolutePath(), remotePath); } catch (final SftpException e) { throw new TransferException("Failed to upload: " + sourceFile.getAbsolutePath(), e); } try { channel.chmod(permissions, remotePath); } catch (final SftpException e) { if (!ignoreChmodErrors) { throw new TransferException("Failed to chmod uploaded file.", e); } } } finally { point.collect(); } } UploadProcessor(final FileRequest request, final boolean ignoreChmodErrors, final int permissions); @Override void run(final ChannelSftp channel);  }### Answer:
@Test public void testRun() throws Exception { final File targetFile = new File("target/unit/upload.test"); final ChannelSftp mockChannel = mock(ChannelSftp.class); final FileRequest mockRequest = mock(FileRequest.class); when(mockRequest.getSourceFile()).thenReturn(new File("target/unit/dummy.src").toURI()); when(mockRequest.getTargetFile()).thenReturn(targetFile.toURI()); final UploadProcessor processor = new UploadProcessor(mockRequest, false, Uploader.GOD_PERMISSIONS); processor.run(mockChannel); verify(mockChannel, times(1)).put(new File(mockRequest.getSourceFile()).getAbsolutePath(), mockRequest.getTargetFile().getPath()); }
@Test(expected = TransferException.class) public void testPutFailure() { final Answer<Object> exceptionThrower = new Answer<Object>() { @Override public Object answer(final InvocationOnMock invocation) throws Throwable { throw mock(SftpException.class); } }; final ChannelSftp mockChannel = mock(ChannelSftp.class, exceptionThrower); final FileRequest mockRequest = mock(FileRequest.class); when(mockRequest.getSourceFile()).thenReturn(new File("target/unit/dummy.src").toURI()); when(mockRequest.getTargetFile()).thenReturn(new File("target/unit/dummy.out").toURI()); new UploadProcessor(mockRequest, false, Uploader.GOD_PERMISSIONS).run(mockChannel); }
@Test public void testSwallowsChmodException() throws Exception { final URI targetFile = new File("target/unit/dummy.out").toURI(); final FileRequest mockRequest = mock(FileRequest.class); when(mockRequest.getSourceFile()).thenReturn(new File("target/unit/dummy.src").toURI()); when(mockRequest.getTargetFile()).thenReturn(targetFile); final ChannelSftp channel = mock(ChannelSftp.class); doThrow(new SftpException(1, "I am an exception")).when(channel).chmod(Uploader.GOD_PERMISSIONS, targetFile.getPath()); new UploadProcessor(mockRequest, true, Uploader.GOD_PERMISSIONS).run(channel); }
@Test(expected = TransferException.class) public void testPassesChmodException() throws Exception { final URI targetFile = new File("target/unit/dummy.out").toURI(); final FileRequest mockRequest = mock(FileRequest.class); when(mockRequest.getSourceFile()).thenReturn(new File("target/unit/dummy.src").toURI()); when(mockRequest.getTargetFile()).thenReturn(targetFile); final ChannelSftp channel = mock(ChannelSftp.class); doThrow(new SftpException(1, "I am an exception")).when(channel).chmod(Uploader.GOD_PERMISSIONS, targetFile.getPath()); new UploadProcessor(mockRequest, false, Uploader.GOD_PERMISSIONS).run(channel); }