FastInfosetCompressionService implements CompressionService { @Override public ByteArrayOutputStream compress(final InputStream inputStream) { InputStream source = inputStream; try { ByteArrayOutputStream fis = outputStreamFactory.createByteArrayOutputStream(); SAXDocumentSerializer saxDocumentSerializer = documentSerializerFactory.createSAXDocumentSerializer(); saxDocumentSerializer.setOutputStream(fis); SAXParserFactory saxParserFactory = saxParserCreator.createSAXParserFactory(); saxParserFactory.setNamespaceAware(true); SAXParser saxParser = saxParserFactory.newSAXParser(); saxParser.parse(source, saxDocumentSerializer); return fis; } catch (Exception e) { throw new SystemException("Could not perform fastinfoset compression!", e); } }  @Override ByteArrayOutputStream compress(final InputStream inputStream); @Override ByteArrayOutputStream decompress(final InputStream inputStream);  }
@Test public void testCompressShouldCallParse() throws ParserConfigurationException, SAXException, IOException { given(saxParserFactory.newSAXParser()).willReturn(saxParser); underTest.compress(inputStream); verify(saxParser).parse(inputStream, saxDocumentSerializer); }
@Test public void testCompressShouldSetNameSpaceAwareTrue() throws ParserConfigurationException, SAXException { given(saxParserFactory.newSAXParser()).willReturn(saxParser); underTest.compress(inputStream); verify(saxParserFactory).setNamespaceAware(true); }
@Test public void testCompressShouldReturnByteArrayOutputStream() throws ParserConfigurationException, SAXException { given(outputStreamFactory.createByteArrayOutputStream()).willReturn(baos); given(saxParserFactory.newSAXParser()).willReturn(saxParser); ByteArrayOutputStream actual = underTest.compress(inputStream); Assert.assertEquals(actual, baos); }
@Test(expectedExceptions = SystemException.class) public void testCompressShouldThrowSystemExceptionWhenParserConfigExceptionIsCatched() throws ParserConfigurationException, SAXException { given(saxParserFactory.newSAXParser()).willThrow(new ParserConfigurationException()); underTest.compress(inputStream); }
@Test(expectedExceptions = SystemException.class) public void testCompressShouldThrowSystemExceptionWhenSAXExceptionIsCatched() throws ParserConfigurationException, SAXException { given(saxParserFactory.newSAXParser()).willThrow(new SAXException()); underTest.compress(inputStream); }
@Test(expectedExceptions = SystemException.class) public void testCompressShouldThrowSystemExceptionWhenIOExceptionIsCatched() throws ParserConfigurationException, SAXException, IOException { given(saxParserFactory.newSAXParser()).willReturn(saxParser); willThrow(new IOException()).given(saxParser).parse(inputStream, saxDocumentSerializer); underTest.compress(inputStream); }