GzipCompressionService implements CompressionService { @Override public ByteArrayOutputStream decompress(final InputStream inputStream) { OutputStream writer = outputStreamFactory.createByteArrayOutputStream(); try { GZIPInputStream gzipStream = gzipInputStreamFactory.createInputStream(inputStream); IOUtils.copy(gzipStream, writer); } catch (IOException e) { throw new SystemException("Could not ungzip message body!", e); } return (ByteArrayOutputStream) writer; }  @Override ByteArrayOutputStream compress(final InputStream inputStream); @Override ByteArrayOutputStream decompress(final InputStream inputStream);  }
@Test(expectedExceptions = SystemException.class) public void testDecompressShouldThrowExceptionWhenStreamCopyThrowsIOException() throws IOException { given(gzipInputStreamFactory.createInputStream(source)).willReturn(gzipInputStream); given(gzipInputStream.read((byte[]) Mockito.any())).willThrow(new IOException()); underTest.decompress(source); }
@Test public void testDecompressShouldDoDecompression() throws IOException { given(gzipInputStreamFactory.createInputStream(source)).willReturn(gzipInputStream); given(gzipInputStream.read((byte[]) Mockito.any())).willReturn(1, -1); underTest.decompress(source); verify(gzipInputStream, times(2)).read(Mockito.any(byte[].class)); }
@Test public void testDecompressShouldReturnWriter() throws IOException { given(outputStreamFactory.createByteArrayOutputStream()).willReturn(baos); given(gzipInputStreamFactory.createInputStream(source)).willReturn(gzipInputStream); given(gzipInputStream.read((byte[]) Mockito.any())).willReturn(1, -1); ByteArrayOutputStream actual = underTest.decompress(source); assertEquals(actual, baos); }