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

DocumentImportNode

public final class DocumentImportNode extends DOMTestCase
The importNode method imports a node from another document to this document. The returned node has no parent; (parentNode is null). The source node is not altered or removed from the original document but a new copy of the source node is created. Using the method importNode with deep=true, import the attribute, "street" of the second element node, from a list of nodes whose local names are "address" and namespaceURI "http://www.nist.gov" into the same document. Check the parentNode, nodeName, nodeType and nodeValue of the imported node to verify if it has been imported correctly.
author
IBM
author
Neil Delima
see
http://www.w3.org/TR/DOM-Level-2-Core/core
see
http://www.w3.org/TR/DOM-Level-2-Core/core#Core-Document-importNode

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 voidtestImportNode10()

        Document doc;
        DocumentFragment docFragment;
        NodeList childList;
        boolean success;
        Node addressNode;
        
        Node importedDocFrag;
        doc = (Document) load("staffNS", builder);
        docFragment = doc.createDocumentFragment();
        childList = doc.getElementsByTagNameNS("*", "address");
        addressNode = childList.item(0);
        docFragment.appendChild(addressNode);
        importedDocFrag = doc.importNode(docFragment, true);
        success = importedDocFrag.hasChildNodes();
        assertTrue("documentimportnode10", success);
    
public voidtestImportNode11()

        Document doc;
        Element docElement;
        Node imported;
        boolean success;
        String nodeNameOrig;
        String nodeNameImported;
        doc = (Document) load("staffNS", builder);
        docElement = doc.getDocumentElement();
        imported = doc.importNode(docElement, false);
        success = imported.hasChildNodes();
        assertFalse("documentimportnode11", success);
        nodeNameImported = imported.getNodeName();
        nodeNameOrig = docElement.getNodeName();
        assertEquals("documentimportnode11_NodeName", nodeNameImported,
                nodeNameOrig);
    
public voidtestImportNode12()

        Document doc;
        NodeList childList;
        Node imported;
        Node addressElem;
        NodeList addressElemChildren;
        NodeList importedChildren;
        int addressElemLen;
        int importedLen;
        doc = (Document) load("staffNS", builder);
        childList = doc.getElementsByTagNameNS("*", "address");
        addressElem = childList.item(0);
        imported = doc.importNode(addressElem, true);
        addressElemChildren = addressElem.getChildNodes();
        importedChildren = imported.getChildNodes();
        addressElemLen = (int) addressElemChildren.getLength();
        importedLen = (int) importedChildren.getLength();
        assertEquals("documentimportnode12", importedLen, addressElemLen);
    
public voidtestImportNode13()

        Document doc;
        NodeList childList;
        Node imported;
        NodeList importedList;
        Node employeeElem;
        int importedLen;
        doc = (Document) load("staffNS", builder);
        childList = doc.getElementsByTagNameNS("*", "employee");
        employeeElem = childList.item(0);
        imported = doc.importNode(employeeElem, false);
        importedList = imported.getChildNodes();
        importedLen = (int) importedList.getLength();
        assertEquals("documentimportnode13", 0, importedLen);
    
public voidtestImportNode15()

        Document doc;
        
        Node textImport;
        Node textToImport;
        String nodeValue;
        doc = (Document) load("staffNS", builder);
        
        textToImport = doc
                .createTextNode("Document.importNode test for a TEXT_NODE");
        textImport = doc.importNode(textToImport, true);
        nodeValue = textImport.getNodeValue();
        assertEquals("documentimportnode15",
                "Document.importNode test for a TEXT_NODE", nodeValue);
    
public voidtestImportNode17()

        Document doc;
        
        Node commentImport;
        Node commentToImport;
        String nodeValue;
        doc = (Document) load("staffNS", builder);
        
        commentToImport = doc
                .createComment("Document.importNode test for a COMMENT_NODE");
        commentImport = doc.importNode(commentToImport, true);
        nodeValue = commentImport.getNodeValue();
        assertEquals("documentimportnode17",
                "Document.importNode test for a COMMENT_NODE", nodeValue);
    
public voidtestImportNode18()

        Document doc;
        
        ProcessingInstruction piImport;
        ProcessingInstruction piToImport;
        String piData;
        String piTarget;
        doc = (Document) load("staffNS", builder);
        
        piToImport = doc.createProcessingInstruction("Target", "Data");
        piImport = (ProcessingInstruction) doc.importNode(piToImport, false);
        piTarget = piImport.getTarget();
        piData = piImport.getData();
        assertEquals("documentimportnode18_Target", "Target", piTarget);
        assertEquals("documentimportnode18_Data", "Data", piData);
    
public voidtestImportNode2()
Runs the test case.

throws
Throwable Any uncaught exception causes test to fail

        Document doc;
        Document docImported;
        Element element;
        Attr attr;
        Node importedAttr;
        String nodeName;
        int nodeType;
        String nodeValue;
        NodeList addresses;
        Node attrsParent;
        doc = (Document) load("staffNS", builder);
        docImported = (Document) load("staff", builder);
        addresses = doc
                .getElementsByTagNameNS("http://www.nist.gov", "address");
        element = (Element) addresses.item(1);
        attr = element.getAttributeNodeNS("http://www.nist.gov", "zone");
        importedAttr = docImported.importNode(attr, false);
        nodeName = importedAttr.getNodeName();
        nodeType = (int) importedAttr.getNodeType();
        nodeValue = importedAttr.getNodeValue();
        attrsParent = importedAttr.getParentNode();
        assertNull("documentimportnode02_parentNull", attrsParent);
        assertEquals("documentimportnode02_nodeName", "emp:zone", nodeName);
        assertEquals("documentimportnode02_nodeType", 2, nodeType);
        assertEquals("documentimportnode02_nodeValue", "CANADA", nodeValue);
    
public voidtestImportNode5()

        Document doc;
        Document docImported;
        Attr attr;
        Node importedAttr;
        String nodeName;
        int nodeType;
        String nodeValue;
        String namespaceURI;
        doc = (Document) load("staffNS", builder);
        docImported = (Document) load("staff", builder);
        attr = doc.createAttributeNS("http://www.w3.org/DOM/Test", "a_:b0");
        importedAttr = docImported.importNode(attr, false);
        nodeName = importedAttr.getNodeName();
        nodeValue = importedAttr.getNodeValue();
        nodeType = (int) importedAttr.getNodeType();
        namespaceURI = importedAttr.getNamespaceURI();
        assertEquals("documentimportnode05_nodeName", "a_:b0", nodeName);
        assertEquals("documentimportnode05_nodeType", 2, nodeType);
        assertEquals("documentimportnode05_nodeValue", "", nodeValue);
        assertEquals("documentimportnode05_namespaceURI",
                "http://www.w3.org/DOM/Test", namespaceURI);
    
public voidtestImportNode6()

        Document doc;
        
        doc = (Document) load("staffNS", builder);

        {
            boolean success = false;
            try {
                doc.importNode(doc, false);
            } catch (DOMException ex) {
                success = (ex.code == DOMException.NOT_SUPPORTED_ERR);
            }
            assertTrue("throw_NOT_SUPPORTED_ERR", success);
        }
    
public voidtestImportNode7()

        Document doc;
        
        DocumentType docType;
        doc = (Document) load("staffNS", builder);
        docType = doc.getDoctype();

        {
            boolean success = false;
            try {
                doc.importNode(docType, true);
            } catch (DOMException ex) {
                success = (ex.code == DOMException.NOT_SUPPORTED_ERR);
            }
            assertTrue("throw_NOT_SUPPORTED_ERR", success);
        }
    
public voidtestImportNode8()

        Document doc;
        
        DocumentType docType;
        DOMImplementation domImpl;
        String nullNS = null;

        doc = (Document) load("staffNS", builder);
        domImpl = doc.getImplementation();
        docType = domImpl.createDocumentType("test:root", nullNS, nullNS);

        {
            boolean success = false;
            try {
                doc.importNode(docType, true);
            } catch (DOMException ex) {
                success = (ex.code == DOMException.NOT_SUPPORTED_ERR);
            }
            assertTrue("throw_NOT_SUPPORTED_ERR", success);
        }
    
public voidtestImportNode9()

        Document doc;
        DocumentFragment docFragment;
        NodeList childList;
        boolean success;
        Node addressNode;
        
        Node importedDocFrag;
        doc = (Document) load("staffNS", builder);
        docFragment = doc.createDocumentFragment();
        childList = doc.getElementsByTagNameNS("*", "address");
        addressNode = childList.item(0);
        docFragment.appendChild(addressNode);
        importedDocFrag = doc.importNode(docFragment, false);
        success = importedDocFrag.hasChildNodes();
        assertFalse("documentimportnode09", success);