Home | History | Annotate | Download | only in xml
      1 /*
      2  * Copyright (C) 2013 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 
     17 package libcore.xml;
     18 
     19 import java.io.IOException;
     20 import java.io.InputStream;
     21 import java.io.OutputStream;
     22 import java.io.Reader;
     23 import java.io.Writer;
     24 import junit.framework.TestCase;
     25 import org.kxml2.io.KXmlParser;
     26 import org.kxml2.io.KXmlSerializer;
     27 import org.xmlpull.v1.XmlPullParser;
     28 import org.xmlpull.v1.XmlPullParserException;
     29 import org.xmlpull.v1.XmlPullParserFactory;
     30 import org.xmlpull.v1.XmlSerializer;
     31 
     32 public class XmlPullParserFactoryTest extends TestCase {
     33 
     34     public void testDefaultNewInstance() throws Exception {
     35         XmlPullParserFactory factory = XmlPullParserFactory.newInstance(null, null);
     36         XmlPullParser parser = factory.newPullParser();
     37         XmlSerializer serializer = factory.newSerializer();
     38 
     39         assertNotNull(parser);
     40         assertNotNull(serializer);
     41         assertTrue(parser instanceof KXmlParser);
     42         assertTrue(serializer instanceof KXmlSerializer);
     43     }
     44 
     45     /**
     46      * Tests that trying to instantiate a parser with an empty list of
     47      * parsers and serializers fails.
     48      */
     49     public void testOverriding_emptyClassList() {
     50         TestXmlPullParserFactory tf = new TestXmlPullParserFactory(null, null);
     51 
     52         try {
     53             tf.newPullParser();
     54             fail();
     55         } catch (XmlPullParserException expected) {
     56         }
     57 
     58         try {
     59             tf.newPullParser();
     60             fail();
     61         } catch (XmlPullParserException expected) {
     62         }
     63     }
     64 
     65     public void testOverriding_customClassList() throws Exception {
     66         TestXmlPullParserFactory tf = new TestXmlPullParserFactory(
     67                 new String[] { "libcore.xml.XmlPullParserFactoryTest$XmlPullParserStub" },
     68                 new String[] { "libcore.xml.XmlPullParserFactoryTest$XmlSerializerStub" });
     69 
     70         assertTrue(tf.newPullParser() instanceof XmlPullParserStub);
     71         assertTrue(tf.newSerializer() instanceof XmlSerializerStub);
     72 
     73         // Also check that we ignore instantiation errors as long as
     74         // at least one parser / serializer is instantiable.
     75         tf = new TestXmlPullParserFactory(
     76                 new String[] {
     77                         "libcore.xml.XmlPullParserFactoryTest$InaccessibleXmlParser",
     78                         "libcore.xml.XmlPullParserFactoryTest$XmlPullParserStub" },
     79                 new String[] {
     80                         "libcore.xml.XmlPullParserFactoryTest$InaccessibleXmlSerializer",
     81                         "libcore.xml.XmlPullParserFactoryTest$XmlSerializerStub" });
     82 
     83         assertTrue(tf.newPullParser() instanceof XmlPullParserStub);
     84         assertTrue(tf.newSerializer() instanceof XmlSerializerStub);
     85     }
     86 
     87     // https://b/12956724
     88     public void testSetFeature_setsFeatureOnlyIfTrue() throws Exception {
     89         TestXmlPullParserFactory tf = new TestXmlPullParserFactory(
     90                 new String[] { "libcore.xml.XmlPullParserFactoryTest$XmlParserThatHatesAllFeatures" }, null);
     91 
     92         tf.setFeature("foo", false);
     93         tf.newPullParser();
     94     }
     95 
     96 
     97     /**
     98      * A class that makes use of inherited XmlPullParserFactory fields to check they are
     99      * supported.
    100      */
    101     static final class TestXmlPullParserFactory extends XmlPullParserFactory {
    102         TestXmlPullParserFactory(String[] parserClassList, String[] serializerClassList) {
    103             super();
    104             parserClasses.remove(0);
    105             serializerClasses.remove(0);
    106 
    107             try {
    108                 if (parserClassList != null) {
    109                     for (String parserClass : parserClassList) {
    110                         parserClasses.add(Class.forName(parserClass));
    111                     }
    112                 }
    113 
    114                 if (serializerClassList != null) {
    115                     for (String serializerClass : serializerClassList) {
    116                         serializerClasses.add(Class.forName(serializerClass));
    117                     }
    118                 }
    119             } catch (ClassNotFoundException ignored) {
    120                 throw new AssertionError(ignored);
    121             }
    122         }
    123     }
    124 
    125     public static final class XmlParserThatHatesAllFeatures extends XmlPullParserStub {
    126         @Override
    127         public void setFeature(String name, boolean state) {
    128             fail();
    129         }
    130     }
    131 
    132     static final class InaccessibleXmlSerializer extends XmlSerializerStub {
    133     }
    134 
    135     static final class InaccessibleXmlParser extends XmlPullParserStub {
    136     }
    137 
    138     public static class XmlSerializerStub implements XmlSerializer {
    139 
    140         public void setFeature(String name, boolean state) throws IllegalArgumentException,
    141                 IllegalStateException {
    142         }
    143 
    144         public boolean getFeature(String name) {
    145             return false;
    146         }
    147 
    148         public void setProperty(String name, Object value) {
    149         }
    150 
    151         public Object getProperty(String name) {
    152             return null;
    153         }
    154 
    155         public void setOutput(OutputStream os, String encoding) throws IOException {
    156         }
    157 
    158         public void setOutput(Writer writer) throws IOException {
    159         }
    160 
    161         public void startDocument(String encoding, Boolean standalone) throws IOException {
    162         }
    163 
    164         public void endDocument() throws IOException {
    165         }
    166 
    167         public void setPrefix(String prefix, String namespace) throws IOException {
    168         }
    169 
    170         public String getPrefix(String namespace, boolean generatePrefix) throws IllegalArgumentException {
    171             return null;
    172         }
    173 
    174         public int getDepth() {
    175             return 0;
    176         }
    177 
    178         public String getNamespace() {
    179             return null;
    180         }
    181 
    182         public String getName() {
    183             return null;
    184         }
    185 
    186         public XmlSerializer startTag(String namespace, String name) throws IOException {
    187             return null;
    188         }
    189 
    190         public XmlSerializer attribute(String namespace, String name, String value) throws IOException {
    191             return null;
    192         }
    193 
    194         public XmlSerializer endTag(String namespace, String name) throws IOException {
    195             return null;
    196         }
    197 
    198         public XmlSerializer text(String text) throws IOException {
    199             return null;
    200         }
    201 
    202         public XmlSerializer text(char[] buf, int start, int len) throws IOException {
    203             return null;
    204         }
    205 
    206         public void cdsect(String text) throws IOException {
    207         }
    208 
    209         public void entityRef(String text) throws IOException {
    210         }
    211 
    212         public void processingInstruction(String text) throws IOException {
    213         }
    214 
    215         public void comment(String text) throws IOException {
    216         }
    217 
    218         public void docdecl(String text) throws IOException {
    219         }
    220 
    221         public void ignorableWhitespace(String text) throws IOException {
    222         }
    223 
    224         public void flush() throws IOException {
    225         }
    226     }
    227 
    228     public static class XmlPullParserStub implements XmlPullParser {
    229         public void setFeature(String name, boolean state) throws XmlPullParserException {
    230         }
    231 
    232         public boolean getFeature(String name) {
    233             return false;
    234         }
    235 
    236         public void setProperty(String name, Object value) throws XmlPullParserException {
    237         }
    238 
    239         public Object getProperty(String name) {
    240             return null;
    241         }
    242 
    243         public void setInput(Reader in) throws XmlPullParserException {
    244         }
    245 
    246         public void setInput(InputStream inputStream, String inputEncoding)
    247                 throws XmlPullParserException {
    248         }
    249 
    250         public String getInputEncoding() {
    251             return null;
    252         }
    253 
    254         public void defineEntityReplacementText(String entityName, String replacementText)
    255                 throws XmlPullParserException {
    256         }
    257 
    258         public int getNamespaceCount(int depth) throws XmlPullParserException {
    259             return 0;
    260         }
    261 
    262         public String getNamespacePrefix(int pos) throws XmlPullParserException {
    263             return null;
    264         }
    265 
    266         public String getNamespaceUri(int pos) throws XmlPullParserException {
    267             return null;
    268         }
    269 
    270         public String getNamespace(String prefix) {
    271             return null;
    272         }
    273 
    274         public int getDepth() {
    275             return 0;
    276         }
    277 
    278         public String getPositionDescription() {
    279             return null;
    280         }
    281 
    282         public int getLineNumber() {
    283             return 0;
    284         }
    285 
    286         public int getColumnNumber() {
    287             return 0;
    288         }
    289 
    290         public boolean isWhitespace() throws XmlPullParserException {
    291             return false;
    292         }
    293 
    294         public String getText() {
    295             return null;
    296         }
    297 
    298         public char[] getTextCharacters(int[] holderForStartAndLength) {
    299             return null;
    300         }
    301 
    302         public String getNamespace() {
    303             return null;
    304         }
    305 
    306         public String getName() {
    307             return null;
    308         }
    309 
    310         public String getPrefix() {
    311             return null;
    312         }
    313 
    314         public boolean isEmptyElementTag() throws XmlPullParserException {
    315             return false;
    316         }
    317 
    318         public int getAttributeCount() {
    319             return 0;
    320         }
    321 
    322         public String getAttributeNamespace(int index) {
    323             return null;
    324         }
    325 
    326         public String getAttributeName(int index) {
    327             return null;
    328         }
    329 
    330         public String getAttributePrefix(int index) {
    331             return null;
    332         }
    333 
    334         public String getAttributeType(int index) {
    335             return null;
    336         }
    337 
    338         public boolean isAttributeDefault(int index) {
    339             return false;
    340         }
    341 
    342         public String getAttributeValue(int index) {
    343             return null;
    344         }
    345 
    346         public String getAttributeValue(String namespace, String name) {
    347             return null;
    348         }
    349 
    350         public int getEventType() throws XmlPullParserException {
    351             return 0;
    352         }
    353 
    354         public int next() throws XmlPullParserException, IOException {
    355             return 0;
    356         }
    357 
    358         public int nextToken() throws XmlPullParserException, IOException {
    359             return 0;
    360         }
    361 
    362         public void require(int type, String namespace, String name)
    363                 throws XmlPullParserException, IOException {
    364         }
    365 
    366         public String nextText() throws XmlPullParserException, IOException {
    367             return null;
    368         }
    369 
    370         public int nextTag() throws XmlPullParserException, IOException {
    371             return 0;
    372         }
    373     }
    374 }
    375