Search in sources :

Example 1 with InvocationCounter

use of org.apache.axiom.testutils.InvocationCounter in project webservices-axiom by apache.

the class TestIOExceptionInGetText method runTest.

@Override
protected void runTest() throws Throwable {
    // Construct a stream that will throw an exception in the middle of a text node.
    // We need to create a very large document, because some parsers (such as some
    // versions of XLXP) have a large input buffer and would throw an exception already
    // when the XMLStreamReader is created.
    StringBuffer xml = new StringBuffer("<root>");
    for (int i = 0; i < 100000; i++) {
        xml.append('x');
    }
    InputStream in = new ExceptionInputStream(new ByteArrayInputStream(xml.toString().getBytes("ASCII")));
    XMLStreamReader originalReader = StAXUtils.createXMLStreamReader(in);
    InvocationCounter invocationCounter = new InvocationCounter();
    XMLStreamReader reader = (XMLStreamReader) invocationCounter.createProxy(originalReader);
    OMElement element = OMXMLBuilderFactory.createStAXOMBuilder(metaFactory.getOMFactory(), reader).getDocumentElement();
    try {
        element.getNextOMSibling();
        fail("Expected exception");
    } catch (Exception ex) {
    // Expected
    }
    assertTrue(invocationCounter.getInvocationCount() > 0);
    invocationCounter.reset();
    try {
        element.getNextOMSibling();
        fail("Expected exception");
    } catch (Exception ex) {
    // Expected
    }
    assertEquals(0, invocationCounter.getInvocationCount());
}
Also used : XMLStreamReader(javax.xml.stream.XMLStreamReader) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) ExceptionInputStream(org.apache.axiom.testutils.io.ExceptionInputStream) InputStream(java.io.InputStream) ExceptionInputStream(org.apache.axiom.testutils.io.ExceptionInputStream) OMElement(org.apache.axiom.om.OMElement) InvocationCounter(org.apache.axiom.testutils.InvocationCounter)

Example 2 with InvocationCounter

use of org.apache.axiom.testutils.InvocationCounter in project webservices-axiom by apache.

the class TestInvalidXML method runTest.

@Override
protected void runTest() throws Throwable {
    XMLStreamReader originalReader = StAXUtils.createXMLStreamReader(TestInvalidXML.class.getResourceAsStream("invalid_xml.xml"));
    InvocationCounter invocationCounter = new InvocationCounter();
    XMLStreamReader reader = (XMLStreamReader) invocationCounter.createProxy(originalReader);
    OMElement element = OMXMLBuilderFactory.createStAXOMBuilder(metaFactory.getOMFactory(), reader).getDocumentElement();
    DeferredParsingException exception;
    try {
        element.getNextOMSibling();
        exception = null;
    } catch (DeferredParsingException ex) {
        exception = ex;
    }
    assertThat(exception).isNotNull();
    assertTrue(invocationCounter.getInvocationCount() > 0);
    invocationCounter.reset();
    // Intentionally call builder again to make sure the same error is returned.
    DeferredParsingException exception2;
    try {
        element.getNextOMSibling();
        exception2 = null;
    } catch (DeferredParsingException ex) {
        exception2 = ex;
    }
    assertThat(invocationCounter.getInvocationCount()).isEqualTo(0);
    assertThat(exception2).isNotNull();
    assertThat(exception2.getMessage()).isEqualTo(exception.getMessage());
}
Also used : DeferredParsingException(org.apache.axiom.om.DeferredParsingException) XMLStreamReader(javax.xml.stream.XMLStreamReader) OMElement(org.apache.axiom.om.OMElement) InvocationCounter(org.apache.axiom.testutils.InvocationCounter)

Aggregations

XMLStreamReader (javax.xml.stream.XMLStreamReader)2 OMElement (org.apache.axiom.om.OMElement)2 InvocationCounter (org.apache.axiom.testutils.InvocationCounter)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 InputStream (java.io.InputStream)1 DeferredParsingException (org.apache.axiom.om.DeferredParsingException)1 ExceptionInputStream (org.apache.axiom.testutils.io.ExceptionInputStream)1