1 /* 2 * Copyright (c) 2012, 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 /* 25 * @test 26 * @bug 4507256 27 * @run main SetAttributeNode 28 * @summary Tests the functionality of IIOMetadataNode.setAttributeNode(). 29 * Four separate tests are involved: 30 * 1) Tests whether a DOMException.INUSE_ATTRIBUTE_ERR is thrown if newAttr 31 * is already an attribute of another Element object. 32 * 2) Tests whether setAttributeNode() returns the old attribute if it is 33 * replaced. 34 * 3) Tests whether setAttributeNode() returns null if the new attribute is 35 * not replacing an existing attribute. 36 * 4) Tests whether the new attribute successfully replaces an existing one. 37 */ 38 39 import javax.imageio.metadata.IIOMetadataNode; 40 import org.w3c.dom.Attr; 41 import org.w3c.dom.DOMException; 42 import org.w3c.dom.Element; 43 import org.w3c.dom.TypeInfo; 44 45 public class SetAttributeNode { 46 test1()47 public static void test1() { 48 IIOMetadataNode parent = new IIOMetadataNode("parent"); 49 IIOMetadataNode elem = new IIOMetadataNode("elem"); 50 51 MyAttrNode attrNode = new MyAttrNode("name", "value"); 52 elem.setAttributeNode(attrNode); 53 attrNode.setOwnerElement(elem); 54 55 try { 56 parent.setAttributeNode(attrNode); 57 } catch (DOMException e) { 58 if (e.code != DOMException.INUSE_ATTRIBUTE_ERR) { 59 throw new RuntimeException("Test 1 failed: " + 60 "Invalid exception code: " + 61 e.code); 62 } 63 return; 64 } 65 66 throw new RuntimeException("Test 1 failed: DOMException not thrown"); 67 } 68 test2()69 public static void test2() { 70 String name = "attr"; 71 String oldValue = "old value"; 72 String newValue = "new value"; 73 Attr retAttr; 74 75 IIOMetadataNode parent = new IIOMetadataNode("parent"); 76 MyAttrNode attrNode1 = new MyAttrNode(name, oldValue); 77 MyAttrNode attrNode2 = new MyAttrNode(name, newValue); 78 79 retAttr = parent.setAttributeNode(attrNode1); 80 retAttr = parent.setAttributeNode(attrNode2); 81 82 String actName = retAttr.getNodeName(); 83 String actValue = retAttr.getValue(); 84 85 if (!actName.equals(name) || !actValue.equals(oldValue)) { 86 throw new RuntimeException("Test 2 failed: Invalid attribute " + 87 "returned: " + 88 "(name: " + actName + 89 ", value: " + actValue + ")"); 90 } 91 } 92 test3()93 public static void test3() { 94 IIOMetadataNode parent = new IIOMetadataNode("parent"); 95 MyAttrNode attrNode = new MyAttrNode("name", "value"); 96 Attr retAttr = parent.setAttributeNode(attrNode); 97 98 if (retAttr != null) { 99 throw new RuntimeException("Test 3 failed: Return value is " + 100 "non-null"); 101 } 102 } 103 test4()104 public static void test4() { 105 String name = "name"; 106 String correctValue = "correct value"; 107 String wrongValue = "wrong value"; 108 109 IIOMetadataNode parent = new IIOMetadataNode("parent"); 110 MyAttrNode attrNode1 = new MyAttrNode(name, wrongValue); 111 MyAttrNode attrNode2 = new MyAttrNode(name, correctValue); 112 113 parent.setAttributeNode(attrNode1); 114 parent.setAttributeNode(attrNode2); 115 116 Attr actAttr = parent.getAttributeNode(name); 117 String actValue = actAttr.getValue(); 118 119 if (!actValue.equals(correctValue)) { 120 throw new RuntimeException("Test 4 failed: Return value is: " + 121 actValue); 122 } 123 } 124 main(String[] args)125 public static void main(String[] args) { 126 test1(); 127 test2(); 128 test3(); 129 test4(); 130 } 131 } 132 133 class MyAttrNode extends IIOMetadataNode implements Attr { 134 135 private Element owner; 136 private String name; 137 private String value; 138 MyAttrNode(String name, String value)139 public MyAttrNode(String name, String value) { 140 this.name = name; 141 this.value = value; 142 } 143 getOwnerElement()144 public Element getOwnerElement() { 145 return owner; 146 } 147 setOwnerElement(Element owner)148 public void setOwnerElement(Element owner) { 149 this.owner = owner; 150 } 151 getName()152 public String getName() { 153 return name; 154 } 155 getValue()156 public String getValue() { 157 return value; 158 } 159 setValue(String value)160 public void setValue(String value) { 161 this.value = value; 162 } 163 getSpecified()164 public boolean getSpecified() { 165 return false; 166 } 167 getSchemaTypeInfo()168 public TypeInfo getSchemaTypeInfo() { 169 return null; 170 } 171 isId()172 public boolean isId() { 173 return false; 174 } 175 } 176