@Override public void onComplete(File file) { fileConsumer.onComplete(file); }
@Override public void onException(ConversionFormatException exception) { callback.onException(target, exception); } }
@Override protected void onConversionCancelled() { callback.onCancel(target); }
@Override public void onException(File file, Exception e) { try { reportException(e); } finally { fileConsumer.onException(file, e); } } }
@Override protected void onConversionFinished(LocalConversionContext conversionContext) { callback.onComplete(target); }
@Override public void onCancel() { fileConsumer.onCancel(file); }
@Override public void onException(Exception e) { fileConsumer.onException(file, e); }
@Override public void onComplete(InputStream inputStream) { try { FileOutputStream fileOutputStream = new FileOutputStream(file); fileOutputStream.getChannel().lock(); try { ByteStreams.copy(inputStream, fileOutputStream); } finally { Closeables.close(inputStream, true); // Note: This will implicitly release the file lock. Closeables.close(fileOutputStream, false); } } catch (IOException e) { throw new FileSystemInteractionException(String.format("Could not copy result to %s", file), e); } fileConsumer.onComplete(file); }
@Override public void onCancel(File file) { fileConsumer.onCancel(file); }
@Override protected void onConversionFailed(RuntimeException e) { callback.onException(target, e); }
@Test(timeout = DEFAULT_CONVERSION_TIMEOUT) public void testFileSourceToFileConsumerFuture() throws Exception { File source = validFile(true), target = makeTarget(true); IFileSource fileSource = mock(IFileSource.class); when(fileSource.getFile()).thenReturn(source); IFileConsumer fileConsumer = mock(IFileConsumer.class); assertTrue(getConverter().convert(fileSource).as(validInputType()).to(target, fileConsumer).as(validTargetType()).schedule().get()); assertTrue(source.exists()); assertTrue(target.exists()); verify(fileSource, times(1)).getFile(); verify(fileSource, times(1)).onConsumed(source); verifyNoMoreInteractions(fileSource); verify(fileConsumer, times(1)).onComplete(target); verifyNoMoreInteractions(fileConsumer); }
@Test(timeout = DEFAULT_CONVERSION_TIMEOUT, expected = ConversionFormatException.class) public void testConversionWithUnknownSourceFormatFuture() throws Exception { File target = makeTarget(false); IFileConsumer fileConsumer = mock(IFileConsumer.class); try { getConverter().convert(validFile(true)).as(UNKNOWN_TYPE) .to(target, fileConsumer).as(validTargetType()) .schedule().get(); fail(); } catch (ExecutionException e) { verify(fileConsumer).onException(eq(target), any(ConversionFormatException.class)); verifyNoMoreInteractions(fileConsumer); assertFalse(target.exists()); throw (Exception) e.getCause(); } }
@Test(timeout = DEFAULT_CONVERSION_TIMEOUT) public void testFileSourceToFileConsumerExecute() throws Exception { File source = validFile(true), target = makeTarget(true); IFileSource fileSource = mock(IFileSource.class); when(fileSource.getFile()).thenReturn(source); IFileConsumer fileConsumer = mock(IFileConsumer.class); assertTrue(getConverter().convert(fileSource).as(validInputType()).to(target, fileConsumer).as(validTargetType()).execute()); assertTrue(source.exists()); assertTrue(target.exists()); verify(fileSource, times(1)).getFile(); verify(fileSource, times(1)).onConsumed(source); verifyNoMoreInteractions(fileSource); verify(fileConsumer, times(1)).onComplete(target); verifyNoMoreInteractions(fileConsumer); }
@Test(timeout = DEFAULT_CONVERSION_TIMEOUT, expected = ConversionFormatException.class) public void testConversionWithUnknownTargetFormatFuture() throws Exception { File target = makeTarget(false); IFileConsumer fileConsumer = mock(IFileConsumer.class); try { getConverter().convert(validFile(true)).as(validInputType()) .to(target, fileConsumer).as(UNKNOWN_TYPE) .schedule().get(); fail(); } catch (ExecutionException e) { verify(fileConsumer).onException(eq(target), any(ConversionFormatException.class)); verifyNoMoreInteractions(fileConsumer); assertFalse(target.exists()); throw (Exception) e.getCause(); } }
@Test(timeout = DEFAULT_CONVERSION_TIMEOUT, expected = ConversionFormatException.class) public void testConversionWithUnknownTargetFormatExecute() throws Exception { File target = makeTarget(false); IFileConsumer fileConsumer = mock(IFileConsumer.class); try { getConverter().convert(validFile(true)).as(validInputType()) .to(target, fileConsumer).as(UNKNOWN_TYPE) .execute(); fail(); } catch (ConversionFormatException e) { verify(fileConsumer).onException(eq(target), any(ConversionFormatException.class)); verifyNoMoreInteractions(fileConsumer); assertFalse(target.exists()); throw e; } }
@Test(timeout = DEFAULT_CONVERSION_TIMEOUT, expected = ConversionFormatException.class) public void testConversionWithUnknownSourceFormatExecute() throws Exception { File target = makeTarget(false); IFileConsumer fileConsumer = mock(IFileConsumer.class); try { getConverter().convert(validFile(true)).as(UNKNOWN_TYPE) .to(target, fileConsumer).as(validTargetType()) .execute(); fail(); } catch (ConversionFormatException e) { verify(fileConsumer).onException(eq(target), any(ConversionFormatException.class)); verifyNoMoreInteractions(fileConsumer); assertFalse(target.exists()); throw e; } }
@Test(timeout = DEFAULT_CONVERSION_TIMEOUT, expected = ConverterAccessException.class) public void testFileToFileFuture() throws Exception { IFileConsumer fileConsumer = mock(IFileConsumer.class); File target = makeTarget(false); try { getConverter() .convert(validFile(true)).as(validInputType()) .to(target, fileConsumer).as(validTargetType()) .schedule().get(); fail(); } catch (ExecutionException e) { verify(fileConsumer).onException(eq(target), any(ConverterAccessException.class)); verifyNoMoreInteractions(fileConsumer); assertFalse(target.exists()); assertPostConverterState(); throw (Exception) e.getCause(); } }
@Test(timeout = DEFAULT_CONVERSION_TIMEOUT, expected = ConverterAccessException.class) public void testFileToFileExecute() throws Exception { IFileConsumer fileConsumer = mock(IFileConsumer.class); File target = makeTarget(false); try { getConverter() .convert(validFile(true)).as(validInputType()) .to(target, fileConsumer).as(validTargetType()) .execute(); fail(); } catch (ConverterAccessException e) { verify(fileConsumer).onException(eq(target), any(ConverterAccessException.class)); verifyNoMoreInteractions(fileConsumer); assertFalse(target.exists()); assertPostConverterState(); throw e; } }