test/jdk/javax/imageio/GetNumImages.java
changeset 47216 71c04702a3d5
parent 45351 f7f615a3aaff
equal deleted inserted replaced
47215:4ebc2e2fb97c 47216:71c04702a3d5
       
     1 /*
       
     2  * Copyright (c) 2003, 2017, 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 4892609
       
    27  * @summary Tests that the appropriate IllegalStateException is thrown if
       
    28  *          ImageReader.getNumImages() is called with a null source or if
       
    29  *          allowSearch is specified at the same time that seekForwardOnly is
       
    30  *          true
       
    31  */
       
    32 
       
    33 import java.io.ByteArrayInputStream;
       
    34 import java.util.Iterator;
       
    35 
       
    36 import javax.imageio.ImageIO;
       
    37 import javax.imageio.ImageReader;
       
    38 import javax.imageio.spi.IIORegistry;
       
    39 import javax.imageio.spi.ImageReaderSpi;
       
    40 import javax.imageio.stream.ImageInputStream;
       
    41 
       
    42 public class GetNumImages {
       
    43 
       
    44     public static void main(String[] args) throws Exception {
       
    45         IIORegistry registry = IIORegistry.getDefaultInstance();
       
    46 
       
    47         // test ImageReader.getNumImages() for all available ImageReaders,
       
    48         // with no source set
       
    49         Iterator readerspis = registry.getServiceProviders(ImageReaderSpi.class,
       
    50                                                            false);
       
    51         while (readerspis.hasNext()) {
       
    52             boolean caughtEx = false;
       
    53             ImageReaderSpi readerspi = (ImageReaderSpi)readerspis.next();
       
    54             ImageReader reader = readerspi.createReaderInstance();
       
    55             try {
       
    56                 reader.getNumImages(false);
       
    57             } catch (IllegalStateException ise) {
       
    58                 // caught exception, everything's okay
       
    59                 caughtEx = true;
       
    60             }
       
    61 
       
    62             if (!caughtEx) {
       
    63                 throw new RuntimeException("Test failed: exception was not " +
       
    64                                            "thrown for null input: " +
       
    65                                            reader);
       
    66             }
       
    67         }
       
    68 
       
    69         // test ImageReader.getNumImages() for all available ImageReaders,
       
    70         // with source set, seekForwardOnly and allowSearch both true
       
    71         readerspis = registry.getServiceProviders(ImageReaderSpi.class,
       
    72                                                   false);
       
    73         while (readerspis.hasNext()) {
       
    74             boolean caughtEx = false;
       
    75             ImageReaderSpi readerspi = (ImageReaderSpi)readerspis.next();
       
    76             ImageReader reader = readerspi.createReaderInstance();
       
    77             byte[] barr = new byte[100];
       
    78             ByteArrayInputStream bais = new ByteArrayInputStream(barr);
       
    79             ImageInputStream iis = ImageIO.createImageInputStream(bais);
       
    80             try {
       
    81                 reader.setInput(iis, true);
       
    82                 reader.getNumImages(true);
       
    83             } catch (IllegalStateException ise) {
       
    84                 // caught exception, everything's okay
       
    85                 caughtEx = true;
       
    86             }
       
    87 
       
    88             if (!caughtEx) {
       
    89                 throw new RuntimeException("Test failed: exception was not " +
       
    90                                            "thrown when allowSearch and " +
       
    91                                            "seekForwardOnly are both true: " +
       
    92                                            reader);
       
    93             }
       
    94         }
       
    95     }
       
    96 }