Search in sources :

Example 1 with MockHandler

use of org.apache.harmony.tests.org.xml.sax.support.MockHandler in project j2objc by google.

the class DocumentBuilderTest method testSetErrorHandler.

public void testSetErrorHandler() {
    // Ordinary case
    InputStream source = new ByteArrayInputStream("</a>".getBytes());
    MethodLogger logger = new MethodLogger();
    ErrorHandler handler = new MockHandler(logger);
    try {
        db = dbf.newDocumentBuilder();
        db.setErrorHandler(handler);
        db.parse(source);
    } catch (SAXParseException e) {
    // Expected, ErrorHandler does not mask exception
    } catch (Exception e) {
        throw new RuntimeException("Unexpected exception", e);
    }
    assertEquals("error", logger.getMethod());
    assertTrue(logger.getArgs()[0] instanceof SAXParseException);
    // null case
    source = new ByteArrayInputStream("</a>".getBytes());
    try {
        db = dbf.newDocumentBuilder();
        db.setErrorHandler(null);
        db.parse(source);
    } catch (SAXParseException e) {
    // Expected
    } catch (Exception e) {
        throw new RuntimeException("Unexpected exception", e);
    }
}
Also used : ErrorHandler(org.xml.sax.ErrorHandler) ByteArrayInputStream(java.io.ByteArrayInputStream) FileInputStream(java.io.FileInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) SAXParseException(org.xml.sax.SAXParseException) MockHandler(org.apache.harmony.tests.org.xml.sax.support.MockHandler) MethodLogger(org.apache.harmony.tests.org.xml.sax.support.MethodLogger) IOException(java.io.IOException) SAXParseException(org.xml.sax.SAXParseException) SAXException(org.xml.sax.SAXException)

Example 2 with MockHandler

use of org.apache.harmony.tests.org.xml.sax.support.MockHandler in project j2objc by google.

the class DocumentBuilderTest method testReset.

public void testReset() {
    // Make sure EntityResolver gets reset
    InputStream source = new ByteArrayInputStream("<a>&foo;</a>".getBytes());
    InputStream entity = new ByteArrayInputStream("bar".getBytes());
    MockResolver resolver = new MockResolver();
    resolver.addEntity("foo", "foo", new InputSource(entity));
    Document d;
    try {
        db = dbf.newDocumentBuilder();
        db.setEntityResolver(resolver);
        db.reset();
        d = db.parse(source);
    } catch (Exception e) {
        throw new RuntimeException("Unexpected exception", e);
    }
    Element root = (Element) d.getElementsByTagName("a").item(0);
    assertEquals("foo", ((EntityReference) root.getFirstChild()).getNodeName());
    // Make sure ErrorHandler gets reset
    source = new ByteArrayInputStream("</a>".getBytes());
    MethodLogger logger = new MethodLogger();
    ErrorHandler handler = new MockHandler(logger);
    try {
        db = dbf.newDocumentBuilder();
        db.setErrorHandler(handler);
        db.reset();
        d = db.parse(source);
    } catch (SAXParseException e) {
    // Expected
    } catch (Exception e) {
        throw new RuntimeException("Unexpected exception", e);
    }
    assertEquals(0, logger.size());
}
Also used : ErrorHandler(org.xml.sax.ErrorHandler) InputSource(org.xml.sax.InputSource) ByteArrayInputStream(java.io.ByteArrayInputStream) FileInputStream(java.io.FileInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) SAXParseException(org.xml.sax.SAXParseException) Element(org.w3c.dom.Element) MockHandler(org.apache.harmony.tests.org.xml.sax.support.MockHandler) MockResolver(org.apache.harmony.tests.org.xml.sax.support.MockResolver) Document(org.w3c.dom.Document) MethodLogger(org.apache.harmony.tests.org.xml.sax.support.MethodLogger) IOException(java.io.IOException) SAXParseException(org.xml.sax.SAXParseException) SAXException(org.xml.sax.SAXException)

Aggregations

ByteArrayInputStream (java.io.ByteArrayInputStream)2 FileInputStream (java.io.FileInputStream)2 IOException (java.io.IOException)2 InputStream (java.io.InputStream)2 MethodLogger (org.apache.harmony.tests.org.xml.sax.support.MethodLogger)2 MockHandler (org.apache.harmony.tests.org.xml.sax.support.MockHandler)2 ErrorHandler (org.xml.sax.ErrorHandler)2 SAXException (org.xml.sax.SAXException)2 SAXParseException (org.xml.sax.SAXParseException)2 MockResolver (org.apache.harmony.tests.org.xml.sax.support.MockResolver)1 Document (org.w3c.dom.Document)1 Element (org.w3c.dom.Element)1 InputSource (org.xml.sax.InputSource)1