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 dalvik.annotation.TestTargets;
25import dalvik.annotation.TestLevel;
26import dalvik.annotation.TestTargetNew;
27import dalvik.annotation.TestTargetClass;
28
29import org.w3c.dom.Element;
30import org.w3c.dom.Document;
31import org.w3c.dom.Attr;
32import org.w3c.dom.NamedNodeMap;
33import org.w3c.dom.NodeList;
34import org.w3c.dom.DOMException;
35import org.w3c.dom.EntityReference;
36
37import javax.xml.parsers.DocumentBuilder;
38
39/**
40 * Testing Element.setAttributeNodeNS: If an attribute with that local name and
41 * that namespace URI is already present in the element, it is replaced by the
42 * new one. Create a new element and two new attribute nodes (in the same
43 * namespace and same localNames). Add the two new attribute nodes to the
44 * element node using the setAttributeNodeNS method. Check that only one
45 * attribute is added, check the value of this attribute.
46 *
47 * @author IBM
48 * @author Neil Delima
49 * @see <a
50 *      href="http://www.w3.org/TR/DOM-Level-2-Core/core#ID-ElSetAtNodeNS">http://www.w3.org/TR/DOM-Level-2-Core/core#ID-ElSetAtNodeNS</a>
51 */
52@TestTargetClass(Element.class)
53public final class ElementSetAttributeNodeNS extends DOMTestCase {
54
55    DOMDocumentBuilderFactory factory;
56
57    DocumentBuilder builder;
58
59    protected void setUp() throws Exception {
60        super.setUp();
61        try {
62            factory = new DOMDocumentBuilderFactory(DOMDocumentBuilderFactory
63                    .getConfiguration2());
64            builder = factory.getBuilder();
65        } catch (Exception e) {
66            fail("Unexpected exception" + e.getMessage());
67        }
68    }
69
70    protected void tearDown() throws Exception {
71        factory = null;
72        builder = null;
73        super.tearDown();
74    }
75
76    /**
77     * Runs the test case.
78     *
79     * @throws Throwable
80     *             Any uncaught exception causes test to fail
81     */
82    @TestTargetNew(
83        level = TestLevel.PARTIAL_COMPLETE,
84        notes = "Doesn't verify DOMException.",
85        method = "setAttributeNodeNS",
86        args = {org.w3c.dom.Attr.class}
87    )
88    public void testSetAttributeNodeNS1() throws Throwable {
89        Document doc;
90        Element element;
91        Attr attribute1;
92        Attr attribute2;
93        Attr attrNode;
94        String attrName;
95        String attrNS;
96
97        NamedNodeMap attributes;
98
99        int length;
100        doc = (Document) load("staff", builder);
101        element = doc.createElementNS("http://www.w3.org/DOM/Test/Level2",
102                "new:element");
103        attribute1 = doc.createAttributeNS("http://www.w3.org/DOM/Test/att1",
104                "p1:att");
105        attribute2 = doc.createAttributeNS("http://www.w3.org/DOM/Test/att1",
106                "p2:att");
107        attribute2.setValue("value2");
108        element.setAttributeNodeNS(attribute1);
109        element.setAttributeNodeNS(attribute2);
110        attrNode = element.getAttributeNodeNS(
111                "http://www.w3.org/DOM/Test/att1", "att");
112        attrName = attrNode.getNodeName();
113        attrNS = attrNode.getNamespaceURI();
114        assertEquals("elementsetattributenodens01_attrName", "p2:att", attrName);
115        assertEquals("elementsetattributenodens01_attrNS",
116                "http://www.w3.org/DOM/Test/att1", attrNS);
117        attributes = element.getAttributes();
118        length = (int) attributes.getLength();
119        assertEquals("length", 1, length);
120    }
121    @TestTargetNew(
122        level = TestLevel.PARTIAL_COMPLETE,
123        notes = "Doesn't verify DOMException.",
124        method = "setAttributeNodeNS",
125        args = {org.w3c.dom.Attr.class}
126    )
127    public void testSetAttributeNodeNS2() throws Throwable {
128        Document doc;
129        Element element;
130        Element element2;
131        Attr attribute;
132        Attr attributeCloned;
133        Attr newAttr;
134        NodeList elementList;
135        String attrName;
136        String attrValue;
137        String nullNS = null;
138
139        doc = (Document) load("staffNS", builder);
140        elementList = doc.getElementsByTagNameNS("http://www.nist.gov",
141                "address");
142        element = (Element) elementList.item(1);
143        attribute = element.getAttributeNodeNS(nullNS, "street");
144        attributeCloned = (Attr) attribute.cloneNode(true);
145        element2 = (Element) elementList.item(2);
146        newAttr = element2.setAttributeNodeNS(attributeCloned);
147        attrName = newAttr.getNodeName();
148        attrValue = newAttr.getNodeValue();
149        assertEquals("elementsetattributenodens02_attrName", "street", attrName);
150        assertEquals("elementsetattributenodens02_attrValue", "Yes", attrValue);
151    }
152    @TestTargetNew(
153        level = TestLevel.PARTIAL_COMPLETE,
154        notes = "Verifies DOMException with INUSE_ATTRIBUTE_ERR code.",
155        method = "setAttributeNodeNS",
156        args = {org.w3c.dom.Attr.class}
157    )
158    public void testSetAttributeNodeNS3() throws Throwable {
159        Document doc;
160        Element element1;
161        Element element2;
162        Attr attribute;
163
164        NodeList elementList;
165        String nullNS = null;
166
167        doc = (Document) load("staffNS", builder);
168        elementList = doc.getElementsByTagNameNS("http://www.nist.gov",
169                "address");
170        element1 = (Element) elementList.item(1);
171        attribute = element1.getAttributeNodeNS(nullNS, "street");
172        element2 = (Element) elementList.item(2);
173
174        {
175            boolean success = false;
176            try {
177                element2.setAttributeNodeNS(attribute);
178            } catch (DOMException ex) {
179                success = (ex.code == DOMException.INUSE_ATTRIBUTE_ERR);
180            }
181            assertTrue("elementsetattributenodens03", success);
182        }
183    }
184    @TestTargetNew(
185        level = TestLevel.PARTIAL_COMPLETE,
186        notes = "Verifies DOMException with INUSE_ATTRIBUTE_ERR code.",
187        method = "setAttributeNodeNS",
188        args = {org.w3c.dom.Attr.class}
189    )
190    public void testSetAttributeNodeNS4() throws Throwable {
191        Document doc;
192        Element element1;
193        Element element2;
194        Attr attribute;
195
196        doc = (Document) load("staffNS", builder);
197        element1 = doc.createElementNS("http://www.w3.org/DOM/Test", "elem1");
198        element2 = doc.createElementNS("http://www.w3.org/DOM/Test", "elem2");
199        attribute = doc.createAttributeNS("http://www.w3.org/DOM/Test", "attr");
200        element1.setAttributeNodeNS(attribute);
201
202        {
203            boolean success = false;
204            try {
205                element2.setAttributeNodeNS(attribute);
206            } catch (DOMException ex) {
207                success = (ex.code == DOMException.INUSE_ATTRIBUTE_ERR);
208            }
209            assertTrue("elementsetattributenodens04", success);
210        }
211    }
212    @TestTargetNew(
213        level = TestLevel.PARTIAL_COMPLETE,
214        notes = "Verifies DOMException with WRONG_DOCUMENT_ERR code.",
215        method = "setAttributeNodeNS",
216        args = {org.w3c.dom.Attr.class}
217    )
218    public void testSetAttributeNodeNS5() throws Throwable {
219        Document doc;
220        Document docAlt;
221        Element element;
222        Attr attribute;
223
224        doc = (Document) load("staffNS", builder);
225        docAlt = (Document) load("staffNS", builder);
226        element = doc.createElementNS("http://www.w3.org/DOM/Test", "elem1");
227        attribute = docAlt.createAttributeNS("http://www.w3.org/DOM/Test",
228                "attr");
229
230        {
231            boolean success = false;
232            try {
233                element.setAttributeNodeNS(attribute);
234            } catch (DOMException ex) {
235                success = (ex.code == DOMException.WRONG_DOCUMENT_ERR);
236            }
237            assertTrue("throw_WRONG_DOCUMENT_ERR", success);
238        }
239    }
240    @TestTargetNew(
241        level = TestLevel.PARTIAL_COMPLETE,
242        notes = "Verifies DOMException with NO_MODIFICATION_ALLOWED_ERR code.",
243        method = "setAttributeNodeNS",
244        args = {org.w3c.dom.Attr.class}
245    )
246    public void _testSetAttributeNodeNS6() throws Throwable {
247        Document doc;
248        Element element;
249        Attr attribute;
250        Attr attribute2;
251        EntityReference entRef;
252        NodeList elementList;
253
254        doc = (Document) load("staffNS", builder);
255        element = doc.createElementNS("http://www.w3.org/DOM/Test", "elem1");
256        attribute = doc.createAttributeNS("http://www.w3.org/DOM/Test", "attr");
257        entRef = doc.createEntityReference("ent4");
258        attribute.appendChild(entRef);
259        element.setAttributeNodeNS(attribute);
260        elementList = entRef.getChildNodes();
261        element = (Element) elementList.item(0);
262        attribute2 = doc.createAttributeNS("http://www.w3.org/DOM/Test",
263                "attr2");
264
265        {
266            boolean success = false;
267            try {
268                element.setAttributeNodeNS(attribute2);
269            } catch (DOMException ex) {
270                success = (ex.code == DOMException.NO_MODIFICATION_ALLOWED_ERR);
271            }
272            assertTrue("elementsetattributenodens06", success);
273        }
274    }
275
276}
277