@Override public void onCancel() { inputStreamConsumer.onCancel(); }
@Override public void onComplete(InputStream inputStream) { inputStreamConsumer.onComplete(inputStream); }
@Override public void onException(ConversionFormatException exception) { callback.onException(exception); } }
switch (this) { case OK: callback.onComplete(new ByteArrayInputStream(asReply(message).getBytes(Charsets.UTF_8))); return MockResult.indicating(true); case CANCEL: callback.onCancel(); return MockResult.forCancellation(); case CONVERTER_ERROR: { Exception exception = new ConverterAccessException(asReply(message)); callback.onException(exception); return MockResult.indicating(exception); callback.onException(exception); return MockResult.indicating(exception); callback.onException(exception); return MockResult.indicating(exception); callback.onException(exception); return MockResult.indicating(exception); callback.onException(exception); return MockResult.indicating(exception);
@Override public void onException(Exception e) { try { reportException(e); } finally { inputStreamConsumer.onException(e); } } }
@Override protected void onConversionFinished(RemoteConversionContext conversionContext) throws Exception { Response response = conversionContext.getWebResponse().get(); // We do not need to check the status again, this callback will only be triggered on a successful conversion. try { consumer.onComplete(response.readEntity(InputStream.class)); } finally { response.close(); } }
@Override protected void onConversionCancelled() { consumer.onCancel(); }
@Override protected void onConversionFailed(RuntimeException e) { consumer.onException(e); }
@Override public void onComplete(File file) { try { inputStreamConsumer.onComplete(new DeleteFileOnCloseInputStream(file)); } catch (IOException e) { throw new FileSystemInteractionException(String.format("Could not process file: %s", file), e); } }
@Override public void onCancel(File file) { try { inputStreamConsumer.onCancel(); } finally { tryDelete(file); } }
@Override public void onException(File file, Exception e) { try { inputStreamConsumer.onException(e); } finally { tryDelete(file); } }
@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(); }
@Test(timeout = DEFAULT_CONVERSION_TIMEOUT, expected = ConverterAccessException.class) public void testInputStreamToInputStreamConsumerFuture() throws Exception { InputStream inputStream = spy(new FileInputStream(validFile(true))); IInputStreamConsumer inputStreamConsumer = mock(IInputStreamConsumer.class); try { getConverter() .convert(inputStream).as(validInputType()) .to(inputStreamConsumer).as(validTargetType()) .schedule().get(); fail(); } catch (ExecutionException e) { verify(inputStreamConsumer).onException(any(ConverterAccessException.class)); verifyNoMoreInteractions(inputStreamConsumer); verify(inputStream).close(); assertPostConverterState(); throw (Exception) e.getCause(); } }
@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, expected = ConverterAccessException.class) public void testInputStreamToInputStreamConsumerExecute() throws Exception { InputStream inputStream = spy(new FileInputStream(validFile(true))); IInputStreamConsumer inputStreamConsumer = mock(IInputStreamConsumer.class); try { getConverter() .convert(inputStream).as(validInputType()) .to(inputStreamConsumer).as(validTargetType()) .execute(); fail(); } catch (ConverterAccessException e) { verify(inputStreamConsumer).onException(any(ConverterAccessException.class)); verifyNoMoreInteractions(inputStreamConsumer); verify(inputStream).close(); assertPostConverterState(); throw e; } }