jdk/test/javax/imageio/GetNumImages.java
author serb
Mon, 22 May 2017 19:54:23 -0700
changeset 45351 f7f615a3aaff
permissions -rw-r--r--
8177628: Opensource unit/regression tests for ImageIO Reviewed-by: prr, pnarayanan
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
45351
f7f615a3aaff 8177628: Opensource unit/regression tests for ImageIO
serb
parents:
diff changeset
     1
/*
f7f615a3aaff 8177628: Opensource unit/regression tests for ImageIO
serb
parents:
diff changeset
     2
 * Copyright (c) 2003, 2017, Oracle and/or its affiliates. All rights reserved.
f7f615a3aaff 8177628: Opensource unit/regression tests for ImageIO
serb
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
f7f615a3aaff 8177628: Opensource unit/regression tests for ImageIO
serb
parents:
diff changeset
     4
 *
f7f615a3aaff 8177628: Opensource unit/regression tests for ImageIO
serb
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
f7f615a3aaff 8177628: Opensource unit/regression tests for ImageIO
serb
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
f7f615a3aaff 8177628: Opensource unit/regression tests for ImageIO
serb
parents:
diff changeset
     7
 * published by the Free Software Foundation.
f7f615a3aaff 8177628: Opensource unit/regression tests for ImageIO
serb
parents:
diff changeset
     8
 *
f7f615a3aaff 8177628: Opensource unit/regression tests for ImageIO
serb
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
f7f615a3aaff 8177628: Opensource unit/regression tests for ImageIO
serb
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
f7f615a3aaff 8177628: Opensource unit/regression tests for ImageIO
serb
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
f7f615a3aaff 8177628: Opensource unit/regression tests for ImageIO
serb
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
f7f615a3aaff 8177628: Opensource unit/regression tests for ImageIO
serb
parents:
diff changeset
    13
 * accompanied this code).
f7f615a3aaff 8177628: Opensource unit/regression tests for ImageIO
serb
parents:
diff changeset
    14
 *
f7f615a3aaff 8177628: Opensource unit/regression tests for ImageIO
serb
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
f7f615a3aaff 8177628: Opensource unit/regression tests for ImageIO
serb
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
f7f615a3aaff 8177628: Opensource unit/regression tests for ImageIO
serb
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
f7f615a3aaff 8177628: Opensource unit/regression tests for ImageIO
serb
parents:
diff changeset
    18
 *
f7f615a3aaff 8177628: Opensource unit/regression tests for ImageIO
serb
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
f7f615a3aaff 8177628: Opensource unit/regression tests for ImageIO
serb
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
f7f615a3aaff 8177628: Opensource unit/regression tests for ImageIO
serb
parents:
diff changeset
    21
 * questions.
f7f615a3aaff 8177628: Opensource unit/regression tests for ImageIO
serb
parents:
diff changeset
    22
 */
f7f615a3aaff 8177628: Opensource unit/regression tests for ImageIO
serb
parents:
diff changeset
    23
f7f615a3aaff 8177628: Opensource unit/regression tests for ImageIO
serb
parents:
diff changeset
    24
/*
f7f615a3aaff 8177628: Opensource unit/regression tests for ImageIO
serb
parents:
diff changeset
    25
 * @test
f7f615a3aaff 8177628: Opensource unit/regression tests for ImageIO
serb
parents:
diff changeset
    26
 * @bug 4892609
f7f615a3aaff 8177628: Opensource unit/regression tests for ImageIO
serb
parents:
diff changeset
    27
 * @summary Tests that the appropriate IllegalStateException is thrown if
f7f615a3aaff 8177628: Opensource unit/regression tests for ImageIO
serb
parents:
diff changeset
    28
 *          ImageReader.getNumImages() is called with a null source or if
f7f615a3aaff 8177628: Opensource unit/regression tests for ImageIO
serb
parents:
diff changeset
    29
 *          allowSearch is specified at the same time that seekForwardOnly is
f7f615a3aaff 8177628: Opensource unit/regression tests for ImageIO
serb
parents:
diff changeset
    30
 *          true
f7f615a3aaff 8177628: Opensource unit/regression tests for ImageIO
serb
parents:
diff changeset
    31
 */
f7f615a3aaff 8177628: Opensource unit/regression tests for ImageIO
serb
parents:
diff changeset
    32
f7f615a3aaff 8177628: Opensource unit/regression tests for ImageIO
serb
parents:
diff changeset
    33
import java.io.ByteArrayInputStream;
f7f615a3aaff 8177628: Opensource unit/regression tests for ImageIO
serb
parents:
diff changeset
    34
import java.util.Iterator;
f7f615a3aaff 8177628: Opensource unit/regression tests for ImageIO
serb
parents:
diff changeset
    35
f7f615a3aaff 8177628: Opensource unit/regression tests for ImageIO
serb
parents:
diff changeset
    36
import javax.imageio.ImageIO;
f7f615a3aaff 8177628: Opensource unit/regression tests for ImageIO
serb
parents:
diff changeset
    37
import javax.imageio.ImageReader;
f7f615a3aaff 8177628: Opensource unit/regression tests for ImageIO
serb
parents:
diff changeset
    38
import javax.imageio.spi.IIORegistry;
f7f615a3aaff 8177628: Opensource unit/regression tests for ImageIO
serb
parents:
diff changeset
    39
import javax.imageio.spi.ImageReaderSpi;
f7f615a3aaff 8177628: Opensource unit/regression tests for ImageIO
serb
parents:
diff changeset
    40
import javax.imageio.stream.ImageInputStream;
f7f615a3aaff 8177628: Opensource unit/regression tests for ImageIO
serb
parents:
diff changeset
    41
f7f615a3aaff 8177628: Opensource unit/regression tests for ImageIO
serb
parents:
diff changeset
    42
public class GetNumImages {
f7f615a3aaff 8177628: Opensource unit/regression tests for ImageIO
serb
parents:
diff changeset
    43
f7f615a3aaff 8177628: Opensource unit/regression tests for ImageIO
serb
parents:
diff changeset
    44
    public static void main(String[] args) throws Exception {
f7f615a3aaff 8177628: Opensource unit/regression tests for ImageIO
serb
parents:
diff changeset
    45
        IIORegistry registry = IIORegistry.getDefaultInstance();
f7f615a3aaff 8177628: Opensource unit/regression tests for ImageIO
serb
parents:
diff changeset
    46
f7f615a3aaff 8177628: Opensource unit/regression tests for ImageIO
serb
parents:
diff changeset
    47
        // test ImageReader.getNumImages() for all available ImageReaders,
f7f615a3aaff 8177628: Opensource unit/regression tests for ImageIO
serb
parents:
diff changeset
    48
        // with no source set
f7f615a3aaff 8177628: Opensource unit/regression tests for ImageIO
serb
parents:
diff changeset
    49
        Iterator readerspis = registry.getServiceProviders(ImageReaderSpi.class,
f7f615a3aaff 8177628: Opensource unit/regression tests for ImageIO
serb
parents:
diff changeset
    50
                                                           false);
f7f615a3aaff 8177628: Opensource unit/regression tests for ImageIO
serb
parents:
diff changeset
    51
        while (readerspis.hasNext()) {
f7f615a3aaff 8177628: Opensource unit/regression tests for ImageIO
serb
parents:
diff changeset
    52
            boolean caughtEx = false;
f7f615a3aaff 8177628: Opensource unit/regression tests for ImageIO
serb
parents:
diff changeset
    53
            ImageReaderSpi readerspi = (ImageReaderSpi)readerspis.next();
f7f615a3aaff 8177628: Opensource unit/regression tests for ImageIO
serb
parents:
diff changeset
    54
            ImageReader reader = readerspi.createReaderInstance();
f7f615a3aaff 8177628: Opensource unit/regression tests for ImageIO
serb
parents:
diff changeset
    55
            try {
f7f615a3aaff 8177628: Opensource unit/regression tests for ImageIO
serb
parents:
diff changeset
    56
                reader.getNumImages(false);
f7f615a3aaff 8177628: Opensource unit/regression tests for ImageIO
serb
parents:
diff changeset
    57
            } catch (IllegalStateException ise) {
f7f615a3aaff 8177628: Opensource unit/regression tests for ImageIO
serb
parents:
diff changeset
    58
                // caught exception, everything's okay
f7f615a3aaff 8177628: Opensource unit/regression tests for ImageIO
serb
parents:
diff changeset
    59
                caughtEx = true;
f7f615a3aaff 8177628: Opensource unit/regression tests for ImageIO
serb
parents:
diff changeset
    60
            }
f7f615a3aaff 8177628: Opensource unit/regression tests for ImageIO
serb
parents:
diff changeset
    61
f7f615a3aaff 8177628: Opensource unit/regression tests for ImageIO
serb
parents:
diff changeset
    62
            if (!caughtEx) {
f7f615a3aaff 8177628: Opensource unit/regression tests for ImageIO
serb
parents:
diff changeset
    63
                throw new RuntimeException("Test failed: exception was not " +
f7f615a3aaff 8177628: Opensource unit/regression tests for ImageIO
serb
parents:
diff changeset
    64
                                           "thrown for null input: " +
f7f615a3aaff 8177628: Opensource unit/regression tests for ImageIO
serb
parents:
diff changeset
    65
                                           reader);
f7f615a3aaff 8177628: Opensource unit/regression tests for ImageIO
serb
parents:
diff changeset
    66
            }
f7f615a3aaff 8177628: Opensource unit/regression tests for ImageIO
serb
parents:
diff changeset
    67
        }
f7f615a3aaff 8177628: Opensource unit/regression tests for ImageIO
serb
parents:
diff changeset
    68
f7f615a3aaff 8177628: Opensource unit/regression tests for ImageIO
serb
parents:
diff changeset
    69
        // test ImageReader.getNumImages() for all available ImageReaders,
f7f615a3aaff 8177628: Opensource unit/regression tests for ImageIO
serb
parents:
diff changeset
    70
        // with source set, seekForwardOnly and allowSearch both true
f7f615a3aaff 8177628: Opensource unit/regression tests for ImageIO
serb
parents:
diff changeset
    71
        readerspis = registry.getServiceProviders(ImageReaderSpi.class,
f7f615a3aaff 8177628: Opensource unit/regression tests for ImageIO
serb
parents:
diff changeset
    72
                                                  false);
f7f615a3aaff 8177628: Opensource unit/regression tests for ImageIO
serb
parents:
diff changeset
    73
        while (readerspis.hasNext()) {
f7f615a3aaff 8177628: Opensource unit/regression tests for ImageIO
serb
parents:
diff changeset
    74
            boolean caughtEx = false;
f7f615a3aaff 8177628: Opensource unit/regression tests for ImageIO
serb
parents:
diff changeset
    75
            ImageReaderSpi readerspi = (ImageReaderSpi)readerspis.next();
f7f615a3aaff 8177628: Opensource unit/regression tests for ImageIO
serb
parents:
diff changeset
    76
            ImageReader reader = readerspi.createReaderInstance();
f7f615a3aaff 8177628: Opensource unit/regression tests for ImageIO
serb
parents:
diff changeset
    77
            byte[] barr = new byte[100];
f7f615a3aaff 8177628: Opensource unit/regression tests for ImageIO
serb
parents:
diff changeset
    78
            ByteArrayInputStream bais = new ByteArrayInputStream(barr);
f7f615a3aaff 8177628: Opensource unit/regression tests for ImageIO
serb
parents:
diff changeset
    79
            ImageInputStream iis = ImageIO.createImageInputStream(bais);
f7f615a3aaff 8177628: Opensource unit/regression tests for ImageIO
serb
parents:
diff changeset
    80
            try {
f7f615a3aaff 8177628: Opensource unit/regression tests for ImageIO
serb
parents:
diff changeset
    81
                reader.setInput(iis, true);
f7f615a3aaff 8177628: Opensource unit/regression tests for ImageIO
serb
parents:
diff changeset
    82
                reader.getNumImages(true);
f7f615a3aaff 8177628: Opensource unit/regression tests for ImageIO
serb
parents:
diff changeset
    83
            } catch (IllegalStateException ise) {
f7f615a3aaff 8177628: Opensource unit/regression tests for ImageIO
serb
parents:
diff changeset
    84
                // caught exception, everything's okay
f7f615a3aaff 8177628: Opensource unit/regression tests for ImageIO
serb
parents:
diff changeset
    85
                caughtEx = true;
f7f615a3aaff 8177628: Opensource unit/regression tests for ImageIO
serb
parents:
diff changeset
    86
            }
f7f615a3aaff 8177628: Opensource unit/regression tests for ImageIO
serb
parents:
diff changeset
    87
f7f615a3aaff 8177628: Opensource unit/regression tests for ImageIO
serb
parents:
diff changeset
    88
            if (!caughtEx) {
f7f615a3aaff 8177628: Opensource unit/regression tests for ImageIO
serb
parents:
diff changeset
    89
                throw new RuntimeException("Test failed: exception was not " +
f7f615a3aaff 8177628: Opensource unit/regression tests for ImageIO
serb
parents:
diff changeset
    90
                                           "thrown when allowSearch and " +
f7f615a3aaff 8177628: Opensource unit/regression tests for ImageIO
serb
parents:
diff changeset
    91
                                           "seekForwardOnly are both true: " +
f7f615a3aaff 8177628: Opensource unit/regression tests for ImageIO
serb
parents:
diff changeset
    92
                                           reader);
f7f615a3aaff 8177628: Opensource unit/regression tests for ImageIO
serb
parents:
diff changeset
    93
            }
f7f615a3aaff 8177628: Opensource unit/regression tests for ImageIO
serb
parents:
diff changeset
    94
        }
f7f615a3aaff 8177628: Opensource unit/regression tests for ImageIO
serb
parents:
diff changeset
    95
    }
f7f615a3aaff 8177628: Opensource unit/regression tests for ImageIO
serb
parents:
diff changeset
    96
}