1package tests.org.w3c.dom;
2
3import dalvik.annotation.TestTargets;
4import dalvik.annotation.TestLevel;
5import dalvik.annotation.TestTargetNew;
6import dalvik.annotation.TestTargetClass;
7
8import java.util.ArrayList;
9import java.util.List;
10
11import org.w3c.dom.DOMImplementation;
12import org.w3c.dom.Document;
13import org.w3c.dom.DocumentType;
14import org.w3c.dom.DOMException;
15
16import javax.xml.parsers.DocumentBuilder;
17
18/**
19 * The createDocument method with valid arguments, should create a DOM Document
20 * of the specified type.
21 *
22 * Call the createDocument on this DOMImplementation with createDocument
23 * ("http://www.w3.org/DOMTest/L2",see the array below for valid QNames,null).
24 * Check if the returned Document object is is empty with no Document Element.
25 *
26 * @author IBM
27 * @author Neil Delima
28 * @see <a
29 *      href="http://www.w3.org/TR/DOM-Level-2-Core/core#Level-2-Core-DOM-createDocument">http://www.w3.org/TR/DOM-Level-2-Core/core#Level-2-Core-DOM-createDocument</a>
30 */
31@TestTargetClass(DOMImplementation.class)
32public final class DOMImplementationCreateDocument extends DOMTestCase {
33
34    DOMDocumentBuilderFactory factory;
35
36    DocumentBuilder builder;
37
38    protected void setUp() throws Exception {
39        super.setUp();
40        try {
41            factory = new DOMDocumentBuilderFactory(DOMDocumentBuilderFactory
42                    .getConfiguration1());
43            builder = factory.getBuilder();
44        } catch (Exception e) {
45            fail("Unexpected exception" + e.getMessage());
46        }
47    }
48
49    protected void tearDown() throws Exception {
50        factory = null;
51        builder = null;
52        super.tearDown();
53    }
54
55    /**
56     * Runs the test case.
57     *
58     * @throws Throwable
59     *             Any uncaught exception causes test to fail
60     */
61    @TestTargetNew(
62        level = TestLevel.PARTIAL,
63        notes = "Doesn't verify DOMException.",
64        method = "createDocument",
65        args = {java.lang.String.class, java.lang.String.class, org.w3c.dom.DocumentType.class}
66    )
67    public void testCreateDocument3() throws Throwable {
68        Document doc;
69        DOMImplementation domImpl;
70        Document newDoc;
71        DocumentType docType = null;
72
73        String namespaceURI = "http://www.w3.org/DOMTest/L2";
74        String qualifiedName;
75        List<String> qualifiedNames = new ArrayList<String>();
76        qualifiedNames.add("_:_");
77        qualifiedNames.add("_:h0");
78        qualifiedNames.add("_:test");
79        qualifiedNames.add("l_:_");
80        qualifiedNames.add("ns:_0");
81        qualifiedNames.add("ns:a0");
82        qualifiedNames.add("ns0:test");
83        qualifiedNames.add("a.b:c");
84        qualifiedNames.add("a-b:c");
85        qualifiedNames.add("a-b:c");
86
87        doc = (Document) load("staffNS", builder);
88        domImpl = doc.getImplementation();
89        for (int indexN1006B = 0; indexN1006B < qualifiedNames.size(); indexN1006B++) {
90            qualifiedName = (String) qualifiedNames.get(indexN1006B);
91            newDoc = domImpl.createDocument(namespaceURI, qualifiedName,
92                    docType);
93            assertNotNull("domimplementationcreatedocument03", newDoc);
94        }
95    }
96    @TestTargetNew(
97        level = TestLevel.PARTIAL,
98        notes = "Verifies DOMException with NAMESPACE_ERR code.",
99        method = "createDocument",
100        args = {java.lang.String.class, java.lang.String.class, org.w3c.dom.DocumentType.class}
101    )
102    public void testCreateDocument4() throws Throwable {
103        Document doc;
104        DOMImplementation domImpl;
105
106        String namespaceURI = null;
107
108        String qualifiedName = "dom:root";
109        DocumentType docType = null;
110
111        doc = (Document) load("staffNS", builder);
112        domImpl = doc.getImplementation();
113
114        {
115            boolean success = false;
116            try {
117                domImpl.createDocument(namespaceURI, qualifiedName, docType);
118            } catch (DOMException ex) {
119                success = (ex.code == DOMException.NAMESPACE_ERR);
120            }
121            assertTrue("domimplementationcreatedocument04", success);
122        }
123    }
124    @TestTargetNew(
125        level = TestLevel.PARTIAL,
126        notes = "Verifies DOMException with NAMESPACE_ERR code.",
127        method = "createDocument",
128        args = {java.lang.String.class, java.lang.String.class, org.w3c.dom.DocumentType.class}
129    )
130    public void testCreateDocument5() throws Throwable {
131        Document doc;
132        DOMImplementation domImpl;
133
134        String namespaceURI = "http://www.w3.org/xml/1998/namespace";
135        String qualifiedName = "xml:root";
136        DocumentType docType = null;
137
138        doc = (Document) load("staffNS", builder);
139        domImpl = doc.getImplementation();
140
141        {
142            boolean success = false;
143            try {
144                domImpl.createDocument(namespaceURI, qualifiedName, docType);
145            } catch (DOMException ex) {
146                success = (ex.code == DOMException.NAMESPACE_ERR);
147            }
148            assertTrue("domimplementationcreatedocument05", success);
149        }
150    }
151    @TestTargetNew(
152        level = TestLevel.PARTIAL,
153        notes = "Verifies DOMException with NAMESPACE_ERR code.",
154        method = "createDocument",
155        args = {java.lang.String.class, java.lang.String.class, org.w3c.dom.DocumentType.class}
156    )
157    public void testCreateDocument7() throws Throwable {
158        Document doc;
159        DOMImplementation domImpl;
160
161        String namespaceURI = "http://www.w3.org/DOMTest/level2";
162        DocumentType docType = null;
163
164        doc = (Document) load("staffNS", builder);
165        domImpl = doc.getImplementation();
166
167        {
168            boolean success = false;
169            try {
170                domImpl.createDocument(namespaceURI, ":", docType);
171            } catch (DOMException ex) {
172                success = (ex.code == DOMException.NAMESPACE_ERR);
173            }
174            assertTrue("domimplementationcreatedocument07", success);
175        }
176    }
177}
178