1 /* 2 * Copyright (c) 2003, 2016, 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 static org.testng.Assert.assertNotNull; 26 import static org.testng.Assert.assertTrue; 27 import static org.xml.sax.ptests.SAXTestConst.XML_DIR; 28 29 import java.io.FileInputStream; 30 31 import javax.xml.parsers.SAXParserFactory; 32 33 import org.testng.annotations.Listeners; 34 import org.testng.annotations.Test; 35 import org.xml.sax.HandlerBase; 36 import org.xml.sax.InputSource; 37 import org.xml.sax.SAXException; 38 import org.xml.sax.XMLReader; 39 import org.xml.sax.helpers.XMLReaderAdapter; 40 41 /** 42 * Class containing the test cases for XMLReaderAdapter API 43 */ 44 /* 45 * @test 46 * @library /javax/xml/jaxp/libs 47 * @run testng/othervm -DrunSecMngr=true org.xml.sax.ptests.XMLReaderAdapterTest 48 * @run testng/othervm org.xml.sax.ptests.XMLReaderAdapterTest 49 */ 50 @Listeners({jaxp.library.FilePolicy.class}) 51 public class XMLReaderAdapterTest { 52 /** 53 * http://xml.org/sax/features/namespace-prefixes property name. 54 */ 55 private final static String NM_PREFIXES_PROPERTY 56 = "http://xml.org/sax/features/namespace-prefixes"; 57 58 /** 59 * To test the constructor that uses "org.xml.sax.driver" property 60 * @throws org.xml.sax.SAXException If the embedded driver cannot be 61 * instantiated or if the org.xml.sax.driver property is not specified. 62 */ 63 @Test 64 public void constructor01() throws SAXException { 65 assertNotNull(new XMLReaderAdapter()); 66 } 67 68 /** 69 * To test the constructor that uses XMLReader. 70 * 71 * @throws Exception If any errors occur. 72 */ 73 @Test 74 public void constructor02() throws Exception { 75 XMLReader xmlReader = SAXParserFactory.newInstance().newSAXParser().getXMLReader(); 76 assertNotNull(new XMLReaderAdapter(xmlReader)); 77 } 78 79 /** 80 * To test the parse method. The specification says that this method 81 * will throw an exception if the embedded XMLReader does not support 82 * the http://xml.org/sax/features/namespace-prefixes property. 83 * 84 * @throws Exception If any errors occur. 85 */ 86 @Test 87 public void nsfeature01() throws Exception { 88 XMLReader xmlReader = SAXParserFactory.newInstance().newSAXParser().getXMLReader(); 89 if (!xmlReader.getFeature(NM_PREFIXES_PROPERTY)) { 90 xmlReader.setFeature(NM_PREFIXES_PROPERTY, true); 91 } 92 assertTrue(xmlReader.getFeature(NM_PREFIXES_PROPERTY)); 93 } 94 95 /** 96 * To test the parse method. The specification says that this method 97 * will throw an exception if the embedded XMLReader does not support 98 * the http://xml.org/sax/features/namespace-prefixes property. 99 * 100 * @throws Exception If any errors occur. 101 */ 102 @Test 103 public void parse01() throws Exception { 104 try (FileInputStream fis = new FileInputStream(XML_DIR + "namespace1.xml")) { 105 XMLReader xmlReader = SAXParserFactory.newInstance().newSAXParser().getXMLReader(); 106 if (!xmlReader.getFeature(NM_PREFIXES_PROPERTY)) { 107 xmlReader.setFeature(NM_PREFIXES_PROPERTY, true); 108 } 109 XMLReaderAdapter xmlRA = new XMLReaderAdapter(xmlReader); 110 xmlRA.setDocumentHandler(new HandlerBase()); 111 xmlRA.parse(new InputSource(fis)); 112 } 113 } 114 } 115 116