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.DOMImplementation;
28import org.w3c.dom.Document;
29import org.w3c.dom.DOMException;
30import org.w3c.dom.DocumentType;
31
32import javax.xml.parsers.DocumentBuilder;
33
34/**
35 * The "createDocumentType(qualifiedName,publicId,systemId)" method for a
36 * DOMImplementation should raise NAMESPACE_ERR DOMException if qualifiedName is
37 * malformed.
38 *
39 * Retrieve the DOMImplementation on the XMLNS Document. Invoke method
40 * createDocumentType(qualifiedName,publicId,systemId) on the retrieved
41 * DOMImplementation with qualifiedName being the literal string
42 * "prefix::local", publicId as "STAFF", and systemId as "staff". Method should
43 * raise NAMESPACE_ERR DOMException.
44 *
45 * @author NIST
46 * @author Mary Brady
47 * @see <a
48 *      href="http://www.w3.org/TR/DOM-Level-2-Core/core#xpointer(id('ID-258A00AF')/constant[@name='NAMESPACE_ERR'])">http://www.w3.org/TR/DOM-Level-2-Core/core#xpointer(id('ID-258A00AF')/constant[@name='NAMESPACE_ERR'])</a>
49 * @see <a
50 *      href="http://www.w3.org/TR/DOM-Level-2-Core/core#Level-2-Core-DOM-createDocType">http://www.w3.org/TR/DOM-Level-2-Core/core#Level-2-Core-DOM-createDocType</a>
51 * @see <a
52 *      href="http://www.w3.org/TR/DOM-Level-2-Core/core#xpointer(id('Level-2-Core-DOM-createDocType')/raises/exception[@name='DOMException']/descr/p[substring-before(.,':')='NAMESPACE_ERR'])">http://www.w3.org/TR/DOM-Level-2-Core/core#xpointer(id('Level-2-Core-DOM-createDocType')/raises/exception[@name='DOMException']/descr/p[substring-before(.,':')='NAMESPACE_ERR'])</a>
53 */
54public final class CreateDocumentType extends DOMTestCase {
55
56    DOMDocumentBuilderFactory factory;
57
58    DocumentBuilder builder;
59
60    protected void setUp() throws Exception {
61        super.setUp();
62        try {
63            factory = new DOMDocumentBuilderFactory(DOMDocumentBuilderFactory
64                    .getConfiguration1());
65            builder = factory.getBuilder();
66        } catch (Exception e) {
67            fail("Unexpected exception" + e.getMessage());
68        }
69    }
70
71    protected void tearDown() throws Exception {
72        factory = null;
73        builder = null;
74        super.tearDown();
75    }
76
77    /**
78     * Runs the test case.
79     *
80     * @throws Throwable
81     *             Any uncaught exception causes test to fail
82     */
83    public void testCreateDocumentType1() throws Throwable {
84        String publicId = "STAFF";
85        String systemId = "staff.xml";
86        String malformedName = "prefix::local";
87        Document doc;
88        DOMImplementation domImpl;
89
90        doc = (Document) load("staffNS", builder);
91        domImpl = doc.getImplementation();
92
93        {
94            boolean success = false;
95            try {
96                domImpl.createDocumentType(malformedName, publicId, systemId);
97            } catch (DOMException ex) {
98                success = (ex.code == DOMException.NAMESPACE_ERR);
99            }
100            assertTrue("throw_NAMESPACE_ERR", success);
101        }
102    }
103    public void testCreateDocumentType2() throws Throwable {
104        String publicId = "http://www.localhost.com/";
105        String systemId = "myDoc.dtd";
106        String qualifiedName;
107        Document doc;
108
109        DOMImplementation domImpl;
110        List<String> illegalQNames = new ArrayList<String>();
111        illegalQNames.add("edi:{");
112        illegalQNames.add("edi:}");
113        illegalQNames.add("edi:~");
114        illegalQNames.add("edi:'");
115        illegalQNames.add("edi:!");
116        illegalQNames.add("edi:@");
117        illegalQNames.add("edi:#");
118        illegalQNames.add("edi:$");
119        illegalQNames.add("edi:%");
120        illegalQNames.add("edi:^");
121        illegalQNames.add("edi:&");
122        illegalQNames.add("edi:*");
123        illegalQNames.add("edi:(");
124        illegalQNames.add("edi:)");
125        illegalQNames.add("edi:+");
126        illegalQNames.add("edi:=");
127        illegalQNames.add("edi:[");
128        illegalQNames.add("edi:]");
129        illegalQNames.add("edi:\\");
130        illegalQNames.add("edi:/");
131        illegalQNames.add("edi:;");
132        illegalQNames.add("edi:`");
133        illegalQNames.add("edi:<");
134        illegalQNames.add("edi:>");
135        illegalQNames.add("edi:,");
136        illegalQNames.add("edi:a ");
137        illegalQNames.add("edi:\"");
138
139        doc = (Document) load("staffNS", builder);
140        for (int indexN1009A = 0; indexN1009A < illegalQNames.size(); indexN1009A++) {
141            qualifiedName = (String) illegalQNames.get(indexN1009A);
142            domImpl = doc.getImplementation();
143
144            {
145                boolean success = false;
146                try {
147                    domImpl.createDocumentType(qualifiedName, publicId,
148                            systemId);
149                } catch (DOMException ex) {
150                    success = (ex.code == DOMException.INVALID_CHARACTER_ERR);
151                }
152                assertTrue("throw_INVALID_CHARACTER_ERR", success);
153            }
154        }
155    }
156    public void testCreateDocumentType3() throws Throwable {
157
158        String qualifiedName = "prefix:myDoc";
159        String publicId = "http://www.localhost.com";
160        String systemId = "myDoc.dtd";
161        Document doc;
162        DOMImplementation domImpl;
163        DocumentType newType = null;
164
165        String nodeName;
166        String nodeValue;
167        doc = (Document) load("staffNS", builder);
168        domImpl = doc.getImplementation();
169        newType = domImpl.createDocumentType(qualifiedName, publicId, systemId);
170        nodeName = newType.getNodeName();
171        assertEquals("nodeName", "prefix:myDoc", nodeName);
172        nodeValue = newType.getNodeValue();
173        assertNull("nodeValue", nodeValue);
174    }
175    public void testCreateDocumentType4() throws Throwable {
176        String publicId = "http://www.example.com/";
177        String systemId = "myDoc.dtd";
178
179        DOMImplementation domImpl;
180        domImpl = builder.getDOMImplementation();
181
182        {
183            boolean success = false;
184            try {
185                domImpl.createDocumentType("", publicId, systemId);
186            } catch (DOMException ex) {
187                success = (ex.code == DOMException.NAMESPACE_ERR);
188            }
189            assertTrue("throw_NAMESPACE_ERR", success);
190        }
191    }
192}
193