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 java.util.ArrayList;
     25 import java.util.List;
     26 
     27 import org.w3c.dom.Document;
     28 import org.w3c.dom.DOMException;
     29 import org.w3c.dom.Element;
     30 
     31 import javax.xml.parsers.DocumentBuilder;
     32 
     33 /**
     34  * The "createElementNS(namespaceURI,qualifiedName)" method for a Document
     35  * should raise NAMESPACE_ERR DOMException if qualifiedName is malformed.
     36  *
     37  * Invoke method createElementNS(namespaceURI,qualifiedName) on the XMLNS
     38  * Document with namespaceURI being the literal string
     39  * "http://www.ecommerce.org/", and qualifiedName as "prefix::local". Method
     40  * should raise NAMESPACE_ERR DOMException.
     41  *
     42  * @author NIST
     43  * @author Mary Brady
     44  * @see <a
     45  *      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>
     46  * @see <a
     47  *      href="http://www.w3.org/TR/DOM-Level-2-Core/core#ID-DocCrElNS">http://www.w3.org/TR/DOM-Level-2-Core/core#ID-DocCrElNS</a>
     48  * @see <a
     49  *      href="http://www.w3.org/TR/DOM-Level-2-Core/core#xpointer(id('ID-DocCrElNS')/raises/exception[@name='DOMException']/descr/p[substring-before(.,':')='NAMESPACE_ERR'])">http://www.w3.org/TR/DOM-Level-2-Core/core#xpointer(id('ID-DocCrElNS')/raises/exception[@name='DOMException']/descr/p[substring-before(.,':')='NAMESPACE_ERR'])</a>
     50  */
     51 public final class CreateElementNS extends DOMTestCase {
     52 
     53     DOMDocumentBuilderFactory factory;
     54 
     55     DocumentBuilder builder;
     56 
     57     protected void setUp() throws Exception {
     58         super.setUp();
     59         try {
     60             factory = new DOMDocumentBuilderFactory(DOMDocumentBuilderFactory
     61                     .getConfiguration1());
     62             builder = factory.getBuilder();
     63         } catch (Exception e) {
     64             fail("Unexpected exception" + e.getMessage());
     65         }
     66     }
     67 
     68     protected void tearDown() throws Exception {
     69         factory = null;
     70         builder = null;
     71         super.tearDown();
     72     }
     73 
     74     /**
     75      * Runs the test case.
     76      *
     77      * @throws Throwable
     78      *             Any uncaught exception causes test to fail
     79      */
     80     public void testCreateElementNS1() throws Throwable {
     81         String namespaceURI = "http://www.ecommerce.org/";
     82         String malformedName = "prefix::local";
     83         Document doc;
     84 
     85         doc = (Document) load("staffNS", builder);
     86 
     87         {
     88             boolean success = false;
     89             try {
     90                 doc.createElementNS(namespaceURI, malformedName);
     91             } catch (DOMException ex) {
     92                 success = (ex.code == DOMException.NAMESPACE_ERR);
     93             }
     94             assertTrue("throw_NAMESPACE_ERR", success);
     95         }
     96     }
     97     public void testCreateElementNS2() throws Throwable {
     98         String namespaceURI = null;
     99 
    100         String qualifiedName = "prefix:local";
    101         Document doc;
    102 
    103         doc = (Document) load("staffNS", builder);
    104 
    105         {
    106             boolean success = false;
    107             try {
    108                 doc.createElementNS(namespaceURI, qualifiedName);
    109             } catch (DOMException ex) {
    110                 success = (ex.code == DOMException.NAMESPACE_ERR);
    111             }
    112             assertTrue("throw_NAMESPACE_ERR", success);
    113         }
    114     }
    115     public void testCreateElementNS3() throws Throwable {
    116         String namespaceURI = "http://www.wedding.com/";
    117         String qualifiedName;
    118         Document doc;
    119 
    120         List<String> illegalQNames = new ArrayList<String>();
    121         illegalQNames.add("person:{");
    122         illegalQNames.add("person:}");
    123         illegalQNames.add("person:~");
    124         illegalQNames.add("person:'");
    125         illegalQNames.add("person:!");
    126         illegalQNames.add("person:@");
    127         illegalQNames.add("person:#");
    128         illegalQNames.add("person:$");
    129         illegalQNames.add("person:%");
    130         illegalQNames.add("person:^");
    131         illegalQNames.add("person:&");
    132         illegalQNames.add("person:*");
    133         illegalQNames.add("person:(");
    134         illegalQNames.add("person:)");
    135         illegalQNames.add("person:+");
    136         illegalQNames.add("person:=");
    137         illegalQNames.add("person:[");
    138         illegalQNames.add("person:]");
    139         illegalQNames.add("person:\\");
    140         illegalQNames.add("person:/");
    141         illegalQNames.add("person:;");
    142         illegalQNames.add("person:`");
    143         illegalQNames.add("person:<");
    144         illegalQNames.add("person:>");
    145         illegalQNames.add("person:,");
    146         illegalQNames.add("person:a ");
    147         illegalQNames.add("person:\"");
    148 
    149         doc = (Document) load("staffNS", builder);
    150         for (int indexN10098 = 0; indexN10098 < illegalQNames.size(); indexN10098++) {
    151             qualifiedName = (String) illegalQNames.get(indexN10098);
    152 
    153             {
    154                 boolean success = false;
    155                 try {
    156                     doc.createElementNS(namespaceURI, qualifiedName);
    157                 } catch (DOMException ex) {
    158                     success = (ex.code == DOMException.INVALID_CHARACTER_ERR);
    159                 }
    160                 assertTrue("throw_INVALID_CHARACTER_ERR", success);
    161             }
    162         }
    163     }
    164     public void testCreateElementNS4() throws Throwable {
    165         String namespaceURI = "http://www.w3.org/XML/1998/namespaces";
    166         String qualifiedName = "xml:element1";
    167         Document doc;
    168 
    169         doc = (Document) load("staffNS", builder);
    170 
    171         {
    172             boolean success = false;
    173             try {
    174                 doc.createElementNS(namespaceURI, qualifiedName);
    175             } catch (DOMException ex) {
    176                 success = (ex.code == DOMException.NAMESPACE_ERR);
    177             }
    178             assertTrue("throw_NAMESPACE_ERR", success);
    179         }
    180     }
    181     public void testCreateElementNS5() throws Throwable {
    182         String namespaceURI = "http://www.nist.gov";
    183         String qualifiedName = "gov:faculty";
    184         Document doc;
    185         Element newElement;
    186         String elementName;
    187         doc = (Document) load("staffNS", builder);
    188         newElement = doc.createElementNS(namespaceURI, qualifiedName);
    189         elementName = newElement.getTagName();
    190         assertEquals("throw_Equals", qualifiedName, elementName);
    191     }
    192     public void testCreateElementNS6() throws Throwable {
    193         String namespaceURI = "http://www.example.com/";
    194 
    195         Document doc;
    196 
    197         doc = (Document) load("hc_staff", builder);
    198 
    199         {
    200             // BEGIN android-changed
    201             //     Our exception priorities differ from the spec
    202             try {
    203                 doc.createElementNS(namespaceURI, "");
    204                 fail();
    205             } catch (DOMException ex) {
    206             }
    207             // END android-changed
    208         }
    209     }
    210 }
    211