test/jdk/javax/imageio/metadata/NthItemNodeListTest.java
author erikj
Tue, 12 Sep 2017 19:03:39 +0200
changeset 47216 71c04702a3d5
parent 41794 jdk/test/javax/imageio/metadata/NthItemNodeListTest.java@e30c6574b042
permissions -rw-r--r--
8187443: Forest Consolidation: Move files to unified layout Reviewed-by: darcy, ihse
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
41794
e30c6574b042 8167281: IIOMetadataNode bugs in getElementsByTagName and NodeList.item methods
jdv
parents:
diff changeset
     1
/*
e30c6574b042 8167281: IIOMetadataNode bugs in getElementsByTagName and NodeList.item methods
jdv
parents:
diff changeset
     2
 * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
e30c6574b042 8167281: IIOMetadataNode bugs in getElementsByTagName and NodeList.item methods
jdv
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
e30c6574b042 8167281: IIOMetadataNode bugs in getElementsByTagName and NodeList.item methods
jdv
parents:
diff changeset
     4
 *
e30c6574b042 8167281: IIOMetadataNode bugs in getElementsByTagName and NodeList.item methods
jdv
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
e30c6574b042 8167281: IIOMetadataNode bugs in getElementsByTagName and NodeList.item methods
jdv
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
e30c6574b042 8167281: IIOMetadataNode bugs in getElementsByTagName and NodeList.item methods
jdv
parents:
diff changeset
     7
 * published by the Free Software Foundation.
e30c6574b042 8167281: IIOMetadataNode bugs in getElementsByTagName and NodeList.item methods
jdv
parents:
diff changeset
     8
 *
e30c6574b042 8167281: IIOMetadataNode bugs in getElementsByTagName and NodeList.item methods
jdv
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
e30c6574b042 8167281: IIOMetadataNode bugs in getElementsByTagName and NodeList.item methods
jdv
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
e30c6574b042 8167281: IIOMetadataNode bugs in getElementsByTagName and NodeList.item methods
jdv
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
e30c6574b042 8167281: IIOMetadataNode bugs in getElementsByTagName and NodeList.item methods
jdv
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
e30c6574b042 8167281: IIOMetadataNode bugs in getElementsByTagName and NodeList.item methods
jdv
parents:
diff changeset
    13
 * accompanied this code).
e30c6574b042 8167281: IIOMetadataNode bugs in getElementsByTagName and NodeList.item methods
jdv
parents:
diff changeset
    14
 *
e30c6574b042 8167281: IIOMetadataNode bugs in getElementsByTagName and NodeList.item methods
jdv
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
e30c6574b042 8167281: IIOMetadataNode bugs in getElementsByTagName and NodeList.item methods
jdv
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
e30c6574b042 8167281: IIOMetadataNode bugs in getElementsByTagName and NodeList.item methods
jdv
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
e30c6574b042 8167281: IIOMetadataNode bugs in getElementsByTagName and NodeList.item methods
jdv
parents:
diff changeset
    18
 *
e30c6574b042 8167281: IIOMetadataNode bugs in getElementsByTagName and NodeList.item methods
jdv
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
e30c6574b042 8167281: IIOMetadataNode bugs in getElementsByTagName and NodeList.item methods
jdv
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
e30c6574b042 8167281: IIOMetadataNode bugs in getElementsByTagName and NodeList.item methods
jdv
parents:
diff changeset
    21
 * questions.
e30c6574b042 8167281: IIOMetadataNode bugs in getElementsByTagName and NodeList.item methods
jdv
parents:
diff changeset
    22
 */
e30c6574b042 8167281: IIOMetadataNode bugs in getElementsByTagName and NodeList.item methods
jdv
parents:
diff changeset
    23
e30c6574b042 8167281: IIOMetadataNode bugs in getElementsByTagName and NodeList.item methods
jdv
parents:
diff changeset
    24
/*
e30c6574b042 8167281: IIOMetadataNode bugs in getElementsByTagName and NodeList.item methods
jdv
parents:
diff changeset
    25
 * @test
e30c6574b042 8167281: IIOMetadataNode bugs in getElementsByTagName and NodeList.item methods
jdv
parents:
diff changeset
    26
 * @bug     8167281
e30c6574b042 8167281: IIOMetadataNode bugs in getElementsByTagName and NodeList.item methods
jdv
parents:
diff changeset
    27
 * @summary Test verifies that accessing nth item in NodeList doesn't throw
e30c6574b042 8167281: IIOMetadataNode bugs in getElementsByTagName and NodeList.item methods
jdv
parents:
diff changeset
    28
 *          IndexOutOfBoundsException.
e30c6574b042 8167281: IIOMetadataNode bugs in getElementsByTagName and NodeList.item methods
jdv
parents:
diff changeset
    29
 * @run     main NthItemNodeListTest
e30c6574b042 8167281: IIOMetadataNode bugs in getElementsByTagName and NodeList.item methods
jdv
parents:
diff changeset
    30
 */
e30c6574b042 8167281: IIOMetadataNode bugs in getElementsByTagName and NodeList.item methods
jdv
parents:
diff changeset
    31
e30c6574b042 8167281: IIOMetadataNode bugs in getElementsByTagName and NodeList.item methods
jdv
parents:
diff changeset
    32
import java.awt.image.BufferedImage;
e30c6574b042 8167281: IIOMetadataNode bugs in getElementsByTagName and NodeList.item methods
jdv
parents:
diff changeset
    33
import java.io.ByteArrayInputStream;
e30c6574b042 8167281: IIOMetadataNode bugs in getElementsByTagName and NodeList.item methods
jdv
parents:
diff changeset
    34
import java.io.ByteArrayOutputStream;
e30c6574b042 8167281: IIOMetadataNode bugs in getElementsByTagName and NodeList.item methods
jdv
parents:
diff changeset
    35
import java.io.IOException;
e30c6574b042 8167281: IIOMetadataNode bugs in getElementsByTagName and NodeList.item methods
jdv
parents:
diff changeset
    36
import javax.imageio.ImageIO;
e30c6574b042 8167281: IIOMetadataNode bugs in getElementsByTagName and NodeList.item methods
jdv
parents:
diff changeset
    37
import javax.imageio.ImageReader;
e30c6574b042 8167281: IIOMetadataNode bugs in getElementsByTagName and NodeList.item methods
jdv
parents:
diff changeset
    38
import javax.imageio.metadata.IIOMetadata;
e30c6574b042 8167281: IIOMetadataNode bugs in getElementsByTagName and NodeList.item methods
jdv
parents:
diff changeset
    39
import javax.imageio.metadata.IIOMetadataFormatImpl;
e30c6574b042 8167281: IIOMetadataNode bugs in getElementsByTagName and NodeList.item methods
jdv
parents:
diff changeset
    40
import javax.imageio.stream.ImageInputStream;
e30c6574b042 8167281: IIOMetadataNode bugs in getElementsByTagName and NodeList.item methods
jdv
parents:
diff changeset
    41
import javax.imageio.stream.MemoryCacheImageInputStream;
e30c6574b042 8167281: IIOMetadataNode bugs in getElementsByTagName and NodeList.item methods
jdv
parents:
diff changeset
    42
import org.w3c.dom.Element;
e30c6574b042 8167281: IIOMetadataNode bugs in getElementsByTagName and NodeList.item methods
jdv
parents:
diff changeset
    43
import org.w3c.dom.Node;
e30c6574b042 8167281: IIOMetadataNode bugs in getElementsByTagName and NodeList.item methods
jdv
parents:
diff changeset
    44
import org.w3c.dom.NodeList;
e30c6574b042 8167281: IIOMetadataNode bugs in getElementsByTagName and NodeList.item methods
jdv
parents:
diff changeset
    45
e30c6574b042 8167281: IIOMetadataNode bugs in getElementsByTagName and NodeList.item methods
jdv
parents:
diff changeset
    46
public class NthItemNodeListTest {
e30c6574b042 8167281: IIOMetadataNode bugs in getElementsByTagName and NodeList.item methods
jdv
parents:
diff changeset
    47
e30c6574b042 8167281: IIOMetadataNode bugs in getElementsByTagName and NodeList.item methods
jdv
parents:
diff changeset
    48
    public static void main(String[] args) throws IOException {
e30c6574b042 8167281: IIOMetadataNode bugs in getElementsByTagName and NodeList.item methods
jdv
parents:
diff changeset
    49
        // Generate some trivial image and save it to a temporary array
e30c6574b042 8167281: IIOMetadataNode bugs in getElementsByTagName and NodeList.item methods
jdv
parents:
diff changeset
    50
        ByteArrayOutputStream tmp = new ByteArrayOutputStream();
e30c6574b042 8167281: IIOMetadataNode bugs in getElementsByTagName and NodeList.item methods
jdv
parents:
diff changeset
    51
        ImageIO.write(new BufferedImage(1, 1, BufferedImage.TYPE_INT_RGB),
e30c6574b042 8167281: IIOMetadataNode bugs in getElementsByTagName and NodeList.item methods
jdv
parents:
diff changeset
    52
                "gif", tmp);
e30c6574b042 8167281: IIOMetadataNode bugs in getElementsByTagName and NodeList.item methods
jdv
parents:
diff changeset
    53
e30c6574b042 8167281: IIOMetadataNode bugs in getElementsByTagName and NodeList.item methods
jdv
parents:
diff changeset
    54
        // Read it back in
e30c6574b042 8167281: IIOMetadataNode bugs in getElementsByTagName and NodeList.item methods
jdv
parents:
diff changeset
    55
        ImageInputStream in = new MemoryCacheImageInputStream(
e30c6574b042 8167281: IIOMetadataNode bugs in getElementsByTagName and NodeList.item methods
jdv
parents:
diff changeset
    56
                new ByteArrayInputStream(tmp.toByteArray()));
e30c6574b042 8167281: IIOMetadataNode bugs in getElementsByTagName and NodeList.item methods
jdv
parents:
diff changeset
    57
        ImageReader reader = ImageIO.getImageReaders(in).next();
e30c6574b042 8167281: IIOMetadataNode bugs in getElementsByTagName and NodeList.item methods
jdv
parents:
diff changeset
    58
        reader.setInput(in);
e30c6574b042 8167281: IIOMetadataNode bugs in getElementsByTagName and NodeList.item methods
jdv
parents:
diff changeset
    59
e30c6574b042 8167281: IIOMetadataNode bugs in getElementsByTagName and NodeList.item methods
jdv
parents:
diff changeset
    60
        // Retrieve standard image metadata tree
e30c6574b042 8167281: IIOMetadataNode bugs in getElementsByTagName and NodeList.item methods
jdv
parents:
diff changeset
    61
        IIOMetadata meta = reader.getImageMetadata(0);
e30c6574b042 8167281: IIOMetadataNode bugs in getElementsByTagName and NodeList.item methods
jdv
parents:
diff changeset
    62
        if (meta == null || !meta.isStandardMetadataFormatSupported()) {
e30c6574b042 8167281: IIOMetadataNode bugs in getElementsByTagName and NodeList.item methods
jdv
parents:
diff changeset
    63
            throw new Error("Test failure: Missing metadata");
e30c6574b042 8167281: IIOMetadataNode bugs in getElementsByTagName and NodeList.item methods
jdv
parents:
diff changeset
    64
        }
e30c6574b042 8167281: IIOMetadataNode bugs in getElementsByTagName and NodeList.item methods
jdv
parents:
diff changeset
    65
        Element root = (Element) meta.
e30c6574b042 8167281: IIOMetadataNode bugs in getElementsByTagName and NodeList.item methods
jdv
parents:
diff changeset
    66
                getAsTree(IIOMetadataFormatImpl.standardMetadataFormatName);
e30c6574b042 8167281: IIOMetadataNode bugs in getElementsByTagName and NodeList.item methods
jdv
parents:
diff changeset
    67
e30c6574b042 8167281: IIOMetadataNode bugs in getElementsByTagName and NodeList.item methods
jdv
parents:
diff changeset
    68
        NodeList nodeList = root.
e30c6574b042 8167281: IIOMetadataNode bugs in getElementsByTagName and NodeList.item methods
jdv
parents:
diff changeset
    69
                getElementsByTagName(root.getFirstChild().getNodeName());
e30c6574b042 8167281: IIOMetadataNode bugs in getElementsByTagName and NodeList.item methods
jdv
parents:
diff changeset
    70
        /*
e30c6574b042 8167281: IIOMetadataNode bugs in getElementsByTagName and NodeList.item methods
jdv
parents:
diff changeset
    71
         * Accessing the nth node should return null and not throw
e30c6574b042 8167281: IIOMetadataNode bugs in getElementsByTagName and NodeList.item methods
jdv
parents:
diff changeset
    72
         * IndexOutOfBoundsException.
e30c6574b042 8167281: IIOMetadataNode bugs in getElementsByTagName and NodeList.item methods
jdv
parents:
diff changeset
    73
         */
e30c6574b042 8167281: IIOMetadataNode bugs in getElementsByTagName and NodeList.item methods
jdv
parents:
diff changeset
    74
        Node n = (nodeList.item(nodeList.getLength()));
e30c6574b042 8167281: IIOMetadataNode bugs in getElementsByTagName and NodeList.item methods
jdv
parents:
diff changeset
    75
    }
e30c6574b042 8167281: IIOMetadataNode bugs in getElementsByTagName and NodeList.item methods
jdv
parents:
diff changeset
    76
}
e30c6574b042 8167281: IIOMetadataNode bugs in getElementsByTagName and NodeList.item methods
jdv
parents:
diff changeset
    77