1 /* 2 * Copyright (C) 2007 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 package org.apache.harmony.tests.javax.xml.parsers; 17 18 import java.io.ByteArrayInputStream; 19 import java.io.IOException; 20 import java.io.InputStream; 21 import java.util.HashMap; 22 import java.util.Properties; 23 import java.util.Vector; 24 25 import javax.xml.parsers.FactoryConfigurationError; 26 import javax.xml.parsers.ParserConfigurationException; 27 import javax.xml.parsers.SAXParser; 28 import javax.xml.parsers.SAXParserFactory; 29 30 import junit.framework.TestCase; 31 32 import org.xml.sax.Attributes; 33 import org.xml.sax.SAXException; 34 import org.xml.sax.SAXNotRecognizedException; 35 import org.xml.sax.SAXNotSupportedException; 36 import org.xml.sax.helpers.DefaultHandler; 37 38 import dalvik.annotation.KnownFailure; 39 40 public class SAXParserFactoryTest extends TestCase { 41 42 SAXParserFactory spf; 43 44 InputStream is1; 45 46 static HashMap<String, String> ns; 47 48 static Vector<String> el; 49 50 static HashMap<String, String> attr; 51 setUp()52 public void setUp() throws Exception { 53 spf = SAXParserFactory.newInstance(); 54 55 is1 = getClass().getResourceAsStream("/simple.xml"); 56 57 ns = new HashMap<String, String>(); 58 attr = new HashMap<String, String>(); 59 el = new Vector<String>(); 60 } 61 tearDown()62 public void tearDown() throws Exception { 63 is1.close(); 64 super.tearDown(); 65 } 66 67 // AndroidOnly("Android SAX implementation is non-validating") test_Constructor()68 public void test_Constructor() { 69 MySAXParserFactory mpf = new MySAXParserFactory(); 70 assertTrue(mpf instanceof SAXParserFactory); 71 assertFalse(mpf.isValidating()); 72 } 73 74 /** 75 * javax.xml.parsers.SAXParserFactory#getSchema(). 76 * TBD getSchema() IS NOT SUPPORTED 77 */ 78 /* public void test_getSchema() { 79 assertNull(spf.getSchema()); 80 SchemaFactory sf = 81 SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); 82 try { 83 Schema schema = sf.newSchema(); 84 spf.setSchema(schema); 85 assertNotNull(spf.getSchema()); 86 } catch (SAXException sax) { 87 fail("Unexpected exception " + sax.toString()); 88 } 89 } 90 */ 91 test_getSchema()92 public void test_getSchema() { 93 try { 94 spf.getSchema(); 95 fail("UnsupportedOperationException is expected"); 96 } catch (UnsupportedOperationException ignored) {} 97 } 98 test_setSchema()99 public void test_setSchema() { 100 try { 101 spf.setSchema(null); 102 fail("UnsupportedOperationException is expected"); 103 } catch (UnsupportedOperationException ignored) {} 104 } 105 test_newInstanceLjavaLangString_LjavaLangClassLoader()106 public void test_newInstanceLjavaLangString_LjavaLangClassLoader() { 107 SAXParserFactory.newInstance("org.apache.harmony.xml.parsers.SAXParserFactoryImpl", null); 108 109 try { 110 SAXParserFactory.newInstance("non-existing-class", null); 111 fail("FactoryConfigurationError is expected"); 112 } catch (FactoryConfigurationError ignored) {} 113 114 try { 115 SAXParserFactory.newInstance(null, null); 116 fail("FactoryConfigurationError is expected"); 117 } catch (FactoryConfigurationError ignored) {} 118 } 119 test_setIsNamespaceAware()120 public void test_setIsNamespaceAware() { 121 spf.setNamespaceAware(true); 122 assertTrue(spf.isNamespaceAware()); 123 spf.setNamespaceAware(false); 124 assertFalse(spf.isNamespaceAware()); 125 spf.setNamespaceAware(true); 126 assertTrue(spf.isNamespaceAware()); 127 } 128 test_setIsValidating()129 public void test_setIsValidating() { 130 spf.setValidating(true); 131 assertTrue(spf.isValidating()); 132 spf.setValidating(false); 133 assertFalse(spf.isValidating()); 134 spf.setValidating(true); 135 assertTrue(spf.isValidating()); 136 } 137 test_setIsXIncludeAware()138 public void test_setIsXIncludeAware() { 139 spf.setXIncludeAware(true); 140 assertTrue(spf.isXIncludeAware()); 141 spf.setXIncludeAware(false); 142 assertFalse(spf.isXIncludeAware()); 143 } 144 145 @KnownFailure("Dalvik doesn't honor system properties when choosing a SAX implementation") test_newInstance()146 public void test_newInstance() { 147 try { 148 SAXParserFactory dtf = SAXParserFactory.newInstance(); 149 assertNotNull("New Instance of DatatypeFactory is null", dtf); 150 151 System.setProperty("javax.xml.parsers.SAXParserFactory", 152 "org.apache.harmony.xml.parsers.SAXParserFactoryImpl"); 153 154 SAXParserFactory spf1 = SAXParserFactory.newInstance(); 155 assertTrue(spf1 instanceof org.apache.harmony.xml.parsers.SAXParserFactoryImpl); 156 157 String key = "javax.xml.parsers.SAXParserFactory = org.apache.harmony.xml.parsers.SAXParserFactoryImpl"; 158 159 ByteArrayInputStream bis = new ByteArrayInputStream(key.getBytes()); 160 Properties prop = System.getProperties(); 161 prop.load(bis); 162 SAXParserFactory spf2 = SAXParserFactory.newInstance(); 163 assertTrue(spf2 instanceof org.apache.harmony.xml.parsers.SAXParserFactoryImpl); 164 165 System.setProperty("javax.xml.parsers.SAXParserFactory", ""); 166 try { 167 SAXParserFactory.newInstance(); 168 fail("Expected FactoryConfigurationError was not thrown"); 169 } catch (FactoryConfigurationError e) { 170 // expected 171 } 172 } catch (IOException ioe) { 173 fail("Unexpected exception " + ioe.toString()); 174 } 175 } 176 test_newSAXParser()177 public void test_newSAXParser() { 178 // Ordinary case 179 try { 180 SAXParser sp = spf.newSAXParser(); 181 assertTrue(sp instanceof SAXParser); 182 sp.parse(is1, new MyHandler()); 183 } catch(Exception e) { 184 throw new RuntimeException("Unexpected exception", e); 185 } 186 187 // Exception case 188 spf.setValidating(true); 189 try { 190 SAXParser sp = spf.newSAXParser(); 191 } catch(ParserConfigurationException e) { 192 // Expected, since Android doesn't have a validating parser. 193 } catch (SAXException e) { 194 throw new RuntimeException("Unexpected exception", e); 195 } 196 } 197 test_setFeatureLjava_lang_StringZ()198 public void test_setFeatureLjava_lang_StringZ() { 199 // We can't verify ParserConfigurationException and 200 // SAXNotSupportedException since these are never 201 // thrown by Android. 202 203 String[] features = { 204 "http://xml.org/sax/features/namespaces", 205 "http://xml.org/sax/features/validation" }; 206 for (int i = 0; i < features.length; i++) { 207 try { 208 spf.setFeature(features[i], true); 209 assertTrue(spf.getFeature(features[i])); 210 spf.setFeature(features[i], false); 211 assertFalse(spf.getFeature(features[i])); 212 } catch (ParserConfigurationException pce) { 213 fail("ParserConfigurationException is thrown"); 214 } catch (SAXNotRecognizedException snre) { 215 fail("SAXNotRecognizedException is thrown"); 216 } catch (SAXNotSupportedException snse) { 217 fail("SAXNotSupportedException is thrown"); 218 } 219 } 220 221 try { 222 spf.setFeature("", true); 223 fail("SAXNotRecognizedException is not thrown"); 224 } catch (ParserConfigurationException pce) { 225 fail("ParserConfigurationException is thrown"); 226 } catch (SAXNotRecognizedException snre) { 227 //expected 228 } catch (SAXNotSupportedException snse) { 229 fail("SAXNotSupportedException is thrown"); 230 } catch (NullPointerException npe) { 231 fail("NullPointerException is thrown"); 232 } 233 234 try { 235 spf.setFeature("http://xml.org/sax/features/unknown-feature", true); 236 } catch (ParserConfigurationException pce) { 237 fail("ParserConfigurationException is thrown"); 238 } catch (SAXNotRecognizedException snre) { 239 fail("SAXNotRecognizedException is thrown"); 240 } catch (SAXNotSupportedException snse) { 241 // Acceptable, although this doesn't happen an Android. 242 } catch (NullPointerException npe) { 243 fail("NullPointerException is thrown"); 244 } 245 246 try { 247 spf.setFeature(null, true); 248 fail("NullPointerException is not thrown"); 249 } catch (ParserConfigurationException pce) { 250 fail("ParserConfigurationException is thrown"); 251 } catch (SAXNotRecognizedException snre) { 252 fail("SAXNotRecognizedException is thrown"); 253 } catch (SAXNotSupportedException snse) { 254 fail("SAXNotSupportedException is thrown"); 255 } catch (NullPointerException npe) { 256 // expected 257 } 258 } 259 test_setNamespaceAwareZ()260 public void test_setNamespaceAwareZ() throws Exception { 261 MyHandler mh = new MyHandler(); 262 263 spf.setNamespaceAware(true); 264 InputStream is = getClass().getResourceAsStream("/simple_ns.xml"); 265 spf.newSAXParser().parse(is, mh); 266 is.close(); 267 268 spf.setNamespaceAware(false); 269 is = getClass().getResourceAsStream("/simple_ns.xml"); 270 spf.newSAXParser().parse(is, mh); 271 is.close(); 272 } 273 274 /* public void test_setSchemaLjavax_xml_validation_Schema() { 275 SchemaFactory sf = 276 SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); 277 try { 278 Schema schema = sf.newSchema(); 279 spf.setSchema(schema); 280 assertNotNull(spf.getSchema()); 281 } catch (SAXException sax) { 282 fail("Unexpected exception " + sax.toString()); 283 } 284 } 285 */ 286 287 // public void test_setValidatingZ() { 288 // MyHandler mh = new MyHandler(); 289 // InputStream is2 = getClass().getResourceAsStream("/recipe.xml"); 290 // try { 291 // spf.setValidating(true); 292 // assertTrue(spf.isValidating()); 293 // spf.newSAXParser().parse(is2, mh); 294 // } catch (org.xml.sax.SAXException se) { 295 // fail("SAXException was thrown during parsing"); 296 // } catch (javax.xml.parsers.ParserConfigurationException pce) { 297 // fail("ParserConfigurationException was thrown during parsing"); 298 // } catch (IOException ioe) { 299 // fail("IOException was thrown during parsing"); 300 // } finally { 301 // try { 302 // is2.close(); 303 // } catch(Exception ioee) {} 304 // } 305 // InputStream is3 = getClass().getResourceAsStream("/recipe1.xml"); 306 // try { 307 // assertTrue(spf.isValidating()); 308 // spf.newSAXParser().parse(is3, mh); 309 // } catch (org.xml.sax.SAXException se) { 310 // fail("SAXException was thrown during parsing"); 311 // } catch (javax.xml.parsers.ParserConfigurationException pce) { 312 // fail("ParserConfigurationException was thrown during parsing"); 313 // } catch (IOException ioe) { 314 // fail("IOEXception was thrown during parsing: " + ioe.getMessage()); 315 // } finally { 316 // try { 317 // is3.close(); 318 // } catch(Exception ioee) {} 319 // } 320 // is2 = getClass().getResourceAsStream("/recipe.xml"); 321 // try { 322 // spf.setValidating(false); 323 // assertFalse(spf.isValidating()); 324 // spf.newSAXParser().parse(is2, mh); 325 // } catch (org.xml.sax.SAXException se) { 326 // fail("SAXException was thrown during parsing"); 327 // } catch (javax.xml.parsers.ParserConfigurationException pce) { 328 // fail("ParserConfigurationException was thrown during parsing"); 329 // } catch (IOException ioe) { 330 // fail("IOException was thrown during parsing"); 331 // } finally { 332 // try { 333 // is2.close(); 334 // } catch(Exception ioee) {} 335 // } 336 // is3 = getClass().getResourceAsStream("/recipe1.xml"); 337 // try { 338 // assertFalse(spf.isValidating()); 339 // spf.newSAXParser().parse(is3, mh); 340 // } catch (org.xml.sax.SAXException se) { 341 // fail("SAXException was thrown during parsing"); 342 // } catch (javax.xml.parsers.ParserConfigurationException pce) { 343 // fail("ParserConfigurationException was thrown during parsing"); 344 // } catch (IOException ioe) { 345 // fail("IOEXception was thrown during parsing: " + ioe.getMessage()); 346 // } finally { 347 // try { 348 // is3.close(); 349 // } catch(Exception ioee) {} 350 // } 351 // } 352 353 // public void test_setXIncludeAwareZ() { 354 // spf.setXIncludeAware(true); 355 // MyHandler mh = new MyHandler(); 356 // InputStream is = getClass().getResourceAsStream("/simple_ns.xml"); 357 // try { 358 // spf.newSAXParser().parse(is, mh); 359 // } catch(javax.xml.parsers.ParserConfigurationException pce) { 360 // fail("ParserConfigurationException was thrown during parsing"); 361 // } catch(org.xml.sax.SAXException se) { 362 // fail("SAXException was thrown during parsing"); 363 // } catch(IOException ioe) { 364 // fail("IOException was thrown during parsing"); 365 // } finally { 366 // try { 367 // is.close(); 368 // } catch(Exception ioee) {} 369 // } 370 // spf.setXIncludeAware(false); 371 // is = getClass().getResourceAsStream("/simple_ns.xml"); 372 // try { 373 // is = getClass().getResourceAsStream("/simple_ns.xml"); 374 // spf.newSAXParser().parse(is, mh); 375 // } catch(javax.xml.parsers.ParserConfigurationException pce) { 376 // fail("ParserConfigurationException was thrown during parsing"); 377 // } catch(org.xml.sax.SAXException se) { 378 // fail("SAXException was thrown during parsing"); 379 // } catch(IOException ioe) { 380 // fail("IOException was thrown during parsing"); 381 // } finally { 382 // try { 383 // is.close(); 384 // } catch(Exception ioee) {} 385 // } 386 // is = getClass().getResourceAsStream("/simple_ns.xml"); 387 // try { 388 // spf.setXIncludeAware(true); 389 // spf.newSAXParser().parse(is, mh); 390 // } catch(javax.xml.parsers.ParserConfigurationException pce) { 391 // fail("ParserConfigurationException was thrown during parsing"); 392 // } catch(org.xml.sax.SAXException se) { 393 // fail("SAXException was thrown during parsing"); 394 // } catch(IOException ioe) { 395 // fail("IOException was thrown during parsing"); 396 // } finally { 397 // try { 398 // is.close(); 399 // } catch(Exception ioee) {} 400 // } 401 // } 402 403 static class MyHandler extends DefaultHandler { 404 startElement(String uri, String localName, String qName, Attributes atts)405 public void startElement(String uri, String localName, String qName, 406 Attributes atts) { 407 408 el.add(qName); 409 if (!uri.equals("")) 410 ns.put(qName, uri); 411 for (int i = 0; i < atts.getLength(); i++) { 412 attr.put(atts.getQName(i), atts.getValue(i)); 413 } 414 415 } 416 } 417 418 class MySAXParserFactory extends SAXParserFactory { 419 MySAXParserFactory()420 public MySAXParserFactory() { 421 super(); 422 } 423 newSAXParser()424 public SAXParser newSAXParser() { 425 return null; 426 } 427 setFeature(String name, boolean value)428 public void setFeature(String name, boolean value) throws 429 ParserConfigurationException, SAXNotRecognizedException, 430 SAXNotSupportedException { 431 432 } 433 getFeature(String name)434 public boolean getFeature(String name) throws 435 ParserConfigurationException, SAXNotRecognizedException, 436 SAXNotSupportedException { 437 return true; 438 } 439 440 } 441 442 } 443