XSLTStreamingOutput implements StreamingOutput { @Override public void write(OutputStream output) throws IOException { try { Transformer transformer = templates.newTransformer(); transformer.transform(source, new StreamResult(output)); } catch (TransformerException tre) { throw new IOException(tre.getMessage(), tre); } } XSLTStreamingOutput(Source source, Templates templates); @Override void write(OutputStream output);  }
@Test public void writesTransformedContentToOutputStream() throws Exception { ByteArrayOutputStream out = new ByteArrayOutputStream(); final String transformedContent = "<xml/>"; doAnswer(doTransformation(out, transformedContent)).when(transformer).transform(same(source), isA(StreamResult.class)); xsltStreamingOutput.write(out); assertEquals(transformedContent, out.toString()); }
@Test(expected = IOException.class) public void throwsIoExceptionWhenCreationOfTransformerFromTemplateFails() throws Exception { when(templates.newTransformer()).thenThrow(new TransformerConfigurationException()); ByteArrayOutputStream out = new ByteArrayOutputStream(); xsltStreamingOutput.write(out); }
@Test(expected = IOException.class) public void throwsIoExceptionWhenTransformationFails() throws Exception { doThrow(TransformerException.class).when(transformer).transform(same(source), isA(StreamResult.class)); ByteArrayOutputStream out = new ByteArrayOutputStream(); xsltStreamingOutput.write(out); }