TraxUtils { public static void doWithSource(Source source, SourceCallback callback) throws Exception { if (source instanceof DOMSource) { callback.domSource(((DOMSource) source).getNode()); return; } else if (StaxUtils.isStaxSource(source)) { XMLStreamReader streamReader = StaxUtils.getXMLStreamReader(source); if (streamReader != null) { callback.staxSource(streamReader); return; } else { XMLEventReader eventReader = StaxUtils.getXMLEventReader(source); if (eventReader != null) { callback.staxSource(eventReader); return; } } } else if (source instanceof SAXSource) { SAXSource saxSource = (SAXSource) source; callback.saxSource(saxSource.getXMLReader(), saxSource.getInputSource()); return; } else if (source instanceof StreamSource) { StreamSource streamSource = (StreamSource) source; if (streamSource.getInputStream() != null) { callback.streamSource(streamSource.getInputStream()); return; } else if (streamSource.getReader() != null) { callback.streamSource(streamSource.getReader()); return; } } if (StringUtils.hasLength(source.getSystemId())) { String systemId = source.getSystemId(); callback.source(systemId); } else { throw new IllegalArgumentException("Unknown Source type: " + source.getClass()); } }  static Document getDocument(DOMSource source); static void doWithSource(Source source, SourceCallback callback); static void doWithResult(Result result, ResultCallback callback);  }
@Test public void testDoWithDomSource() throws Exception { DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactoryUtils.newInstance(); DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder(); Document document = documentBuilder.newDocument(); TraxUtils.SourceCallback mock = createMock(TraxUtils.SourceCallback.class); mock.domSource(document); replay(mock); TraxUtils.doWithSource(new DOMSource(document), mock); verify(mock); }
@Test public void testDoWithSaxSource() throws Exception { XMLReader reader = XMLReaderFactory.createXMLReader(); InputSource inputSource = new InputSource(); TraxUtils.SourceCallback mock = createMock(TraxUtils.SourceCallback.class); mock.saxSource(reader, inputSource); replay(mock); TraxUtils.doWithSource(new SAXSource(reader, inputSource), mock); verify(mock); }
@Test public void testDoWithStaxSourceEventReader() throws Exception { XMLInputFactory inputFactory = XMLInputFactoryUtils.newInstance(); XMLEventReader eventReader = inputFactory.createXMLEventReader(new StringReader("<element/>")); TraxUtils.SourceCallback mock = createMock(TraxUtils.SourceCallback.class); mock.staxSource(eventReader); replay(mock); TraxUtils.doWithSource(StaxUtils.createStaxSource(eventReader), mock); verify(mock); }
@Test public void testDoWithStaxSourceStreamReader() throws Exception { XMLInputFactory inputFactory = XMLInputFactoryUtils.newInstance(); XMLStreamReader streamReader = inputFactory.createXMLStreamReader(new StringReader("<element/>")); TraxUtils.SourceCallback mock = createMock(TraxUtils.SourceCallback.class); mock.staxSource(streamReader); replay(mock); TraxUtils.doWithSource(StaxUtils.createStaxSource(streamReader), mock); verify(mock); }
@Test public void testDoWithStreamSourceInputStream() throws Exception { byte[] xml = "<element/>".getBytes("UTF-8"); InputStream inputStream = new ByteArrayInputStream(xml); TraxUtils.SourceCallback mock = createMock(TraxUtils.SourceCallback.class); mock.streamSource(inputStream); replay(mock); TraxUtils.doWithSource(new StreamSource(inputStream), mock); verify(mock); }
@Test public void testDoWithStreamSourceReader() throws Exception { String xml = "<element/>"; Reader reader = new StringReader(xml); TraxUtils.SourceCallback mock = createMock(TraxUtils.SourceCallback.class); mock.streamSource(reader); replay(mock); TraxUtils.doWithSource(new StreamSource(reader), mock); verify(mock); }
@Test public void testDoWithSystemIdSource() throws Exception { String systemId = "http: TraxUtils.SourceCallback mock = createMock(TraxUtils.SourceCallback.class); mock.source(systemId); replay(mock); TraxUtils.doWithSource(new StreamSource(systemId), mock); verify(mock); }
@Test public void testDoWithInvalidSource() throws Exception { Source source = new Source() { public void setSystemId(String systemId) {} public String getSystemId() { return null; } }; try { TraxUtils.doWithSource(source, null); Assert.fail("IllegalArgumentException expected"); } catch (IllegalArgumentException ex) { } }