1 /* 2 * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 package org.xml.sax.ptests; 24 25 import java.io.FileInputStream; 26 import java.io.FilePermission; 27 import java.io.IOException; 28 import javax.xml.parsers.ParserConfigurationException; 29 import javax.xml.parsers.SAXParserFactory; 30 import jaxp.library.JAXPBaseTest; 31 import static org.testng.Assert.assertNotNull; 32 import static org.testng.Assert.assertTrue; 33 import org.testng.annotations.Test; 34 import org.xml.sax.HandlerBase; 35 import org.xml.sax.InputSource; 36 import org.xml.sax.SAXException; 37 import org.xml.sax.XMLReader; 38 import org.xml.sax.helpers.XMLReaderAdapter; 39 import static org.xml.sax.ptests.SAXTestConst.XML_DIR; 40 41 /** 42 * Class containing the test cases for XMLReaderAdapter API 43 */ 44 public class XMLReaderAdapterTest extends JAXPBaseTest { 45 /** 46 * http://xml.org/sax/features/namespace-prefixes property name. 47 */ 48 private final static String NM_PREFIXES_PROPERTY 49 = "http://xml.org/sax/features/namespace-prefixes"; 50 51 /** 52 * To test the constructor that uses "org.xml.sax.driver" property 53 * @throws org.xml.sax.SAXException If the embedded driver cannot be 54 * instantiated or if the org.xml.sax.driver property is not specified. 55 */ 56 @Test 57 public void constructor01() throws SAXException { 58 assertNotNull(new XMLReaderAdapter()); 59 } 60 61 /** 62 * To test the constructor that uses XMLReader. 63 * 64 * @throws SAXException If there is a problem processing the document. 65 * @throws ParserConfigurationException if a DocumentBuilder cannot be 66 * created which satisfies the configuration requested. 67 */ 68 @Test 69 public void constructor02() throws ParserConfigurationException, SAXException { 70 XMLReader xmlReader = SAXParserFactory.newInstance().newSAXParser().getXMLReader(); 71 assertNotNull(new XMLReaderAdapter(xmlReader)); 72 } 73 74 /** 75 * To test the parse method. The specification says that this method 76 * will throw an exception if the embedded XMLReader does not support 77 * the http://xml.org/sax/features/namespace-prefixes property. 78 * 79 * @throws SAXException If there is a problem processing the document. 80 * @throws ParserConfigurationException if a DocumentBuilder cannot be 81 * created which satisfies the configuration requested. 82 */ 83 @Test 84 public void nsfeature01() throws ParserConfigurationException, SAXException { 85 XMLReader xmlReader = SAXParserFactory.newInstance().newSAXParser().getXMLReader(); 86 if (!xmlReader.getFeature(NM_PREFIXES_PROPERTY)) { 87 xmlReader.setFeature(NM_PREFIXES_PROPERTY, true); 88 } 89 assertTrue(xmlReader.getFeature(NM_PREFIXES_PROPERTY)); 90 } 91 92 /** 93 * To test the parse method. The specification says that this method 94 * will throw an exception if the embedded XMLReader does not support 95 * the http://xml.org/sax/features/namespace-prefixes property. 96 * 97 * @throws SAXException If there is a problem processing the document. 98 * @throws ParserConfigurationException if a DocumentBuilder cannot be 99 * created which satisfies the configuration requested. 100 * @throws IOException if the file exists but is a directory rather than 101 * a regular file, does not exist but cannot be created, or cannot 102 * be opened for any other reason. 103 */ 104 @Test 105 public void parse01() throws IOException, SAXException, 106 ParserConfigurationException { 107 setPermissions(new FilePermission(XML_DIR + "/-", "read")); 108 try (FileInputStream fis = new FileInputStream(XML_DIR + "namespace1.xml")) { 109 XMLReader xmlReader = SAXParserFactory.newInstance().newSAXParser().getXMLReader(); 110 if (!xmlReader.getFeature(NM_PREFIXES_PROPERTY)) { 111 xmlReader.setFeature(NM_PREFIXES_PROPERTY, true); 112 } 113 XMLReaderAdapter xmlRA = new XMLReaderAdapter(xmlReader); 114 xmlRA.setDocumentHandler(new HandlerBase()); 115 xmlRA.parse(new InputSource(fis)); 116 } 117 setPermissions(); 118 } 119 }