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

SetAttributeNodeNS

public final class SetAttributeNodeNS extends DOMTestCase
The "setAttributeNode(newAttr)" method raises an "INUSE_ATTRIBUTE_ERR DOMException if the "newAttr" is already an attribute of another element. Retrieve the first emp:address and append a newly created element. The "createAttributeNS(namespaceURI,qualifiedName)" and "setAttributeNodeNS(newAttr)" methods are invoked to create and add a new attribute to the newly created Element. The "setAttributeNodeNS(newAttr)" method is once again called to add the new attribute causing an exception to be raised since the attribute is already an attribute of another element.
author
NIST
author
Mary Brady
see
http://www.w3.org/TR/DOM-Level-2-Core/core#xpointer(id('ID-258A00AF')/constant[@name='INUSE_ATTRIBUTE_ERR'])
see
http://www.w3.org/TR/DOM-Level-2-Core/core#ID-ElSetAtNodeNS
see
http://www.w3.org/TR/DOM-Level-2-Core/core#xpointer(id('ID-ElSetAtNodeNS')/raises/exception[@name='DOMException']/descr/p[substring-before(.,':')='INUSE_ATTRIBUTE_ERR'])

Fields Summary
DOMDocumentBuilderFactory
factory
DocumentBuilder
builder
Constructors Summary
Methods Summary
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 voidtestSetAttributeNode1()
Runs the test case.

throws
Throwable Any uncaught exception causes test to fail

        String namespaceURI = "http://www.newattr.com";
        String qualifiedName = "emp:newAttr";
        Document doc;
        Element newElement;
        Attr newAttr;
        NodeList elementList;
        Node testAddr;

        doc = (Document) load("staffNS", builder);
        elementList = doc.getElementsByTagName("emp:address");
        testAddr = elementList.item(0);
        assertNotNull("empAddrNotNull", testAddr);
        newElement = doc.createElement("newElement");
        testAddr.appendChild(newElement);
        newAttr = doc.createAttributeNS(namespaceURI, qualifiedName);
        newElement.setAttributeNodeNS(newAttr);

        {
            boolean success = false;
            try {
                ((Element) /* Node */testAddr).setAttributeNodeNS(newAttr);
            } catch (DOMException ex) {
                success = (ex.code == DOMException.INUSE_ATTRIBUTE_ERR);
            }
            assertTrue("throw_INUSE_ATTRIBUTE_ERR", success);
        }
    
public voidtestSetAttributeNode3()

        String namespaceURI = "http://www.newattr.com";
        String qualifiedName = "emp:newAttr";
        Document doc;
        NodeList elementList;
        Node testAddr;
        Attr newAttr;
        Attr newAddrAttr;
        doc = (Document) load("staffNS", builder);
        elementList = doc.getElementsByTagName("emp:address");
        testAddr = elementList.item(0);
        assertNotNull("empAddrNotNull", testAddr);
        newAttr = doc.createAttributeNS(namespaceURI, qualifiedName);
        newAddrAttr = ((Element) /* Node */testAddr)
                .setAttributeNodeNS(newAttr);
        assertNull("throw_Null", newAddrAttr);
    
public voidtestSetAttributeNode4()

        Document doc;
        NodeList elementList;
        Node testAddr;
        Attr newAttr;
        Attr newAddrAttr;
        String newName;
        doc = (Document) load("staffNS", builder);
        elementList = doc.getElementsByTagName("emp:address");
        testAddr = elementList.item(0);
        assertNotNull("empAddrNotNull", testAddr);
        newAttr = doc.createAttributeNS("http://www.nist.gov", "xxx:domestic");
        newAddrAttr = ((Element) /* Node */testAddr)
                .setAttributeNodeNS(newAttr);
        newName = newAddrAttr.getNodeName();
        assertEquals("nodeName", "emp:domestic", newName);
    
public voidtestSetAttributeNode5()

        String namespaceURI = "http://www.newattr.com";
        String qualifiedName = "emp:newAttr";
        Document doc1;
        Document doc2;
        Attr newAttr;
        NodeList elementList;
        Node testAddr;

        doc1 = (Document) load("staffNS", builder);
        doc2 = (Document) load("staffNS", builder);
        newAttr = doc2.createAttributeNS(namespaceURI, qualifiedName);
        elementList = doc1.getElementsByTagName("emp:address");
        testAddr = elementList.item(0);

        {
            boolean success = false;
            try {
                ((Element) /* Node */testAddr).setAttributeNodeNS(newAttr);
            } catch (DOMException ex) {
                success = (ex.code == DOMException.WRONG_DOCUMENT_ERR);
            }
            assertTrue("throw_WRONG_DOCUMENT_ERR", success);
        }