Home | History | Annotate | Download | only in dom
      1 /*
      2  This Java source file was generated by test-to-java.xsl
      3  and is a derived work from the source document.
      4  The source document contained the following notice:
      5 
      6 
      7 
      8  Copyright (c) 2001 World Wide Web Consortium,
      9  (Massachusetts Institute of Technology, Institut National de
     10  Recherche en Informatique et en Automatique, Keio University).  All
     11  Rights Reserved.  This program is distributed under the W3C's Software
     12  Intellectual Property License.  This program is distributed in the
     13  hope that it will be useful, but WITHOUT ANY WARRANTY; without even
     14  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
     15  PURPOSE.
     16 
     17  See W3C License http://www.w3.org/Consortium/Legal/ for more details.
     18 
     19 
     20  */
     21 
     22 package tests.org.w3c.dom;
     23 
     24 import dalvik.annotation.TestTargets;
     25 import dalvik.annotation.TestLevel;
     26 import dalvik.annotation.TestTargetNew;
     27 import dalvik.annotation.TestTargetClass;
     28 
     29 import org.w3c.dom.Element;
     30 import org.w3c.dom.Document;
     31 import org.w3c.dom.Node;
     32 import org.w3c.dom.NodeList;
     33 import org.w3c.dom.DOMException;
     34 import org.w3c.dom.Attr;
     35 
     36 import javax.xml.parsers.DocumentBuilder;
     37 
     38 /**
     39  * The "setAttributeNS(namespaceURI,qualifiedName,Value)" method raises a
     40  * INVALID_CHARACTER_ERR DOMException if the specified prefix contains an
     41  * illegal character.
     42  *
     43  * Attempt to add a new attribute on the first employee node. An exception
     44  * should be raised since the "qualifiedName" has an invalid character.
     45  *
     46  * @author NIST
     47  * @author Mary Brady
     48  * @see <a
     49  *      href="http://www.w3.org/TR/DOM-Level-2-Core/core#xpointer(id('ID-258A00AF')/constant[@name='INVALID_CHARACTER_ERR'])">http://www.w3.org/TR/DOM-Level-2-Core/core#xpointer(id('ID-258A00AF')/constant[@name='INVALID_CHARACTER_ERR'])</a>
     50  * @see <a
     51  *      href="http://www.w3.org/TR/DOM-Level-2-Core/core#ID-ElSetAttrNS">http://www.w3.org/TR/DOM-Level-2-Core/core#ID-ElSetAttrNS</a>
     52  * @see <a
     53  *      href="http://www.w3.org/TR/DOM-Level-2-Core/core#xpointer(id('ID-ElSetAttrNS')/raises/exception[@name='DOMException']/descr/p[substring-before(.,':')='INVALID_CHARACTER_ERR'])">http://www.w3.org/TR/DOM-Level-2-Core/core#xpointer(id('ID-ElSetAttrNS')/raises/exception[@name='DOMException']/descr/p[substring-before(.,':')='INVALID_CHARACTER_ERR'])</a>
     54  */
     55 @TestTargetClass(Element.class)
     56 public final class SetAttributeNS extends DOMTestCase {
     57 
     58     DOMDocumentBuilderFactory factory;
     59 
     60     DocumentBuilder builder;
     61 
     62     protected void setUp() throws Exception {
     63         super.setUp();
     64         try {
     65             factory = new DOMDocumentBuilderFactory(DOMDocumentBuilderFactory
     66                     .getConfiguration1());
     67             builder = factory.getBuilder();
     68         } catch (Exception e) {
     69             fail("Unexpected exception" + e.getMessage());
     70         }
     71     }
     72 
     73     protected void tearDown() throws Exception {
     74         factory = null;
     75         builder = null;
     76         super.tearDown();
     77     }
     78 
     79     /**
     80      * Runs the test case.
     81      *
     82      * @throws Throwable
     83      *             Any uncaught exception causes test to fail
     84      */
     85     @TestTargetNew(
     86         level = TestLevel.PARTIAL_COMPLETE,
     87         notes = "Verifies DOMException with INVALID_CHARACTER_ERR code.",
     88         method = "setAttributeNS",
     89         args = {java.lang.String.class, java.lang.String.class, java.lang.String.class}
     90     )
     91     public void testSetAttributeNS1() throws Throwable {
     92         String namespaceURI = "http://www.nist.gov";
     93         String qualifiedName = "emp:qual?name";
     94         Document doc;
     95         NodeList elementList;
     96         Node testAddr;
     97         doc = (Document) load("staffNS", builder);
     98         elementList = doc.getElementsByTagName("employee");
     99         testAddr = elementList.item(0);
    100 
    101         {
    102             boolean success = false;
    103             try {
    104                 ((Element) /* Node */testAddr).setAttributeNS(namespaceURI,
    105                         qualifiedName, "newValue");
    106             } catch (DOMException ex) {
    107                 success = (ex.code == DOMException.INVALID_CHARACTER_ERR);
    108             }
    109             assertTrue("throw_INVALID_CHARACTER_ERR", success);
    110         }
    111     }
    112     @TestTargetNew(
    113         level = TestLevel.PARTIAL_COMPLETE,
    114         notes = "Verifies DOMException with NAMESPACE_ERR code.",
    115         method = "setAttributeNS",
    116         args = {java.lang.String.class, java.lang.String.class, java.lang.String.class}
    117     )
    118     public void testSetAttributeNS2() throws Throwable {
    119         String namespaceURI = "http://www.nist.gov";
    120         String qualifiedName = "emp:";
    121         Document doc;
    122         NodeList elementList;
    123         Node testAddr;
    124         doc = (Document) load("staffNS", builder);
    125         elementList = doc.getElementsByTagName("emp:employee");
    126         testAddr = elementList.item(0);
    127 
    128         {
    129             // BEGIN android-changed
    130             //     Our exception priorities differ from the spec
    131             try {
    132                 ((Element) /* Node */testAddr).setAttributeNS(namespaceURI,
    133                         qualifiedName, "newValue");
    134                 fail();
    135             } catch (DOMException ex) {
    136             }
    137             // END android-changed
    138         }
    139     }
    140 
    141 // Assumes validation.
    142 //    public void testSetAttributeNS3() throws Throwable {
    143 //        String namespaceURI = "www.xyz.com";
    144 //        String qualifiedName = "emp:local1";
    145 //        Document doc;
    146 //        NodeList genderList;
    147 //        Node gender;
    148 //        NodeList genList;
    149 //        Node gen;
    150 //        NodeList gList;
    151 //        Element genElement;
    152 //        int nodeType;
    153 //        doc = (Document) load("staffNS", builder);
    154 //        genderList = doc.getElementsByTagName("gender");
    155 //        gender = genderList.item(2);
    156 //        genList = gender.getChildNodes();
    157 //        gen = genList.item(0);
    158 //        nodeType = (int) gen.getNodeType();
    159 //
    160 //        if (1 == nodeType) {
    161 //            gen = doc.createEntityReference("ent4");
    162 //            assertNotNull("createdEntRefNotNull", gen);
    163 //        }
    164 //        gList = gen.getChildNodes();
    165 //        genElement = (Element) gList.item(0);
    166 //        assertNotNull("notnull", genElement);
    167 //
    168 //        {
    169 //            boolean success = false;
    170 //            try {
    171 //                genElement.setAttributeNS(namespaceURI, qualifiedName,
    172 //                        "newValue");
    173 //            } catch (DOMException ex) {
    174 //                success = (ex.code == DOMException.NO_MODIFICATION_ALLOWED_ERR);
    175 //            }
    176 //            assertTrue("throw_NO_MODIFICATION_ALLOWED_ERR", success);
    177 //        }
    178 //    }
    179     @TestTargetNew(
    180         level = TestLevel.PARTIAL_COMPLETE,
    181         notes = "Verifies positive functionality.",
    182         method = "setAttributeNS",
    183         args = {String.class, String.class, String.class}
    184     )
    185     public void testSetAttributeNS4() throws Throwable {
    186         Document doc;
    187         NodeList elementList;
    188         Node testAddr;
    189         Attr addrAttr;
    190         String resultAttr;
    191         String resultNamespaceURI;
    192         String resultLocalName;
    193         String resultPrefix;
    194         doc = (Document) load("staffNS", builder);
    195         elementList = doc.getElementsByTagName("emp:address");
    196         testAddr = elementList.item(0);
    197         assertNotNull("empAddrNotNull", testAddr);
    198         ((Element) /* Node */testAddr).setAttributeNS("http://www.nist.gov",
    199                 "newprefix:zone", "newValue");
    200         addrAttr = ((Element) /* Node */testAddr).getAttributeNodeNS(
    201                 "http://www.nist.gov", "zone");
    202         resultAttr = ((Element) /* Node */testAddr).getAttributeNS(
    203                 "http://www.nist.gov", "zone");
    204         assertEquals("attrValue", "newValue", resultAttr);
    205         resultNamespaceURI = addrAttr.getNamespaceURI();
    206         assertEquals("nsuri", "http://www.nist.gov", resultNamespaceURI);
    207         resultLocalName = addrAttr.getLocalName();
    208         assertEquals("lname", "zone", resultLocalName);
    209         resultPrefix = addrAttr.getPrefix();
    210         assertEquals("prefix", "newprefix", resultPrefix);
    211     }
    212 
    213     @TestTargetNew(
    214         level = TestLevel.PARTIAL_COMPLETE,
    215         notes = "Verifies positive functionality.",
    216         method = "setAttributeNS",
    217         args = {java.lang.String.class, java.lang.String.class, java.lang.String.class}
    218     )
    219     public void testSetAttributeNS5() throws Throwable {
    220         String localName = "newAttr";
    221         String namespaceURI = "http://www.newattr.com";
    222         String qualifiedName = "emp:newAttr";
    223         Document doc;
    224         NodeList elementList;
    225         Node testAddr;
    226 
    227         String resultAttr;
    228         doc = (Document) load("staffNS", builder);
    229         elementList = doc.getElementsByTagName("emp:address");
    230         testAddr = elementList.item(0);
    231         assertNotNull("empAddrNotNull", testAddr);
    232         ((Element) /* Node */testAddr).setAttributeNS(namespaceURI,
    233                 qualifiedName, "<newValue>");
    234         resultAttr = ((Element) /* Node */testAddr).getAttributeNS(
    235                 namespaceURI, localName);
    236         assertEquals("throw_Equals", "<newValue>", resultAttr);
    237     }
    238     @TestTargetNew(
    239         level = TestLevel.PARTIAL_COMPLETE,
    240         notes = "Verifies DOMException with NAMESPACE_ERR code.",
    241         method = "setAttributeNS",
    242         args = {java.lang.String.class, java.lang.String.class, java.lang.String.class}
    243     )
    244     public void testSetAttributeNS6() throws Throwable {
    245         String namespaceURI = "http://www.nist.gov";
    246         String qualifiedName = "xml:qualifiedName";
    247         Document doc;
    248         NodeList elementList;
    249         Node testAddr;
    250         doc = (Document) load("staffNS", builder);
    251         elementList = doc.getElementsByTagName("employee");
    252         testAddr = elementList.item(0);
    253 
    254         {
    255             boolean success = false;
    256             try {
    257                 ((Element) /* Node */testAddr).setAttributeNS(namespaceURI,
    258                         qualifiedName, "newValue");
    259             } catch (DOMException ex) {
    260                 success = (ex.code == DOMException.NAMESPACE_ERR);
    261             }
    262             assertTrue("throw_NAMESPACE_ERR", success);
    263         }
    264     }
    265     @TestTargetNew(
    266         level = TestLevel.PARTIAL_COMPLETE,
    267         notes = "Verifies DOMException with NAMESPACE_ERR.",
    268         method = "setAttributeNS",
    269         args = {java.lang.String.class, java.lang.String.class, java.lang.String.class}
    270     )
    271     public void testSetAttributeNS7() throws Throwable {
    272         String namespaceURI = "http://www.nist.gov";
    273         String qualifiedName = "xmlns";
    274         Document doc;
    275         NodeList elementList;
    276         Node testAddr;
    277         doc = (Document) load("staffNS", builder);
    278         elementList = doc.getElementsByTagName("employee");
    279         testAddr = elementList.item(0);
    280 
    281         {
    282             boolean success = false;
    283             try {
    284                 ((Element) /* Node */testAddr).setAttributeNS(namespaceURI,
    285                         qualifiedName, "newValue");
    286             } catch (DOMException ex) {
    287                 success = (ex.code == DOMException.NAMESPACE_ERR);
    288             }
    289             assertTrue("throw_NAMESPACE_ERR", success);
    290         }
    291     }
    292     @TestTargetNew(
    293         level = TestLevel.PARTIAL_COMPLETE,
    294         notes = "Verifies positive functionality.",
    295         method = "setAttributeNS",
    296         args = {java.lang.String.class, java.lang.String.class, java.lang.String.class}
    297     )
    298     public void testSetAttributeNS9() throws Throwable {
    299         String localName = "newAttr";
    300         String namespaceURI = "http://www.newattr.com";
    301         String qualifiedName = "emp:newAttr";
    302         Document doc;
    303         NodeList elementList;
    304         Node testAddr;
    305         Attr addrAttr;
    306         String resultAttr;
    307         String resultNamespaceURI;
    308         String resultLocalName;
    309         String resultPrefix;
    310         doc = (Document) load("staffNS", builder);
    311         elementList = doc.getElementsByTagName("emp:address");
    312         testAddr = elementList.item(0);
    313         assertNotNull("empAddrNotNull", testAddr);
    314         ((Element) /* Node */testAddr).setAttributeNS(namespaceURI,
    315                 qualifiedName, "newValue");
    316         addrAttr = ((Element) /* Node */testAddr).getAttributeNodeNS(
    317                 namespaceURI, localName);
    318         resultAttr = ((Element) /* Node */testAddr).getAttributeNS(
    319                 namespaceURI, localName);
    320         assertEquals("attrValue", "newValue", resultAttr);
    321         resultNamespaceURI = addrAttr.getNamespaceURI();
    322         assertEquals("nsuri", "http://www.newattr.com", resultNamespaceURI);
    323         resultLocalName = addrAttr.getLocalName();
    324         assertEquals("lname", "newAttr", resultLocalName);
    325         resultPrefix = addrAttr.getPrefix();
    326         assertEquals("prefix", "emp", resultPrefix);
    327     }
    328     @TestTargetNew(
    329         level = TestLevel.PARTIAL_COMPLETE,
    330         notes = "Verifies DOMException with NAMESPACE_ERR code.",
    331         method = "setAttributeNS",
    332         args = {java.lang.String.class, java.lang.String.class, java.lang.String.class}
    333     )
    334     public void testSetAttributeNS10() throws Throwable {
    335         String namespaceURI = "http://www.example.gov";
    336         Document doc;
    337         NodeList elementList;
    338         Node testAddr;
    339         doc = (Document) load("hc_staff", builder);
    340         elementList = doc.getElementsByTagName("em");
    341         testAddr = elementList.item(0);
    342 
    343         {
    344             // BEGIN android-changed
    345             //     Our exception priorities differ from the spec
    346             try {
    347                 ((Element) /* Node */testAddr).setAttributeNS(namespaceURI, "",
    348                         "newValue");
    349                 fail();
    350             } catch (DOMException ex) {
    351             }
    352             // END android-changed
    353         }
    354     }
    355 }
    356