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