Path: blob/master/test/jdk/javax/imageio/metadata/NthItemNodeListTest.java
41152 views
/*1* Copyright (c) 2016, 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 816728126* @summary Test verifies that accessing nth item in NodeList doesn't throw27* IndexOutOfBoundsException.28* @run main NthItemNodeListTest29*/3031import java.awt.image.BufferedImage;32import java.io.ByteArrayInputStream;33import java.io.ByteArrayOutputStream;34import java.io.IOException;35import javax.imageio.ImageIO;36import javax.imageio.ImageReader;37import javax.imageio.metadata.IIOMetadata;38import javax.imageio.metadata.IIOMetadataFormatImpl;39import javax.imageio.stream.ImageInputStream;40import javax.imageio.stream.MemoryCacheImageInputStream;41import org.w3c.dom.Element;42import org.w3c.dom.Node;43import org.w3c.dom.NodeList;4445public class NthItemNodeListTest {4647public static void main(String[] args) throws IOException {48// Generate some trivial image and save it to a temporary array49ByteArrayOutputStream tmp = new ByteArrayOutputStream();50ImageIO.write(new BufferedImage(1, 1, BufferedImage.TYPE_INT_RGB),51"gif", tmp);5253// Read it back in54ImageInputStream in = new MemoryCacheImageInputStream(55new ByteArrayInputStream(tmp.toByteArray()));56ImageReader reader = ImageIO.getImageReaders(in).next();57reader.setInput(in);5859// Retrieve standard image metadata tree60IIOMetadata meta = reader.getImageMetadata(0);61if (meta == null || !meta.isStandardMetadataFormatSupported()) {62throw new Error("Test failure: Missing metadata");63}64Element root = (Element) meta.65getAsTree(IIOMetadataFormatImpl.standardMetadataFormatName);6667NodeList nodeList = root.68getElementsByTagName(root.getFirstChild().getNodeName());69/*70* Accessing the nth node should return null and not throw71* IndexOutOfBoundsException.72*/73Node n = (nodeList.item(nodeList.getLength()));74}75}76777879