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