Home | History | Annotate | Download | only in dom
      1 package tests.org.w3c.dom;
      2 
      3 import dalvik.annotation.TestTargets;
      4 import dalvik.annotation.TestLevel;
      5 import dalvik.annotation.TestTargetNew;
      6 import dalvik.annotation.TestTargetClass;
      7 
      8 import org.w3c.dom.Document;
      9 import org.w3c.dom.Element;
     10 import org.w3c.dom.DOMException;
     11 import org.w3c.dom.DocumentType;
     12 import org.w3c.dom.DOMImplementation;
     13 
     14 import javax.xml.parsers.DocumentBuilder;
     15 
     16 /**
     17  * The method createElementNS creates an element of the given valid
     18  * qualifiedName and NamespaceURI.
     19  *
     20  * Invoke the createElementNS method on this Document object with a valid
     21  * namespaceURI and qualifiedName. Check if a valid Element object is returned
     22  * with the same node attributes.
     23  *
     24  * @author IBM
     25  * @author Neil Delima
     26  * @see <a
     27  *      href="http://www.w3.org/TR/DOM-Level-2-Core/core">http://www.w3.org/TR/DOM-Level-2-Core/core</a>
     28  * @see <a
     29  *      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>
     30  */
     31 @TestTargetClass(Document.class)
     32 public final class DocumentCreateElementNS extends DOMTestCase {
     33 
     34     DOMDocumentBuilderFactory factory;
     35 
     36     DocumentBuilder builder;
     37 
     38     protected void setUp() throws Exception {
     39         super.setUp();
     40         try {
     41             factory = new DOMDocumentBuilderFactory(DOMDocumentBuilderFactory
     42                     .getConfiguration1());
     43             builder = factory.getBuilder();
     44         } catch (Exception e) {
     45             fail("Unexpected exception" + e.getMessage());
     46         }
     47     }
     48 
     49     protected void tearDown() throws Exception {
     50         factory = null;
     51         builder = null;
     52         super.tearDown();
     53     }
     54 
     55     /**
     56      * Runs the test case.
     57      *
     58      * @throws Throwable
     59      *             Any uncaught exception causes test to fail
     60      */
     61     @TestTargetNew(
     62         level = TestLevel.PARTIAL_COMPLETE,
     63         notes = "Verifies positive functionality.",
     64         method = "createElementNS",
     65         args = {java.lang.String.class, java.lang.String.class}
     66     )
     67     public void testCreateElementNS1() throws Throwable {
     68         Document doc;
     69         Element element;
     70         String namespaceURI = "http://www.w3.org/DOM/Test/level2";
     71         String qualifiedName = "XML:XML";
     72         String nodeName;
     73         String nsURI;
     74         String localName;
     75         String prefix;
     76         String tagName;
     77         doc = (Document) load("staffNS", builder);
     78         element = doc.createElementNS(namespaceURI, qualifiedName);
     79         nodeName = element.getNodeName();
     80         nsURI = element.getNamespaceURI();
     81         localName = element.getLocalName();
     82         prefix = element.getPrefix();
     83         tagName = element.getTagName();
     84         assertEquals("documentcreateelementNS01_nodeName", "XML:XML", nodeName);
     85         assertEquals("documentcreateelementNS01_namespaceURI",
     86                 "http://www.w3.org/DOM/Test/level2", nsURI);
     87         assertEquals("documentcreateelementNS01_localName", "XML", localName);
     88         assertEquals("documentcreateelementNS01_prefix", "XML", prefix);
     89         assertEquals("documentcreateelementNS01_tagName", "XML:XML", tagName);
     90     }
     91     @TestTargetNew(
     92         level = TestLevel.PARTIAL_COMPLETE,
     93         notes = "Verifies that createElementNS throws DOMException with INVALID_CHARACTER_ERR code.",
     94         method = "createElementNS",
     95         args = {java.lang.String.class, java.lang.String.class}
     96     )
     97     public void testCreateElementNS2() throws Throwable {
     98         Document doc;
     99 
    100         String namespaceURI = null;
    101 
    102         String qualifiedName = "^^";
    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.INVALID_CHARACTER_ERR);
    111             }
    112             assertTrue("documentcreateelementNS02", success);
    113         }
    114     }
    115     @TestTargetNew(
    116         level = TestLevel.PARTIAL_COMPLETE,
    117         notes = "Verifies that createElementNS throws DOMException with NAMESPACE_ERR code.",
    118         method = "createElementNS",
    119         args = {java.lang.String.class, java.lang.String.class}
    120     )
    121     public void testCreateElementNS5() throws Throwable {
    122         Document doc;
    123 
    124         String namespaceURI = null;
    125 
    126         String qualifiedName = "null:xml";
    127         doc = (Document) load("staffNS", builder);
    128 
    129         {
    130             boolean success = false;
    131             try {
    132                 doc.createElementNS(namespaceURI, qualifiedName);
    133             } catch (DOMException ex) {
    134                 success = (ex.code == DOMException.NAMESPACE_ERR);
    135             }
    136             assertTrue("documentcreateelementNS05", success);
    137         }
    138     }
    139     @TestTargetNew(
    140         level = TestLevel.PARTIAL_COMPLETE,
    141         notes = "Verifies that createElementNS throws DOMException with NAMESPACE_ERR code.",
    142         method = "createElementNS",
    143         args = {java.lang.String.class, java.lang.String.class}
    144     )
    145     public void testCreateElementNS6() throws Throwable {
    146         Document doc;
    147         Document newDoc;
    148         DocumentType docType = null;
    149 
    150         DOMImplementation domImpl;
    151 
    152         String namespaceURI = "http://www.w3.org/xml/1998/namespace ";
    153         String qualifiedName = "xml:root";
    154         doc = (Document) load("staffNS", builder);
    155         domImpl = doc.getImplementation();
    156         newDoc = domImpl.createDocument("http://www.w3.org/DOM/Test",
    157                 "dom:doc", docType);
    158 
    159         {
    160             boolean success = false;
    161             try {
    162                 newDoc.createElementNS(namespaceURI, qualifiedName);
    163             } catch (DOMException ex) {
    164                 success = (ex.code == DOMException.NAMESPACE_ERR);
    165             }
    166             assertTrue("documentcreateelementNS06", success);
    167         }
    168     }
    169 }
    170