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.IOException; 27 import javax.xml.parsers.ParserConfigurationException; 28 import javax.xml.parsers.SAXParserFactory; 29 import static jaxp.library.JAXPTestUtilities.failUnexpected; 30 import static org.testng.Assert.assertFalse; 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.ContentHandler; 35 import org.xml.sax.InputSource; 36 import org.xml.sax.SAXException; 37 import org.xml.sax.SAXNotRecognizedException; 38 import org.xml.sax.SAXNotSupportedException; 39 import org.xml.sax.XMLReader; 40 import org.xml.sax.helpers.ParserAdapter; 41 import org.xml.sax.helpers.XMLFilterImpl; 42 import org.xml.sax.helpers.XMLReaderAdapter; 43 import static org.xml.sax.ptests.SAXTestConst.XML_DIR; 44 45 46 /** 47 * Unit test cases for ParserAdapter API. By default the only features recognized 48 * are namespaces and namespace-prefixes. 49 */ 50 public class ParserAdapterTest { 51 /** 52 * namespaces feature name. 53 */ 54 private static final String NAMESPACES = 55 "http://xml.org/sax/features/namespaces"; 56 57 /** 58 * namespaces-prefiexs feature name. 59 */ 60 private static final String NAMESPACE_PREFIXES = 61 "http://xml.org/sax/features/namespace-prefixes"; 62 63 /** 64 * ParserAdapter instance to share by all tests. 65 */ 66 private final ParserAdapter parserAdapter; 67 68 /** 69 * Initiate ParserAdapter. 70 * @throws ParserConfigurationException 71 * @throws SAXException 72 */ 73 ParserAdapterTest() throws ParserConfigurationException, SAXException { 74 SAXParserFactory spf = SAXParserFactory.newInstance(); 75 XMLReader xmlReader = spf.newSAXParser().getXMLReader(); 76 XMLReaderAdapter xmlReaderAdapter = new XMLReaderAdapter(xmlReader); 77 parserAdapter = new ParserAdapter(xmlReaderAdapter); 78 } 79 80 /** 81 * Verifies parserAdapter.getContentHandler() 82 */ 83 @Test 84 public void contentHandler01() { 85 ContentHandler contentHandler = new XMLFilterImpl(); 86 parserAdapter.setContentHandler(contentHandler); 87 assertNotNull(parserAdapter.getContentHandler()); 88 } 89 90 /** 91 * No exception is expected when set content handler as null. 92 */ 93 @Test 94 public void contentHandler02() { 95 parserAdapter.setContentHandler(null); 96 } 97 98 /** 99 * Verifies parserAdapter.getEntityResolver() 100 */ 101 @Test 102 public void entity01() { 103 XMLFilterImpl xmlFilter = new XMLFilterImpl(); 104 parserAdapter.setEntityResolver(xmlFilter); 105 assertNotNull(parserAdapter.getEntityResolver()); 106 } 107 108 /** 109 * No exception is expected when set entity resolver as null. 110 */ 111 @Test 112 public void entity02() { 113 parserAdapter.setEntityResolver(null); 114 } 115 116 /** 117 * Verifies parserAdapter.getDTDHandler() 118 */ 119 @Test 120 public void dtdHandler01() { 121 XMLFilterImpl xmlFilter = new XMLFilterImpl(); 122 parserAdapter.setDTDHandler(xmlFilter); 123 assertNotNull(parserAdapter.getDTDHandler()); 124 } 125 126 /** 127 * No exception is expected when set DTD handler as null. 128 */ 129 @Test 130 public void dtdHandler02() { 131 parserAdapter.setDTDHandler(null); 132 } 133 134 /** 135 * Verifies parserAdapter.getErrorHandler() 136 */ 137 @Test 138 public void errorHandler01() { 139 XMLFilterImpl eHandler = new XMLFilterImpl(); 140 parserAdapter.setErrorHandler(eHandler); 141 assertNotNull(parserAdapter.getErrorHandler()); 142 } 143 144 /** 145 * No exception is expected when set error handler as null. 146 */ 147 @Test 148 public void errorHandler02() { 149 parserAdapter.setErrorHandler(null); 150 } 151 152 /** 153 * parserAdapter.getFeature(NAMESPACES) returns true be default. 154 */ 155 @Test 156 public void getFeature01() { 157 try { 158 assertTrue(parserAdapter.getFeature(NAMESPACES)); 159 } catch (SAXNotRecognizedException | SAXNotSupportedException ex) { 160 failUnexpected(ex); 161 } 162 } 163 164 /** 165 * parserAdapter.getFeature(NAMESPACE_PREFIXES) returns true be default. 166 */ 167 @Test 168 public void getFeature02() { 169 try { 170 assertFalse(parserAdapter.getFeature(NAMESPACE_PREFIXES)); 171 } catch (SAXNotRecognizedException | SAXNotSupportedException ex) { 172 failUnexpected(ex); 173 } 174 } 175 176 /** 177 * SAXNotRecognizedException thrown when feature name is not known one. 178 * @throws org.xml.sax.SAXNotRecognizedException expected Exception 179 */ 180 @Test(expectedExceptions = SAXNotRecognizedException.class) 181 public void getFeature03() throws SAXNotRecognizedException { 182 try { 183 parserAdapter.getFeature("no-meaning-feature"); 184 } catch (SAXNotSupportedException ex) { 185 failUnexpected(ex); 186 } 187 } 188 189 /** 190 * Obtain getFeature after it's set returns set value. 191 */ 192 @Test 193 public void setFeature01() { 194 try { 195 parserAdapter.setFeature(NAMESPACES, false); 196 assertFalse(parserAdapter.getFeature(NAMESPACES)); 197 } catch (SAXNotRecognizedException | SAXNotSupportedException ex) { 198 failUnexpected(ex); 199 } 200 } 201 202 /** 203 * Obtain getFeature after it's set returns set value. 204 */ 205 @Test 206 public void setFeature02() { 207 try { 208 parserAdapter.setFeature(NAMESPACE_PREFIXES, false); 209 assertFalse(parserAdapter.getFeature(NAMESPACE_PREFIXES)); 210 } catch (SAXNotRecognizedException | SAXNotSupportedException ex) { 211 failUnexpected(ex); 212 } 213 } 214 215 /** 216 * Obtain getFeature after it's set returns set value. 217 */ 218 @Test 219 public void setFeature03() { 220 try { 221 parserAdapter.setFeature(NAMESPACES, true); 222 assertTrue(parserAdapter.getFeature(NAMESPACES)); 223 } catch (SAXNotRecognizedException | SAXNotSupportedException ex) { 224 failUnexpected(ex); 225 } 226 } 227 228 /** 229 * Obtain getFeature after it's set returns set value. 230 */ 231 @Test 232 public void setFeature04() { 233 try { 234 parserAdapter.setFeature(NAMESPACE_PREFIXES, true); 235 assertTrue(parserAdapter.getFeature(NAMESPACE_PREFIXES)); 236 } catch (SAXNotRecognizedException | SAXNotSupportedException ex) { 237 failUnexpected(ex); 238 } 239 } 240 241 /** 242 * NPE expected when parsing a null object by ParserAdapter. 243 */ 244 @Test(expectedExceptions = NullPointerException.class) 245 public void parse01() { 246 try { 247 parserAdapter.parse((InputSource)null); 248 } catch (IOException | SAXException ex) { 249 failUnexpected(ex); 250 } 251 } 252 253 /** 254 * SAXException expected when parsing a wrong-formatter XML with ParserAdapter. 255 * @throws org.xml.sax.SAXException 256 */ 257 @Test(expectedExceptions = SAXException.class) 258 public void parse02() throws SAXException { 259 try(FileInputStream fis = new FileInputStream(XML_DIR + "invalid.xml")) { 260 InputSource is = new InputSource(fis); 261 parserAdapter.parse(is); 262 } catch (IOException ex) { 263 failUnexpected(ex); 264 } 265 } 266 267 /** 268 * Parse a well-formatter XML with ParserAdapter. 269 */ 270 @Test 271 public void parse03() { 272 try(FileInputStream fis = new FileInputStream(XML_DIR + "correct.xml")) { 273 InputSource is = new InputSource(fis); 274 parserAdapter.parse(is); 275 } catch (IOException | SAXException ex) { 276 failUnexpected(ex); 277 } 278 } 279 }