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 tests.api.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.AndroidOnly; 39 import dalvik.annotation.KnownFailure; 40 import dalvik.annotation.TestLevel; 41 import dalvik.annotation.TestTargetClass; 42 import dalvik.annotation.TestTargetNew; 43 import dalvik.annotation.TestTargets; 44 45 @TestTargetClass(SAXParserFactory.class) 46 public class SAXParserFactoryTest extends TestCase { 47 48 SAXParserFactory spf; 49 50 InputStream is1; 51 52 static HashMap<String, String> ns; 53 54 static Vector<String> el; 55 56 static HashMap<String, String> attr; 57 58 public void setUp() throws Exception { 59 spf = SAXParserFactory.newInstance(); 60 61 is1 = getClass().getResourceAsStream("/simple.xml"); 62 63 ns = new HashMap<String, String>(); 64 attr = new HashMap<String, String>(); 65 el = new Vector<String>(); 66 } 67 68 public void tearDown() throws Exception { 69 is1.close(); 70 super.tearDown(); 71 } 72 73 @TestTargetNew( 74 level = TestLevel.COMPLETE, 75 notes = "", 76 method = "SAXParserFactory", 77 args = {} 78 ) 79 @AndroidOnly("Android SAX implementation is non-validating") 80 public void test_Constructor() { 81 MySAXParserFactory mpf = new MySAXParserFactory(); 82 assertTrue(mpf instanceof SAXParserFactory); 83 assertFalse(mpf.isValidating()); 84 } 85 86 /** 87 * @tests javax.xml.parsers.SAXParserFactory#getSchema(). 88 * TBD getSchema() IS NOT SUPPORTED 89 */ 90 /* public void test_getSchema() { 91 assertNull(spf.getSchema()); 92 SchemaFactory sf = 93 SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); 94 try { 95 Schema schema = sf.newSchema(); 96 spf.setSchema(schema); 97 assertNotNull(spf.getSchema()); 98 } catch (SAXException sax) { 99 fail("Unexpected exception " + sax.toString()); 100 } 101 } 102 */ 103 104 @TestTargets({ 105 @TestTargetNew( 106 level = TestLevel.COMPLETE, 107 notes = "", 108 method = "isNamespaceAware", 109 args = {} 110 ), 111 @TestTargetNew( 112 level = TestLevel.COMPLETE, 113 notes = "", 114 method = "setNamespaceAware", 115 args = {boolean.class} 116 ) 117 }) 118 public void test_setIsNamespaceAware() { 119 spf.setNamespaceAware(true); 120 assertTrue(spf.isNamespaceAware()); 121 spf.setNamespaceAware(false); 122 assertFalse(spf.isNamespaceAware()); 123 spf.setNamespaceAware(true); 124 assertTrue(spf.isNamespaceAware()); 125 } 126 127 @TestTargets({ 128 @TestTargetNew( 129 level = TestLevel.COMPLETE, 130 notes = "", 131 method = "isValidating", 132 args = {} 133 ), 134 @TestTargetNew( 135 level = TestLevel.SUFFICIENT, 136 notes = "", 137 method = "setValidating", 138 args = {boolean.class} 139 ) 140 }) 141 public void test_setIsValidating() { 142 spf.setValidating(true); 143 assertTrue(spf.isValidating()); 144 spf.setValidating(false); 145 assertFalse(spf.isValidating()); 146 spf.setValidating(true); 147 assertTrue(spf.isValidating()); 148 } 149 150 @TestTargets({ 151 @TestTargetNew( 152 level = TestLevel.COMPLETE, 153 notes = "", 154 method = "isXIncludeAware", 155 args = {} 156 ), 157 @TestTargetNew( 158 level = TestLevel.SUFFICIENT, 159 notes = "", 160 method = "setXIncludeAware", 161 args = {boolean.class} 162 ) 163 }) 164 public void test_setIsXIncludeAware() { 165 spf.setXIncludeAware(true); 166 assertTrue(spf.isXIncludeAware()); 167 spf.setXIncludeAware(false); 168 assertFalse(spf.isXIncludeAware()); 169 } 170 171 @TestTargetNew( 172 level = TestLevel.COMPLETE, 173 notes = "", 174 method = "newInstance", 175 args = {} 176 ) 177 @KnownFailure("Dalvik doesn't honor system properties when choosing a SAX implementation") 178 public void test_newInstance() { 179 try { 180 SAXParserFactory dtf = SAXParserFactory.newInstance(); 181 assertNotNull("New Instance of DatatypeFactory is null", dtf); 182 183 System.setProperty("javax.xml.parsers.SAXParserFactory", 184 "org.apache.harmony.xml.parsers.SAXParserFactoryImpl"); 185 186 SAXParserFactory spf1 = SAXParserFactory.newInstance(); 187 assertTrue(spf1 instanceof org.apache.harmony.xml.parsers.SAXParserFactoryImpl); 188 189 String key = "javax.xml.parsers.SAXParserFactory = org.apache.harmony.xml.parsers.SAXParserFactoryImpl"; 190 191 ByteArrayInputStream bis = new ByteArrayInputStream(key.getBytes()); 192 Properties prop = System.getProperties(); 193 prop.load(bis); 194 SAXParserFactory spf2 = SAXParserFactory.newInstance(); 195 assertTrue(spf2 instanceof org.apache.harmony.xml.parsers.SAXParserFactoryImpl); 196 197 System.setProperty("javax.xml.parsers.SAXParserFactory", ""); 198 try { 199 SAXParserFactory.newInstance(); 200 fail("Expected FactoryConfigurationError was not thrown"); 201 } catch (FactoryConfigurationError e) { 202 // expected 203 } 204 } catch (IOException ioe) { 205 fail("Unexpected exception " + ioe.toString()); 206 } 207 } 208 209 @TestTargetNew( 210 level = TestLevel.SUFFICIENT, 211 notes = "SAXException untested; unused on Android", 212 method = "newSAXParser", 213 args = {} 214 ) 215 public void test_newSAXParser() { 216 // Ordinary case 217 try { 218 SAXParser sp = spf.newSAXParser(); 219 assertTrue(sp instanceof SAXParser); 220 sp.parse(is1, new MyHandler()); 221 } catch(Exception e) { 222 throw new RuntimeException("Unexpected exception", e); 223 } 224 225 // Exception case 226 spf.setValidating(true); 227 try { 228 SAXParser sp = spf.newSAXParser(); 229 } catch(ParserConfigurationException e) { 230 // Expected, since Android doesn't have a validating parser. 231 } catch (SAXException e) { 232 throw new RuntimeException("Unexpected exception", e); 233 } 234 } 235 236 @TestTargets({ 237 @TestTargetNew( 238 level = TestLevel.SUFFICIENT, 239 method = "setFeature", 240 notes = "ParserConfigurationException untested; unused on Android", 241 args = {java.lang.String.class, boolean.class} 242 ), 243 @TestTargetNew( 244 level = TestLevel.SUFFICIENT, 245 method = "getFeature", 246 notes = "ParserConfigurationException untested; unused on Android", 247 args = {java.lang.String.class} 248 ) 249 }) 250 public void test_setFeatureLjava_lang_StringZ() { 251 // We can't verify ParserConfigurationException and 252 // SAXNotSupportedException since these are never 253 // thrown by Android. 254 255 String[] features = { 256 "http://xml.org/sax/features/namespaces", 257 "http://xml.org/sax/features/validation" }; 258 for (int i = 0; i < features.length; i++) { 259 try { 260 spf.setFeature(features[i], true); 261 assertTrue(spf.getFeature(features[i])); 262 spf.setFeature(features[i], false); 263 assertFalse(spf.getFeature(features[i])); 264 } catch (ParserConfigurationException pce) { 265 fail("ParserConfigurationException is thrown"); 266 } catch (SAXNotRecognizedException snre) { 267 fail("SAXNotRecognizedException is thrown"); 268 } catch (SAXNotSupportedException snse) { 269 fail("SAXNotSupportedException is thrown"); 270 } 271 } 272 273 try { 274 spf.setFeature("", true); 275 fail("SAXNotRecognizedException is not thrown"); 276 } catch (ParserConfigurationException pce) { 277 fail("ParserConfigurationException is thrown"); 278 } catch (SAXNotRecognizedException snre) { 279 //expected 280 } catch (SAXNotSupportedException snse) { 281 fail("SAXNotSupportedException is thrown"); 282 } catch (NullPointerException npe) { 283 fail("NullPointerException is thrown"); 284 } 285 286 try { 287 spf.setFeature("http://xml.org/sax/features/unknown-feature", true); 288 } catch (ParserConfigurationException pce) { 289 fail("ParserConfigurationException is thrown"); 290 } catch (SAXNotRecognizedException snre) { 291 fail("SAXNotRecognizedException is thrown"); 292 } catch (SAXNotSupportedException snse) { 293 // Acceptable, although this doesn't happen an Android. 294 } catch (NullPointerException npe) { 295 fail("NullPointerException is thrown"); 296 } 297 298 try { 299 spf.setFeature(null, true); 300 fail("NullPointerException is not thrown"); 301 } catch (ParserConfigurationException pce) { 302 fail("ParserConfigurationException is thrown"); 303 } catch (SAXNotRecognizedException snre) { 304 fail("SAXNotRecognizedException is thrown"); 305 } catch (SAXNotSupportedException snse) { 306 fail("SAXNotSupportedException is thrown"); 307 } catch (NullPointerException npe) { 308 // expected 309 } 310 } 311 312 @TestTargetNew( 313 level = TestLevel.COMPLETE, 314 method = "setNamespaceAware", 315 args = {boolean.class} 316 ) 317 public void test_setNamespaceAwareZ() throws Exception { 318 MyHandler mh = new MyHandler(); 319 320 spf.setNamespaceAware(true); 321 InputStream is = getClass().getResourceAsStream("/simple_ns.xml"); 322 spf.newSAXParser().parse(is, mh); 323 is.close(); 324 325 spf.setNamespaceAware(false); 326 is = getClass().getResourceAsStream("/simple_ns.xml"); 327 spf.newSAXParser().parse(is, mh); 328 is.close(); 329 } 330 331 /* public void test_setSchemaLjavax_xml_validation_Schema() { 332 SchemaFactory sf = 333 SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); 334 try { 335 Schema schema = sf.newSchema(); 336 spf.setSchema(schema); 337 assertNotNull(spf.getSchema()); 338 } catch (SAXException sax) { 339 fail("Unexpected exception " + sax.toString()); 340 } 341 } 342 */ 343 344 // public void test_setValidatingZ() { 345 // MyHandler mh = new MyHandler(); 346 // InputStream is2 = getClass().getResourceAsStream("/recipe.xml"); 347 // try { 348 // spf.setValidating(true); 349 // assertTrue(spf.isValidating()); 350 // spf.newSAXParser().parse(is2, mh); 351 // } catch (org.xml.sax.SAXException se) { 352 // fail("SAXException was thrown during parsing"); 353 // } catch (javax.xml.parsers.ParserConfigurationException pce) { 354 // fail("ParserConfigurationException was thrown during parsing"); 355 // } catch (IOException ioe) { 356 // fail("IOException was thrown during parsing"); 357 // } finally { 358 // try { 359 // is2.close(); 360 // } catch(Exception ioee) {} 361 // } 362 // InputStream is3 = getClass().getResourceAsStream("/recipe1.xml"); 363 // try { 364 // assertTrue(spf.isValidating()); 365 // spf.newSAXParser().parse(is3, mh); 366 // } catch (org.xml.sax.SAXException se) { 367 // fail("SAXException was thrown during parsing"); 368 // } catch (javax.xml.parsers.ParserConfigurationException pce) { 369 // fail("ParserConfigurationException was thrown during parsing"); 370 // } catch (IOException ioe) { 371 // fail("IOEXception was thrown during parsing: " + ioe.getMessage()); 372 // } finally { 373 // try { 374 // is3.close(); 375 // } catch(Exception ioee) {} 376 // } 377 // is2 = getClass().getResourceAsStream("/recipe.xml"); 378 // try { 379 // spf.setValidating(false); 380 // assertFalse(spf.isValidating()); 381 // spf.newSAXParser().parse(is2, mh); 382 // } catch (org.xml.sax.SAXException se) { 383 // fail("SAXException was thrown during parsing"); 384 // } catch (javax.xml.parsers.ParserConfigurationException pce) { 385 // fail("ParserConfigurationException was thrown during parsing"); 386 // } catch (IOException ioe) { 387 // fail("IOException was thrown during parsing"); 388 // } finally { 389 // try { 390 // is2.close(); 391 // } catch(Exception ioee) {} 392 // } 393 // is3 = getClass().getResourceAsStream("/recipe1.xml"); 394 // try { 395 // assertFalse(spf.isValidating()); 396 // spf.newSAXParser().parse(is3, mh); 397 // } catch (org.xml.sax.SAXException se) { 398 // fail("SAXException was thrown during parsing"); 399 // } catch (javax.xml.parsers.ParserConfigurationException pce) { 400 // fail("ParserConfigurationException was thrown during parsing"); 401 // } catch (IOException ioe) { 402 // fail("IOEXception was thrown during parsing: " + ioe.getMessage()); 403 // } finally { 404 // try { 405 // is3.close(); 406 // } catch(Exception ioee) {} 407 // } 408 // } 409 410 // public void test_setXIncludeAwareZ() { 411 // spf.setXIncludeAware(true); 412 // MyHandler mh = new MyHandler(); 413 // InputStream is = getClass().getResourceAsStream("/simple_ns.xml"); 414 // try { 415 // spf.newSAXParser().parse(is, mh); 416 // } catch(javax.xml.parsers.ParserConfigurationException pce) { 417 // fail("ParserConfigurationException was thrown during parsing"); 418 // } catch(org.xml.sax.SAXException se) { 419 // fail("SAXException was thrown during parsing"); 420 // } catch(IOException ioe) { 421 // fail("IOException was thrown during parsing"); 422 // } finally { 423 // try { 424 // is.close(); 425 // } catch(Exception ioee) {} 426 // } 427 // spf.setXIncludeAware(false); 428 // is = getClass().getResourceAsStream("/simple_ns.xml"); 429 // try { 430 // is = getClass().getResourceAsStream("/simple_ns.xml"); 431 // spf.newSAXParser().parse(is, mh); 432 // } catch(javax.xml.parsers.ParserConfigurationException pce) { 433 // fail("ParserConfigurationException was thrown during parsing"); 434 // } catch(org.xml.sax.SAXException se) { 435 // fail("SAXException was thrown during parsing"); 436 // } catch(IOException ioe) { 437 // fail("IOException was thrown during parsing"); 438 // } finally { 439 // try { 440 // is.close(); 441 // } catch(Exception ioee) {} 442 // } 443 // is = getClass().getResourceAsStream("/simple_ns.xml"); 444 // try { 445 // spf.setXIncludeAware(true); 446 // spf.newSAXParser().parse(is, mh); 447 // } catch(javax.xml.parsers.ParserConfigurationException pce) { 448 // fail("ParserConfigurationException was thrown during parsing"); 449 // } catch(org.xml.sax.SAXException se) { 450 // fail("SAXException was thrown during parsing"); 451 // } catch(IOException ioe) { 452 // fail("IOException was thrown during parsing"); 453 // } finally { 454 // try { 455 // is.close(); 456 // } catch(Exception ioee) {} 457 // } 458 // } 459 460 static class MyHandler extends DefaultHandler { 461 462 public void startElement(String uri, String localName, String qName, 463 Attributes atts) { 464 465 el.add(qName); 466 if (!uri.equals("")) 467 ns.put(qName, uri); 468 for (int i = 0; i < atts.getLength(); i++) { 469 attr.put(atts.getQName(i), atts.getValue(i)); 470 } 471 472 } 473 } 474 475 class MySAXParserFactory extends SAXParserFactory { 476 477 public MySAXParserFactory() { 478 super(); 479 } 480 481 public SAXParser newSAXParser() { 482 return null; 483 } 484 485 public void setFeature(String name, boolean value) throws 486 ParserConfigurationException, SAXNotRecognizedException, 487 SAXNotSupportedException { 488 489 } 490 491 public boolean getFeature(String name) throws 492 ParserConfigurationException, SAXNotRecognizedException, 493 SAXNotSupportedException { 494 return true; 495 } 496 497 } 498 499 } 500