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

ElementSetAttributeNS

public final class ElementSetAttributeNS extends DOMTestCase
The method setAttributeNS adds a new attribute. Create a new element and add a new attribute node to it using the setAttributeNS method. Check if the attribute was correctly set by invoking the getAttributeNodeNS method and checking the nodeName and nodeValue of the returned nodes.
author
IBM
author
Neil Delima
see
http://www.w3.org/TR/DOM-Level-2-Core/core#ID-ElSetAttrNS

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 voidtestSetAttributeNS1()
Runs the test case.

throws
Throwable Any uncaught exception causes test to fail

        Document doc;
        Element element;
        Attr attribute;
        String attrName;
        String attrValue;
        doc = (Document) load("staff", builder);
        element = doc.createElementNS("http://www.w3.org/DOM", "dom:elem");
        element.setAttributeNS("http://www.w3.org/DOM/Test/setAttributeNS",
                "attr", "value");
        attribute = element.getAttributeNodeNS(
                "http://www.w3.org/DOM/Test/setAttributeNS", "attr");
        attrName = attribute.getNodeName();
        attrValue = attribute.getNodeValue();
        assertEquals("elementsetattributens01_attrName", "attr", attrName);
        assertEquals("elementsetattributens01_attrValue", "value", attrValue);
    
public voidtestSetAttributeNS2()

        Document doc;
        Element element;
        Attr attribute;
        NodeList elementList;
        String attrName;
        String attrValue;
        doc = (Document) load("staff", builder);
        elementList = doc.getElementsByTagNameNS("*", "address");
        element = (Element) elementList.item(0);
        element.setAttributeNS("http://www.w3.org/DOM/Test/setAttributeNS",
                "this:street", "Silver Street");
        attribute = element.getAttributeNodeNS(
                "http://www.w3.org/DOM/Test/setAttributeNS", "street");
        attrName = attribute.getNodeName();
        attrValue = attribute.getNodeValue();
        assertEquals("elementsetattributens02_attrName", "this:street",
                attrName);
        assertEquals("elementsetattributens02_attrValue", "Silver Street",
                attrValue);
    
public voidtestSetAttributeNS3()

        Document doc;
        Element element;
        Attr attribute;
        NodeList elementList;
        String attrName;
        String attrValue;
        doc = (Document) load("staffNS", builder);
        elementList = doc.getElementsByTagName("emp:employee");
        element = (Element) elementList.item(0);
        assertNotNull("empEmployeeNotNull", element);
        element.setAttributeNS("http://www.w3.org/DOM/Test/1", "defaultAttr",
                "default1");
        element.setAttributeNS("http://www.w3.org/DOM/Test/2", "defaultAttr",
                "default2");
        attribute = element.getAttributeNodeNS("http://www.w3.org/DOM/Test/1",
                "defaultAttr");
        attrName = attribute.getNodeName();
        attrValue = attribute.getNodeValue();
        assertEquals("elementsetattributens03_attrName", "defaultAttr",
                attrName);
        assertEquals("elementsetattributens03_attrValue", "default1", attrValue);
    
public voidtestSetAttributeNS4()

        Document doc;
        Element element;
        String qualifiedName;
        List<String> qualifiedNames = new ArrayList<String>();
        qualifiedNames.add("/");
        qualifiedNames.add("//");
        qualifiedNames.add("\\");
        qualifiedNames.add(";");
        qualifiedNames.add("&");
        qualifiedNames.add("*");
        qualifiedNames.add("]]");
        qualifiedNames.add(">");
        qualifiedNames.add("<");

        doc = (Document) load("staffNS", builder);
        element = doc.createElementNS("http://www.w3.org/DOM/Test/L2",
                "dom:elem");
        for (int indexN10058 = 0; indexN10058 < qualifiedNames.size(); indexN10058++) {
            qualifiedName = (String) qualifiedNames.get(indexN10058);

            {
                boolean success = false;
                try {
                    element.setAttributeNS("http://www.w3.org/DOM/Test/L2",
                            qualifiedName, "test");
                } catch (DOMException ex) {
                    success = (ex.code == DOMException.INVALID_CHARACTER_ERR);
                }
                assertTrue("elementsetattributens04", success);
            }
        }
    
public voidtestSetAttributeNS5()

        Document doc;
        Element element;
        String nullNS = null;

        doc = (Document) load("staffNS", builder);
        element = doc.createElementNS("http://www.w3.org/DOM/Test/L2",
                "dom:elem");

        {
            boolean success = false;
            try {
                element.setAttributeNS(nullNS, "dom:root", "test");
            } catch (DOMException ex) {
                success = (ex.code == DOMException.NAMESPACE_ERR);
            }
            assertTrue("elementsetattributens05", success);
        }
    
public voidtestSetAttributeNS8()

        Document doc;
        Element element;
        doc = (Document) load("staffNS", builder);
        element = doc.createElementNS("http://www.w3.org/DOMTest/level2",
                "dom:elem");

        {
            boolean success = false;
            try {
                element.setAttributeNS("http://www.w3.org/DOMTest/level2",
                        "xmlns", "test");
            } catch (DOMException ex) {
                success = (ex.code == DOMException.NAMESPACE_ERR);
            }
            assertTrue("elementsetattributens08_Err1", success);
        }

        {
            boolean success = false;
            try {
                element.setAttributeNS("http://www.w3.org/DOMTest/level2",
                        "xmlns:root", "test");
            } catch (DOMException ex) {
                success = (ex.code == DOMException.NAMESPACE_ERR);
            }
            assertTrue("elementsetattributens08_Err2", success);
        }
    
public voidtestSetAttributeNSURINull()

          String namespaceURI = null;

          String qualifiedName = "emp:qualifiedName";
          Document doc;
          NodeList elementList;
          Node testAddr;
          doc = (Document) load("staff", builder);
          elementList = doc.getElementsByTagName("employee");
          testAddr = elementList.item(0);
          
          {
             boolean success = false;
             try {
                ((Element) /*Node */testAddr).setAttributeNS(namespaceURI, qualifiedName, "newValue");
              } catch (DOMException ex) {
                success = (ex.code == DOMException.NAMESPACE_ERR);
             }
             assertTrue("throw_NAMESPACE_ERR", success);
          }