< prev index next >

test/javax/xml/jaxp/functional/org/xml/sax/ptests/XMLReaderTest.java

Print this page

        

*** 21,38 **** * questions. */ package org.xml.sax.ptests; import java.io.FileInputStream; import java.io.IOException; import javax.xml.parsers.ParserConfigurationException; import javax.xml.parsers.SAXParserFactory; ! import static jaxp.library.JAXPTestUtilities.failUnexpected; import static org.testng.Assert.assertFalse; import static org.testng.Assert.assertNotNull; import static org.testng.Assert.assertNull; import static org.testng.Assert.assertTrue; import org.testng.annotations.Test; import org.xml.sax.InputSource; import org.xml.sax.SAXException; import org.xml.sax.SAXNotRecognizedException; import org.xml.sax.SAXNotSupportedException; --- 21,42 ---- * questions. */ package org.xml.sax.ptests; import java.io.FileInputStream; + import java.io.FilePermission; import java.io.IOException; import javax.xml.parsers.ParserConfigurationException; import javax.xml.parsers.SAXParserFactory; ! import jaxp.library.JAXPBaseTest; ! import static org.testng.Assert.assertEquals; import static org.testng.Assert.assertFalse; import static org.testng.Assert.assertNotNull; import static org.testng.Assert.assertNull; import static org.testng.Assert.assertTrue; + import org.testng.annotations.AfterGroups; + import org.testng.annotations.BeforeGroups; import org.testng.annotations.Test; import org.xml.sax.InputSource; import org.xml.sax.SAXException; import org.xml.sax.SAXNotRecognizedException; import org.xml.sax.SAXNotSupportedException;
*** 43,88 **** import static org.xml.sax.ptests.SAXTestConst.XML_DIR; /** * Class containing the test cases for SAXParser API */ ! public class XMLReaderTest { /** * XML namespaces. */ ! private static final String NAMESPACES = ! "http://xml.org/sax/features/namespaces"; /** * XML namespaces prefixes. */ ! private static final String NAMESPACE_PREFIXES = ! "http://xml.org/sax/features/namespace-prefixes"; /** * A string intern name. */ ! private static final String STRING_INTERNING = ! "http://xml.org/sax/features/string-interning"; /** * Validation name. */ ! private static final String VALIDATION = ! "http://xml.org/sax/features/validation"; /** * A general external entities name */ ! private static final String EXTERNAL_G_ENTITIES = ! "http://xml.org/sax/features/external-general-entities"; /** * A external parameter entities name */ ! private static final String EXTERNAL_P_ENTITIES = ! "http://xml.org/sax/features/external-parameter-entities"; /** * XML DOM node name. */ private static final String DOM_NODE = "http://xml.org/sax/properties/dom-node"; --- 47,93 ---- import static org.xml.sax.ptests.SAXTestConst.XML_DIR; /** * Class containing the test cases for SAXParser API */ ! public class XMLReaderTest extends JAXPBaseTest { ! /** * XML namespaces. */ ! private static final String NAMESPACES ! = "http://xml.org/sax/features/namespaces"; /** * XML namespaces prefixes. */ ! private static final String NAMESPACE_PREFIXES ! = "http://xml.org/sax/features/namespace-prefixes"; /** * A string intern name. */ ! private static final String STRING_INTERNING ! = "http://xml.org/sax/features/string-interning"; /** * Validation name. */ ! private static final String VALIDATION ! = "http://xml.org/sax/features/validation"; /** * A general external entities name */ ! private static final String EXTERNAL_G_ENTITIES ! = "http://xml.org/sax/features/external-general-entities"; /** * A external parameter entities name */ ! private static final String EXTERNAL_P_ENTITIES ! = "http://xml.org/sax/features/external-parameter-entities"; /** * XML DOM node name. */ private static final String DOM_NODE = "http://xml.org/sax/properties/dom-node";
*** 93,629 **** private static final String XML_STRING = "http://xml.org/sax/properties/xml-string"; /** * Declare handler name */ ! private static final String DECL_HANDLER = ! "http://xml.org/sax/properties/declaration-handler"; /** * Lexical handler name */ ! private static final String LEXICAL_HANDLER = ! "http://xml.org/sax/properties/lexical-handler"; /** * According to the SAX2 specs, All XMLReaders are required to recognize the ! * http://xml.org/sax/features/namespaces feature names. ! * This test case is to test this. */ @Test ! public void featureNS01() { ! try { SAXParserFactory spf = SAXParserFactory.newInstance(); XMLReader xmlReader = spf.newSAXParser().getXMLReader(); assertFalse(xmlReader.getFeature(NAMESPACES)); - } catch (ParserConfigurationException | SAXException ex) { - failUnexpected(ex); - } } /** * According to the SAX2 specs, All XMLReaders are required to recognize the ! * http://xml.org/sax/features/namespaces feature names. ! * This test case is to test this. */ @Test ! public void featureNS02() { ! try { SAXParserFactory spf = SAXParserFactory.newInstance(); spf.setNamespaceAware(true); XMLReader xmlReader = spf.newSAXParser().getXMLReader(); assertTrue(xmlReader.getFeature(NAMESPACES)); - } catch (ParserConfigurationException | SAXException ex) { - failUnexpected(ex); - } } /** * Obtain http://xml.org/sax/features/namespaces feature name after it's * just set. Expect it's same as set value. */ @Test ! public void featureNS03() { ! try { SAXParserFactory spf = SAXParserFactory.newInstance(); XMLReader xmlReader = spf.newSAXParser().getXMLReader(); xmlReader.setFeature(NAMESPACES, true); assertTrue(xmlReader.getFeature(NAMESPACES)); xmlReader.setFeature(NAMESPACES, false); assertFalse(xmlReader.getFeature(NAMESPACES)); - } catch (ParserConfigurationException | SAXException ex) { - failUnexpected(ex); - } } /** * According to the SAX2 specs, All XMLReaders are required to recognize the ! * http://xml.org/sax/features/namespace-prefixes feature names. ! * This test case is to test this. */ @Test ! public void featureNSP01() { ! try { SAXParserFactory spf = SAXParserFactory.newInstance(); XMLReader xmlReader = spf.newSAXParser().getXMLReader(); assertTrue(xmlReader.getFeature(NAMESPACE_PREFIXES)); - - } catch (ParserConfigurationException | SAXException ex) { - failUnexpected(ex); - } } /** * According to the SAX2 specs, All XMLReaders are required to recognize the ! * http://xml.org/sax/features/namespace-prefixes feature names. ! * This test case is to test this. */ @Test ! public void featureNSP02() { ! try { SAXParserFactory spf = SAXParserFactory.newInstance(); spf.setNamespaceAware(true); XMLReader xmlReader = spf.newSAXParser().getXMLReader(); assertFalse(xmlReader.getFeature(NAMESPACE_PREFIXES)); - } catch (ParserConfigurationException | SAXException ex) { - failUnexpected(ex); - } } /** * Obtain http://xml.org/sax/features/namespaces-prefixes feature name after * it's just set. Expect it's same as set value. */ @Test ! public void featureNSP03() { ! try { SAXParserFactory spf = SAXParserFactory.newInstance(); XMLReader xmlReader = spf.newSAXParser().getXMLReader(); xmlReader.setFeature(NAMESPACE_PREFIXES, true); assertTrue(xmlReader.getFeature(NAMESPACE_PREFIXES)); xmlReader.setFeature(NAMESPACE_PREFIXES, false); assertFalse(xmlReader.getFeature(NAMESPACE_PREFIXES)); - } catch (ParserConfigurationException | SAXException ex) { - failUnexpected(ex); - } } /** * getFeature returns true if a feature has not been preset when namespace * awareness is set. */ @Test ! public void featureSI01() { ! try { SAXParserFactory spf = SAXParserFactory.newInstance(); spf.setNamespaceAware(true); XMLReader xmlReader = spf.newSAXParser().getXMLReader(); assertTrue(xmlReader.getFeature(STRING_INTERNING)); - } catch (SAXException | ParserConfigurationException ex) { - failUnexpected(ex); - } } /** * getFeature with validation feature name returns the value that * setValidation set. */ @Test ! public void featureV01() { ! try { SAXParserFactory spf = SAXParserFactory.newInstance(); assertFalse(spf.newSAXParser().getXMLReader().getFeature(VALIDATION)); spf.setValidating(true); assertTrue(spf.newSAXParser().getXMLReader().getFeature(VALIDATION)); - } catch (SAXException | ParserConfigurationException ex) { - failUnexpected(ex); - } } /** * getFeature returns the value that a feature has been preset as when * namespace awareness is set. */ @Test ! public void featureV02() { ! try { SAXParserFactory spf = SAXParserFactory.newInstance(); XMLReader xmlReader = spf.newSAXParser().getXMLReader(); xmlReader.setFeature(VALIDATION, true); assertTrue(xmlReader.getFeature(VALIDATION)); - xmlReader.setFeature(VALIDATION, false); assertFalse(xmlReader.getFeature(VALIDATION)); - } catch (SAXException | ParserConfigurationException ex) { - failUnexpected(ex); - } } /** * getFeature returns true if a feature has not been preset when namespace * awareness is set. */ @Test ! public void featureEGE01() { ! try { SAXParserFactory spf = SAXParserFactory.newInstance(); spf.setNamespaceAware(true); XMLReader xmlReader = spf.newSAXParser().getXMLReader(); assertTrue(xmlReader.getFeature(EXTERNAL_G_ENTITIES)); - } catch (SAXException | ParserConfigurationException ex) { - failUnexpected(ex); - } } /** * getFeature returns false if a feature has been preset as false when * namespace awareness is set. */ @Test ! public void featureEGE02() { ! try { SAXParserFactory spf = SAXParserFactory.newInstance(); spf.setNamespaceAware(true); XMLReader xmlReader = spf.newSAXParser().getXMLReader(); xmlReader.setFeature(EXTERNAL_G_ENTITIES, false); assertFalse(xmlReader.getFeature(EXTERNAL_G_ENTITIES)); - } catch (SAXException | ParserConfigurationException ex) { - failUnexpected(ex); - } } /** * getFeature returns true if a feature has not been preset when namespace * awareness is set. */ @Test ! public void featureEPE01() { ! try { SAXParserFactory spf = SAXParserFactory.newInstance(); spf.setNamespaceAware(true); XMLReader xmlReader = spf.newSAXParser().getXMLReader(); assertTrue(xmlReader.getFeature(EXTERNAL_P_ENTITIES)); - } catch (SAXException | ParserConfigurationException ex) { - failUnexpected(ex); - } } /** * getFeature returns false if a feature has been preset as false when * namespace awareness is set. */ @Test ! public void featureEPE02() { ! try { SAXParserFactory spf = SAXParserFactory.newInstance(); spf.setNamespaceAware(true); XMLReader xmlReader = spf.newSAXParser().getXMLReader(); xmlReader.setFeature(EXTERNAL_P_ENTITIES, false); assertFalse(xmlReader.getFeature(EXTERNAL_P_ENTITIES)); - } catch (SAXException | ParserConfigurationException ex) { - failUnexpected(ex); - } } /** * getFeature with a unknown feature name throws SAXNotRecognizedException. ! * @throws SAXNotRecognizedException If the feature value can't be assigned ! * or retrieved. */ @Test(expectedExceptions = SAXNotRecognizedException.class) ! public void featureNE01() throws SAXNotRecognizedException { ! try { SAXParserFactory spf = SAXParserFactory.newInstance(); spf.setNamespaceAware(true); ! XMLReader xmlReader = spf.newSAXParser().getXMLReader(); ! boolean noMeaningFeature = xmlReader.getFeature("no-meaning-feature"); ! } catch(SAXNotRecognizedException ex) { ! throw ex; ! } catch (SAXException | ParserConfigurationException ex) { ! failUnexpected(ex); ! } } /** * No exception expected when set entity resolver as simple entity resolver. */ @Test ! public void entity01() { ! try { SAXParserFactory spf = SAXParserFactory.newInstance(); spf.setNamespaceAware(true); XMLReader xmlReader = spf.newSAXParser().getXMLReader(); XMLFilterImpl xmlFilter = new XMLFilterImpl(); xmlReader.setEntityResolver(xmlFilter); ! assertNotNull(xmlReader.getEntityResolver()); ! } catch (SAXException | ParserConfigurationException ex) { ! failUnexpected(ex); ! } } /** * No NPE expected when set entity resolver as null. */ @Test ! public void entity02() { ! try { SAXParserFactory spf = SAXParserFactory.newInstance(); spf.setNamespaceAware(true); ! XMLReader xmlReader = spf.newSAXParser().getXMLReader(); ! xmlReader.setEntityResolver(null); ! } catch (SAXException | ParserConfigurationException ex) { ! failUnexpected(ex); ! } } /** * No exception expected when set DTD handler as simple DTD handler. */ @Test ! public void dtdhandler01() { ! try { SAXParserFactory spf = SAXParserFactory.newInstance(); spf.setNamespaceAware(true); XMLReader xmlReader = spf.newSAXParser().getXMLReader(); XMLFilterImpl xmlFilter = new XMLFilterImpl(); xmlReader.setDTDHandler(xmlFilter); ! assertNotNull(xmlReader.getDTDHandler()); ! } catch (SAXException | ParserConfigurationException ex) { ! failUnexpected(ex); ! } } /** * No NPE expected when set DTD handler as null. */ @Test ! public void dtdhandler02() { ! try { SAXParserFactory spf = SAXParserFactory.newInstance(); spf.setNamespaceAware(true); ! XMLReader xmlReader = spf.newSAXParser().getXMLReader(); ! xmlReader.setDTDHandler(null); ! } catch (SAXException | ParserConfigurationException ex) { ! failUnexpected(ex); ! } } /** * No exception expected when set content handler as simple content handler. */ @Test ! public void contenthandler01() { ! try { SAXParserFactory spf = SAXParserFactory.newInstance(); spf.setNamespaceAware(true); XMLReader xmlReader = spf.newSAXParser().getXMLReader(); XMLFilterImpl xmlFilter = new XMLFilterImpl(); xmlReader.setContentHandler(xmlFilter); ! assertNotNull(xmlReader.getContentHandler()); ! } catch (SAXException | ParserConfigurationException ex) { ! failUnexpected(ex); ! } } /** * No NPE expected when set content handler as null. */ @Test ! public void contenthandler02() { ! try { SAXParserFactory spf = SAXParserFactory.newInstance(); spf.setNamespaceAware(true); ! XMLReader xmlReader = spf.newSAXParser().getXMLReader(); ! xmlReader.setContentHandler(null); ! } catch (SAXException | ParserConfigurationException ex) { ! failUnexpected(ex); ! } } /** * No exception expected when set content handler as simple error handler. */ @Test ! public void errorhandler01() { ! try { SAXParserFactory spf = SAXParserFactory.newInstance(); spf.setNamespaceAware(true); XMLReader xmlReader = spf.newSAXParser().getXMLReader(); xmlReader.setErrorHandler(new XMLFilterImpl()); assertNotNull(xmlReader.getErrorHandler()); - } catch (SAXException | ParserConfigurationException ex) { - failUnexpected(ex); - } } /** * No NPE expected when set error handler as null. */ @Test ! public void errorhandler02() { ! try { SAXParserFactory spf = SAXParserFactory.newInstance(); spf.setNamespaceAware(true); XMLReader xmlReader = spf.newSAXParser().getXMLReader(); xmlReader.setErrorHandler(null); - } catch (SAXException | ParserConfigurationException ex) { - failUnexpected(ex); - } } /** * Parse a null input source throw NPE. */ @Test(expectedExceptions = NullPointerException.class) ! public void parse01() { ! try { SAXParserFactory spf = SAXParserFactory.newInstance(); spf.setNamespaceAware(true); ! XMLReader xmlReader = spf.newSAXParser().getXMLReader(); ! xmlReader.parse((InputSource)null); ! } catch (SAXException | ParserConfigurationException | IOException ex) { ! failUnexpected(ex); } } /** * Unit test for parse a error-formatted file. SAXException is expected. ! * @throws org.xml.sax.SAXException parsing failed. ! */ ! @Test(expectedExceptions = SAXException.class) ! public void parse02() throws SAXException { ! try (FileInputStream fis = new FileInputStream(XML_DIR + "invalid.xml")){ SAXParserFactory spf = SAXParserFactory.newInstance(); spf.setNamespaceAware(true); ! XMLReader xmlReader = spf.newSAXParser().getXMLReader(); ! InputSource is = new InputSource(fis); ! xmlReader.parse(is); ! } catch (ParserConfigurationException | IOException ex) { ! failUnexpected(ex); } } /** * Unit test for parse a well-formatted file. No exception is expected. ! */ ! @Test ! public void parse03(){ try (FileInputStream fis = new FileInputStream(XML_DIR + "correct2.xml")) { SAXParserFactory spf = SAXParserFactory.newInstance(); spf.setNamespaceAware(true); ! XMLReader xmlReader = spf.newSAXParser().getXMLReader(); ! InputSource is = new InputSource(fis); ! xmlReader.parse(is); ! } catch (IOException | SAXException | ParserConfigurationException ex) { ! failUnexpected(ex); } } /** ! * Modified by IBM ! * Xerces does not support this feature and it is not mandatory ! * @throws org.xml.sax.SAXNotSupportedException */ @Test(expectedExceptions = SAXNotSupportedException.class) ! public void xrProperty01() throws SAXNotSupportedException { ! try { SAXParserFactory spf = SAXParserFactory.newInstance(); XMLReader xmlReader = spf.newSAXParser().getXMLReader(); xmlReader.getProperty(XML_STRING); - } catch(SAXNotSupportedException ex) { - throw ex; - } catch (SAXException | ParserConfigurationException ex){ - failUnexpected(ex); - } } /** * SAXNotSupportedException thrown if property name is known but no value * assigned to this property. ! * @throws org.xml.sax.SAXNotSupportedException when XMLReader recognizes ! * the property name but cannot determine its value at this time. */ @Test(expectedExceptions = SAXNotSupportedException.class) ! public void xrProperty02() throws SAXNotSupportedException { ! try { SAXParserFactory spf = SAXParserFactory.newInstance(); XMLReader xmlReader = spf.newSAXParser().getXMLReader(); assertNull(xmlReader.getProperty(DOM_NODE)); - } catch (SAXNotSupportedException ex) { - throw ex; - } catch (SAXException | ParserConfigurationException ex){ - failUnexpected(ex); - } } - /** * XMLReader.getProperty returns null if LEXICAL_HANDLER wasn't set. */ @Test ! public void xrProperty03() { ! try { SAXParserFactory spf = SAXParserFactory.newInstance(); XMLReader xmlReader = spf.newSAXParser().getXMLReader(); assertNull(xmlReader.getProperty(LEXICAL_HANDLER)); - } catch (SAXException | ParserConfigurationException ex){ - failUnexpected(ex); - } } /** * XMLReader.getProperty returns null if DECL_HANDLER wasn't set. */ @Test ! public void xrProperty04() { ! try { SAXParserFactory spf = SAXParserFactory.newInstance(); XMLReader xmlReader = spf.newSAXParser().getXMLReader(); assertNull(xmlReader.getProperty(DECL_HANDLER)); - } catch (SAXException | ParserConfigurationException ex){ - failUnexpected(ex); - } } /** * XMLReader.setProperty/getProperty for LEXICAL_HANDLER unit test. */ @Test ! public void xrProperty05() { ! try { SAXParserFactory spf = SAXParserFactory.newInstance(); XMLReader xmlReader = spf.newSAXParser().getXMLReader(); MyLexicalHandler myLexicalHandler = new MyLexicalHandler(); xmlReader.setProperty(LEXICAL_HANDLER, myLexicalHandler); assertNotNull(xmlReader.getProperty(LEXICAL_HANDLER)); - } catch (SAXException | ParserConfigurationException ex){ - failUnexpected(ex); - } } /** * XMLReader.setProperty/getProperty for DECL_HANDLER unit test. */ @Test ! public void xrProperty06() { ! try { SAXParserFactory spf = SAXParserFactory.newInstance(); XMLReader xmlReader = spf.newSAXParser().getXMLReader(); MyDeclHandler myDeclHandler = new MyDeclHandler(); xmlReader.setProperty(DECL_HANDLER, myDeclHandler); assertNotNull(xmlReader.getProperty(DECL_HANDLER)); - } catch (ParserConfigurationException | SAXException ex){ - failUnexpected(ex); - } } } /** * Simple LexicalHandler that skips every lexical event. */ class MyLexicalHandler implements LexicalHandler { /** * Report an XML comment anywhere in the document. * * @param ch An array holding the characters in the comment. * @param start The starting position in the array. --- 98,640 ---- private static final String XML_STRING = "http://xml.org/sax/properties/xml-string"; /** * Declare handler name */ ! private static final String DECL_HANDLER ! = "http://xml.org/sax/properties/declaration-handler"; /** * Lexical handler name */ ! private static final String LEXICAL_HANDLER ! = "http://xml.org/sax/properties/lexical-handler"; /** * According to the SAX2 specs, All XMLReaders are required to recognize the ! * http://xml.org/sax/features/namespaces feature names. This test case is ! * to test this. ! * ! * @throws SAXException If there is a problem processing the document. ! * @throws ParserConfigurationException if a DocumentBuilder cannot be ! * created which satisfies the configuration requested. */ @Test ! public void featureNS01() throws ParserConfigurationException, SAXException { SAXParserFactory spf = SAXParserFactory.newInstance(); XMLReader xmlReader = spf.newSAXParser().getXMLReader(); assertFalse(xmlReader.getFeature(NAMESPACES)); } /** * According to the SAX2 specs, All XMLReaders are required to recognize the ! * http://xml.org/sax/features/namespaces feature names. This test case is ! * to test this. ! * ! * @throws SAXException If there is a problem processing the document. ! * @throws ParserConfigurationException if a DocumentBuilder cannot be ! * created which satisfies the configuration requested. */ @Test ! public void featureNS02() throws ParserConfigurationException, SAXException { SAXParserFactory spf = SAXParserFactory.newInstance(); spf.setNamespaceAware(true); XMLReader xmlReader = spf.newSAXParser().getXMLReader(); assertTrue(xmlReader.getFeature(NAMESPACES)); } /** * Obtain http://xml.org/sax/features/namespaces feature name after it's * just set. Expect it's same as set value. + * + * @throws SAXException If there is a problem processing the document. + * @throws ParserConfigurationException if a DocumentBuilder cannot be + * created which satisfies the configuration requested. */ @Test ! public void featureNS03() throws ParserConfigurationException, SAXException { SAXParserFactory spf = SAXParserFactory.newInstance(); XMLReader xmlReader = spf.newSAXParser().getXMLReader(); xmlReader.setFeature(NAMESPACES, true); assertTrue(xmlReader.getFeature(NAMESPACES)); xmlReader.setFeature(NAMESPACES, false); assertFalse(xmlReader.getFeature(NAMESPACES)); } /** * According to the SAX2 specs, All XMLReaders are required to recognize the ! * http://xml.org/sax/features/namespace-prefixes feature names. This test ! * case is to test this. ! * ! * @throws SAXException If there is a problem processing the document. ! * @throws ParserConfigurationException if a DocumentBuilder cannot be ! * created which satisfies the configuration requested. */ @Test ! public void featureNSP01() throws ParserConfigurationException, SAXException { SAXParserFactory spf = SAXParserFactory.newInstance(); XMLReader xmlReader = spf.newSAXParser().getXMLReader(); assertTrue(xmlReader.getFeature(NAMESPACE_PREFIXES)); } /** * According to the SAX2 specs, All XMLReaders are required to recognize the ! * http://xml.org/sax/features/namespace-prefixes feature names. This test ! * case is to test this. ! * ! * @throws SAXException If there is a problem processing the document. ! * @throws ParserConfigurationException if a DocumentBuilder cannot be ! * created which satisfies the configuration requested. */ @Test ! public void featureNSP02() throws ParserConfigurationException, SAXException { SAXParserFactory spf = SAXParserFactory.newInstance(); spf.setNamespaceAware(true); XMLReader xmlReader = spf.newSAXParser().getXMLReader(); assertFalse(xmlReader.getFeature(NAMESPACE_PREFIXES)); } /** * Obtain http://xml.org/sax/features/namespaces-prefixes feature name after * it's just set. Expect it's same as set value. + * + * @throws SAXException If there is a problem processing the document. + * @throws ParserConfigurationException if a DocumentBuilder cannot be + * created which satisfies the configuration requested. */ @Test ! public void featureNSP03() throws ParserConfigurationException, SAXException { SAXParserFactory spf = SAXParserFactory.newInstance(); XMLReader xmlReader = spf.newSAXParser().getXMLReader(); xmlReader.setFeature(NAMESPACE_PREFIXES, true); assertTrue(xmlReader.getFeature(NAMESPACE_PREFIXES)); xmlReader.setFeature(NAMESPACE_PREFIXES, false); assertFalse(xmlReader.getFeature(NAMESPACE_PREFIXES)); } /** * getFeature returns true if a feature has not been preset when namespace * awareness is set. + * + * @throws SAXException If there is a problem processing the document. + * @throws ParserConfigurationException if a DocumentBuilder cannot be + * created which satisfies the configuration requested. */ @Test ! public void featureSI01() throws ParserConfigurationException, SAXException { SAXParserFactory spf = SAXParserFactory.newInstance(); spf.setNamespaceAware(true); XMLReader xmlReader = spf.newSAXParser().getXMLReader(); assertTrue(xmlReader.getFeature(STRING_INTERNING)); } /** * getFeature with validation feature name returns the value that * setValidation set. + * + * @throws SAXException If there is a problem processing the document. + * @throws ParserConfigurationException if a DocumentBuilder cannot be + * created which satisfies the configuration requested. */ @Test ! public void featureV01() throws ParserConfigurationException, SAXException { SAXParserFactory spf = SAXParserFactory.newInstance(); assertFalse(spf.newSAXParser().getXMLReader().getFeature(VALIDATION)); spf.setValidating(true); assertTrue(spf.newSAXParser().getXMLReader().getFeature(VALIDATION)); } /** * getFeature returns the value that a feature has been preset as when * namespace awareness is set. + * + * @throws SAXException If there is a problem processing the document. + * @throws ParserConfigurationException if a DocumentBuilder cannot be + * created which satisfies the configuration requested. */ @Test ! public void featureV02() throws ParserConfigurationException, SAXException { SAXParserFactory spf = SAXParserFactory.newInstance(); XMLReader xmlReader = spf.newSAXParser().getXMLReader(); xmlReader.setFeature(VALIDATION, true); assertTrue(xmlReader.getFeature(VALIDATION)); xmlReader.setFeature(VALIDATION, false); assertFalse(xmlReader.getFeature(VALIDATION)); } /** * getFeature returns true if a feature has not been preset when namespace * awareness is set. + * + * @throws SAXException If there is a problem processing the document. + * @throws ParserConfigurationException if a DocumentBuilder cannot be + * created which satisfies the configuration requested. */ @Test ! public void featureEGE01() throws ParserConfigurationException, SAXException { SAXParserFactory spf = SAXParserFactory.newInstance(); spf.setNamespaceAware(true); XMLReader xmlReader = spf.newSAXParser().getXMLReader(); assertTrue(xmlReader.getFeature(EXTERNAL_G_ENTITIES)); } /** * getFeature returns false if a feature has been preset as false when * namespace awareness is set. + * + * @throws SAXException If there is a problem processing the document. + * @throws ParserConfigurationException if a DocumentBuilder cannot be + * created which satisfies the configuration requested. */ @Test ! public void featureEGE02() throws ParserConfigurationException, SAXException { SAXParserFactory spf = SAXParserFactory.newInstance(); spf.setNamespaceAware(true); XMLReader xmlReader = spf.newSAXParser().getXMLReader(); xmlReader.setFeature(EXTERNAL_G_ENTITIES, false); assertFalse(xmlReader.getFeature(EXTERNAL_G_ENTITIES)); } /** * getFeature returns true if a feature has not been preset when namespace * awareness is set. + * + * @throws SAXException If there is a problem processing the document. + * @throws ParserConfigurationException if a DocumentBuilder cannot be + * created which satisfies the configuration requested. */ @Test ! public void featureEPE01() throws ParserConfigurationException, SAXException { SAXParserFactory spf = SAXParserFactory.newInstance(); spf.setNamespaceAware(true); XMLReader xmlReader = spf.newSAXParser().getXMLReader(); assertTrue(xmlReader.getFeature(EXTERNAL_P_ENTITIES)); } /** * getFeature returns false if a feature has been preset as false when * namespace awareness is set. + * + * @throws SAXException If there is a problem processing the document. + * @throws ParserConfigurationException if a DocumentBuilder cannot be + * created which satisfies the configuration requested. */ @Test ! public void featureEPE02() throws ParserConfigurationException, SAXException { SAXParserFactory spf = SAXParserFactory.newInstance(); spf.setNamespaceAware(true); XMLReader xmlReader = spf.newSAXParser().getXMLReader(); xmlReader.setFeature(EXTERNAL_P_ENTITIES, false); assertFalse(xmlReader.getFeature(EXTERNAL_P_ENTITIES)); } /** * getFeature with a unknown feature name throws SAXNotRecognizedException. ! * ! * @throws SAXException If there is a problem processing the document. ! * @throws ParserConfigurationException if a DocumentBuilder cannot be ! * created which satisfies the configuration requested. */ @Test(expectedExceptions = SAXNotRecognizedException.class) ! public void featureNE01() throws ParserConfigurationException, SAXException { SAXParserFactory spf = SAXParserFactory.newInstance(); spf.setNamespaceAware(true); ! spf.newSAXParser().getXMLReader().getFeature("no-meaning-feature"); } /** * No exception expected when set entity resolver as simple entity resolver. + * + * @throws SAXException If there is a problem processing the document. + * @throws ParserConfigurationException if a DocumentBuilder cannot be + * created which satisfies the configuration requested. */ @Test ! public void entity01() throws ParserConfigurationException, SAXException { SAXParserFactory spf = SAXParserFactory.newInstance(); spf.setNamespaceAware(true); XMLReader xmlReader = spf.newSAXParser().getXMLReader(); XMLFilterImpl xmlFilter = new XMLFilterImpl(); xmlReader.setEntityResolver(xmlFilter); ! assertEquals(xmlReader.getEntityResolver(), xmlFilter); } /** * No NPE expected when set entity resolver as null. + * + * @throws SAXException If there is a problem processing the document. + * @throws ParserConfigurationException if a DocumentBuilder cannot be + * created which satisfies the configuration requested. */ @Test ! public void entity02() throws ParserConfigurationException, SAXException { SAXParserFactory spf = SAXParserFactory.newInstance(); spf.setNamespaceAware(true); ! spf.newSAXParser().getXMLReader().setEntityResolver(null); } /** * No exception expected when set DTD handler as simple DTD handler. + * + * @throws SAXException If there is a problem processing the document. + * @throws ParserConfigurationException if a DocumentBuilder cannot be + * created which satisfies the configuration requested. */ @Test ! public void dtdhandler01() throws ParserConfigurationException, SAXException { SAXParserFactory spf = SAXParserFactory.newInstance(); spf.setNamespaceAware(true); XMLReader xmlReader = spf.newSAXParser().getXMLReader(); XMLFilterImpl xmlFilter = new XMLFilterImpl(); xmlReader.setDTDHandler(xmlFilter); ! assertEquals(xmlReader.getDTDHandler(), xmlFilter); } /** * No NPE expected when set DTD handler as null. + * + * @throws SAXException If there is a problem processing the document. + * @throws ParserConfigurationException if a DocumentBuilder cannot be + * created which satisfies the configuration requested. */ @Test ! public void dtdhandler02() throws ParserConfigurationException, SAXException { SAXParserFactory spf = SAXParserFactory.newInstance(); spf.setNamespaceAware(true); ! spf.newSAXParser().getXMLReader().setDTDHandler(null); } /** * No exception expected when set content handler as simple content handler. + * + * @throws SAXException If there is a problem processing the document. + * @throws ParserConfigurationException if a DocumentBuilder cannot be + * created which satisfies the configuration requested. */ @Test ! public void contenthandler01() throws ParserConfigurationException, SAXException { SAXParserFactory spf = SAXParserFactory.newInstance(); spf.setNamespaceAware(true); XMLReader xmlReader = spf.newSAXParser().getXMLReader(); XMLFilterImpl xmlFilter = new XMLFilterImpl(); xmlReader.setContentHandler(xmlFilter); ! assertEquals(xmlReader.getContentHandler(), xmlFilter); } /** * No NPE expected when set content handler as null. + * + * @throws SAXException If there is a problem processing the document. + * @throws ParserConfigurationException if a DocumentBuilder cannot be + * created which satisfies the configuration requested. */ @Test ! public void contenthandler02() throws ParserConfigurationException, SAXException { SAXParserFactory spf = SAXParserFactory.newInstance(); spf.setNamespaceAware(true); ! spf.newSAXParser().getXMLReader().setContentHandler(null); } /** * No exception expected when set content handler as simple error handler. + * + * @throws SAXException If there is a problem processing the document. + * @throws ParserConfigurationException if a DocumentBuilder cannot be + * created which satisfies the configuration requested. */ @Test ! public void errorhandler01() throws ParserConfigurationException, SAXException { SAXParserFactory spf = SAXParserFactory.newInstance(); spf.setNamespaceAware(true); XMLReader xmlReader = spf.newSAXParser().getXMLReader(); xmlReader.setErrorHandler(new XMLFilterImpl()); assertNotNull(xmlReader.getErrorHandler()); } /** * No NPE expected when set error handler as null. + * + * @throws SAXException If there is a problem processing the document. + * @throws ParserConfigurationException if a DocumentBuilder cannot be + * created which satisfies the configuration requested. */ @Test ! public void errorhandler02() throws ParserConfigurationException, SAXException { SAXParserFactory spf = SAXParserFactory.newInstance(); spf.setNamespaceAware(true); XMLReader xmlReader = spf.newSAXParser().getXMLReader(); xmlReader.setErrorHandler(null); } /** * Parse a null input source throw NPE. + * + * @throws SAXException If there is a problem processing the document. + * @throws ParserConfigurationException if a DocumentBuilder cannot be + * created which satisfies the configuration requested. + * @throws IOException An IO exception from the parser, possibly from a byte + * stream or character stream supplied by the application. */ @Test(expectedExceptions = NullPointerException.class) ! public void parse01() throws ParserConfigurationException, SAXException, ! IOException { SAXParserFactory spf = SAXParserFactory.newInstance(); spf.setNamespaceAware(true); ! spf.newSAXParser().getXMLReader().parse((InputSource) null); ! } ! ! /** ! * Save system property for restoring. ! */ ! @BeforeGroups (groups = {"readLocalFiles"}) ! public void setFilePermissions() { ! setPermissions(new FilePermission(XML_DIR + "/-", "read")); } + + /** + * Restore the system property. + */ + @AfterGroups (groups = {"readLocalFiles"}) + public void restoreFilePermissions() { + setPermissions(); } /** * Unit test for parse a error-formatted file. SAXException is expected. ! * ! * @throws SAXException If there is a problem processing the document. ! * @throws ParserConfigurationException if a DocumentBuilder cannot be ! * created which satisfies the configuration requested. ! * @throws IOException An IO exception from the parser, possibly from a byte ! * stream or character stream supplied by the application. ! */ ! @Test(groups = {"readLocalFiles"}, expectedExceptions = SAXException.class) ! public void parse02() throws ParserConfigurationException, SAXException, ! IOException { ! try (FileInputStream fis = new FileInputStream(XML_DIR + "invalid.xml")) { SAXParserFactory spf = SAXParserFactory.newInstance(); spf.setNamespaceAware(true); ! spf.newSAXParser().getXMLReader().parse(new InputSource(fis)); } } /** * Unit test for parse a well-formatted file. No exception is expected. ! * ! * @throws SAXException If there is a problem processing the document. ! * @throws ParserConfigurationException if a DocumentBuilder cannot be ! * created which satisfies the configuration requested. ! * @throws IOException An IO exception from the parser, possibly from a byte ! * stream or character stream supplied by the application. ! */ ! @Test(groups = {"readLocalFiles"}) ! public void parse03() throws ParserConfigurationException, SAXException, ! IOException { try (FileInputStream fis = new FileInputStream(XML_DIR + "correct2.xml")) { SAXParserFactory spf = SAXParserFactory.newInstance(); spf.setNamespaceAware(true); ! spf.newSAXParser().getXMLReader().parse(new InputSource(fis)); } } /** ! * Modified by IBM Xerces does not support this feature and it is not ! * mandatory. ! * ! * @throws SAXException If there is a problem processing the document. ! * @throws ParserConfigurationException if a DocumentBuilder cannot be ! * created which satisfies the configuration requested. */ @Test(expectedExceptions = SAXNotSupportedException.class) ! public void xrProperty01() throws ParserConfigurationException, SAXException { SAXParserFactory spf = SAXParserFactory.newInstance(); XMLReader xmlReader = spf.newSAXParser().getXMLReader(); xmlReader.getProperty(XML_STRING); } /** * SAXNotSupportedException thrown if property name is known but no value * assigned to this property. ! * ! * @throws SAXException If there is a problem processing the document. ! * @throws ParserConfigurationException if a DocumentBuilder cannot be ! * created which satisfies the configuration requested. */ @Test(expectedExceptions = SAXNotSupportedException.class) ! public void xrProperty02() throws ParserConfigurationException, SAXException { SAXParserFactory spf = SAXParserFactory.newInstance(); XMLReader xmlReader = spf.newSAXParser().getXMLReader(); assertNull(xmlReader.getProperty(DOM_NODE)); } /** * XMLReader.getProperty returns null if LEXICAL_HANDLER wasn't set. + * + * @throws SAXException If there is a problem processing the document. + * @throws ParserConfigurationException if a DocumentBuilder cannot be + * created which satisfies the configuration requested. */ @Test ! public void xrProperty03() throws ParserConfigurationException, SAXException { SAXParserFactory spf = SAXParserFactory.newInstance(); XMLReader xmlReader = spf.newSAXParser().getXMLReader(); assertNull(xmlReader.getProperty(LEXICAL_HANDLER)); } /** * XMLReader.getProperty returns null if DECL_HANDLER wasn't set. + * + * @throws SAXException If there is a problem processing the document. + * @throws ParserConfigurationException if a DocumentBuilder cannot be + * created which satisfies the configuration requested. */ @Test ! public void xrProperty04() throws ParserConfigurationException, SAXException { SAXParserFactory spf = SAXParserFactory.newInstance(); XMLReader xmlReader = spf.newSAXParser().getXMLReader(); assertNull(xmlReader.getProperty(DECL_HANDLER)); } /** * XMLReader.setProperty/getProperty for LEXICAL_HANDLER unit test. + * + * @throws SAXException If there is a problem processing the document. + * @throws ParserConfigurationException if a DocumentBuilder cannot be + * created which satisfies the configuration requested. */ @Test ! public void xrProperty05() throws ParserConfigurationException, SAXException { SAXParserFactory spf = SAXParserFactory.newInstance(); XMLReader xmlReader = spf.newSAXParser().getXMLReader(); MyLexicalHandler myLexicalHandler = new MyLexicalHandler(); xmlReader.setProperty(LEXICAL_HANDLER, myLexicalHandler); assertNotNull(xmlReader.getProperty(LEXICAL_HANDLER)); } /** * XMLReader.setProperty/getProperty for DECL_HANDLER unit test. + * + * @throws SAXException If there is a problem processing the document. + * @throws ParserConfigurationException if a DocumentBuilder cannot be + * created which satisfies the configuration requested. */ @Test ! public void xrProperty06() throws ParserConfigurationException, SAXException { SAXParserFactory spf = SAXParserFactory.newInstance(); XMLReader xmlReader = spf.newSAXParser().getXMLReader(); MyDeclHandler myDeclHandler = new MyDeclHandler(); xmlReader.setProperty(DECL_HANDLER, myDeclHandler); assertNotNull(xmlReader.getProperty(DECL_HANDLER)); } } /** * Simple LexicalHandler that skips every lexical event. */ class MyLexicalHandler implements LexicalHandler { + /** * Report an XML comment anywhere in the document. * * @param ch An array holding the characters in the comment. * @param start The starting position in the array.
*** 665,676 **** /** * Report the start of DTD declarations, if any. * * @param name The document type name. ! * @param publicId The declared public identifier for the external DTD subset. ! * @param systemId The declared system identifier for the external DTD subset. */ @Override public void startDTD(String name, String publicId, String systemId) { } --- 676,689 ---- /** * Report the start of DTD declarations, if any. * * @param name The document type name. ! * @param publicId The declared public identifier for the external DTD ! * subset. ! * @param systemId The declared system identifier for the external DTD ! * subset. */ @Override public void startDTD(String name, String publicId, String systemId) { }
*** 686,737 **** /** * Simple DeclHandler that skips every DTD declaration event. */ class MyDeclHandler implements DeclHandler { /** * Report an attribute type declaration. * @param eName The name of the associated element. * @param aName The name of the attribute. * @param type A string representing the attribute type. * @param mode A string representing the attribute defaulting mode ! * ("#IMPLIED", "#REQUIRED", or "#FIXED") or null if ! * none of these applies. ! * @param value A string representing the attribute's default value, ! * or null if there is none. */ @Override public void attributeDecl(String eName, String aName, String type, String valueDefault, String value) { } /** * Report an element type declaration. * @param name The element type name. * @param model The content model as a normalized string. */ @Override public void elementDecl(String name, String model) { } /** * Report a parsed external entity declaration. ! * @param name The name of the entity. If it is a parameter ! * entity, the name will begin with '%'. ! * @param publicId The entity's public identifier, or null if none ! * was given. * @param systemId The entity's system identifier. */ @Override public void externalEntityDecl(String name, String publicId, String systemId) { } /** * Report an internal entity declaration. ! * @param name The name of the entity. If it is a parameter ! * entity, the name will begin with '%'. * @param value The replacement text of the entity. */ @Override public void internalEntityDecl(String name, String value) { } --- 699,754 ---- /** * Simple DeclHandler that skips every DTD declaration event. */ class MyDeclHandler implements DeclHandler { + /** * Report an attribute type declaration. + * * @param eName The name of the associated element. * @param aName The name of the attribute. * @param type A string representing the attribute type. * @param mode A string representing the attribute defaulting mode ! * ("#IMPLIED", "#REQUIRED", or "#FIXED") or null if none of these applies. ! * @param value A string representing the attribute's default value, or null ! * if there is none. */ @Override public void attributeDecl(String eName, String aName, String type, String valueDefault, String value) { } /** * Report an element type declaration. + * * @param name The element type name. * @param model The content model as a normalized string. */ @Override public void elementDecl(String name, String model) { } /** * Report a parsed external entity declaration. ! * ! * @param name The name of the entity. If it is a parameter entity, the name ! * will begin with '%'. ! * @param publicId The entity's public identifier, or null if none was ! * given. * @param systemId The entity's system identifier. */ @Override public void externalEntityDecl(String name, String publicId, String systemId) { } /** * Report an internal entity declaration. ! * ! * @param name The name of the entity. If it is a parameter entity, the name ! * will begin with '%'. * @param value The replacement text of the entity. */ @Override public void internalEntityDecl(String name, String value) { }
< prev index next >