Path: blob/master/test/jdk/javax/imageio/metadata/SetAttributeNode.java
41152 views
/*1* Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 450725626* @run main SetAttributeNode27* @summary Tests the functionality of IIOMetadataNode.setAttributeNode().28* Four separate tests are involved:29* 1) Tests whether a DOMException.INUSE_ATTRIBUTE_ERR is thrown if newAttr30* is already an attribute of another Element object.31* 2) Tests whether setAttributeNode() returns the old attribute if it is32* replaced.33* 3) Tests whether setAttributeNode() returns null if the new attribute is34* not replacing an existing attribute.35* 4) Tests whether the new attribute successfully replaces an existing one.36*/3738import javax.imageio.metadata.IIOMetadataNode;39import org.w3c.dom.Attr;40import org.w3c.dom.DOMException;41import org.w3c.dom.Element;42import org.w3c.dom.TypeInfo;4344public class SetAttributeNode {4546public static void test1() {47IIOMetadataNode parent = new IIOMetadataNode("parent");48IIOMetadataNode elem = new IIOMetadataNode("elem");4950MyAttrNode attrNode = new MyAttrNode("name", "value");51elem.setAttributeNode(attrNode);52attrNode.setOwnerElement(elem);5354try {55parent.setAttributeNode(attrNode);56} catch (DOMException e) {57if (e.code != DOMException.INUSE_ATTRIBUTE_ERR) {58throw new RuntimeException("Test 1 failed: " +59"Invalid exception code: " +60e.code);61}62return;63}6465throw new RuntimeException("Test 1 failed: DOMException not thrown");66}6768public static void test2() {69String name = "attr";70String oldValue = "old value";71String newValue = "new value";72Attr retAttr;7374IIOMetadataNode parent = new IIOMetadataNode("parent");75MyAttrNode attrNode1 = new MyAttrNode(name, oldValue);76MyAttrNode attrNode2 = new MyAttrNode(name, newValue);7778retAttr = parent.setAttributeNode(attrNode1);79retAttr = parent.setAttributeNode(attrNode2);8081String actName = retAttr.getNodeName();82String actValue = retAttr.getValue();8384if (!actName.equals(name) || !actValue.equals(oldValue)) {85throw new RuntimeException("Test 2 failed: Invalid attribute " +86"returned: " +87"(name: " + actName +88", value: " + actValue + ")");89}90}9192public static void test3() {93IIOMetadataNode parent = new IIOMetadataNode("parent");94MyAttrNode attrNode = new MyAttrNode("name", "value");95Attr retAttr = parent.setAttributeNode(attrNode);9697if (retAttr != null) {98throw new RuntimeException("Test 3 failed: Return value is " +99"non-null");100}101}102103public static void test4() {104String name = "name";105String correctValue = "correct value";106String wrongValue = "wrong value";107108IIOMetadataNode parent = new IIOMetadataNode("parent");109MyAttrNode attrNode1 = new MyAttrNode(name, wrongValue);110MyAttrNode attrNode2 = new MyAttrNode(name, correctValue);111112parent.setAttributeNode(attrNode1);113parent.setAttributeNode(attrNode2);114115Attr actAttr = parent.getAttributeNode(name);116String actValue = actAttr.getValue();117118if (!actValue.equals(correctValue)) {119throw new RuntimeException("Test 4 failed: Return value is: " +120actValue);121}122}123124public static void main(String[] args) {125test1();126test2();127test3();128test4();129}130}131132class MyAttrNode extends IIOMetadataNode implements Attr {133134private Element owner;135private String name;136private String value;137138public MyAttrNode(String name, String value) {139this.name = name;140this.value = value;141}142143public Element getOwnerElement() {144return owner;145}146147public void setOwnerElement(Element owner) {148this.owner = owner;149}150151public String getName() {152return name;153}154155public String getValue() {156return value;157}158159public void setValue(String value) {160this.value = value;161}162163public boolean getSpecified() {164return false;165}166167public TypeInfo getSchemaTypeInfo() {168return null;169}170171public boolean isId() {172return false;173}174}175176177