1 /*
   2  * Copyright (c) 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 /*
  25  * @test
  26  * @bug     8167281
  27  * @summary Test verifies that accessing nth item in NodeList doesn't throw
  28  *          IndexOutOfBoundsException.
  29  * @run     main NthItemNodeListTest
  30  */
  31 
  32 import java.awt.image.BufferedImage;
  33 import java.io.ByteArrayInputStream;
  34 import java.io.ByteArrayOutputStream;
  35 import java.io.IOException;
  36 import javax.imageio.ImageIO;
  37 import javax.imageio.ImageReader;
  38 import javax.imageio.metadata.IIOMetadata;
  39 import javax.imageio.metadata.IIOMetadataFormatImpl;
  40 import javax.imageio.stream.ImageInputStream;
  41 import javax.imageio.stream.MemoryCacheImageInputStream;
  42 import org.w3c.dom.Element;
  43 import org.w3c.dom.Node;
  44 import org.w3c.dom.NodeList;
  45 
  46 public class NthItemNodeListTest {
  47 
  48     public static void main(String[] args) throws IOException {
  49         // Generate some trivial image and save it to a temporary array
  50         ByteArrayOutputStream tmp = new ByteArrayOutputStream();
  51         ImageIO.write(new BufferedImage(1, 1, BufferedImage.TYPE_INT_RGB),
  52                 "gif", tmp);
  53 
  54         // Read it back in
  55         ImageInputStream in = new MemoryCacheImageInputStream(
  56                 new ByteArrayInputStream(tmp.toByteArray()));
  57         ImageReader reader = ImageIO.getImageReaders(in).next();
  58         reader.setInput(in);
  59 
  60         // Retrieve standard image metadata tree
  61         IIOMetadata meta = reader.getImageMetadata(0);
  62         if (meta == null || !meta.isStandardMetadataFormatSupported()) {
  63             throw new Error("Test failure: Missing metadata");
  64         }
  65         Element root = (Element) meta.
  66                 getAsTree(IIOMetadataFormatImpl.standardMetadataFormatName);
  67 
  68         NodeList nodeList = root.
  69                 getElementsByTagName(root.getFirstChild().getNodeName());
  70         /*
  71          * Accessing the nth node should return null and not throw
  72          * IndexOutOfBoundsException.
  73          */
  74         Node n = (nodeList.item(nodeList.getLength()));
  75     }
  76 }
  77