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 24 package javax.xml.parsers.ptests; 25 26 import static jaxp.library.JAXPTestUtilities.failUnexpected; 27 import static org.testng.Assert.assertFalse; 28 import static org.testng.Assert.assertTrue; 29 30 import javax.xml.parsers.ParserConfigurationException; 31 import javax.xml.parsers.SAXParser; 32 import javax.xml.parsers.SAXParserFactory; 33 34 import org.testng.annotations.Test; 35 import org.xml.sax.SAXException; 36 import org.xml.sax.SAXNotRecognizedException; 37 import org.xml.sax.SAXNotSupportedException; 38 39 /** 40 * Class containing the test cases for SAXParserFactory API 41 */ 42 public class SAXParserFactTest { 43 44 private static final String NAMESPACES = "http://xml.org/sax/features/namespaces"; 45 private static final String NAMESPACE_PREFIXES = "http://xml.org/sax/features/namespace-prefixes"; 46 private static final String STRING_INTERNING = "http://xml.org/sax/features/string-interning"; 47 private static final String VALIDATION = "http://xml.org/sax/features/validation"; 48 private static final String EXTERNAL_G_ENTITIES = "http://xml.org/sax/features/external-general-entities"; 49 private static final String EXTERNAL_P_ENTITIES = "http://xml.org/sax/features/external-parameter-entities"; 50 51 /** 52 * Testcase to test if newSAXParser() method returns SAXParser. 53 */ 54 @Test 55 public void testParser01() { 56 try { 57 SAXParserFactory spf = SAXParserFactory.newInstance(); 58 SAXParser saxparser = spf.newSAXParser(); 59 } catch (ParserConfigurationException | SAXException e) { 60 failUnexpected(e); 61 } 62 } 63 64 /** 65 * Testcase to test the default functionality (No validation) of the parser. 66 */ 67 @Test 68 public void testValidate01() { 69 SAXParserFactory spf = SAXParserFactory.newInstance(); 70 assertFalse(spf.isValidating()); 71 } 72 73 /** 74 * Testcase to test the functionality of setValidating and isvalidating 75 * methods. 76 */ 77 @Test 78 public void testValidate02() { 79 SAXParserFactory spf = SAXParserFactory.newInstance(); 80 spf.setValidating(true); 81 assertTrue(spf.isValidating()); 82 } 83 84 /** 85 * Parser should not be namespaceaware by default. 86 */ 87 @Test 88 public void testNamespace01() { 89 SAXParserFactory spf = SAXParserFactory.newInstance(); 90 assertFalse(spf.isNamespaceAware()); 91 } 92 93 /** 94 * Testcase to test the functionality of setNamespaceAware and 95 * isNamespaceAware methods. 96 */ 97 @Test 98 public void testNamespace02() { 99 SAXParserFactory spf = SAXParserFactory.newInstance(); 100 spf.setNamespaceAware(true); 101 assertTrue(spf.isNamespaceAware()); 102 } 103 104 /** 105 * Testcase to test the functionality of setNamespaceAware and getFeature() 106 * methods for namespaces property. 107 */ 108 @Test 109 public void testFeature01() { 110 try { 111 SAXParserFactory spf = SAXParserFactory.newInstance(); 112 assertFalse(spf.getFeature(NAMESPACES)); 113 114 spf.setNamespaceAware(true); 115 assertTrue(spf.getFeature(NAMESPACES)); 116 } catch (ParserConfigurationException | SAXNotRecognizedException | SAXNotSupportedException e) { 117 failUnexpected(e); 118 } 119 } 120 121 /** 122 * Testcase to test the functionality of setFeature and getFeature methods 123 * for namespaces property. 124 */ 125 @Test 126 public void testFeature02() { 127 try { 128 SAXParserFactory spf = SAXParserFactory.newInstance(); 129 130 spf.setFeature(NAMESPACES, true); 131 assertTrue(spf.getFeature(NAMESPACES)); 132 133 spf.setFeature(NAMESPACES, false); 134 assertFalse(spf.getFeature(NAMESPACES)); 135 } catch (ParserConfigurationException | SAXNotRecognizedException | SAXNotSupportedException e) { 136 failUnexpected(e); 137 } 138 } 139 140 /** 141 * Testcase to test the functionality of setFeature and getFeature methods 142 * for namespace-prefixes property. 143 */ 144 @Test 145 public void testFeature03() { 146 try { 147 SAXParserFactory spf = SAXParserFactory.newInstance(); 148 149 spf.setFeature(NAMESPACE_PREFIXES, true); 150 assertTrue(spf.getFeature(NAMESPACE_PREFIXES)); 151 152 spf.setFeature(NAMESPACE_PREFIXES, false); 153 assertFalse(spf.getFeature(NAMESPACE_PREFIXES)); 154 } catch (ParserConfigurationException | SAXNotRecognizedException | SAXNotSupportedException e) { 155 failUnexpected(e); 156 } 157 } 158 159 /** 160 * Testcase to test the functionality of getFeature method for 161 * string-interning property. 162 */ 163 @Test 164 public void testFeature04() { 165 try { 166 SAXParserFactory spf = SAXParserFactory.newInstance(); 167 assertTrue(spf.getFeature(STRING_INTERNING)); 168 } catch (ParserConfigurationException | SAXNotRecognizedException | SAXNotSupportedException e) { 169 failUnexpected(e); 170 } 171 } 172 173 /** 174 * Testcase to test the functionality of getFeature and setValidating 175 * methods for validation property. 176 */ 177 @Test 178 public void testFeature05() { 179 try { 180 SAXParserFactory spf = SAXParserFactory.newInstance(); 181 assertFalse(spf.getFeature(VALIDATION)); 182 spf.setValidating(true); 183 assertTrue(spf.getFeature(VALIDATION)); 184 } catch (ParserConfigurationException | SAXNotRecognizedException | SAXNotSupportedException e) { 185 failUnexpected(e); 186 } 187 188 } 189 190 /** 191 * Testcase to test the functionality of setFeature and getFeature methods 192 * for validation property. 193 */ 194 @Test 195 public void testFeature06() { 196 try { 197 SAXParserFactory spf = SAXParserFactory.newInstance(); 198 199 spf.setFeature(VALIDATION, true); 200 assertTrue(spf.getFeature(VALIDATION)); 201 202 spf.setFeature(VALIDATION, false); 203 assertFalse(spf.getFeature(VALIDATION)); 204 } catch (ParserConfigurationException | SAXNotRecognizedException | SAXNotSupportedException e) { 205 failUnexpected(e); 206 } 207 208 } 209 210 /** 211 * Testcase to test the functionality of getFeature method for 212 * external-general-entities property. 213 */ 214 @Test 215 public void testFeature07() { 216 try { 217 SAXParserFactory spf = SAXParserFactory.newInstance(); 218 assertTrue(spf.getFeature(EXTERNAL_G_ENTITIES)); 219 } catch (ParserConfigurationException | SAXNotRecognizedException | SAXNotSupportedException e) { 220 failUnexpected(e); 221 } 222 223 } 224 225 /** 226 * Testcase to test the functionality of setFeature and getFeature methods 227 * for external-general-entities property. 228 */ 229 @Test 230 public void testFeature08() { 231 try { 232 SAXParserFactory spf = SAXParserFactory.newInstance(); 233 spf.setFeature(EXTERNAL_G_ENTITIES, false); 234 assertFalse(spf.getFeature(EXTERNAL_G_ENTITIES)); 235 } catch (ParserConfigurationException | SAXNotRecognizedException | SAXNotSupportedException e) { 236 failUnexpected(e); 237 } 238 } 239 240 /** 241 * Testcase to test the functionality of getFeature method for 242 * external-parameter-entities property. 243 */ 244 @Test 245 public void testFeature09() { 246 try { 247 SAXParserFactory spf = SAXParserFactory.newInstance(); 248 assertTrue(spf.getFeature(EXTERNAL_P_ENTITIES)); 249 } catch (ParserConfigurationException | SAXNotRecognizedException | SAXNotSupportedException e) { 250 failUnexpected(e); 251 } 252 } 253 254 /** 255 * Testcase to test the functionality of setFeature method for 256 * external-parameter-entitie property. 257 */ 258 @Test 259 public void testFeature10() { 260 try { 261 SAXParserFactory spf = SAXParserFactory.newInstance(); 262 spf.setFeature(EXTERNAL_P_ENTITIES, false); 263 } catch (ParserConfigurationException | SAXNotRecognizedException | SAXNotSupportedException e) { 264 failUnexpected(e); 265 } 266 267 } 268 }