FastInfosetCompressionService implements CompressionService { @Override public ByteArrayOutputStream decompress(final InputStream inputStream) { StreamResult streamResult = streamResultFactory.createStreamResult(); InputStream fiDocument = inputStream; ByteArrayOutputStream baos = outputStreamFactory.createByteArrayOutputStream(); try { Transformer tx = fastInfosetTransformerFactory.createTransformer(); tx.setOutputProperty("{http: tx.setOutputProperty(OutputKeys.INDENT, "yes"); FastInfosetSource fastInfosetSource = fastInfosetSourceFactory.createFastInfosetSource(fiDocument); tx.transform(fastInfosetSource, streamResult); fiDocument.close(); baos.write(streamResult.getWriter().toString().getBytes()); } catch (TransformerFactoryConfigurationError | Exception e) { throw new SystemException("Could not perform fastinfoset decompression!", e); } return baos; }  @Override ByteArrayOutputStream compress(final InputStream inputStream); @Override ByteArrayOutputStream decompress(final InputStream inputStream);  }
@Test public void testDecompressShouldDoTransform() throws TransformerException { StreamResult result = new StreamResult(new StringWriter()); given(transformerFactory.createTransformer()).willReturn(transformer); given(fastInfosetSourceFactory.createFastInfosetSource(inputStream)).willReturn(fISource); given(streamResultFactory.createStreamResult()).willReturn(result); given(outputStreamFactory.createByteArrayOutputStream()).willReturn(baos); underTest.decompress(inputStream); verify(transformer).transform(fISource, result); }
@Test public void testDecompressShouldReturnOutputStream() throws TransformerException { StreamResult result = new StreamResult(new StringWriter()); given(transformerFactory.createTransformer()).willReturn(transformer); given(fastInfosetSourceFactory.createFastInfosetSource(inputStream)).willReturn(fISource); given(streamResultFactory.createStreamResult()).willReturn(result); given(outputStreamFactory.createByteArrayOutputStream()).willReturn(baos); ByteArrayOutputStream actual = underTest.decompress(inputStream); Assert.assertEquals(actual, baos); }
@Test public void testDecompressShouldCloseTheInputStream() throws TransformerException, IOException { StreamResult result = new StreamResult(new StringWriter()); given(transformerFactory.createTransformer()).willReturn(transformer); given(fastInfosetSourceFactory.createFastInfosetSource(inputStream)).willReturn(fISource); given(streamResultFactory.createStreamResult()).willReturn(result); given(outputStreamFactory.createByteArrayOutputStream()).willReturn(baos); underTest.decompress(inputStream); verify(inputStream).close(); }
@Test(expectedExceptions = SystemException.class) public void testDecompressWhenTransformerConfigurationExceptionIsThrownShouldThrowException() throws TransformerConfigurationException { StreamResult result = new StreamResult(new StringWriter()); given(transformerFactory.createTransformer()).willThrow(new TransformerConfigurationException()); given(streamResultFactory.createStreamResult()).willReturn(result); underTest.decompress(inputStream); }
@Test(expectedExceptions = SystemException.class) public void testDecompressWhenTransformerFactoryConfigurationErrorIsThrownShouldLogError() throws TransformerConfigurationException { StreamResult result = new StreamResult(new StringWriter()); given(transformerFactory.createTransformer()).willThrow(new TransformerFactoryConfigurationError()); given(streamResultFactory.createStreamResult()).willReturn(result); underTest.decompress(inputStream); }
@Test(expectedExceptions = SystemException.class) public void testDecompressWhenInputStreamCloseThrowsIOExceptionShouldLogError() throws TransformerConfigurationException, IOException { StreamResult result = new StreamResult(new StringWriter()); given(transformerFactory.createTransformer()).willReturn(transformer); given(fastInfosetSourceFactory.createFastInfosetSource(inputStream)).willReturn(fISource); willThrow(new IOException()).given(inputStream).close(); given(streamResultFactory.createStreamResult()).willReturn(result); underTest.decompress(inputStream); }