Path: blob/master/test/jdk/javax/imageio/GetNumImages.java
41144 views
/*1* Copyright (c) 2003, 2017, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 489260926* @summary Tests that the appropriate IllegalStateException is thrown if27* ImageReader.getNumImages() is called with a null source or if28* allowSearch is specified at the same time that seekForwardOnly is29* true30*/3132import java.io.ByteArrayInputStream;33import java.util.Iterator;3435import javax.imageio.ImageIO;36import javax.imageio.ImageReader;37import javax.imageio.spi.IIORegistry;38import javax.imageio.spi.ImageReaderSpi;39import javax.imageio.stream.ImageInputStream;4041public class GetNumImages {4243public static void main(String[] args) throws Exception {44IIORegistry registry = IIORegistry.getDefaultInstance();4546// test ImageReader.getNumImages() for all available ImageReaders,47// with no source set48Iterator readerspis = registry.getServiceProviders(ImageReaderSpi.class,49false);50while (readerspis.hasNext()) {51boolean caughtEx = false;52ImageReaderSpi readerspi = (ImageReaderSpi)readerspis.next();53ImageReader reader = readerspi.createReaderInstance();54try {55reader.getNumImages(false);56} catch (IllegalStateException ise) {57// caught exception, everything's okay58caughtEx = true;59}6061if (!caughtEx) {62throw new RuntimeException("Test failed: exception was not " +63"thrown for null input: " +64reader);65}66}6768// test ImageReader.getNumImages() for all available ImageReaders,69// with source set, seekForwardOnly and allowSearch both true70readerspis = registry.getServiceProviders(ImageReaderSpi.class,71false);72while (readerspis.hasNext()) {73boolean caughtEx = false;74ImageReaderSpi readerspi = (ImageReaderSpi)readerspis.next();75ImageReader reader = readerspi.createReaderInstance();76byte[] barr = new byte[100];77ByteArrayInputStream bais = new ByteArrayInputStream(barr);78ImageInputStream iis = ImageIO.createImageInputStream(bais);79try {80reader.setInput(iis, true);81reader.getNumImages(true);82} catch (IllegalStateException ise) {83// caught exception, everything's okay84caughtEx = true;85}8687if (!caughtEx) {88throw new RuntimeException("Test failed: exception was not " +89"thrown when allowSearch and " +90"seekForwardOnly are both true: " +91reader);92}93}94}95}969798