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