1 /*
   2  * Copyright (c) 2003, 2016, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 package org.w3c.dom.ptests;
  25 
  26 import static org.testng.Assert.assertEquals;
  27 import static org.testng.Assert.assertFalse;
  28 import static org.testng.Assert.assertNull;
  29 import static org.testng.Assert.assertTrue;
  30 import static org.w3c.dom.ptests.DOMTestUtil.createDOM;
  31 
  32 import org.testng.annotations.Listeners;
  33 import org.testng.annotations.Test;
  34 import org.w3c.dom.Attr;
  35 import org.w3c.dom.Document;
  36 import org.w3c.dom.Element;
  37 import org.w3c.dom.NamedNodeMap;
  38 
  39 
  40 /*
  41  * @summary Test for the Attr Interface
  42  */
  43 @Listeners({jaxp.library.FilePolicy.class})
  44 public class AttrTest {
  45     /*
  46      * Verify getName method against both existing Attr and new Attr.
  47      */
  48     @Test
  49     public void testGetName() throws Exception {
  50         Document document = createDOM("Attr01.xml");
  51         //test a new created Attr
  52         Attr attr = document.createAttribute("newAttribute");
  53         assertEquals(attr.getName(), "newAttribute");
  54 
  55         //test a Attr loaded from xml file
  56         Element elemNode = (Element) document.getElementsByTagName("book").item(1);
  57         Attr attr2 = (Attr) elemNode.getAttributes().item(0);
  58         assertEquals(attr2.getName(), "category1");
  59     }
  60 
  61     /*
  62      * Verify getOwnerElement method against both existing Attr and new Attr.
  63      */
  64     @Test
  65     public void testGetOwnerElement() throws Exception {
  66         Document document = createDOM("Attr01.xml");
  67 
  68         //test Attr loaded from xml file
  69         Element elemNode = (Element) document.getElementsByTagName("book").item(1);
  70         NamedNodeMap nnMap = elemNode.getAttributes();
  71         for (int i = 0; i < nnMap.getLength(); i++) {
  72             Attr attr = (Attr) nnMap.item(i);
  73             assertEquals(attr.getOwnerElement().getNodeName(), "book");
  74         }
  75 
  76         //test an Attr without owner node
  77         Attr attr = document.createAttribute("newAttribute");
  78         assertNull(attr.getOwnerElement());
  79 
  80     }
  81 
  82     /*
  83      * Verify getSpecified method works as the spec.
  84      */
  85     @Test
  86     public void testGetSpecified1() throws Exception {
  87         Document document = createDOM("Attr01.xml");
  88 
  89         Element elemNode = (Element) document.getElementsByTagName("book").item(1);
  90         Attr attr = elemNode.getAttributeNode("category1");
  91         assertTrue(attr.getSpecified());
  92 
  93     }
  94 
  95     /*
  96      * In this xml file, the dtd has the value for the attrribute, but the xml
  97      * element does not specify the value for the attrribute, as per the spec it
  98      * should return false.
  99      */
 100     @Test
 101     public void testGetSpecified2() throws Exception {
 102 
 103         Document document = createDOM("Attr2.xml");
 104         Element elemNode = (Element) document.getElementsByTagName("Name").item(0);
 105         Attr attr = elemNode.getAttributeNode("type");
 106 
 107         assertFalse(attr.getSpecified());
 108     }
 109 
 110     /*
 111      * Creating a new attribute, the owner element is null since the attribute
 112      * has just been created, getSpecified should return true.
 113      */
 114     @Test
 115     public void testNewCreatedAttribute() throws Exception {
 116         Document document = createDOM("Attr01.xml");
 117         Attr attr = document.createAttribute("newAttribute");
 118         assertTrue(attr.getSpecified());
 119         assertNull(attr.getOwnerElement());
 120 
 121     }
 122 
 123     /*
 124      * The xml file includes the dtd having the IMPLIED value for the attrribute
 125      * and the xml element does not specify the value. As per the spec it should
 126      * not be seen as a part of the structure model hence getAttributeNode
 127      * rerurn null if the attribute is even found.
 128      */
 129     @Test
 130     public void testIMPLIEDAttribute() throws Exception {
 131         Document document = createDOM("Attr3.xml");
 132         Element elemNode = (Element) document.getElementsByTagName("Name").item(0);
 133         Attr attr = elemNode.getAttributeNode("type");
 134         assertNull(attr);
 135     }
 136 
 137     /*
 138      * Test setValue method and verify by getValue method.
 139      */
 140     @Test
 141     public void testSetValue() throws Exception {
 142         Document document = createDOM("Attr01.xml");
 143         Attr attr = document.createAttribute("newAttribute");
 144         attr.setValue("newVal");
 145         assertEquals(attr.getValue(), "newVal");
 146 
 147     }
 148 
 149 }