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.Attr;
     29 import org.w3c.dom.DOMException;
     30 import org.w3c.dom.DocumentType;
     31 import org.w3c.dom.DOMImplementation;
     32 
     33 import javax.xml.parsers.DocumentBuilder;
     34 
     35 /**
     36  * The method createAttributeNS creates an attribute of the given qualified name
     37  * and namespace URI
     38  *
     39  * Invoke the createAttributeNS method on this Document object with a null
     40  * namespaceURI, and a qualifiedName without a prefix. This should return a
     41  * valid Attr node object.
     42  *
     43  * @author IBM
     44  * @author Neil Delima
     45  * @see <a
     46  *      href="http://www.w3.org/TR/DOM-Level-2-Core/core">http://www.w3.org/TR/DOM-Level-2-Core/core</a>
     47  * @see <a
     48  *      href="http://www.w3.org/TR/DOM-Level-2-Core/core#ID-DocCrAttrNS">http://www.w3.org/TR/DOM-Level-2-Core/core#ID-DocCrAttrNS</a>
     49  */
     50 public final class DocumentCreateAttributeNS extends DOMTestCase {
     51 
     52     DOMDocumentBuilderFactory factory;
     53 
     54     DocumentBuilder builder;
     55 
     56     protected void setUp() throws Exception {
     57         super.setUp();
     58         try {
     59             factory = new DOMDocumentBuilderFactory(DOMDocumentBuilderFactory
     60                     .getConfiguration1());
     61             builder = factory.getBuilder();
     62         } catch (Exception e) {
     63             fail("Unexpected exception" + e.getMessage());
     64         }
     65     }
     66 
     67     protected void tearDown() throws Exception {
     68         factory = null;
     69         builder = null;
     70         super.tearDown();
     71     }
     72 
     73     /**
     74      * Runs the test case.
     75      *
     76      * @throws Throwable
     77      *             Any uncaught exception causes test to fail
     78      */
     79     public void testCreateAttributeNS1() throws Throwable {
     80         Document doc;
     81         Attr attribute;
     82         String namespaceURI = null;
     83 
     84         String qualifiedName = "test";
     85 
     86         String nodeName;
     87 
     88         doc = (Document) load("staffNS", builder);
     89         attribute = doc.createAttributeNS(namespaceURI, qualifiedName);
     90         nodeName = attribute.getNodeName();
     91 
     92         assertEquals("documentcreateattributeNS01", "test", nodeName);
     93     }
     94 
     95     /**
     96      * Runs the test case.
     97      *
     98      * @throws Throwable
     99      *             Any uncaught exception causes test to fail
    100      */
    101     public void testCreateAttributeNS2() throws Throwable {
    102         Document doc;
    103         Attr attribute1;
    104         Attr attribute2;
    105         String name;
    106         String nodeName;
    107         String nodeValue;
    108         String prefix;
    109         String namespaceURI;
    110         doc = (Document) load("staffNS", builder);
    111         attribute1 = doc.createAttributeNS(
    112                 "http://www.w3.org/XML/1998/namespace", "xml:xml");
    113         name = attribute1.getName();
    114         nodeName = attribute1.getNodeName();
    115         nodeValue = attribute1.getNodeValue();
    116         prefix = attribute1.getPrefix();
    117         namespaceURI = attribute1.getNamespaceURI();
    118         assertEquals("documentcreateattributeNS02_att1_name", "xml:xml", name);
    119         assertEquals("documentcreateattributeNS02_att1_nodeName", "xml:xml",
    120                 nodeName);
    121         assertEquals("documentcreateattributeNS02_att1_nodeValue", "",
    122                 nodeValue);
    123         assertEquals("documentcreateattributeNS02_att1_prefix", "xml", prefix);
    124         assertEquals("documentcreateattributeNS02_att1_namespaceURI",
    125                 "http://www.w3.org/XML/1998/namespace", namespaceURI);
    126         attribute2 = doc.createAttributeNS("http://www.w3.org/2000/xmlns/",
    127                 "xmlns");
    128         name = attribute2.getName();
    129         nodeName = attribute2.getNodeName();
    130         nodeValue = attribute2.getNodeValue();
    131         prefix = attribute2.getPrefix();
    132         namespaceURI = attribute2.getNamespaceURI();
    133         assertEquals("documentcreateattributeNS02_att2_name", "xmlns", name);
    134         assertEquals("documentcreateattributeNS02_att2_nodeName", "xmlns",
    135                 nodeName);
    136         assertEquals("documentcreateattributeNS02_att2_nodeValue", "",
    137                 nodeValue);
    138         assertEquals("documentcreateattributeNS02_att2_namespaceURI",
    139                 "http://www.w3.org/2000/xmlns/", namespaceURI);
    140     }
    141     public void testCreateAttributeNS3() throws Throwable {
    142         Document doc;
    143 
    144         String namespaceURI = "http://www.w3.org/DOM/Test/Level2";
    145         String qualifiedName;
    146         List<String> qualifiedNames = new ArrayList<String>();
    147         qualifiedNames.add("/");
    148         qualifiedNames.add("//");
    149         qualifiedNames.add("\\");
    150         qualifiedNames.add(";");
    151         qualifiedNames.add("&");
    152         qualifiedNames.add("*");
    153         qualifiedNames.add("]]");
    154         qualifiedNames.add(">");
    155         qualifiedNames.add("<");
    156 
    157         doc = (Document) load("staffNS", builder);
    158         for (int indexN1005A = 0; indexN1005A < qualifiedNames.size(); indexN1005A++) {
    159             qualifiedName = (String) qualifiedNames.get(indexN1005A);
    160 
    161             {
    162                 boolean success = false;
    163                 try {
    164                     doc.createAttributeNS(namespaceURI, qualifiedName);
    165                 } catch (DOMException ex) {
    166                     success = (ex.code == DOMException.INVALID_CHARACTER_ERR);
    167                 }
    168                 assertTrue("documentcreateattributeNS03", success);
    169             }
    170         }
    171     }
    172     public void testCreateAttributeNS4() throws Throwable {
    173         Document doc;
    174 
    175         String namespaceURI = "http://www.w3.org/DOM/Test/Level2";
    176         String qualifiedName;
    177         List<String> qualifiedNames = new ArrayList<String>();
    178         qualifiedNames.add("_:");
    179         qualifiedNames.add(":0a");
    180         qualifiedNames.add(":");
    181         qualifiedNames.add("a:b:c");
    182         qualifiedNames.add("_::a");
    183 
    184         doc = (Document) load("staffNS", builder);
    185         for (int indexN1004E = 0; indexN1004E < qualifiedNames.size(); indexN1004E++) {
    186             qualifiedName = (String) qualifiedNames.get(indexN1004E);
    187 
    188             {
    189 
    190                 // BEGIN android-changed
    191                 //     Our exception priorities differ from the spec
    192                 try {
    193                     doc.createAttributeNS(namespaceURI, qualifiedName);
    194                     fail();
    195                 } catch (DOMException ex) {
    196                 }
    197                 // END android-changed
    198             }
    199         }
    200     }
    201     public void testCreateAttributeNS5() throws Throwable {
    202         Document doc;
    203         Document newDoc;
    204         DocumentType docType = null;
    205 
    206         DOMImplementation domImpl;
    207 
    208         String namespaceURI = null;
    209 
    210         String qualifiedName = "abc:def";
    211         doc = (Document) load("staffNS", builder);
    212         domImpl = doc.getImplementation();
    213         newDoc = domImpl.createDocument("http://www.w3.org/DOM/Test",
    214                 "dom:doc", docType);
    215 
    216         {
    217             boolean success = false;
    218             try {
    219                 newDoc.createAttributeNS(namespaceURI, qualifiedName);
    220             } catch (DOMException ex) {
    221                 success = (ex.code == DOMException.NAMESPACE_ERR);
    222             }
    223             assertTrue("documentcreateattributeNS05", success);
    224         }
    225     }
    226     public void testCreateAttributeNS6() throws Throwable {
    227         Document doc;
    228         Document newDoc;
    229         DocumentType docType = null;
    230 
    231         DOMImplementation domImpl;
    232 
    233         String namespaceURI = "http://www.w3.org/XML/1998 /namespace";
    234         String qualifiedName = "xml:root";
    235         doc = (Document) load("staffNS", builder);
    236         domImpl = doc.getImplementation();
    237         newDoc = domImpl.createDocument("http://www.w3.org/DOM/Test",
    238                 "dom:doc", docType);
    239 
    240         {
    241             boolean success = false;
    242             try {
    243                 newDoc.createAttributeNS(namespaceURI, qualifiedName);
    244             } catch (DOMException ex) {
    245                 success = (ex.code == DOMException.NAMESPACE_ERR);
    246             }
    247             assertTrue("documentcreateattributeNS06", success);
    248         }
    249     }
    250     public void testCreateAttributeNS7() throws Throwable {
    251         Document doc;
    252 
    253         String namespaceURI = "http://www.W3.org/2000/xmlns";
    254         String qualifiedName = "xmlns";
    255         doc = (Document) load("staffNS", builder);
    256 
    257         {
    258             boolean success = false;
    259             try {
    260                 doc.createAttributeNS(namespaceURI, qualifiedName);
    261             } catch (DOMException ex) {
    262                 success = (ex.code == DOMException.NAMESPACE_ERR);
    263             }
    264             assertTrue("documentcreateattributeNS07", success);
    265         }
    266     }
    267 }
    268