@Test(timeout = DEFAULT_CONVERSION_TIMEOUT) public void testInputStreamToOutputStreamFuture() throws Exception { File source = validFile(true), target = makeTarget(true); InputStream inputStream = spy(new FileInputStream(source)); assertTrue(target.createNewFile()); OutputStream outputStream = spy(new FileOutputStream(target)); assertTrue(getConverter().convert(inputStream).as(validInputType()).to(outputStream).as(validTargetType()).schedule().get()); assertTrue(source.exists()); assertTrue(target.exists()); verify(inputStream, times(1)).close(); verify(outputStream, times(1)).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(); }
@POST public void convert( InputStream inputStream, @Suspended AsyncResponse asyncResponse, @HeaderParam(HttpHeaders.CONTENT_TYPE) String inputType, @HeaderParam(HttpHeaders.ACCEPT) String responseType, @DefaultValue("" + IConverter.JOB_PRIORITY_NORMAL) @HeaderParam(ConverterNetworkProtocol.HEADER_JOB_PRIORITY) int priority) { DocumentType targetType = new DocumentType(responseType); // The received input stream does not need to be closed since the underlying channel is automatically closed with responding. // If the stream was closed manually, this would in contrast lead to a NullPointerException since the channel was already detached. webConverterConfiguration.getConverter() .convert(inputStream, false).as(new DocumentType(inputType)) .to(AsynchronousConversionResponse.to(asyncResponse, targetType, webConverterConfiguration.getTimeout())).as(targetType) .prioritizeWith(priority) .schedule(); } }
@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 testInputStreamToOutputStreamExecute() throws Exception { File source = validFile(true), target = makeTarget(true); InputStream inputStream = spy(new FileInputStream(source)); assertTrue(target.createNewFile()); OutputStream outputStream = spy(new FileOutputStream(target)); assertTrue(getConverter().convert(inputStream).as(validInputType()).to(outputStream).as(validTargetType()).execute()); assertTrue(source.exists()); assertTrue(target.exists()); verify(inputStream, times(1)).close(); verify(outputStream, times(1)).close(); }
@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) 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 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, 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 = 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 = 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; } }
@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, 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 = 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; } }
@Test(timeout = DEFAULT_CONVERSION_TIMEOUT, expected = FileSystemInteractionException.class) public void testInexistentInputFileFuture() throws Exception { File source = inexistentFile(false), target = makeTarget(false); try { getConverter().convert(source).as(validInputType()).to(target).as(validTargetType()).schedule().get(); } catch (ExecutionException e) { assertFalse(source.exists()); assertFalse(target.exists()); throw (Exception) e.getCause(); } }
@Test(timeout = DEFAULT_CONVERSION_TIMEOUT, expected = ConversionInputException.class) public void testCorruptInputFileExecute() throws Exception { File source = invalidFile(true), target = makeTarget(false); try { getConverter().convert(source).as(validInputType()).to(target).as(validTargetType()).execute(); } catch (ConversionInputException e) { assertTrue(source.exists()); assertFalse(target.exists()); throw e; } }
@Test(timeout = DEFAULT_CONVERSION_TIMEOUT, expected = ConversionInputException.class) public void testCorruptInputFileFuture() throws Exception { File source = invalidFile(true), target = makeTarget(false); try { getConverter().convert(source).as(validInputType()).to(target).as(validTargetType()).schedule().get(); } catch (ExecutionException e) { assertTrue(source.exists()); assertFalse(target.exists()); throw (Exception) e.getCause(); } }
@Test(timeout = DEFAULT_CONVERSION_TIMEOUT, expected = FileSystemInteractionException.class) public void testInexistentInputFileExecute() throws Exception { File source = inexistentFile(false), target = makeTarget(false); try { getConverter().convert(source).as(validInputType()).to(target).as(validTargetType()).execute(); } catch (FileSystemInteractionException e) { assertFalse(source.exists()); assertFalse(target.exists()); throw e; } }
@Test(timeout = DEFAULT_CONVERSION_TIMEOUT) public void testFileToFileFuture() throws Exception { File source = validFile(true), target = makeTarget(true); assertTrue(getConverter().convert(source).as(validInputType()).to(target).as(validTargetType()).schedule().get()); assertTrue(source.exists()); assertTrue(target.exists()); }