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 org.w3c.dom.Node;
     25 import org.w3c.dom.Document;
     26 import org.w3c.dom.NodeList;
     27 import org.w3c.dom.Element;
     28 import org.w3c.dom.Attr;
     29 import org.w3c.dom.DOMException;
     30 
     31 import javax.xml.parsers.DocumentBuilder;
     32 
     33 /**
     34  * The "getPrefix()" method for a Node returns the namespace prefix of the node,
     35  * and for nodes of any type other than ELEMENT_NODE and ATTRIBUTE_NODE and
     36  * nodes created with a DOM Level 1 method, this is null.
     37  *
     38  * Create an new Element with the createElement() method. Invoke the
     39  * "getPrefix()" method on the newly created element node will cause "null" to
     40  * be returned.
     41  *
     42  * @author NIST
     43  * @author Mary Brady
     44  * @see <a
     45  *      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>
     46  */
     47 public final class Prefix extends DOMTestCase {
     48 
     49     DOMDocumentBuilderFactory factory;
     50 
     51     DocumentBuilder builder;
     52 
     53     protected void setUp() throws Exception {
     54         super.setUp();
     55         try {
     56             factory = new DOMDocumentBuilderFactory(DOMDocumentBuilderFactory
     57                     .getConfiguration2());
     58             builder = factory.getBuilder();
     59         } catch (Exception e) {
     60             fail("Unexpected exception" + e.getMessage());
     61         }
     62     }
     63 
     64     protected void tearDown() throws Exception {
     65         factory = null;
     66         builder = null;
     67         super.tearDown();
     68     }
     69 
     70     /**
     71      * Runs the test case.
     72      *
     73      * @throws Throwable
     74      *             Any uncaught exception causes test to fail
     75      */
     76     public void testGetPrefix1() throws Throwable {
     77         Document doc;
     78         Node createdNode;
     79         String prefix;
     80         doc = (Document) load("staffNS", builder);
     81         createdNode = doc.createElement("test:employee");
     82         prefix = createdNode.getPrefix();
     83         assertNull("throw_Null", prefix);
     84     }
     85     public void testGetPrefix2() throws Throwable {
     86         Document doc;
     87         NodeList elementList;
     88         Node testEmployee;
     89         Node textNode;
     90         String prefix;
     91         doc = (Document) load("staffNS", builder);
     92         elementList = doc.getElementsByTagName("emp:employeeId");
     93         testEmployee = elementList.item(0);
     94         assertNotNull("empEmployeeNotNull", testEmployee);
     95         textNode = testEmployee.getFirstChild();
     96         prefix = textNode.getPrefix();
     97         assertNull("textNodePrefix", prefix);
     98     }
     99     public void testGetPrefix3() throws Throwable {
    100         Document doc;
    101         NodeList elementList;
    102         Node testEmployee;
    103         String prefix;
    104         doc = (Document) load("staffNS", builder);
    105         elementList = doc.getElementsByTagName("emp:employee");
    106         testEmployee = elementList.item(0);
    107         assertNotNull("empEmployeeNotNull", testEmployee);
    108         prefix = testEmployee.getPrefix();
    109         assertEquals("prefix", "emp", prefix);
    110     }
    111     public void testGetPrefix4() throws Throwable {
    112         Document doc;
    113         NodeList elementList;
    114         Node testEmployee;
    115         String prefix;
    116         doc = (Document) load("staffNS", builder);
    117         elementList = doc.getElementsByTagName("employee");
    118         testEmployee = elementList.item(0);
    119         prefix = testEmployee.getPrefix();
    120         assertNull("throw_Null", prefix);
    121     }
    122     public void testGetPrefix5() throws Throwable {
    123         Document doc;
    124         NodeList elementList;
    125         Element addrNode;
    126         Attr addrAttr;
    127         doc = (Document) load("staffNS", builder);
    128         elementList = doc.getElementsByTagName("emp:address");
    129         addrNode = (Element) elementList.item(0);
    130         assertNotNull("empAddrNotNull", addrNode);
    131         addrAttr = addrNode.getAttributeNode("emp:domestic");
    132 
    133         {
    134             boolean success = false;
    135             try {
    136                 addrAttr.setPrefix("xmlns");
    137             } catch (DOMException ex) {
    138                 success = (ex.code == DOMException.NAMESPACE_ERR);
    139             }
    140             assertTrue("throw_NAMESPACE_ERR", success);
    141         }
    142     }
    143     public void _testGetPrefix6() throws Throwable {
    144         Document doc;
    145         NodeList elementList;
    146         Node employeeNode;
    147         doc = (Document) load("staffNS", builder);
    148         elementList = doc.getElementsByTagName("employee");
    149         employeeNode = elementList.item(0);
    150 
    151         {
    152             boolean success = false;
    153             try {
    154                 employeeNode.setPrefix("pre^fix xmlns='http//www.nist.gov'");
    155             } catch (DOMException ex) {
    156                 success = (ex.code == DOMException.INVALID_CHARACTER_ERR);
    157             }
    158             assertTrue("throw_INVALID_CHARACTER_ERR", success);
    159         }
    160     }
    161     public void testGetPrefix7() throws Throwable {
    162         Document doc;
    163         NodeList elementList;
    164         Node employeeNode;
    165         doc = (Document) load("staffNS", builder);
    166         elementList = doc.getElementsByTagName("employee");
    167         employeeNode = elementList.item(0);
    168 
    169         {
    170             boolean success = false;
    171             try {
    172                 employeeNode.setPrefix("emp::");
    173             } catch (DOMException ex) {
    174                 success = (ex.code == DOMException.NAMESPACE_ERR);
    175             }
    176             assertTrue("throw_NAMESPACE_ERR", success);
    177         }
    178     }
    179 
    180 // Assumes validation.
    181 //    public void testGetPrefix8() throws Throwable {
    182 //        Document doc;
    183 //        NodeList genderList;
    184 //        Node genderNode;
    185 //        Node entRef;
    186 //        Node entElement;
    187 //
    188 //        int nodeType;
    189 //        doc = (Document) load("staff", builder);
    190 //        genderList = doc.getElementsByTagName("gender");
    191 //        genderNode = genderList.item(2);
    192 //        entRef = genderNode.getFirstChild();
    193 //        nodeType = (int) entRef.getNodeType();
    194 //
    195 //        if (1 == nodeType) {
    196 //            entRef = doc.createEntityReference("ent4");
    197 //            assertNotNull("createdEntRefNotNull", entRef);
    198 //        }
    199 //        entElement = entRef.getFirstChild();
    200 //        assertNotNull("entElement", entElement);
    201 //        doc.createElement("text3");
    202 //
    203 //        {
    204 //            boolean success = false;
    205 //            try {
    206 //                entElement.setPrefix("newPrefix");
    207 //            } catch (DOMException ex) {
    208 //                success = (ex.code == DOMException.NO_MODIFICATION_ALLOWED_ERR);
    209 //            }
    210 //            assertTrue("throw_NO_MODIFICATION_ALLOWED_ERR", success);
    211 //        }
    212 //    }
    213     public void _testGetPrefix9() throws Throwable {
    214         Document doc;
    215         NodeList elementList;
    216         Element addrNode;
    217         Attr addrAttr;
    218         doc = (Document) load("staffNS", builder);
    219         elementList = doc.getElementsByTagName("address");
    220         addrNode = (Element) elementList.item(3);
    221         addrAttr = addrNode.getAttributeNode("xmlns");
    222 
    223         {
    224             boolean success = false;
    225             try {
    226                 addrAttr.setPrefix("xxx");
    227             } catch (DOMException ex) {
    228                 success = (ex.code == DOMException.NAMESPACE_ERR);
    229             }
    230             assertTrue("throw_NAMESPACE_ERR", success);
    231         }
    232     }
    233     public void testGetPrefix10() throws Throwable {
    234         Document doc;
    235         NodeList elementList;
    236         Node employeeNode;
    237         doc = (Document) load("staffNS", builder);
    238         elementList = doc.getElementsByTagName("employee");
    239         employeeNode = elementList.item(1);
    240 
    241         {
    242             boolean success = false;
    243             try {
    244                 employeeNode.setPrefix("xml");
    245             } catch (DOMException ex) {
    246                 success = (ex.code == DOMException.NAMESPACE_ERR);
    247             }
    248             assertTrue("throw_NAMESPACE_ERR", success);
    249         }
    250     }
    251     public void testGetPrefix11() throws Throwable {
    252         Document doc;
    253         NodeList elementList;
    254         Node employeeNode;
    255 
    256         doc = (Document) load("staffNS", builder);
    257         elementList = doc.getElementsByTagName("employee");
    258         employeeNode = elementList.item(1);
    259         employeeNode.getNamespaceURI();
    260 
    261         {
    262             boolean success = false;
    263             try {
    264                 employeeNode.setPrefix("employee1");
    265             } catch (DOMException ex) {
    266                 success = (ex.code == DOMException.NAMESPACE_ERR);
    267             }
    268             assertTrue("throw_NAMESPACE_ERR", success);
    269         }
    270     }
    271 }
    272