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.Element;
     25 import org.w3c.dom.Document;
     26 import org.w3c.dom.Attr;
     27 import org.w3c.dom.NamedNodeMap;
     28 import org.w3c.dom.NodeList;
     29 import org.w3c.dom.DOMException;
     30 import org.w3c.dom.EntityReference;
     31 
     32 import javax.xml.parsers.DocumentBuilder;
     33 
     34 /**
     35  * Testing Element.setAttributeNodeNS: If an attribute with that local name and
     36  * that namespace URI is already present in the element, it is replaced by the
     37  * new one. Create a new element and two new attribute nodes (in the same
     38  * namespace and same localNames). Add the two new attribute nodes to the
     39  * element node using the setAttributeNodeNS method. Check that only one
     40  * attribute is added, check the value of this attribute.
     41  *
     42  * @author IBM
     43  * @author Neil Delima
     44  * @see <a
     45  *      href="http://www.w3.org/TR/DOM-Level-2-Core/core#ID-ElSetAtNodeNS">http://www.w3.org/TR/DOM-Level-2-Core/core#ID-ElSetAtNodeNS</a>
     46  */
     47 public final class ElementSetAttributeNodeNS extends DOMTestCase {
     48 
     49     DOMDocumentBuilderFactory factory;
     50 
     51     DocumentBuilder builder;
     52 
     53     protected void setUp() throws Exception {
     54         super.setUp();
     55         try {
     56             factory = new DOMDocumentBuilderFactory(DOMDocumentBuilderFactory
     57                     .getConfiguration2());
     58             builder = factory.getBuilder();
     59         } catch (Exception e) {
     60             fail("Unexpected exception" + e.getMessage());
     61         }
     62     }
     63 
     64     protected void tearDown() throws Exception {
     65         factory = null;
     66         builder = null;
     67         super.tearDown();
     68     }
     69 
     70     /**
     71      * Runs the test case.
     72      *
     73      * @throws Throwable
     74      *             Any uncaught exception causes test to fail
     75      */
     76     public void testSetAttributeNodeNS1() throws Throwable {
     77         Document doc;
     78         Element element;
     79         Attr attribute1;
     80         Attr attribute2;
     81         Attr attrNode;
     82         String attrName;
     83         String attrNS;
     84 
     85         NamedNodeMap attributes;
     86 
     87         int length;
     88         doc = (Document) load("staff", builder);
     89         element = doc.createElementNS("http://www.w3.org/DOM/Test/Level2",
     90                 "new:element");
     91         attribute1 = doc.createAttributeNS("http://www.w3.org/DOM/Test/att1",
     92                 "p1:att");
     93         attribute2 = doc.createAttributeNS("http://www.w3.org/DOM/Test/att1",
     94                 "p2:att");
     95         attribute2.setValue("value2");
     96         element.setAttributeNodeNS(attribute1);
     97         element.setAttributeNodeNS(attribute2);
     98         attrNode = element.getAttributeNodeNS(
     99                 "http://www.w3.org/DOM/Test/att1", "att");
    100         attrName = attrNode.getNodeName();
    101         attrNS = attrNode.getNamespaceURI();
    102         assertEquals("elementsetattributenodens01_attrName", "p2:att", attrName);
    103         assertEquals("elementsetattributenodens01_attrNS",
    104                 "http://www.w3.org/DOM/Test/att1", attrNS);
    105         attributes = element.getAttributes();
    106         length = (int) attributes.getLength();
    107         assertEquals("length", 1, length);
    108     }
    109     public void testSetAttributeNodeNS2() throws Throwable {
    110         Document doc;
    111         Element element;
    112         Element element2;
    113         Attr attribute;
    114         Attr attributeCloned;
    115         Attr newAttr;
    116         NodeList elementList;
    117         String attrName;
    118         String attrValue;
    119         String nullNS = null;
    120 
    121         doc = (Document) load("staffNS", builder);
    122         elementList = doc.getElementsByTagNameNS("http://www.nist.gov",
    123                 "address");
    124         element = (Element) elementList.item(1);
    125         attribute = element.getAttributeNodeNS(nullNS, "street");
    126         attributeCloned = (Attr) attribute.cloneNode(true);
    127         element2 = (Element) elementList.item(2);
    128         newAttr = element2.setAttributeNodeNS(attributeCloned);
    129         attrName = newAttr.getNodeName();
    130         attrValue = newAttr.getNodeValue();
    131         assertEquals("elementsetattributenodens02_attrName", "street", attrName);
    132         assertEquals("elementsetattributenodens02_attrValue", "Yes", attrValue);
    133     }
    134     public void testSetAttributeNodeNS3() throws Throwable {
    135         Document doc;
    136         Element element1;
    137         Element element2;
    138         Attr attribute;
    139 
    140         NodeList elementList;
    141         String nullNS = null;
    142 
    143         doc = (Document) load("staffNS", builder);
    144         elementList = doc.getElementsByTagNameNS("http://www.nist.gov",
    145                 "address");
    146         element1 = (Element) elementList.item(1);
    147         attribute = element1.getAttributeNodeNS(nullNS, "street");
    148         element2 = (Element) elementList.item(2);
    149 
    150         {
    151             boolean success = false;
    152             try {
    153                 element2.setAttributeNodeNS(attribute);
    154             } catch (DOMException ex) {
    155                 success = (ex.code == DOMException.INUSE_ATTRIBUTE_ERR);
    156             }
    157             assertTrue("elementsetattributenodens03", success);
    158         }
    159     }
    160     public void testSetAttributeNodeNS4() throws Throwable {
    161         Document doc;
    162         Element element1;
    163         Element element2;
    164         Attr attribute;
    165 
    166         doc = (Document) load("staffNS", builder);
    167         element1 = doc.createElementNS("http://www.w3.org/DOM/Test", "elem1");
    168         element2 = doc.createElementNS("http://www.w3.org/DOM/Test", "elem2");
    169         attribute = doc.createAttributeNS("http://www.w3.org/DOM/Test", "attr");
    170         element1.setAttributeNodeNS(attribute);
    171 
    172         {
    173             boolean success = false;
    174             try {
    175                 element2.setAttributeNodeNS(attribute);
    176             } catch (DOMException ex) {
    177                 success = (ex.code == DOMException.INUSE_ATTRIBUTE_ERR);
    178             }
    179             assertTrue("elementsetattributenodens04", success);
    180         }
    181     }
    182     public void testSetAttributeNodeNS5() throws Throwable {
    183         Document doc;
    184         Document docAlt;
    185         Element element;
    186         Attr attribute;
    187 
    188         doc = (Document) load("staffNS", builder);
    189         docAlt = (Document) load("staffNS", builder);
    190         element = doc.createElementNS("http://www.w3.org/DOM/Test", "elem1");
    191         attribute = docAlt.createAttributeNS("http://www.w3.org/DOM/Test",
    192                 "attr");
    193 
    194         {
    195             boolean success = false;
    196             try {
    197                 element.setAttributeNodeNS(attribute);
    198             } catch (DOMException ex) {
    199                 success = (ex.code == DOMException.WRONG_DOCUMENT_ERR);
    200             }
    201             assertTrue("throw_WRONG_DOCUMENT_ERR", success);
    202         }
    203     }
    204     public void _testSetAttributeNodeNS6() throws Throwable {
    205         Document doc;
    206         Element element;
    207         Attr attribute;
    208         Attr attribute2;
    209         EntityReference entRef;
    210         NodeList elementList;
    211 
    212         doc = (Document) load("staffNS", builder);
    213         element = doc.createElementNS("http://www.w3.org/DOM/Test", "elem1");
    214         attribute = doc.createAttributeNS("http://www.w3.org/DOM/Test", "attr");
    215         entRef = doc.createEntityReference("ent4");
    216         attribute.appendChild(entRef);
    217         element.setAttributeNodeNS(attribute);
    218         elementList = entRef.getChildNodes();
    219         element = (Element) elementList.item(0);
    220         attribute2 = doc.createAttributeNS("http://www.w3.org/DOM/Test",
    221                 "attr2");
    222 
    223         {
    224             boolean success = false;
    225             try {
    226                 element.setAttributeNodeNS(attribute2);
    227             } catch (DOMException ex) {
    228                 success = (ex.code == DOMException.NO_MODIFICATION_ALLOWED_ERR);
    229             }
    230             assertTrue("elementsetattributenodens06", success);
    231         }
    232     }
    233 
    234 }
    235