1/*
2 This Java source file was generated by test-to-java.xsl
3 and is a derived work from the source document.
4 The source document contained the following notice:
5
6
7
8 Copyright (c) 2001 World Wide Web Consortium,
9 (Massachusetts Institute of Technology, Institut National de
10 Recherche en Informatique et en Automatique, Keio University).  All
11 Rights Reserved.  This program is distributed under the W3C's Software
12 Intellectual Property License.  This program is distributed in the
13 hope that it will be useful, but WITHOUT ANY WARRANTY; without even
14 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 PURPOSE.
16
17 See W3C License http://www.w3.org/Consortium/Legal/ for more details.
18
19
20 */
21
22package tests.org.w3c.dom;
23
24import java.util.ArrayList;
25import java.util.List;
26
27import org.w3c.dom.Document;
28import org.w3c.dom.Attr;
29import org.w3c.dom.Text;
30import org.w3c.dom.Node;
31import org.w3c.dom.Element;
32import org.w3c.dom.DocumentType;
33import org.w3c.dom.NodeList;
34import org.w3c.dom.CDATASection;
35import org.w3c.dom.Comment;
36import org.w3c.dom.DocumentFragment;
37import org.w3c.dom.EntityReference;
38import org.w3c.dom.ProcessingInstruction;
39import org.w3c.dom.DOMException;
40
41import javax.xml.parsers.DocumentBuilder;
42
43/**
44 * The "importNode(importedNode,deep)" method for a Document should import the
45 * given importedNode into that Document. The importedNode is of type Attr. The
46 * ownerElement is set to null. Specified flag is set to true. Children is
47 * imported.
48 *
49 * Create a new attribute whose name is "elem:attr1" in a different document.
50 * Create a child Text node with value "importedText" for the attribute node
51 * above. Invoke method importNode(importedNode,deep) on this document with
52 * importedNode being the newly created attribute. Method should return a node
53 * whose name matches "elem:attr1" and a child node whose value equals
54 * "importedText". The returned node should belong to this document whose
55 * systemId is "staff.dtd"
56 *
57 * @author NIST
58 * @author Mary Brady
59 * @see <a
60 *      href="http://www.w3.org/TR/DOM-Level-2-Core/core#Core-Document-importNode">http://www.w3.org/TR/DOM-Level-2-Core/core#Core-Document-importNode</a>
61 */
62public final class ImportNode extends DOMTestCase {
63
64    DOMDocumentBuilderFactory factory;
65
66    DocumentBuilder builder;
67
68    protected void setUp() throws Exception {
69        super.setUp();
70        try {
71            factory = new DOMDocumentBuilderFactory(DOMDocumentBuilderFactory
72                    .getConfiguration2());
73            builder = factory.getBuilder();
74        } catch (Exception e) {
75            fail("Unexpected exception" + e.getMessage());
76        }
77    }
78
79    protected void tearDown() throws Exception {
80        factory = null;
81        builder = null;
82        super.tearDown();
83    }
84
85    /**
86     * Runs the test case.
87     *
88     * @throws Throwable
89     *             Any uncaught exception causes test to fail
90     */
91    public void _testImportNode1() throws Throwable {
92        Document doc;
93        Document aNewDoc;
94        Attr newAttr;
95        Text importedChild;
96        Node aNode;
97        Document ownerDocument;
98        Element attrOwnerElement;
99        DocumentType docType;
100        String system;
101        boolean specified;
102        NodeList childList;
103        String nodeName;
104        Node child;
105        String childValue;
106        List<String> expectedResult = new ArrayList<String>();
107        expectedResult.add("elem:attr1");
108        expectedResult.add("importedText");
109
110        doc = (Document) load("staffNS", builder);
111        aNewDoc = (Document) load("staffNS", builder);
112        newAttr = aNewDoc.createAttribute("elem:attr1");
113        importedChild = aNewDoc.createTextNode("importedText");
114        aNode = newAttr.appendChild(importedChild);
115        aNode = doc.importNode(newAttr, false);
116        ownerDocument = aNode.getOwnerDocument();
117        docType = ownerDocument.getDoctype();
118        system = docType.getSystemId();
119        assertNotNull("aNode", aNode);
120        assertURIEquals("systemId", null, null, null, "staffNS.dtd", null,
121                null, null, null, system);
122        attrOwnerElement = ((Attr) /* Node */aNode).getOwnerElement();
123        assertNull("ownerElement", attrOwnerElement);
124        specified = ((Attr) /* Node */aNode).getSpecified();
125        assertTrue("specified", specified);
126        childList = aNode.getChildNodes();
127        assertEquals("childList", 1, childList.getLength());
128        nodeName = aNode.getNodeName();
129        assertEquals("nodeName", "elem:attr1", nodeName);
130        child = aNode.getFirstChild();
131        childValue = child.getNodeValue();
132        assertEquals("childValue", "importedText", childValue);
133    }
134    public void testImportNode2() throws Throwable {
135        Document doc;
136        Document aNewDoc;
137        CDATASection cDataSec;
138        Node aNode;
139        Document ownerDocument;
140        DocumentType docType;
141        String system;
142        String value;
143        doc = (Document) load("staffNS", builder);
144        aNewDoc = (Document) load("staffNS", builder);
145        cDataSec = aNewDoc.createCDATASection("this is CDATASection data");
146        aNode = doc.importNode(cDataSec, false);
147        ownerDocument = aNode.getOwnerDocument();
148        assertNotNull("ownerDocumentNotNull", ownerDocument);
149        docType = ownerDocument.getDoctype();
150        system = docType.getSystemId();
151        assertURIEquals("dtdSystemId", null, null, null, "staffNS.dtd", null,
152                null, null, null, system);
153        value = aNode.getNodeValue();
154        assertEquals("nodeValue", "this is CDATASection data", value);
155    }
156    public void testImportNode3() throws Throwable {
157        Document doc;
158        Document aNewDoc;
159        Comment comment;
160        Node aNode;
161        Document ownerDocument;
162        DocumentType docType;
163        String system;
164        String value;
165        doc = (Document) load("staffNS", builder);
166        aNewDoc = (Document) load("staffNS", builder);
167        comment = aNewDoc.createComment("this is a comment");
168        aNode = doc.importNode(comment, false);
169        ownerDocument = aNode.getOwnerDocument();
170        assertNotNull("ownerDocumentNotNull", ownerDocument);
171        docType = ownerDocument.getDoctype();
172        system = docType.getSystemId();
173        assertURIEquals("systemId", null, null, null, "staffNS.dtd", null,
174                null, null, null, system);
175        value = aNode.getNodeValue();
176        assertEquals("nodeValue", "this is a comment", value);
177    }
178    public void testImportNode4() throws Throwable {
179        Document doc;
180        Document aNewDoc;
181        DocumentFragment docFrag;
182        Comment comment;
183        Node aNode;
184        NodeList children;
185        Node child;
186        String childValue;
187        doc = (Document) load("staff", builder);
188        aNewDoc = (Document) load("staff", builder);
189        docFrag = aNewDoc.createDocumentFragment();
190        comment = aNewDoc.createComment("descendant1");
191        aNode = docFrag.appendChild(comment);
192        aNode = doc.importNode(docFrag, true);
193        children = aNode.getChildNodes();
194        assertEquals("throw_Size", 1, children.getLength());
195        child = aNode.getFirstChild();
196        childValue = child.getNodeValue();
197        assertEquals("descendant1", "descendant1", childValue);
198    }
199    public void testImportNode5() throws Throwable {
200        Document doc;
201        Document aNewDoc;
202        Element element;
203        Node aNode;
204        boolean hasChild;
205        Document ownerDocument;
206        DocumentType docType;
207        String system;
208        String name;
209        NodeList addresses;
210        doc = (Document) load("staffNS", builder);
211        aNewDoc = (Document) load("staffNS", builder);
212        addresses = aNewDoc.getElementsByTagName("emp:address");
213        element = (Element) addresses.item(0);
214        assertNotNull("empAddressNotNull", element);
215        aNode = doc.importNode(element, false);
216        hasChild = aNode.hasChildNodes();
217        assertFalse("hasChild", hasChild);
218        ownerDocument = aNode.getOwnerDocument();
219        docType = ownerDocument.getDoctype();
220        system = docType.getSystemId();
221        assertURIEquals("dtdSystemId", null, null, null, "staffNS.dtd", null,
222                null, null, null, system);
223        name = aNode.getNodeName();
224        assertEquals("nodeName", "emp:address", name);
225    }
226    public void testImportNode6() throws Throwable {
227        Document doc;
228        Document aNewDoc;
229        Element element;
230        Node aNode;
231        boolean hasChild;
232        String name;
233        Node child;
234        String value;
235        NodeList addresses;
236        doc = (Document) load("staffNS", builder);
237        aNewDoc = (Document) load("staffNS", builder);
238        addresses = aNewDoc.getElementsByTagName("emp:address");
239        element = (Element) addresses.item(0);
240        assertNotNull("empAddressNotNull", element);
241        aNode = doc.importNode(element, true);
242        hasChild = aNode.hasChildNodes();
243        assertTrue("throw_True", hasChild);
244        name = aNode.getNodeName();
245        assertEquals("nodeName", "emp:address", name);
246        child = aNode.getFirstChild();
247        value = child.getNodeValue();
248        assertEquals("nodeValue", "27 South Road. Dallas, texas 98556", value);
249    }
250
251// Assumes validation.
252//    public void testImportNode7() throws Throwable {
253//        Document doc;
254//        Document aNewDoc;
255//        Element element;
256//        Node aNode;
257//        NamedNodeMap attributes;
258//        String name;
259//        Node attr;
260//        String lname;
261//        String namespaceURI = "http://www.nist.gov";
262//        String qualifiedName = "emp:employee";
263//        doc = (Document) load("staffNS", builder);
264//        aNewDoc = (Document) load("staff", builder);
265//        element = aNewDoc.createElementNS(namespaceURI, qualifiedName);
266//        aNode = doc.importNode(element, false);
267//        attributes = aNode.getAttributes();
268//        assertEquals("throw_Size", 1, attributes.getLength());
269//        name = aNode.getNodeName();
270//        assertEquals("nodeName", "emp:employee", name);
271//        attr = attributes.item(0);
272//        lname = attr.getLocalName();
273//        assertEquals("lname", "defaultAttr", lname);
274//    }
275    public void testImportNode8() throws Throwable {
276        Document doc;
277        Document aNewDoc;
278        DocumentFragment docFrag;
279        Node aNode;
280        boolean hasChild;
281        Document ownerDocument;
282        DocumentType docType;
283        String system;
284        doc = (Document) load("staffNS", builder);
285        aNewDoc = (Document) load("staffNS", builder);
286        docFrag = aNewDoc.createDocumentFragment();
287        aNode = doc.importNode(docFrag, false);
288        hasChild = aNode.hasChildNodes();
289        assertFalse("hasChild", hasChild);
290        ownerDocument = aNode.getOwnerDocument();
291        docType = ownerDocument.getDoctype();
292        system = docType.getSystemId();
293        assertURIEquals("system", null, null, null, "staffNS.dtd", null, null,
294                null, null, system);
295    }
296
297// Assumes validation.
298//    public void testImportNode9() throws Throwable {
299//        Document doc;
300//        Document aNewDoc;
301//
302//        NamedNodeMap entityList;
303//        Entity entity2;
304//        Entity entity1;
305//        Document ownerDocument;
306//        DocumentType docType;
307//        String system;
308//        String entityName;
309//        String publicVal;
310//        String notationName;
311//        doc = (Document) load("staffNS", builder);
312//        aNewDoc = (Document) load("staffNS", builder);
313//        docType = aNewDoc.getDoctype();
314//        entityList = docType.getEntities();
315//        assertNotNull("entitiesNotNull", entityList);
316//        entity2 = (Entity) entityList.getNamedItem("ent6");
317//        entity1 = (Entity) doc.importNode(entity2, false);
318//        ownerDocument = entity1.getOwnerDocument();
319//        docType = ownerDocument.getDoctype();
320//        system = docType.getSystemId();
321//        assertURIEquals("dtdSystemId", null, null, null, "staffNS.dtd", null,
322//                null, null, null, system);
323//        entityName = entity1.getNodeName();
324//        assertEquals("entityName", "ent6", entityName);
325//        publicVal = entity1.getPublicId();
326//        assertEquals("entityPublicId", "uri", publicVal);
327//        system = entity1.getSystemId();
328//        assertURIEquals("entitySystemId", null, null, null, "file", null, null,
329//                null, null, system);
330//        notationName = entity1.getNotationName();
331//        assertEquals("notationName", "notation2", notationName);
332//    }
333    public void testImportNode10() throws Throwable {
334        Document doc;
335        Document aNewDoc;
336        EntityReference entRef;
337        Node aNode;
338        Document ownerDocument;
339        DocumentType docType;
340        String system;
341        String name;
342        doc = (Document) load("staffNS", builder);
343        aNewDoc = (Document) load("staffNS", builder);
344        entRef = aNewDoc.createEntityReference("entRef1");
345        assertNotNull("createdEntRefNotNull", entRef);
346        entRef.setNodeValue("entRef1Value");
347        aNode = doc.importNode(entRef, false);
348        ownerDocument = aNode.getOwnerDocument();
349        docType = ownerDocument.getDoctype();
350        system = docType.getSystemId();
351        assertURIEquals("systemId", null, null, null, "staffNS.dtd", null,
352                null, null, null, system);
353        name = aNode.getNodeName();
354        assertEquals("nodeName", "entRef1", name);
355    }
356
357// Assumes validation
358//    public void testImportNode11() throws Throwable {
359//        Document doc;
360//        Document aNewDoc;
361//        EntityReference entRef;
362//        Node aNode;
363//        String name;
364//        Node child;
365//        String childValue;
366//        doc = (Document) load("staff", builder);
367//        aNewDoc = (Document) load("staff", builder);
368//        entRef = aNewDoc.createEntityReference("ent3");
369//        assertNotNull("createdEntRefNotNull", entRef);
370//        aNode = doc.importNode(entRef, true);
371//        name = aNode.getNodeName();
372//        assertEquals("entityName", "ent3", name);
373//        child = aNode.getFirstChild();
374//        assertNotNull("child", child);
375//        childValue = child.getNodeValue();
376//        assertEquals("childValue", "Texas", childValue);
377//    }
378
379// Assumes validation.
380//    public void testImportNode12() throws Throwable {
381//        Document doc;
382//        Document aNewDoc;
383//        DocumentType doc1Type;
384//        NamedNodeMap entityList;
385//        Entity entity2;
386//        Entity entity1;
387//        Document ownerDocument;
388//        DocumentType docType;
389//        String system;
390//        String entityName;
391//        Node child;
392//        String childName;
393//        doc = (Document) load("staffNS", builder);
394//        aNewDoc = (Document) load("staffNS", builder);
395//        doc1Type = aNewDoc.getDoctype();
396//        entityList = doc1Type.getEntities();
397//        assertNotNull("entitiesNotNull", entityList);
398//        entity2 = (Entity) entityList.getNamedItem("ent4");
399//        entity1 = (Entity) doc.importNode(entity2, true);
400//        ownerDocument = entity1.getOwnerDocument();
401//        docType = ownerDocument.getDoctype();
402//        system = docType.getSystemId();
403//        assertURIEquals("systemId", null, null, null, "staffNS.dtd", null,
404//                null, null, null, system);
405//        entityName = entity1.getNodeName();
406//        assertEquals("entityName", "ent4", entityName);
407//        child = entity1.getFirstChild();
408//        assertNotNull("notnull", child);
409//        childName = child.getNodeName();
410//        assertEquals("childName", "entElement1", childName);
411//    }
412
413// Assumes validation
414//    public void testImportNode13() throws Throwable {
415//        Document doc;
416//        Document aNewDoc;
417//        DocumentType doc1Type;
418//        NamedNodeMap notationList;
419//        Notation notation;
420//        Notation aNode;
421//        Document ownerDocument;
422//        DocumentType docType;
423//        String system;
424//        String publicVal;
425//        doc = (Document) load("staffNS", builder);
426//        aNewDoc = (Document) load("staffNS", builder);
427//        doc1Type = aNewDoc.getDoctype();
428//        notationList = doc1Type.getNotations();
429//        assertNotNull("notationsNotNull", notationList);
430//        notation = (Notation) notationList.getNamedItem("notation1");
431//        aNode = (Notation) doc.importNode(notation, false);
432//        ownerDocument = aNode.getOwnerDocument();
433//        docType = ownerDocument.getDoctype();
434//        system = docType.getSystemId();
435//        assertURIEquals("systemId", null, null, null, "staffNS.dtd", null,
436//                null, null, null, system);
437//        publicVal = aNode.getPublicId();
438//        assertEquals("publicId", "notation1File", publicVal);
439//        system = aNode.getSystemId();
440//        assertNull("notationSystemId", system);
441//    }
442    public void testImportNode14() throws Throwable {
443        Document doc;
444        Document aNewDoc;
445        ProcessingInstruction pi;
446        ProcessingInstruction aNode;
447        Document ownerDocument;
448        DocumentType docType;
449        String system;
450        String target;
451        String data;
452
453        doc = (Document) load("staffNS", builder);
454        aNewDoc = (Document) load("staffNS", builder);
455        pi = aNewDoc.createProcessingInstruction("target1", "data1");
456        aNode = (ProcessingInstruction) doc.importNode(pi, false);
457        ownerDocument = aNode.getOwnerDocument();
458        assertNotNull("ownerDocumentNotNull", ownerDocument);
459        docType = ownerDocument.getDoctype();
460        system = docType.getSystemId();
461        assertURIEquals("systemId", null, null, null, "staffNS.dtd", null,
462                null, null, null, system);
463        target = aNode.getTarget();
464        assertEquals("piTarget", "target1", target);
465        data = aNode.getData();
466        assertEquals("piData", "data1", data);
467    }
468    public void testImportNode15() throws Throwable {
469        Document doc;
470        Document aNewDoc;
471        Text text;
472        Node aNode;
473        Document ownerDocument;
474        DocumentType docType;
475        String system;
476        String value;
477        doc = (Document) load("staffNS", builder);
478        aNewDoc = (Document) load("staffNS", builder);
479        text = aNewDoc.createTextNode("this is text data");
480        aNode = doc.importNode(text, false);
481        ownerDocument = aNode.getOwnerDocument();
482        assertNotNull("ownerDocumentNotNull", ownerDocument);
483        docType = ownerDocument.getDoctype();
484        system = docType.getSystemId();
485        assertURIEquals("systemId", null, null, null, "staffNS.dtd", null,
486                null, null, null, system);
487        value = aNode.getNodeValue();
488        assertEquals("nodeValue", "this is text data", value);
489    }
490    public void testImportNode16() throws Throwable {
491        Document doc;
492        Document anotherDoc;
493        DocumentType docType;
494
495        doc = (Document) load("staffNS", builder);
496        anotherDoc = (Document) load("staffNS", builder);
497        docType = anotherDoc.getDoctype();
498
499        {
500            boolean success = false;
501            try {
502                doc.importNode(docType, false);
503            } catch (DOMException ex) {
504                success = (ex.code == DOMException.NOT_SUPPORTED_ERR);
505            }
506            assertTrue("throw_NOT_SUPPORTED_ERR", success);
507        }
508    }
509    public void testImportNode17() throws Throwable {
510        Document doc;
511        Document anotherDoc;
512
513        doc = (Document) load("staffNS", builder);
514        anotherDoc = (Document) load("staffNS", builder);
515
516        {
517            boolean success = false;
518            try {
519                doc.importNode(anotherDoc, false);
520            } catch (DOMException ex) {
521                success = (ex.code == DOMException.NOT_SUPPORTED_ERR);
522            }
523            assertTrue("throw_NOT_SUPPORTED_ERR", success);
524        }
525    }
526}
527