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 Element.getElementsByTagName("*") is not empty
  28  *          for valid image.
  29  * @run     main GetElementsByTagNameTest
  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 
  44 public class GetElementsByTagNameTest {
  45 
  46     public static void main(String[] args) throws IOException {
  47         // Generate some trivial image and save it to a temporary array
  48         ByteArrayOutputStream tmp = new ByteArrayOutputStream();
  49         ImageIO.write(new BufferedImage(1, 1, BufferedImage.TYPE_INT_RGB),
  50                 "gif", tmp);
  51 
  52         // Read the stream
  53         ImageInputStream in = new MemoryCacheImageInputStream(
  54                 new ByteArrayInputStream(tmp.toByteArray()));
  55         ImageReader reader = ImageIO.getImageReaders(in).next();
  56         reader.setInput(in);
  57 
  58         // Retrieve standard image metadata tree
  59         IIOMetadata meta = reader.getImageMetadata(0);
  60         if (meta == null || !meta.isStandardMetadataFormatSupported()) {
  61             throw new Error("Test failure: Missing metadata");
  62         }
  63         Element root = (Element) meta.
  64                 getAsTree(IIOMetadataFormatImpl.standardMetadataFormatName);
  65 
  66         // Test getElementsByTagName("*")
  67         if (root.getElementsByTagName("*").getLength() == 0) {
  68             throw new RuntimeException("getElementsByTagName(\"*\") returns"
  69                     + " nothing");
  70         }
  71     }
  72 }
  73