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 org.w3c.dom.NamedNodeMap;
     25 import org.w3c.dom.Document;
     26 import org.w3c.dom.Attr;
     27 import org.w3c.dom.Node;
     28 import org.w3c.dom.NodeList;
     29 import org.w3c.dom.Element;
     30 import org.w3c.dom.DOMException;
     31 import org.w3c.dom.DOMImplementation;
     32 import org.w3c.dom.DocumentType;
     33 
     34 import javax.xml.parsers.DocumentBuilder;
     35 
     36 /**
     37  * The method setNamedItemNS adds a node using its namespaceURI and localName.
     38  * If a node with that namespace URI and that local name is already present in
     39  * this map, it is replaced by the new one.
     40  *
     41  * Retreive the first element whose localName is address and namespaceURI
     42  * http://www.nist.gov", and put its attributes into a named node map. Create a
     43  * new attribute node and add it to this map. Verify if the attr node was
     44  * successfully added by checking the nodeName of the retreived atttribute.
     45  *
     46  * @author IBM
     47  * @author Neil Delima
     48  * @see <a
     49  *      href="http://www.w3.org/TR/DOM-Level-2-Core/core#ID-getNamedItemNS">http://www.w3.org/TR/DOM-Level-2-Core/core#ID-getNamedItemNS</a>
     50  */
     51 public final class NamedNodeMapSetNamedItemNS extends DOMTestCase {
     52 
     53     DOMDocumentBuilderFactory factory;
     54 
     55     DocumentBuilder builder;
     56 
     57     protected void setUp() throws Exception {
     58         super.setUp();
     59         try {
     60             // Changed to configuration #2. This test case just doesn't make
     61             // sense without a namespace-aware parser. It actually fails even
     62             // on the JDK in that case.
     63             factory = new DOMDocumentBuilderFactory(DOMDocumentBuilderFactory
     64                     .getConfiguration2());
     65             builder = factory.getBuilder();
     66         } catch (Exception e) {
     67             fail("Unexpected exception" + e.getMessage());
     68         }
     69     }
     70 
     71     protected void tearDown() throws Exception {
     72         factory = null;
     73         builder = null;
     74         super.tearDown();
     75     }
     76 
     77     /**
     78      * Runs the test case.
     79      *
     80      * @throws Throwable
     81      *             Any uncaught exception causes test to fail
     82      */
     83     public void testSetNamedItemNS1() throws Throwable {
     84         Document doc;
     85         NamedNodeMap attributes;
     86         Node element;
     87         Attr attribute;
     88 
     89         Attr newAttr1;
     90         NodeList elementList;
     91         String attrName;
     92         doc = (Document) load("staffNS", builder);
     93         elementList = doc.getElementsByTagNameNS("http://www.nist.gov",
     94                 "address");
     95         element = elementList.item(0);
     96         attributes = element.getAttributes();
     97         newAttr1 = doc.createAttributeNS("http://www.w3.org/DOM/L1", "streets");
     98         ((Element) /* Node */element).setAttributeNodeNS(newAttr1);
     99         attribute = (Attr) attributes.getNamedItemNS(
    100                 "http://www.w3.org/DOM/L1", "streets");
    101         attrName = attribute.getNodeName();
    102         assertEquals("namednodemapsetnameditemns01", "streets", attrName);
    103     }
    104     public void testSetNamedItemNS2() throws Throwable {
    105         Document doc;
    106         NamedNodeMap attributes;
    107         Element element;
    108         Attr attribute;
    109         Attr attribute1;
    110 
    111         String attrName;
    112         doc = (Document) load("staffNS", builder);
    113         element = doc.createElementNS("http://www.w3.org/DOM/Test", "root");
    114         attribute1 = doc
    115                 .createAttributeNS("http://www.w3.org/DOM/L1", "L1:att");
    116         attributes = element.getAttributes();
    117         attributes.setNamedItemNS(attribute1);
    118         attribute = (Attr) attributes.getNamedItemNS(
    119                 "http://www.w3.org/DOM/L1", "att");
    120         attrName = attribute.getNodeName();
    121         assertEquals("namednodemapsetnameditemns02", "L1:att", attrName);
    122     }
    123     public void testSetNamedItemNS3() throws Throwable {
    124 
    125         Document doc;
    126         Document docAlt;
    127         NamedNodeMap attributes;
    128         NamedNodeMap attributesAlt;
    129         NodeList elementList;
    130         NodeList elementListAlt;
    131         Element element;
    132         Element elementAlt;
    133         Attr attr;
    134 
    135         String nullNS = null;
    136 
    137         doc = (Document) load("staffNS", builder);
    138         elementList = doc.getElementsByTagNameNS("*", "address");
    139         element = (Element) elementList.item(1);
    140         attributes = element.getAttributes();
    141         docAlt = (Document) load("staffNS", builder);
    142         elementListAlt = docAlt.getElementsByTagNameNS("*", "address");
    143         elementAlt = (Element) elementListAlt.item(1);
    144         attributesAlt = elementAlt.getAttributes();
    145         attr = (Attr) attributesAlt.getNamedItemNS(nullNS, "street");
    146         attributesAlt.removeNamedItemNS(nullNS, "street");
    147 
    148         {
    149             boolean success = false;
    150             try {
    151                 attributes.setNamedItemNS(attr);
    152             } catch (DOMException ex) {
    153                 success = (ex.code == DOMException.WRONG_DOCUMENT_ERR);
    154             }
    155             assertTrue("throw_WRONG_DOCUMENT_ERR", success);
    156         }
    157     }
    158     public void testSetNamedItemNS4() throws Throwable {
    159         Document doc;
    160         DOMImplementation domImpl;
    161         Document docAlt;
    162         DocumentType docType = null;
    163 
    164         NamedNodeMap attributes;
    165         NodeList elementList;
    166         Element element;
    167         Attr attrAlt;
    168 
    169         String nullNS = null;
    170 
    171         doc = (Document) load("staffNS", builder);
    172         elementList = doc.getElementsByTagNameNS("*", "address");
    173         element = (Element) elementList.item(1);
    174         attributes = element.getAttributes();
    175         domImpl = doc.getImplementation();
    176         docAlt = domImpl.createDocument(nullNS, "newDoc", docType);
    177         attrAlt = docAlt.createAttributeNS(nullNS, "street");
    178 
    179         {
    180             boolean success = false;
    181             try {
    182                 attributes.setNamedItemNS(attrAlt);
    183             } catch (DOMException ex) {
    184                 success = (ex.code == DOMException.WRONG_DOCUMENT_ERR);
    185             }
    186             assertTrue("throw_WRONG_DOCUMENT_ERR", success);
    187         }
    188     }
    189 
    190 // Assumes validation.
    191 //    public void testSetNamedItemNS5() throws Throwable {
    192 //        Document doc;
    193 //        DocumentType docType;
    194 //        NamedNodeMap entities;
    195 //        NamedNodeMap notations;
    196 //        Entity entity;
    197 //        Notation notation;
    198 //
    199 //        doc = (Document) load("staffNS", builder);
    200 //        docType = doc.getDoctype();
    201 //        entities = docType.getEntities();
    202 //        assertNotNull("entitiesNotNull", entities);
    203 //        notations = docType.getNotations();
    204 //        assertNotNull("notationsNotNull", notations);
    205 //        entity = (Entity) entities.getNamedItem("ent1");
    206 //        notation = (Notation) notations.getNamedItem("notation1");
    207 //
    208 //        {
    209 //            boolean success = false;
    210 //            try {
    211 //                entities.setNamedItemNS(entity);
    212 //            } catch (DOMException ex) {
    213 //                success = (ex.code == DOMException.NO_MODIFICATION_ALLOWED_ERR);
    214 //            }
    215 //            assertTrue("throw_NO_MODIFICATION_ALLOWED_ERR_entities", success);
    216 //        }
    217 //
    218 //        {
    219 //            boolean success = false;
    220 //            try {
    221 //                notations.setNamedItemNS(notation);
    222 //            } catch (DOMException ex) {
    223 //                success = (ex.code == DOMException.NO_MODIFICATION_ALLOWED_ERR);
    224 //            }
    225 //            assertTrue("throw_NO_MODIFICATION_ALLOWED_ERR_notations", success);
    226 //        }
    227 //    }
    228     public void testSetNamedItemNS6() throws Throwable {
    229         Document doc;
    230         NamedNodeMap attributes;
    231         NodeList elementList;
    232         Element element;
    233         Attr attr;
    234 
    235         doc = (Document) load("staffNS", builder);
    236         elementList = doc.getElementsByTagNameNS("*", "address");
    237         element = (Element) elementList.item(0);
    238         attributes = element.getAttributes();
    239         attr = (Attr) attributes.getNamedItemNS("http://www.usa.com",
    240                 "domestic");
    241         element = (Element) elementList.item(1);
    242         attributes = element.getAttributes();
    243 
    244         {
    245             boolean success = false;
    246             try {
    247                 attributes.setNamedItemNS(attr);
    248             } catch (DOMException ex) {
    249                 success = (ex.code == DOMException.INUSE_ATTRIBUTE_ERR);
    250             }
    251             assertTrue("namednodemapsetnameditemns06", success);
    252         }
    253     }
    254     public void testSetNamedItemNS7() throws Throwable {
    255         Document doc;
    256         NamedNodeMap attributes;
    257         NodeList elementList;
    258         Element element;
    259         Attr attr;
    260 
    261         doc = (Document) load("staffNS", builder);
    262         elementList = doc.getElementsByTagNameNS("*", "address");
    263         element = (Element) elementList.item(0);
    264         attributes = element.getAttributes();
    265         attr = (Attr) attributes.getNamedItemNS("http://www.usa.com",
    266                 "domestic");
    267         element = (Element) elementList.item(1);
    268         attributes = element.getAttributes();
    269 
    270         {
    271             boolean success = false;
    272             try {
    273                 attributes.setNamedItemNS(attr);
    274             } catch (DOMException ex) {
    275                 success = (ex.code == DOMException.INUSE_ATTRIBUTE_ERR);
    276             }
    277             assertTrue("namednodemapsetnameditemns07", success);
    278         }
    279     }
    280     public void testSetNamedItemNS8() throws Throwable {
    281         Document doc;
    282         NamedNodeMap attributes;
    283         NodeList elementList;
    284         Element element;
    285         Attr attr;
    286 
    287         doc = (Document) load("staffNS", builder);
    288         elementList = doc.getElementsByTagNameNS("*", "address");
    289         element = (Element) elementList.item(0);
    290         attributes = element.getAttributes();
    291         attr = (Attr) attributes.getNamedItemNS("http://www.usa.com",
    292                 "domestic");
    293         element = (Element) elementList.item(1);
    294         attributes = element.getAttributes();
    295 
    296         {
    297             boolean success = false;
    298             try {
    299                 attributes.setNamedItemNS(attr);
    300             } catch (DOMException ex) {
    301                 success = (ex.code == DOMException.INUSE_ATTRIBUTE_ERR);
    302             }
    303             assertTrue("namednodemapsetnameditemns08", success);
    304         }
    305     }
    306 
    307 // Assumes validation.
    308 //    public void testSetNamedItemNS9() throws Throwable {
    309 //        Document doc;
    310 //        DocumentType docType;
    311 //        NamedNodeMap entities;
    312 //        NamedNodeMap notations;
    313 //        Attr attr;
    314 //        doc = (Document) load("staffNS", builder);
    315 //        docType = doc.getDoctype();
    316 //        entities = docType.getEntities();
    317 //        notations = docType.getNotations();
    318 //        attr = doc.createAttributeNS("http://www.w3.org/DOM/Test", "test");
    319 //
    320 //        {
    321 //            boolean success = false;
    322 //            try {
    323 //                entities.setNamedItemNS(attr);
    324 //            } catch (DOMException ex) {
    325 //                success = (ex.code == DOMException.NO_MODIFICATION_ALLOWED_ERR);
    326 //            }
    327 //            assertTrue("throw_NO_MODIFICATION_ALLOWED_ERR_entities", success);
    328 //        }
    329 //
    330 //        {
    331 //            boolean success = false;
    332 //            try {
    333 //                notations.setNamedItemNS(attr);
    334 //            } catch (DOMException ex) {
    335 //                success = (ex.code == DOMException.NO_MODIFICATION_ALLOWED_ERR);
    336 //            }
    337 //            assertTrue("throw_NO_MODIFICATION_ALLOWED_ERR_notations", success);
    338 //        }
    339 //    }
    340 
    341 // Assumes validation.
    342 //    public void testSetNamedItemNS10() throws Throwable {
    343 //        Document doc;
    344 //        DocumentType docType;
    345 //        NamedNodeMap entities;
    346 //        NamedNodeMap attributes;
    347 //        Entity entity;
    348 //
    349 //        Element element;
    350 //        NodeList elementList;
    351 //
    352 //        doc = (Document) load("staffNS", builder);
    353 //        docType = doc.getDoctype();
    354 //        entities = docType.getEntities();
    355 //        assertNotNull("entitiesNotNull", entities);
    356 //        entity = (Entity) entities.getNamedItem("ent1");
    357 //        elementList = doc.getElementsByTagNameNS("http://www.nist.gov",
    358 //                "address");
    359 //        element = (Element) elementList.item(0);
    360 //        attributes = element.getAttributes();
    361 //
    362 //        {
    363 //            boolean success = false;
    364 //            try {
    365 //                attributes.setNamedItemNS(entity);
    366 //            } catch (DOMException ex) {
    367 //                success = (ex.code == DOMException.HIERARCHY_REQUEST_ERR);
    368 //            }
    369 //            assertTrue("throw_HIERARCHY_REQUEST_ERR", success);
    370 //        }
    371 //    }
    372 
    373 // Assumes validation.
    374 //    public void testSetNamedItemNS11() throws Throwable {
    375 //        Document doc;
    376 //        DocumentType docType;
    377 //        NamedNodeMap notations;
    378 //        NamedNodeMap attributes;
    379 //        Notation notation;
    380 //        Element element;
    381 //        NodeList elementList;
    382 //
    383 //        doc = (Document) load("staffNS", builder);
    384 //        docType = doc.getDoctype();
    385 //        notations = docType.getNotations();
    386 //        assertNotNull("notationsNotNull", notations);
    387 //        notation = (Notation) notations.getNamedItem("notation1");
    388 //        elementList = doc.getElementsByTagNameNS("http://www.nist.gov",
    389 //                "address");
    390 //        element = (Element) elementList.item(0);
    391 //        attributes = element.getAttributes();
    392 //
    393 //        {
    394 //            boolean success = false;
    395 //            try {
    396 //                attributes.setNamedItemNS(notation);
    397 //            } catch (DOMException ex) {
    398 //                success = (ex.code == DOMException.HIERARCHY_REQUEST_ERR);
    399 //            }
    400 //            assertTrue("throw_HIERARCHY_REQUEST_ERR", success);
    401 //        }
    402 //    }
    403 }
    404