Home | History | Annotate | Download | only in ext
      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 
     17 package tests.api.org.xml.sax.ext;
     18 
     19 import junit.framework.TestCase;
     20 
     21 import org.xml.sax.Attributes;
     22 import org.xml.sax.ext.Attributes2Impl;
     23 import org.xml.sax.helpers.AttributesImpl;
     24 
     25 public class Attributes2ImplTest extends TestCase {
     26 
     27     // Note: The original SAX2 implementation of Attributes2Impl is
     28     // severely broken. Thus all of these tests will probably fail
     29     // unless the Android implementation of the class gets fixed.
     30 
     31     private Attributes2Impl empty = new Attributes2Impl();
     32 
     33     private Attributes2Impl multi = new Attributes2Impl();
     34 
     35     private Attributes2Impl cdata = new Attributes2Impl();
     36 
     37     @Override
     38     public void setUp() {
     39         multi.addAttribute("http://some.uri", "foo", "ns1:foo",
     40                 "string", "abc");
     41         multi.addAttribute("http://some.uri", "bar", "ns1:bar",
     42                 "string", "xyz");
     43         multi.addAttribute("http://some.other.uri", "answer", "ns2:answer",
     44                 "int", "42");
     45         multi.addAttribute("http://yet.another.uri", "gabba", "ns3:gabba",
     46                 "string", "gabba");
     47 
     48         multi.setDeclared(0, false);
     49         multi.setSpecified(0, false);
     50 
     51         multi.setDeclared(1, true);
     52         multi.setSpecified(1, false);
     53 
     54         multi.setDeclared(2, false);
     55         multi.setSpecified(2, true);
     56 
     57         multi.setDeclared(3, true);
     58         multi.setSpecified(3, true);
     59 
     60         cdata.addAttribute("http://yet.another.uri", "hey", "ns3:hey",
     61                 "CDATA", "hey");
     62     }
     63 
     64     public void testSetAttributes() {
     65         // Ordinary case with Attributes2Impl
     66         Attributes2Impl attrs = new Attributes2Impl();
     67         attrs.addAttribute("", "", "john", "string", "doe");
     68 
     69         attrs.setAttributes(empty);
     70         assertEquals(0, attrs.getLength());
     71 
     72         attrs.setAttributes(multi);
     73         for (int i = 0; i < multi.getLength(); i++) {
     74             assertEquals(multi.getURI(i), attrs.getURI(i));
     75             assertEquals(multi.getLocalName(i), attrs.getLocalName(i));
     76             assertEquals(multi.getQName(i), attrs.getQName(i));
     77             assertEquals(multi.getType(i), attrs.getType(i));
     78             assertEquals(multi.getValue(i), attrs.getValue(i));
     79             assertEquals(multi.isDeclared(i), attrs.isDeclared(i));
     80             assertEquals(multi.isSpecified(i), attrs.isSpecified(i));
     81         }
     82 
     83         attrs.setAttributes(empty);
     84         assertEquals(0, attrs.getLength());
     85 
     86         // Ordinary case with AttributesImpl
     87         attrs.setAttributes(new AttributesImpl(multi));
     88         assertEquals(multi.getLength(), attrs.getLength());
     89 
     90         for (int i = 0; i < multi.getLength(); i++) {
     91             assertEquals(multi.getURI(i), attrs.getURI(i));
     92             assertEquals(multi.getLocalName(i), attrs.getLocalName(i));
     93             assertEquals(multi.getQName(i), attrs.getQName(i));
     94             assertEquals(multi.getType(i), attrs.getType(i));
     95             assertEquals(multi.getValue(i), attrs.getValue(i));
     96             assertEquals(true, attrs.isDeclared(i));
     97             assertEquals(true, attrs.isSpecified(i));
     98         }
     99 
    100         // Special case with CDATA
    101         attrs.setAttributes(new AttributesImpl(cdata));
    102         assertEquals(1, attrs.getLength());
    103         assertEquals(false, attrs.isDeclared(0));
    104         assertEquals(true, attrs.isSpecified(0));
    105 
    106         // null case
    107         try {
    108             attrs.setAttributes(null);
    109             fail("NullPointerException expected");
    110         } catch (NullPointerException e) {
    111             // Expected
    112         }
    113     }
    114 
    115     public void testAddAttribute() {
    116         Attributes2Impl attrs = new Attributes2Impl();
    117 
    118         // Ordinary case
    119         attrs.addAttribute("http://yet.another.uri", "doe", "john:doe",
    120                 "string", "abc");
    121 
    122         assertEquals(1, attrs.getLength());
    123 
    124         assertEquals("http://yet.another.uri", attrs.getURI(0));
    125         assertEquals("doe", attrs.getLocalName(0));
    126         assertEquals("john:doe", attrs.getQName(0));
    127         assertEquals("string", attrs.getType(0));
    128         assertEquals("abc", attrs.getValue(0));
    129 
    130         assertEquals(true, attrs.isDeclared(0));
    131         assertEquals(true, attrs.isSpecified(0));
    132 
    133         // CDATA case
    134         attrs.addAttribute("http://yet.another.uri", "doe", "jane:doe",
    135                 "CDATA", "abc");
    136 
    137         assertEquals(2, attrs.getLength());
    138 
    139         assertEquals("http://yet.another.uri", attrs.getURI(1));
    140         assertEquals("doe", attrs.getLocalName(1));
    141         assertEquals("jane:doe", attrs.getQName(1));
    142         assertEquals("CDATA", attrs.getType(1));
    143         assertEquals("abc", attrs.getValue(1));
    144 
    145         assertEquals(false, attrs.isDeclared(1));
    146         assertEquals(true, attrs.isSpecified(1));
    147     }
    148 
    149     public void testRemoveAttribute() {
    150         Attributes2Impl attrs = new Attributes2Impl(multi);
    151 
    152         // Ordinary case
    153         attrs.removeAttribute(1);
    154 
    155         assertEquals(3, attrs.getLength());
    156 
    157         assertEquals(multi.getURI(0), attrs.getURI(0));
    158         assertEquals(multi.getLocalName(0), attrs.getLocalName(0));
    159         assertEquals(multi.getQName(0), attrs.getQName(0));
    160         assertEquals(multi.getType(0), attrs.getType(0));
    161         assertEquals(multi.getValue(0), attrs.getValue(0));
    162         assertEquals(multi.isDeclared(0), attrs.isDeclared(0));
    163         assertEquals(multi.isSpecified(0), attrs.isSpecified(0));
    164 
    165         assertEquals(multi.getURI(2), attrs.getURI(1));
    166         assertEquals(multi.getLocalName(2), attrs.getLocalName(1));
    167         assertEquals(multi.getQName(2), attrs.getQName(1));
    168         assertEquals(multi.getType(2), attrs.getType(1));
    169         assertEquals(multi.getValue(2), attrs.getValue(1));
    170         assertEquals(multi.isDeclared(2), attrs.isDeclared(1));
    171         assertEquals(multi.isSpecified(2), attrs.isSpecified(1));
    172 
    173         // Out of range
    174         try {
    175             attrs.removeAttribute(-1);
    176             fail("ArrayIndexOutOfBoundsException expected");
    177         } catch (ArrayIndexOutOfBoundsException e) {
    178             // Expected
    179         }
    180 
    181         try {
    182             attrs.removeAttribute(3);
    183             fail("ArrayIndexOutOfBoundsException expected");
    184         } catch (ArrayIndexOutOfBoundsException e) {
    185             // Expected
    186         }
    187     }
    188 
    189     public void testAttributes2Impl() {
    190         assertEquals(0, empty.getLength());
    191     }
    192 
    193     public void testAttributes2ImplAttributes() {
    194         // Ordinary case with Attributes2Impl
    195         Attributes2Impl attrs = new Attributes2Impl(multi);
    196         assertEquals(multi.getLength(), attrs.getLength());
    197 
    198         for (int i = 0; i < multi.getLength(); i++) {
    199             assertEquals(multi.getURI(i), attrs.getURI(i));
    200             assertEquals(multi.getLocalName(i), attrs.getLocalName(i));
    201             assertEquals(multi.getQName(i), attrs.getQName(i));
    202             assertEquals(multi.getType(i), attrs.getType(i));
    203             assertEquals(multi.getValue(i), attrs.getValue(i));
    204             assertEquals(multi.isDeclared(i), attrs.isDeclared(i));
    205             assertEquals(multi.isSpecified(i), attrs.isSpecified(i));
    206         }
    207 
    208         attrs = new Attributes2Impl(empty);
    209         assertEquals(0, attrs.getLength());
    210 
    211         // Ordinary case with AttributesImpl
    212         attrs = new Attributes2Impl(new AttributesImpl(multi));
    213         assertEquals(multi.getLength(), attrs.getLength());
    214 
    215         for (int i = 0; i < multi.getLength(); i++) {
    216             assertEquals(multi.getURI(i), attrs.getURI(i));
    217             assertEquals(multi.getLocalName(i), attrs.getLocalName(i));
    218             assertEquals(multi.getQName(i), attrs.getQName(i));
    219             assertEquals(multi.getType(i), attrs.getType(i));
    220             assertEquals(multi.getValue(i), attrs.getValue(i));
    221             assertEquals(true, attrs.isDeclared(i));
    222             assertEquals(true, attrs.isSpecified(i));
    223         }
    224 
    225         // Special case with CDATA
    226         attrs = new Attributes2Impl(new AttributesImpl(cdata));
    227         assertEquals(1, attrs.getLength());
    228         assertEquals(false, attrs.isDeclared(0));
    229         assertEquals(true, attrs.isSpecified(0));
    230 
    231         // null case
    232         try {
    233             attrs = new Attributes2Impl(null);
    234             fail("NullPointerException expected");
    235         } catch (NullPointerException e) {
    236             // Expected
    237         }
    238     }
    239 
    240     public void testIsDeclaredInt() {
    241         // Ordinary cases
    242         assertEquals(false, multi.isDeclared(0));
    243         assertEquals(true, multi.isDeclared(1));
    244 
    245         // Out of range
    246         try {
    247             multi.isDeclared(-1);
    248             fail("ArrayIndexOutOfBoundsException expected");
    249         } catch (ArrayIndexOutOfBoundsException e) {
    250             // Expected
    251         }
    252 
    253         try {
    254             multi.isDeclared(4);
    255             fail("ArrayIndexOutOfBoundsException expected");
    256         } catch (ArrayIndexOutOfBoundsException e) {
    257             // Expected
    258         }
    259     }
    260 
    261     public void testIsDeclaredStringString() {
    262         // Ordinary cases
    263         assertEquals(false, multi.isDeclared("http://some.uri", "foo"));
    264         assertEquals(true, multi.isDeclared("http://some.uri", "bar"));
    265 
    266         // Not found
    267         try {
    268             assertFalse(multi.isDeclared("not", "found"));
    269             fail("IllegalArgumentException expected");
    270         } catch (IllegalArgumentException e) {
    271             // Expected
    272         }
    273     }
    274 
    275     public void testIsDeclaredString() {
    276         // Ordinary cases
    277         assertEquals(false, multi.isDeclared("ns1:foo"));
    278         assertEquals(true, multi.isDeclared("ns1:bar"));
    279 
    280         // Not found
    281         try {
    282             assertFalse(multi.isDeclared("notfound"));
    283             fail("IllegalArgumentException expected");
    284         } catch (IllegalArgumentException e) {
    285             // Expected
    286         }
    287     }
    288 
    289     public void testIsSpecifiedInt() {
    290         // Ordinary cases
    291         assertEquals(false, multi.isSpecified(1));
    292         assertEquals(true, multi.isSpecified(2));
    293 
    294         // Out of range
    295         try {
    296             multi.isSpecified(-1);
    297             fail("ArrayIndexOutOfBoundsException expected");
    298         } catch (ArrayIndexOutOfBoundsException e) {
    299             // Expected
    300         }
    301 
    302         try {
    303             multi.isSpecified(4);
    304             fail("ArrayIndexOutOfBoundsException expected");
    305         } catch (ArrayIndexOutOfBoundsException e) {
    306             // Expected
    307         }
    308     }
    309 
    310     public void testIsSpecifiedStringString() {
    311         // Ordinary cases
    312         assertEquals(false, multi.isSpecified("http://some.uri", "bar"));
    313         assertEquals(true, multi.isSpecified("http://some.other.uri", "answer"));
    314 
    315         // Not found
    316         try {
    317             assertFalse(multi.isSpecified("not", "found"));
    318             fail("IllegalArgumentException expected");
    319         } catch (IllegalArgumentException e) {
    320             // Expected
    321         }
    322     }
    323 
    324     public void testIsSpecifiedString() {
    325         // Ordinary cases
    326         assertEquals(false, multi.isSpecified("ns1:bar"));
    327         assertEquals(true, multi.isSpecified("ns2:answer"));
    328 
    329         // Not found
    330         try {
    331             assertFalse(multi.isSpecified("notfound"));
    332             fail("IllegalArgumentException expected");
    333         } catch (IllegalArgumentException e) {
    334             // Expected
    335         }
    336     }
    337 
    338     public void testSetDeclared() {
    339         // Ordinary cases
    340         multi.setSpecified(0, false);
    341         assertEquals(false, multi.isSpecified(0));
    342 
    343         multi.setSpecified(0, true);
    344         assertEquals(true, multi.isSpecified(0));
    345 
    346         multi.setSpecified(0, false);
    347         assertEquals(false, multi.isSpecified(0));
    348 
    349         // Out of range
    350         try {
    351             multi.setSpecified(-1, true);
    352             fail("ArrayIndexOutOfBoundsException expected");
    353         } catch (ArrayIndexOutOfBoundsException e) {
    354             // Expected
    355         }
    356 
    357         try {
    358             multi.setSpecified(5, true);
    359             fail("ArrayIndexOutOfBoundsException expected");
    360         } catch (ArrayIndexOutOfBoundsException e) {
    361             // Expected
    362         }
    363     }
    364 
    365     public void testSetSpecified() {
    366         // Ordinary cases
    367         multi.setSpecified(0, false);
    368         assertEquals(false, multi.isSpecified(0));
    369 
    370         multi.setSpecified(0, true);
    371         assertEquals(true, multi.isSpecified(0));
    372 
    373         multi.setSpecified(0, false);
    374         assertEquals(false, multi.isSpecified(0));
    375 
    376         // Out of range
    377         try {
    378             multi.setSpecified(-1, true);
    379             fail("ArrayIndexOutOfBoundsException expected");
    380         } catch (ArrayIndexOutOfBoundsException e) {
    381             // Expected
    382         }
    383 
    384         try {
    385             multi.setSpecified(5, true);
    386             fail("ArrayIndexOutOfBoundsException expected");
    387         } catch (ArrayIndexOutOfBoundsException e) {
    388             // Expected
    389         }
    390     }
    391 
    392 }
    393