@Override protected InputStream fetchSource() { return source.getInputStream(); }
@Override public File getFile() { inputStream = inputStreamSource.getInputStream(); try { FileOutputStream fileOutputStream = new FileOutputStream(tempStorage); fileOutputStream.getChannel().lock(); try { ByteStreams.copy(inputStream, fileOutputStream); return tempStorage; } finally { // Note: This will implicitly release the file lock. Closeables.close(fileOutputStream, true); } } catch (IOException e) { throw new FileSystemInteractionException(String.format("Could not write stream to file %s", tempStorage), e); } }
@Test(timeout = DEFAULT_CONVERSION_TIMEOUT) public void testInputStreamSourceToInputStreamConsumerFuture() throws Exception { File source = validFile(true); InputStream inputStream = spy(new FileInputStream(source)); IInputStreamSource inputStreamSource = mock(IInputStreamSource.class); when(inputStreamSource.getInputStream()).thenReturn(inputStream); OutputStream outputStream = mock(OutputStream.class); IInputStreamConsumer inputStreamConsumer = mock(IInputStreamConsumer.class); doAnswer(new CloseStreamAnswer()).when(inputStreamConsumer).onComplete(any(InputStream.class)); assertTrue(getConverter().convert(inputStreamSource).as(validInputType()).to(inputStreamConsumer).as(validTargetType()).schedule().get()); assertTrue(source.exists()); verify(inputStreamSource, times(1)).getInputStream(); verify(inputStreamSource, times(1)).onConsumed(any(InputStream.class)); verify(inputStream, never()).close(); inputStream.close(); verify(inputStreamConsumer, times(1)).onComplete(any(InputStream.class)); verifyNoMoreInteractions(inputStreamConsumer); verify(outputStream, never()).close(); }
@Test(timeout = DEFAULT_CONVERSION_TIMEOUT) public void testInputStreamSourceToInputStreamConsumerExecute() throws Exception { File source = validFile(true); InputStream inputStream = spy(new FileInputStream(source)); IInputStreamSource inputStreamSource = mock(IInputStreamSource.class); when(inputStreamSource.getInputStream()).thenReturn(inputStream); OutputStream outputStream = mock(OutputStream.class); IInputStreamConsumer inputStreamConsumer = mock(IInputStreamConsumer.class); doAnswer(new CloseStreamAnswer()).when(inputStreamConsumer).onComplete(any(InputStream.class)); assertTrue(getConverter().convert(inputStreamSource).as(validInputType()).to(inputStreamConsumer).as(validTargetType()).execute()); assertTrue(source.exists()); verify(inputStreamSource, times(1)).getInputStream(); verify(inputStreamSource, times(1)).onConsumed(any(InputStream.class)); verify(inputStream, never()).close(); inputStream.close(); verify(inputStreamConsumer, times(1)).onComplete(any(InputStream.class)); verifyNoMoreInteractions(inputStreamConsumer); verify(outputStream, never()).close(); }