TraxUtils { public static void doWithResult(Result result, ResultCallback callback) throws Exception { if (result instanceof DOMResult) { callback.domResult(((DOMResult) result).getNode()); return; } else if (StaxUtils.isStaxResult(result)) { XMLStreamWriter streamWriter = StaxUtils.getXMLStreamWriter(result); if (streamWriter != null) { callback.staxResult(streamWriter); return; } else { XMLEventWriter eventWriter = StaxUtils.getXMLEventWriter(result); if (eventWriter != null) { callback.staxResult(eventWriter); return; } } } else if (result instanceof SAXResult) { SAXResult saxSource = (SAXResult) result; callback.saxResult(saxSource.getHandler(), saxSource.getLexicalHandler()); return; } else if (result instanceof StreamResult) { StreamResult streamSource = (StreamResult) result; if (streamSource.getOutputStream() != null) { callback.streamResult(streamSource.getOutputStream()); return; } else if (streamSource.getWriter() != null) { callback.streamResult(streamSource.getWriter()); return; } } if (StringUtils.hasLength(result.getSystemId())) { String systemId = result.getSystemId(); callback.result(systemId); } else { throw new IllegalArgumentException("Unknown Result type: " + result.getClass()); } }  static Document getDocument(DOMSource source); static void doWithSource(Source source, SourceCallback callback); static void doWithResult(Result result, ResultCallback callback);  }
@Test public void testDoWithDomResult() throws Exception { DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactoryUtils.newInstance(); DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder(); Document document = documentBuilder.newDocument(); TraxUtils.ResultCallback mock = createMock(TraxUtils.ResultCallback.class); mock.domResult(document); replay(mock); TraxUtils.doWithResult(new DOMResult(document), mock); verify(mock); }
@Test public void testDoWithSaxResult() throws Exception { ContentHandler contentHandler = new DefaultHandler(); LexicalHandler lexicalHandler = new DefaultHandler2(); TraxUtils.ResultCallback mock = createMock(TraxUtils.ResultCallback.class); mock.saxResult(contentHandler, lexicalHandler); replay(mock); SAXResult result = new SAXResult(contentHandler); result.setLexicalHandler(lexicalHandler); TraxUtils.doWithResult(result, mock); verify(mock); }
@Test public void testDoWithStaxResultEventWriter() throws Exception { XMLOutputFactory outputFactory = XMLOutputFactory.newInstance(); XMLEventWriter eventWriter = outputFactory.createXMLEventWriter(new StringWriter()); TraxUtils.ResultCallback mock = createMock(TraxUtils.ResultCallback.class); mock.staxResult(eventWriter); replay(mock); TraxUtils.doWithResult(StaxUtils.createStaxResult(eventWriter), mock); verify(mock); }
@Test public void testDoWithStaxResultStreamWriter() throws Exception { XMLOutputFactory outputFactory = XMLOutputFactory.newInstance(); XMLStreamWriter streamWriter = outputFactory.createXMLStreamWriter(new StringWriter()); TraxUtils.ResultCallback mock = createMock(TraxUtils.ResultCallback.class); mock.staxResult(streamWriter); replay(mock); TraxUtils.doWithResult(StaxUtils.createStaxResult(streamWriter), mock); verify(mock); }
@Test public void testDoWithStreamResultOutputStream() throws Exception { OutputStream outputStream = new ByteArrayOutputStream(); TraxUtils.ResultCallback mock = createMock(TraxUtils.ResultCallback.class); mock.streamResult(outputStream); replay(mock); TraxUtils.doWithResult(new StreamResult(outputStream), mock); verify(mock); }
@Test public void testDoWithStreamResultWriter() throws Exception { Writer writer = new StringWriter(); TraxUtils.ResultCallback mock = createMock(TraxUtils.ResultCallback.class); mock.streamResult(writer); replay(mock); TraxUtils.doWithResult(new StreamResult(writer), mock); verify(mock); }
@Test public void testDoWithSystemIdResult() throws Exception { String systemId = "http: TraxUtils.ResultCallback mock = createMock(TraxUtils.ResultCallback.class); mock.result(systemId); replay(mock); TraxUtils.doWithResult(new StreamResult(systemId), mock); verify(mock); }
@Test public void testDoWithInvalidResult() throws Exception { Result result = new Result() { public void setSystemId(String systemId) {} public String getSystemId() { return null; } }; try { TraxUtils.doWithResult(result, null); Assert.fail("IllegalArgumentException expected"); } catch (IllegalArgumentException ex) { } }