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