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.Node;
28import org.w3c.dom.Document;
29import org.w3c.dom.DocumentFragment;
30import org.w3c.dom.Element;
31import org.w3c.dom.DOMException;
32import org.w3c.dom.Attr;
33import org.w3c.dom.NodeList;
34
35import javax.xml.parsers.DocumentBuilder;
36
37/**
38 * The method setPrefix sets the namespace prefix of this node. Note that
39 * setting this attribute, when permitted, changes the nodeName attribute, which
40 * holds the qualified name, as well as the tagName and name attributes of the
41 * Element and Attr interfaces, when applicable.
42 *
43 * Create a new element node with a namespace prefix. Add it to a new
44 * DocumentFragment Node without a prefix. Call setPrefix on the elemen node.
45 * Check if the prefix was set correctly on the element.
46 *
47 * @author IBM
48 * @author Neil Delima
49 * @see <a
50 *      href="http://www.w3.org/TR/DOM-Level-2-Core/core#ID-NodeNSPrefix">http://www.w3.org/TR/DOM-Level-2-Core/core#ID-NodeNSPrefix</a>
51 */
52public final class NodeSetPrefix extends DOMTestCase {
53
54    DOMDocumentBuilderFactory factory;
55
56    DocumentBuilder builder;
57
58    protected void setUp() throws Exception {
59        super.setUp();
60        try {
61            factory = new DOMDocumentBuilderFactory(DOMDocumentBuilderFactory
62                    .getConfiguration1());
63            builder = factory.getBuilder();
64        } catch (Exception e) {
65            fail("Unexpected exception" + e.getMessage());
66        }
67    }
68
69    protected void tearDown() throws Exception {
70        factory = null;
71        builder = null;
72        super.tearDown();
73    }
74
75    /**
76     * Runs the test case.
77     *
78     * @throws Throwable
79     *             Any uncaught exception causes test to fail
80     */
81    public void testSetPrefix1() throws Throwable {
82        Document doc;
83        DocumentFragment docFragment;
84        Element element;
85        String elementTagName;
86        String elementNodeName;
87
88        doc = (Document) load("staff", builder);
89        docFragment = doc.createDocumentFragment();
90        element = doc.createElementNS("http://www.w3.org/DOM/Test",
91                "emp:address");
92        docFragment.appendChild(element);
93        element.setPrefix("dmstc");
94        elementTagName = element.getTagName();
95        elementNodeName = element.getNodeName();
96        assertEquals("nodesetprefix01_tagname", "dmstc:address", elementTagName);
97        assertEquals("nodesetprefix01_nodeName", "dmstc:address",
98                elementNodeName);
99    }
100
101// TODO Fails on JDK. Why?
102//    public void testSetPrefix2() throws Throwable {
103//        Document doc;
104//        Element element;
105//        Attr attribute;
106//        Attr newAttribute;
107//
108//        NodeList elementList;
109//        String attrName;
110//        String newAttrName;
111//        doc = (Document) load("staffNS", builder);
112//        elementList = doc.getElementsByTagName("address");
113//        element = (Element) elementList.item(1);
114//        newAttribute = doc.createAttributeNS("http://www.w3.org/DOM/Test",
115//                "test:address");
116//        element.setAttributeNodeNS(newAttribute);
117//        newAttribute.setPrefix("dom");
118//        attribute = element
119//                .getAttributeNodeNS("http://www.usa.com", "domestic");
120//        attrName = attribute.getNodeName();
121//        newAttrName = newAttribute.getNodeName();
122//        assertEquals("nodesetprefix02_attrName", "dmstc:domestic", attrName);
123//        assertEquals("nodesetprefix02_newAttrName", "dom:address", newAttrName);
124//    }
125    public void testSetPrefix3() throws Throwable {
126        Document doc;
127        Element element;
128        doc = (Document) load("staffNS", builder);
129        element = doc.createElement("address");
130
131        {
132            boolean success = false;
133            try {
134                element.setPrefix("test");
135            } catch (DOMException ex) {
136                success = (ex.code == DOMException.NAMESPACE_ERR);
137            }
138            assertTrue("throw_NAMESPACE_ERR", success);
139        }
140    }
141
142// Relies on validation, which we don't support.
143//    public void testSetPrefix4() throws Throwable {
144//        Document doc;
145//        Element element;
146//        Attr attribute;
147//        NodeList elementList;
148//        String nullNS = null;
149//
150//        doc = (Document) load("staffNS", builder);
151//        elementList = doc.getElementsByTagName("emp:employee");
152//        element = (Element) elementList.item(0);
153//        assertNotNull("empEmployeeNotNull", element);
154//        attribute = element.getAttributeNodeNS(nullNS, "defaultAttr");
155//
156//        {
157//            boolean success = false;
158//            try {
159//                attribute.setPrefix("test");
160//            } catch (DOMException ex) {
161//                success = (ex.code == DOMException.NAMESPACE_ERR);
162//            }
163//            assertTrue("nodesetprefix04", success);
164//        }
165//    }
166    public void testSetPrefix5() throws Throwable {
167        Document doc;
168        Element element;
169        String prefixValue;
170        List<String> prefixValues = new ArrayList<String>();
171        prefixValues.add("_:");
172        prefixValues.add(":0");
173        prefixValues.add(":");
174        prefixValues.add("_::");
175        prefixValues.add("a:0:c");
176
177        doc = (Document) load("staffNS", builder);
178        element = doc.createElementNS("http://www.w3.org/DOM/Test/L2",
179                "dom:elem");
180        for (int indexN10050 = 0; indexN10050 < prefixValues.size(); indexN10050++) {
181            prefixValue = (String) prefixValues.get(indexN10050);
182
183            {
184                boolean success = false;
185                try {
186                    element.setPrefix(prefixValue);
187                } catch (DOMException ex) {
188                    success = (ex.code == DOMException.NAMESPACE_ERR);
189                }
190                assertTrue("throw_NAMESPACE_ERR", success);
191            }
192        }
193    }
194    public void testSetPrefix6() throws Throwable {
195        Document doc;
196        Element element;
197        doc = (Document) load("staffNS", builder);
198        element = doc.createElementNS("http://www.w3.org/DOM/Test/L2",
199                "dom:elem");
200
201        {
202            boolean success = false;
203            try {
204                element.setPrefix("xml");
205            } catch (DOMException ex) {
206                success = (ex.code == DOMException.NAMESPACE_ERR);
207            }
208            assertTrue("throw_NAMESPACE_ERR", success);
209        }
210    }
211    public void testSetPrefix7() throws Throwable {
212        Document doc;
213        Attr attribute;
214        doc = (Document) load("staffNS", builder);
215        attribute = doc.createAttributeNS("http://www.w3.org/DOM/Test/L2",
216                "abc:elem");
217
218        {
219            boolean success = false;
220            try {
221                attribute.setPrefix("xmlns");
222            } catch (DOMException ex) {
223                success = (ex.code == DOMException.NAMESPACE_ERR);
224            }
225            assertTrue("throw_NAMESPACE_ERR", success);
226        }
227    }
228    public void testSetPrefix8() throws Throwable {
229        Document doc;
230        Element element;
231        NodeList elementList;
232        Attr attribute;
233        doc = (Document) load("staffNS", builder);
234        elementList = doc.getElementsByTagName("employee");
235        element = (Element) elementList.item(0);
236        attribute = element.getAttributeNode("xmlns");
237
238        {
239            boolean success = false;
240            try {
241                attribute.setPrefix("xml");
242            } catch (DOMException ex) {
243                success = (ex.code == DOMException.NAMESPACE_ERR);
244            }
245            assertTrue("throw_NAMESPACE_ERR", success);
246        }
247    }
248    public void _testSetPrefix9() throws Throwable {
249        Document doc;
250        String value = "#$%&'()@";
251        Element element;
252        doc = (Document) load("staffNS", builder);
253        element = doc.createElementNS("http://www.w3.org/DOM/Test/L2",
254                "dom:elem");
255
256        {
257            boolean success = false;
258            try {
259                element.setPrefix(value);
260            } catch (DOMException ex) {
261                success = (ex.code == DOMException.INVALID_CHARACTER_ERR);
262            }
263            assertTrue("throw_INVALID_CHARACTER_ERR", success);
264        }
265    }
266}
267