Search in sources :

Example 1 with MethodLogger

use of tests.api.org.xml.sax.support.MethodLogger in project robovm by robovm.

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(tests.api.org.xml.sax.support.MockHandler) MockResolver(tests.api.org.xml.sax.support.MockResolver) Document(org.w3c.dom.Document) MethodLogger(tests.api.org.xml.sax.support.MethodLogger) IOException(java.io.IOException) SAXParseException(org.xml.sax.SAXParseException) SAXException(org.xml.sax.SAXException)

Example 2 with MethodLogger

use of tests.api.org.xml.sax.support.MethodLogger in project robovm by robovm.

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(tests.api.org.xml.sax.support.MockHandler) MethodLogger(tests.api.org.xml.sax.support.MethodLogger) IOException(java.io.IOException) SAXParseException(org.xml.sax.SAXParseException) SAXException(org.xml.sax.SAXException)

Example 3 with MethodLogger

use of tests.api.org.xml.sax.support.MethodLogger in project robovm by robovm.

the class SAXParserTest method testReset.

public void testReset() {
    try {
        spf = SAXParserFactory.newInstance();
        parser = spf.newSAXParser();
        parser.setProperty(LEXICAL_HANDLER_PROPERTY, new MockHandler(new MethodLogger()));
        parser.reset();
        assertEquals(null, parser.getProperty(LEXICAL_HANDLER_PROPERTY));
    } catch (Exception e) {
        throw new RuntimeException("Unexpected exception", e);
    }
}
Also used : MockHandler(tests.api.org.xml.sax.support.MockHandler) MethodLogger(tests.api.org.xml.sax.support.MethodLogger) SAXNotSupportedException(org.xml.sax.SAXNotSupportedException) SAXNotRecognizedException(org.xml.sax.SAXNotRecognizedException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) SAXException(org.xml.sax.SAXException)

Example 4 with MethodLogger

use of tests.api.org.xml.sax.support.MethodLogger in project robovm by robovm.

the class SAXParserTest method testSetGetProperty.

public void testSetGetProperty() {
    // Ordinary case
    String validName = "http://xml.org/sax/properties/lexical-handler";
    LexicalHandler validValue = new MockHandler(new MethodLogger());
    try {
        SAXParser parser = spf.newSAXParser();
        parser.setProperty(validName, validValue);
        assertEquals(validValue, parser.getProperty(validName));
        parser.setProperty(validName, null);
        assertEquals(null, parser.getProperty(validName));
    } catch (Exception e) {
        throw new RuntimeException("Unexpected exception", e);
    }
    // Unsupported property
    try {
        SAXParser parser = spf.newSAXParser();
        parser.setProperty("foo", "bar");
        fail("SAXNotRecognizedException expected");
    } catch (SAXNotRecognizedException e) {
    // Expected
    } catch (Exception e) {
        throw new RuntimeException("Unexpected exception", e);
    }
    try {
        SAXParser parser = spf.newSAXParser();
        parser.getProperty("foo");
        fail("SAXNotRecognizedException expected");
    } catch (SAXNotRecognizedException e) {
    // Expected
    } catch (Exception e) {
        throw new RuntimeException("Unexpected exception", e);
    }
    // No name case
    try {
        SAXParser parser = spf.newSAXParser();
        parser.setProperty(null, "bar");
        fail("NullPointerException expected");
    } catch (NullPointerException e) {
    // Expected
    } catch (Exception e) {
        throw new RuntimeException("Unexpected exception", e);
    }
    try {
        SAXParser parser = spf.newSAXParser();
        parser.getProperty(null);
        fail("NullPointerException expected");
    } catch (NullPointerException e) {
    // Expected
    } catch (Exception e) {
        throw new RuntimeException("Unexpected exception", e);
    }
}
Also used : LexicalHandler(org.xml.sax.ext.LexicalHandler) SAXParser(javax.xml.parsers.SAXParser) MockHandler(tests.api.org.xml.sax.support.MockHandler) SAXNotRecognizedException(org.xml.sax.SAXNotRecognizedException) MethodLogger(tests.api.org.xml.sax.support.MethodLogger) SAXNotSupportedException(org.xml.sax.SAXNotSupportedException) SAXNotRecognizedException(org.xml.sax.SAXNotRecognizedException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) SAXException(org.xml.sax.SAXException)

Aggregations

IOException (java.io.IOException)4 SAXException (org.xml.sax.SAXException)4 MethodLogger (tests.api.org.xml.sax.support.MethodLogger)4 MockHandler (tests.api.org.xml.sax.support.MockHandler)4 ByteArrayInputStream (java.io.ByteArrayInputStream)2 FileInputStream (java.io.FileInputStream)2 FileNotFoundException (java.io.FileNotFoundException)2 InputStream (java.io.InputStream)2 ErrorHandler (org.xml.sax.ErrorHandler)2 SAXNotRecognizedException (org.xml.sax.SAXNotRecognizedException)2 SAXNotSupportedException (org.xml.sax.SAXNotSupportedException)2 SAXParseException (org.xml.sax.SAXParseException)2 SAXParser (javax.xml.parsers.SAXParser)1 Document (org.w3c.dom.Document)1 Element (org.w3c.dom.Element)1 InputSource (org.xml.sax.InputSource)1 LexicalHandler (org.xml.sax.ext.LexicalHandler)1 MockResolver (tests.api.org.xml.sax.support.MockResolver)1