FileDocCategorySizeDatePackage
ElementSetAttributeNodeNS.javaAPI DocAndroid 1.5 API9794Wed May 06 22:41:06 BST 2009tests.org.w3c.dom

ElementSetAttributeNodeNS

public final class ElementSetAttributeNodeNS extends DOMTestCase
Testing Element.setAttributeNodeNS: If an attribute with that local name and that namespace URI is already present in the element, it is replaced by the new one. Create a new element and two new attribute nodes (in the same namespace and same localNames). Add the two new attribute nodes to the element node using the setAttributeNodeNS method. Check that only one attribute is added, check the value of this attribute.
author
IBM
author
Neil Delima
see
http://www.w3.org/TR/DOM-Level-2-Core/core#ID-ElSetAtNodeNS

Fields Summary
DOMDocumentBuilderFactory
factory
DocumentBuilder
builder
Constructors Summary
Methods Summary
public void_testSetAttributeNodeNS6()

        Document doc;
        Element element;
        Attr attribute;
        Attr attribute2;
        EntityReference entRef;
        NodeList elementList;

        doc = (Document) load("staffNS", builder);
        element = doc.createElementNS("http://www.w3.org/DOM/Test", "elem1");
        attribute = doc.createAttributeNS("http://www.w3.org/DOM/Test", "attr");
        entRef = doc.createEntityReference("ent4");
        attribute.appendChild(entRef);
        element.setAttributeNodeNS(attribute);
        elementList = entRef.getChildNodes();
        element = (Element) elementList.item(0);
        attribute2 = doc.createAttributeNS("http://www.w3.org/DOM/Test",
                "attr2");

        {
            boolean success = false;
            try {
                element.setAttributeNodeNS(attribute2);
            } catch (DOMException ex) {
                success = (ex.code == DOMException.NO_MODIFICATION_ALLOWED_ERR);
            }
            assertTrue("elementsetattributenodens06", success);
        }
    
protected voidsetUp()

        super.setUp();
        try {
            factory = new DOMDocumentBuilderFactory(DOMDocumentBuilderFactory
                    .getConfiguration2());
            builder = factory.getBuilder();
        } catch (Exception e) {
            fail("Unexpected exception" + e.getMessage());
        }
    
protected voidtearDown()

        factory = null;
        builder = null;
        super.tearDown();
    
public voidtestSetAttributeNodeNS1()
Runs the test case.

throws
Throwable Any uncaught exception causes test to fail

        Document doc;
        Element element;
        Attr attribute1;
        Attr attribute2;
        Attr attrNode;
        String attrName;
        String attrNS;
        
        NamedNodeMap attributes;
        
        int length;
        doc = (Document) load("staff", builder);
        element = doc.createElementNS("http://www.w3.org/DOM/Test/Level2",
                "new:element");
        attribute1 = doc.createAttributeNS("http://www.w3.org/DOM/Test/att1",
                "p1:att");
        attribute2 = doc.createAttributeNS("http://www.w3.org/DOM/Test/att1",
                "p2:att");
        attribute2.setValue("value2");
        element.setAttributeNodeNS(attribute1);
        element.setAttributeNodeNS(attribute2);
        attrNode = element.getAttributeNodeNS(
                "http://www.w3.org/DOM/Test/att1", "att");
        attrName = attrNode.getNodeName();
        attrNS = attrNode.getNamespaceURI();
        assertEquals("elementsetattributenodens01_attrName", "p2:att", attrName);
        assertEquals("elementsetattributenodens01_attrNS",
                "http://www.w3.org/DOM/Test/att1", attrNS);
        attributes = element.getAttributes();
        length = (int) attributes.getLength();
        assertEquals("length", 1, length);
    
public voidtestSetAttributeNodeNS2()

        Document doc;
        Element element;
        Element element2;
        Attr attribute;
        Attr attributeCloned;
        Attr newAttr;
        NodeList elementList;
        String attrName;
        String attrValue;
        String nullNS = null;

        doc = (Document) load("staffNS", builder);
        elementList = doc.getElementsByTagNameNS("http://www.nist.gov",
                "address");
        element = (Element) elementList.item(1);
        attribute = element.getAttributeNodeNS(nullNS, "street");
        attributeCloned = (Attr) attribute.cloneNode(true);
        element2 = (Element) elementList.item(2);
        newAttr = element2.setAttributeNodeNS(attributeCloned);
        attrName = newAttr.getNodeName();
        attrValue = newAttr.getNodeValue();
        assertEquals("elementsetattributenodens02_attrName", "street", attrName);
        assertEquals("elementsetattributenodens02_attrValue", "Yes", attrValue);
    
public voidtestSetAttributeNodeNS3()

        Document doc;
        Element element1;
        Element element2;
        Attr attribute;

        NodeList elementList;
        String nullNS = null;

        doc = (Document) load("staffNS", builder);
        elementList = doc.getElementsByTagNameNS("http://www.nist.gov",
                "address");
        element1 = (Element) elementList.item(1);
        attribute = element1.getAttributeNodeNS(nullNS, "street");
        element2 = (Element) elementList.item(2);

        {
            boolean success = false;
            try {
                element2.setAttributeNodeNS(attribute);
            } catch (DOMException ex) {
                success = (ex.code == DOMException.INUSE_ATTRIBUTE_ERR);
            }
            assertTrue("elementsetattributenodens03", success);
        }
    
public voidtestSetAttributeNodeNS4()

        Document doc;
        Element element1;
        Element element2;
        Attr attribute;

        doc = (Document) load("staffNS", builder);
        element1 = doc.createElementNS("http://www.w3.org/DOM/Test", "elem1");
        element2 = doc.createElementNS("http://www.w3.org/DOM/Test", "elem2");
        attribute = doc.createAttributeNS("http://www.w3.org/DOM/Test", "attr");
        element1.setAttributeNodeNS(attribute);

        {
            boolean success = false;
            try {
                element2.setAttributeNodeNS(attribute);
            } catch (DOMException ex) {
                success = (ex.code == DOMException.INUSE_ATTRIBUTE_ERR);
            }
            assertTrue("elementsetattributenodens04", success);
        }
    
public voidtestSetAttributeNodeNS5()

        Document doc;
        Document docAlt;
        Element element;
        Attr attribute;

        doc = (Document) load("staffNS", builder);
        docAlt = (Document) load("staffNS", builder);
        element = doc.createElementNS("http://www.w3.org/DOM/Test", "elem1");
        attribute = docAlt.createAttributeNS("http://www.w3.org/DOM/Test",
                "attr");

        {
            boolean success = false;
            try {
                element.setAttributeNodeNS(attribute);
            } catch (DOMException ex) {
                success = (ex.code == DOMException.WRONG_DOCUMENT_ERR);
            }
            assertTrue("throw_WRONG_DOCUMENT_ERR", success);
        }