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

NamedNodeMapSetNamedItemNS

public final class NamedNodeMapSetNamedItemNS extends DOMTestCase
The method setNamedItemNS adds a node using its namespaceURI and localName. If a node with that namespace URI and that local name is already present in this map, it is replaced by the new one. Retreive the first element whose localName is address and namespaceURI http://www.nist.gov", and put its attributes into a named node map. Create a new attribute node and add it to this map. Verify if the attr node was successfully added by checking the nodeName of the retreived atttribute.
author
IBM
author
Neil Delima
see
http://www.w3.org/TR/DOM-Level-2-Core/core#ID-getNamedItemNS

Fields Summary
DOMDocumentBuilderFactory
factory
DocumentBuilder
builder
Constructors Summary
Methods Summary
protected voidsetUp()

        super.setUp();
        try {
            // Changed to configuration #2. This test case just doesn't make
            // sense without a namespace-aware parser. It actually fails even
            // on the JDK in that case.
            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 voidtestSetNamedItemNS1()
Runs the test case.

throws
Throwable Any uncaught exception causes test to fail

        Document doc;
        NamedNodeMap attributes;
        Node element;
        Attr attribute;

        Attr newAttr1;
        NodeList elementList;
        String attrName;
        doc = (Document) load("staffNS", builder);
        elementList = doc.getElementsByTagNameNS("http://www.nist.gov",
                "address");
        element = elementList.item(0);
        attributes = element.getAttributes();
        newAttr1 = doc.createAttributeNS("http://www.w3.org/DOM/L1", "streets");
        ((Element) /* Node */element).setAttributeNodeNS(newAttr1);
        attribute = (Attr) attributes.getNamedItemNS(
                "http://www.w3.org/DOM/L1", "streets");
        attrName = attribute.getNodeName();
        assertEquals("namednodemapsetnameditemns01", "streets", attrName);
    
public voidtestSetNamedItemNS2()

        Document doc;
        NamedNodeMap attributes;
        Element element;
        Attr attribute;
        Attr attribute1;

        String attrName;
        doc = (Document) load("staffNS", builder);
        element = doc.createElementNS("http://www.w3.org/DOM/Test", "root");
        attribute1 = doc
                .createAttributeNS("http://www.w3.org/DOM/L1", "L1:att");
        attributes = element.getAttributes();
        attributes.setNamedItemNS(attribute1);
        attribute = (Attr) attributes.getNamedItemNS(
                "http://www.w3.org/DOM/L1", "att");
        attrName = attribute.getNodeName();
        assertEquals("namednodemapsetnameditemns02", "L1:att", attrName);
    
public voidtestSetNamedItemNS3()


        Document doc;
        Document docAlt;
        NamedNodeMap attributes;
        NamedNodeMap attributesAlt;
        NodeList elementList;
        NodeList elementListAlt;
        Element element;
        Element elementAlt;
        Attr attr;

        String nullNS = null;

        doc = (Document) load("staffNS", builder);
        elementList = doc.getElementsByTagNameNS("*", "address");
        element = (Element) elementList.item(1);
        attributes = element.getAttributes();
        docAlt = (Document) load("staffNS", builder);
        elementListAlt = docAlt.getElementsByTagNameNS("*", "address");
        elementAlt = (Element) elementListAlt.item(1);
        attributesAlt = elementAlt.getAttributes();
        attr = (Attr) attributesAlt.getNamedItemNS(nullNS, "street");
        attributesAlt.removeNamedItemNS(nullNS, "street");

        {
            boolean success = false;
            try {
                attributes.setNamedItemNS(attr);
            } catch (DOMException ex) {
                success = (ex.code == DOMException.WRONG_DOCUMENT_ERR);
            }
            assertTrue("throw_WRONG_DOCUMENT_ERR", success);
        }
    
public voidtestSetNamedItemNS4()

        Document doc;
        DOMImplementation domImpl;
        Document docAlt; 
        DocumentType docType = null;

        NamedNodeMap attributes;
        NodeList elementList;
        Element element;
        Attr attrAlt;

        String nullNS = null;

        doc = (Document) load("staffNS", builder);
        elementList = doc.getElementsByTagNameNS("*", "address");
        element = (Element) elementList.item(1);
        attributes = element.getAttributes();
        domImpl = doc.getImplementation();
        docAlt = domImpl.createDocument(nullNS, "newDoc", docType);
        attrAlt = docAlt.createAttributeNS(nullNS, "street");

        {
            boolean success = false;
            try {
                attributes.setNamedItemNS(attrAlt);
            } catch (DOMException ex) {
                success = (ex.code == DOMException.WRONG_DOCUMENT_ERR);
            }
            assertTrue("throw_WRONG_DOCUMENT_ERR", success);
        }
    
public voidtestSetNamedItemNS6()

        Document doc;
        NamedNodeMap attributes;
        NodeList elementList;
        Element element;
        Attr attr;

        doc = (Document) load("staffNS", builder);
        elementList = doc.getElementsByTagNameNS("*", "address");
        element = (Element) elementList.item(0);
        attributes = element.getAttributes();
        attr = (Attr) attributes.getNamedItemNS("http://www.usa.com",
                "domestic");
        element = (Element) elementList.item(1);
        attributes = element.getAttributes();

        {
            boolean success = false;
            try {
                attributes.setNamedItemNS(attr);
            } catch (DOMException ex) {
                success = (ex.code == DOMException.INUSE_ATTRIBUTE_ERR);
            }
            assertTrue("namednodemapsetnameditemns06", success);
        }
    
public voidtestSetNamedItemNS7()

        Document doc;
        NamedNodeMap attributes;
        NodeList elementList;
        Element element;
        Attr attr;

        doc = (Document) load("staffNS", builder);
        elementList = doc.getElementsByTagNameNS("*", "address");
        element = (Element) elementList.item(0);
        attributes = element.getAttributes();
        attr = (Attr) attributes.getNamedItemNS("http://www.usa.com",
                "domestic");
        element = (Element) elementList.item(1);
        attributes = element.getAttributes();

        {
            boolean success = false;
            try {
                attributes.setNamedItemNS(attr);
            } catch (DOMException ex) {
                success = (ex.code == DOMException.INUSE_ATTRIBUTE_ERR);
            }
            assertTrue("namednodemapsetnameditemns07", success);
        }
    
public voidtestSetNamedItemNS8()

        Document doc;
        NamedNodeMap attributes;
        NodeList elementList;
        Element element;
        Attr attr;

        doc = (Document) load("staffNS", builder);
        elementList = doc.getElementsByTagNameNS("*", "address");
        element = (Element) elementList.item(0);
        attributes = element.getAttributes();
        attr = (Attr) attributes.getNamedItemNS("http://www.usa.com",
                "domestic");
        element = (Element) elementList.item(1);
        attributes = element.getAttributes();

        {
            boolean success = false;
            try {
                attributes.setNamedItemNS(attr);
            } catch (DOMException ex) {
                success = (ex.code == DOMException.INUSE_ATTRIBUTE_ERR);
            }
            assertTrue("namednodemapsetnameditemns08", success);
        }