1 /* 2 * Copyright (c) 2014, 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 24 package sax; 25 26 import java.io.IOException; 27 28 import javax.xml.parsers.ParserConfigurationException; 29 import javax.xml.parsers.SAXParser; 30 import javax.xml.parsers.SAXParserFactory; 31 32 import org.testng.Assert; 33 import org.testng.AssertJUnit; 34 import org.testng.annotations.Listeners; 35 import org.testng.annotations.Test; 36 import org.xml.sax.SAXException; 37 import org.xml.sax.SAXNotRecognizedException; 38 import org.xml.sax.XMLReader; 39 import org.xml.sax.ext.DefaultHandler2; 40 import org.xml.sax.helpers.DefaultHandler; 41 import org.xml.sax.helpers.ParserAdapter; 42 import org.xml.sax.helpers.XMLFilterImpl; 43 import org.xml.sax.helpers.XMLReaderFactory; 44 45 /* 46 * @test 47 * @library /javax/xml/jaxp/libs /javax/xml/jaxp/unittest 48 * @run testng/othervm -DrunSecMngr=true sax.DefaultHandler2Test 49 * @run testng/othervm sax.DefaultHandler2Test 50 * @summary Test DefaultHandler2. 51 */ 52 @Listeners({jaxp.library.FilePolicy.class}) 53 public class DefaultHandler2Test { 54 55 @Test 56 public void testParse01() { 57 System.out.println("===in testParse01==="); 58 try { 59 DefaultHandler handler = new MyDefaultHandler2(); 60 SAXParserFactory saxFac = SAXParserFactory.newInstance(); 61 System.out.println(saxFac.getFeature("http://xml.org/sax/features/use-locator2")); 62 63 // set use-entity-resolver2 as FALSE to use EntityResolver firstly. 64 saxFac.setFeature("http://xml.org/sax/features/use-entity-resolver2", false); 65 saxFac.setValidating(true); 66 67 SAXParser parser = saxFac.newSAXParser(); 68 parser.setProperty("http://xml.org/sax/properties/lexical-handler", handler); 69 parser.setProperty("http://xml.org/sax/properties/declaration-handler", handler); 70 71 parser.parse(this.getClass().getResource("toys.xml").getFile(), handler); 72 } catch (ParserConfigurationException e) { 73 e.printStackTrace(); 74 Assert.fail("ParserConfigurationException in testParse01()"); 75 } catch (SAXException e) { 76 e.printStackTrace(); 77 Assert.fail("SAXException in testParse01()"); 78 } catch (IOException e) { 79 e.printStackTrace(); 80 Assert.fail("IOException in testParse01()"); 81 } 82 } 83 84 @Test 85 public void testParse02() { 86 System.out.println("===in testParse02==="); 87 try { 88 DefaultHandler handler = new MyDefaultHandler2(); 89 SAXParserFactory saxFac = SAXParserFactory.newInstance(); 90 System.out.println(saxFac.getFeature("http://xml.org/sax/features/use-locator2")); 91 92 // Enable namespace parsing 93 System.out.println(saxFac.getFeature("http://xml.org/sax/features/namespaces")); 94 saxFac.setNamespaceAware(true); 95 96 saxFac.setValidating(true); 97 SAXParser parser = saxFac.newSAXParser(); 98 parser.setProperty("http://xml.org/sax/properties/lexical-handler", handler); 99 parser.setProperty("http://xml.org/sax/properties/declaration-handler", handler); 100 101 parser.parse(this.getClass().getResource("toys.xml").getFile(), handler); 102 } catch (ParserConfigurationException e) { 103 e.printStackTrace(); 104 Assert.fail("ParserConfigurationException in testParse02()"); 105 } catch (SAXException e) { 106 e.printStackTrace(); 107 Assert.fail("SAXException in testParse02()"); 108 } catch (IOException e) { 109 e.printStackTrace(); 110 Assert.fail("IOException in testParse02()"); 111 } 112 } 113 114 @Test 115 public void testParse03() { 116 System.out.println("===in testParse03==="); 117 try { 118 DefaultHandler handler = new MyDefaultHandler2(); 119 120 XMLReader xmlReader = XMLReaderFactory.createXMLReader(); 121 xmlReader.setProperty("http://xml.org/sax/properties/declaration-handler", handler); 122 System.out.println("XMLReader : " + xmlReader.getProperty("http://xml.org/sax/properties/declaration-handler")); 123 124 SAXParserFactory saxFac = SAXParserFactory.newInstance(); 125 SAXParser parser = saxFac.newSAXParser(); 126 parser.setProperty("http://xml.org/sax/properties/declaration-handler", handler); 127 System.out.println("SAXParser : " + parser.getProperty("http://xml.org/sax/properties/declaration-handler")); 128 129 // From https://docs.oracle.com/javase/7/docs/api, 130 // ParserAdapter.setProperty() and ParserAdapter.getProperty() does 131 // not support any property currently. 132 try { 133 ParserAdapter adapter = new ParserAdapter(parser.getParser()); 134 System.out.println("ParserAdapter : " + adapter.getProperty("http://xml.org/sax/properties/declaration-handler")); 135 } catch (SAXNotRecognizedException e) { 136 System.out.println("Expected SAXNotRecognizedException since ParserAdapter.getProperty() does not support any property currently"); 137 } 138 try { 139 ParserAdapter adapter = new ParserAdapter(parser.getParser()); 140 adapter.setProperty("http://xml.org/sax/properties/declaration-handler", handler); 141 } catch (SAXNotRecognizedException e) { 142 System.out.println("Expected SAXNotRecognizedException since ParserAdapter.setProperty() does not support any property currently"); 143 } 144 } catch (SAXException e) { 145 e.printStackTrace(); 146 Assert.fail("SAXException in testParse03()"); 147 } catch (ParserConfigurationException e) { 148 e.printStackTrace(); 149 Assert.fail("ParserConfigurationException in testParse03()"); 150 } 151 152 } 153 154 @Test 155 public void testParse04() { 156 System.out.println("===in testParse04==="); 157 try { 158 DefaultHandler handler = new MyDefaultHandler2(); 159 XMLReader xmlReader = XMLReaderFactory.createXMLReader(); 160 System.out.println(xmlReader.getFeature("http://xml.org/sax/features/namespaces")); 161 xmlReader.setProperty("http://xml.org/sax/properties/declaration-handler", handler); 162 xmlReader.setProperty("http://xml.org/sax/properties/lexical-handler", handler); 163 xmlReader.setContentHandler(handler); 164 165 xmlReader.parse(this.getClass().getResource("toys.xml").getFile()); 166 167 } catch (SAXException e) { 168 e.printStackTrace(); 169 Assert.fail("SAXException in testParse04()"); 170 } catch (IOException e) { 171 e.printStackTrace(); 172 Assert.fail("IOException in testParse04()"); 173 } 174 } 175 176 @Test 177 public void testParse05() { 178 System.out.println("===in testParse05==="); 179 try { 180 DefaultHandler handler = new MyDefaultHandler2(); 181 XMLReader xmlReader = XMLReaderFactory.createXMLReader(); 182 XMLFilterImpl filterImpl = new XMLFilterImpl(xmlReader); 183 System.out.println(xmlReader.getFeature("http://xml.org/sax/features/namespaces")); 184 filterImpl.setProperty("http://xml.org/sax/properties/declaration-handler", handler); 185 filterImpl.setProperty("http://xml.org/sax/properties/lexical-handler", handler); 186 filterImpl.setContentHandler(handler); 187 188 filterImpl.parse(this.getClass().getResource("toys.xml").getFile()); 189 190 } catch (SAXException e) { 191 e.printStackTrace(); 192 Assert.fail("SAXException in testParse05()"); 193 } catch (IOException e) { 194 e.printStackTrace(); 195 Assert.fail("IOException in testParse05()"); 196 } 197 } 198 199 @Test 200 public void testParse06() { 201 System.out.println("===in testParse06==="); 202 try { 203 DefaultHandler handler = new MyDefaultHandler2(); 204 XMLReader xmlReader = XMLReaderFactory.createXMLReader(); 205 XMLFilterImpl filterImpl = new XMLFilterImpl(xmlReader); 206 System.out.println(xmlReader.getFeature("http://xml.org/sax/features/namespaces")); 207 filterImpl.setProperty("http://xml.org/sax/properties/declaration-handler", handler); 208 filterImpl.setProperty("http://xml.org/sax/properties/lexical-handler", handler); 209 filterImpl.setContentHandler(handler); 210 211 AssertJUnit.assertTrue(filterImpl.getProperty("http://xml.org/sax/properties/declaration-handler") instanceof DefaultHandler2); 212 213 // filterImpl.setFeature("http://xml.org/sax/features/external-general-entities", 214 // false) ; 215 // filterImpl.setFeature("http://xml.org/sax/features/external-parameter-entities", 216 // false) ; 217 filterImpl.skippedEntity("name2"); 218 219 filterImpl.parse(this.getClass().getResource("toys.xml").getFile()); 220 } catch (SAXException e) { 221 e.printStackTrace(); 222 Assert.fail("SAXException in testParse06()"); 223 } catch (IOException e) { 224 e.printStackTrace(); 225 Assert.fail("IOException in testParse06()"); 226 } 227 } 228 229 @Test 230 public void testParse07() { 231 System.out.println("===in testParse07==="); 232 try { 233 DefaultHandler handler = new MyDefaultHandler2(); 234 XMLReader xmlReader = XMLReaderFactory.createXMLReader(); 235 XMLFilterImpl filterImpl = new XMLFilterImpl(xmlReader); 236 System.out.println(xmlReader.getFeature("http://xml.org/sax/features/namespaces")); 237 filterImpl.setProperty("http://xml.org/sax/properties/declaration-handler", handler); 238 filterImpl.setProperty("http://xml.org/sax/properties/lexical-handler", handler); 239 filterImpl.setContentHandler(handler); 240 filterImpl.setErrorHandler(handler); 241 AssertJUnit.assertTrue(filterImpl.getProperty("http://xml.org/sax/properties/declaration-handler") instanceof DefaultHandler2); 242 243 filterImpl.setFeature("http://apache.org/xml/features/continue-after-fatal-error", true); 244 filterImpl.parse(this.getClass().getResource("toys_error.xml").getFile()); 245 } catch (SAXException e) { 246 e.printStackTrace(); 247 Assert.fail("SAXException in testParse07()"); 248 } catch (IOException e) { 249 e.printStackTrace(); 250 Assert.fail("IOException in testParse07()"); 251 } 252 } 253 } 254