Home | History | Annotate | Download | only in cts
      1 /*
      2  * Copyright (C) 2010 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 android.sax.cts;
     18 
     19 
     20 import org.xml.sax.Attributes;
     21 import org.xml.sax.InputSource;
     22 import org.xml.sax.XMLReader;
     23 
     24 import android.sax.Element;
     25 import android.sax.ElementListener;
     26 import android.sax.EndElementListener;
     27 import android.sax.EndTextElementListener;
     28 import android.sax.RootElement;
     29 import android.sax.StartElementListener;
     30 import android.sax.TextElementListener;
     31 import android.test.AndroidTestCase;
     32 
     33 import java.io.StringReader;
     34 
     35 import javax.xml.parsers.SAXParser;
     36 import javax.xml.parsers.SAXParserFactory;
     37 
     38 public class ElementTest extends AndroidTestCase {
     39     private static final String ATOM_NAMESPACE = "http://www.w3.org/2005/Atom";
     40     private static final String FEED = "feed";
     41     private static final String XMLFILE = "<feed xmlns='http://www.w3.org/2005/Atom'>"
     42             + "<name><id>bob</id></name>"
     43             + "<entry1><id year=\"2009\">james</id></entry1>"
     44             + "<entry2 year=\"2000\"><id>jim</id></entry2>"
     45             + "<name><id>tom</id></name>"
     46             + "<name><id>brett</id></name></feed>";
     47 
     48     private static final String ID = "id";
     49     private static final String ENTRY1 = "entry1";
     50     private static final String ENTRY2 = "entry2";
     51     private static final String NAME = "name";
     52 
     53     private static final String Y2009 = "2009";
     54     private static final String Y2000 = "2000";
     55     private static final String JIM = "jim";
     56 
     57     private static final String[] NAMES = { "bob", "tom", "brett" };
     58 
     59     private int mNameIndex;
     60     private int mEntry1ListenersCalled;
     61     private int mEntry2ListenersCalled;
     62 
     63     @Override
     64     protected void setUp() throws Exception {
     65         super.setUp();
     66         mNameIndex = 0;
     67         mEntry1ListenersCalled = 0;
     68         mEntry2ListenersCalled = 0;
     69     }
     70 
     71     public void testParse() throws Exception {
     72         RootElement root = new RootElement(ATOM_NAMESPACE, FEED);
     73         assertNotNull(root);
     74         Element name = root.getChild(ATOM_NAMESPACE, NAME);
     75         name.getChild(ATOM_NAMESPACE, ID).setEndTextElementListener(new EndTextElementListener() {
     76             public void end(String body) {
     77                 assertEquals(NAMES[mNameIndex], body);
     78                 mNameIndex++;
     79             }
     80         });
     81 
     82         Element entry1 = root.getChild(ATOM_NAMESPACE, ENTRY1);
     83         entry1.getChild(ATOM_NAMESPACE, ID).setElementListener(new ElementListener() {
     84             public void start(Attributes attributes) {
     85                 assertEquals(Y2009, attributes.getValue(0));
     86                 mEntry1ListenersCalled++;
     87             }
     88 
     89             public void end() {
     90                 mEntry1ListenersCalled++;
     91             }
     92         });
     93 
     94         Element entry2 = root.requireChild(ATOM_NAMESPACE, ENTRY2);
     95         entry2.setStartElementListener(new StartElementListener() {
     96             public void start(Attributes attributes) {
     97                 assertEquals(Y2000, attributes.getValue(0));
     98                 mEntry2ListenersCalled++;
     99             }
    100         });
    101         entry2.setEndElementListener(new EndElementListener() {
    102             public void end() {
    103                 mEntry2ListenersCalled++;
    104             }
    105         });
    106         entry2.getChild(ATOM_NAMESPACE, ID).setTextElementListener(new TextElementListener() {
    107             public void start(Attributes attributes) {
    108                 mEntry2ListenersCalled++;
    109             }
    110 
    111             public void end(String body) {
    112                 assertEquals(JIM, body);
    113                 mEntry2ListenersCalled++;
    114             }
    115         });
    116 
    117         SAXParserFactory spfactory = SAXParserFactory.newInstance();
    118         spfactory.setValidating(false);
    119 
    120         SAXParser saxParser = spfactory.newSAXParser();
    121         XMLReader xmlReader = saxParser.getXMLReader();
    122         xmlReader.setContentHandler(root.getContentHandler());
    123 
    124         InputSource source = new InputSource(new StringReader(XMLFILE));
    125         xmlReader.parse(source);
    126 
    127         assertEquals(NAMES.length, mNameIndex);
    128         assertEquals(2, mEntry1ListenersCalled);
    129         assertEquals(4, mEntry2ListenersCalled);
    130     }
    131 }
    132