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.Attr;
     30 
     31 import javax.xml.parsers.DocumentBuilder;
     32 
     33 public final class CreateAttributeNS extends DOMTestCase {
     34 
     35     DOMDocumentBuilderFactory factory;
     36 
     37     DocumentBuilder builder;
     38 
     39     protected void setUp() throws Exception {
     40         super.setUp();
     41         try {
     42             factory = new DOMDocumentBuilderFactory(DOMDocumentBuilderFactory
     43                     .getConfiguration2());
     44             builder = factory.getBuilder();
     45         } catch (Exception e) {
     46             fail("Unexpected exception" + e.getMessage());
     47         }
     48     }
     49 
     50     protected void tearDown() throws Exception {
     51         factory = null;
     52         builder = null;
     53         super.tearDown();
     54     }
     55 
     56     /**
     57      * Runs the test case.
     58      *
     59      * @throws Throwable
     60      *             Any uncaught exception causes test to fail
     61      */
     62     public void testCreateAttributeNS1() throws Throwable {
     63         String namespaceURI = "http://www.ecommerce.org/";
     64         String malformedName = "prefix::local";
     65         Document doc;
     66 
     67         doc = (Document) load("staffNS", builder);
     68 
     69         {
     70             boolean success = false;
     71             try {
     72                 doc.createAttributeNS(namespaceURI, malformedName);
     73             } catch (DOMException ex) {
     74                 success = (ex.code == DOMException.NAMESPACE_ERR);
     75             }
     76             assertTrue("throw_NAMESPACE_ERR", success);
     77         }
     78     }
     79     public void testCreateAttributeNS2() throws Throwable {
     80         String namespaceURI = null;
     81 
     82         String qualifiedName = "prefix:local";
     83         Document doc;
     84 
     85         doc = (Document) load("staffNS", builder);
     86 
     87         {
     88             boolean success = false;
     89             try {
     90                 doc.createAttributeNS(namespaceURI, qualifiedName);
     91             } catch (DOMException ex) {
     92                 success = (ex.code == DOMException.NAMESPACE_ERR);
     93             }
     94             assertTrue("throw_NAMESPACE_ERR", success);
     95         }
     96     }
     97     public void testCreateAttributeNS3() throws Throwable {
     98         String namespaceURI = "http://www.wedding.com/";
     99         String qualifiedName;
    100         Document doc;
    101 
    102         List<String> illegalQNames = new ArrayList<String>();
    103         illegalQNames.add("person:{");
    104         illegalQNames.add("person:}");
    105         illegalQNames.add("person:~");
    106         illegalQNames.add("person:'");
    107         illegalQNames.add("person:!");
    108         illegalQNames.add("person:@");
    109         illegalQNames.add("person:#");
    110         illegalQNames.add("person:$");
    111         illegalQNames.add("person:%");
    112         illegalQNames.add("person:^");
    113         illegalQNames.add("person:&");
    114         illegalQNames.add("person:*");
    115         illegalQNames.add("person:(");
    116         illegalQNames.add("person:)");
    117         illegalQNames.add("person:+");
    118         illegalQNames.add("person:=");
    119         illegalQNames.add("person:[");
    120         illegalQNames.add("person:]");
    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:a ");
    129         illegalQNames.add("person:\"");
    130 
    131         doc = (Document) load("staffNS", builder);
    132         for (int indexN10090 = 0; indexN10090 < illegalQNames.size(); indexN10090++) {
    133             qualifiedName = (String) illegalQNames.get(indexN10090);
    134             {
    135                 boolean success = false;
    136                 try {
    137                     doc.createAttributeNS(namespaceURI, qualifiedName);
    138                 } catch (DOMException ex) {
    139                     success = (ex.code == DOMException.INVALID_CHARACTER_ERR);
    140                 }
    141                 assertTrue("throw_INVALID_CHARACTER_ERR", success);
    142             }
    143         }
    144     }
    145     public void testCreateAttributeNS4() throws Throwable {
    146         String namespaceURI = "http://www.w3.org/XML/1998/namespaces";
    147         String qualifiedName = "xml:attr1";
    148         Document doc;
    149 
    150         doc = (Document) load("staffNS", builder);
    151 
    152         {
    153             boolean success = false;
    154             try {
    155                 doc.createAttributeNS(namespaceURI, qualifiedName);
    156             } catch (DOMException ex) {
    157                 success = (ex.code == DOMException.NAMESPACE_ERR);
    158             }
    159             assertTrue("throw_NAMESPACE_ERR", success);
    160         }
    161     }
    162     public void testCreateAttributeNS5() throws Throwable {
    163         String namespaceURI = "http://www.ecommerce.org/";
    164         String qualifiedName = "econm:local";
    165         Document doc;
    166         Attr newAttr;
    167         String attrName;
    168         doc = (Document) load("staffNS", builder);
    169         newAttr = doc.createAttributeNS(namespaceURI, qualifiedName);
    170         attrName = newAttr.getName();
    171         assertEquals("throw_Equals", qualifiedName, attrName);
    172     }
    173     public void testCreateAttributeNS6() throws Throwable {
    174         String namespaceURI = "http://www.example.com/";
    175         Document doc;
    176 
    177         doc = (Document) load("hc_staff", builder);
    178 
    179         // BEGIN android-changed
    180         //     Our exception priorities differ from the spec
    181         try {
    182             doc.createAttributeNS(namespaceURI, "");
    183             fail();
    184         } catch (DOMException ex) {
    185         }
    186         // END android-changed
    187     }
    188 }
    189