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