Path: blob/master/test/jdk/javax/imageio/plugins/jpeg/ReadAllThumbnailsTest.java
41152 views
/*1* Copyright (c) 2004, 2016, 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 4958271 816094326* @summary This test verifies that ImageReader.readAll() method is able to read27* all the thumbnails present in the input source without throwing any28* exception while reading through Jpeg data.29* @run main ReadAllThumbnailsTest30*/3132import java.util.Iterator;33import java.io.File;34import javax.imageio.IIOImage;35import javax.imageio.ImageIO;36import javax.imageio.ImageReader;37import javax.imageio.stream.ImageInputStream;3839public class ReadAllThumbnailsTest {4041public ReadAllThumbnailsTest() {4243try {44ImageReader reader = null;4546String fileName = "thumbimg.jpg";47String sep = System.getProperty("file.separator");48String dir = System.getProperty("test.src", ".");49String filePath = dir+sep+fileName;50System.out.println("Test file: " + filePath);51File f = new File(filePath);5253ImageInputStream iis = ImageIO.createImageInputStream(f);54Iterator readerIt = ImageIO.getImageReaders(iis);55if (readerIt.hasNext()) {56reader = (ImageReader) readerIt.next();57}5859if (reader == null) {60error("FAIL: Reader is not available for reading a " +61"JPG image with thumbnails. Test Aborted !!");62}6364reader.setInput(iis);6566if (!reader.readerSupportsThumbnails()) {67error("FAIL: JPG Reader fails to support thumbnails."68+ " Test aborted !!");69}7071int numThumbnails = reader.getNumThumbnails(0);72if (numThumbnails <= 0) {73error(" FAIL: Reader.getNumThumbnails() returns 0 when the " +74"input image contains some thumbnails");75}76IIOImage iioImg = reader.readAll(0, null);77int thumbnailsRead = iioImg.getNumThumbnails();7879if (numThumbnails == thumbnailsRead) {80System.out.println("PASS: Thumbnails are read properly by"81+ " ImageReader.readAll(index, readParam) ");82} else {83error("FAIL: Some of the thumbnails are not read" +84" from the input source when calling" +85" ImageReader.readAll(index, readParam) ");86}8788iis = ImageIO.createImageInputStream(f);89reader.setInput(iis);9091iioImg = null;92Iterator imgIter = reader.readAll(null);93if (imgIter.hasNext()) {94iioImg = (IIOImage) imgIter.next();95thumbnailsRead = iioImg.getNumThumbnails();9697if (numThumbnails == thumbnailsRead) {98System.out.println("PASS: Thumbnails are read properly by"99+ " ImageReader.readAll(Iter)");100} else {101error("FAIL: Some of the thumbnails are not read " +102"from the input source when calling"103+ " ImageReader.readAll(Iter)");104}105} else {106error("FAIL: ImageReader.readAll(Iter) fails to read the image"107+ " & thumbnails from the input source");108}109110} catch (Exception e) {111error(" FAIL: The following exception is thrown by " +112"ImageReader.readAll() method when input source contains " +113"some thumbnails. Exception: " + e.toString());114}115116}117118public final void error(String mesg) {119throw new RuntimeException(mesg);120}121122public static void main(String args[]) {123ReadAllThumbnailsTest test = new ReadAllThumbnailsTest();124}125}126127128