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