Path: blob/master/test/jdk/javax/imageio/plugins/jpeg/JpegNumThumbnailsTest.java
41152 views
/*1* Copyright (c) 2018, 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 495434826* @summary Checks whether JpegImageWriter returns -1 as recommended by the27* specification when getNumThumbnailsSupported method is invoked28* with insufficient data.29* @run main JpegNumThumbnailsTest30*/31import javax.imageio.ImageIO;32import javax.imageio.ImageTypeSpecifier;33import javax.imageio.ImageWriter;34import javax.imageio.metadata.IIOMetadata;35import java.awt.image.BufferedImage;36import java.util.Iterator;37import static java.awt.image.BufferedImage.TYPE_INT_RGB;3839public class JpegNumThumbnailsTest {4041public static void main(String args[]) {42// Test variables.43Iterator<ImageWriter> iterWriter = null;44ImageWriter jpgWriter = null;45IIOMetadata imgMetadata = null;46BufferedImage testImage = null;47ImageTypeSpecifier imgType = null;48int numThumbnails = 0;4950iterWriter = ImageIO.getImageWritersByFormatName("JPEG");51if (iterWriter.hasNext()) {52try {53// JpegImageWriter requires either image type or image metadata54// to determine the number of thumbnails that could be55// supported. Hence we test for all possible input combinations56// and observe the result.57jpgWriter = iterWriter.next();58testImage = new BufferedImage(32, 32, TYPE_INT_RGB);59imgType = ImageTypeSpecifier.createFromRenderedImage(testImage);60imgMetadata = jpgWriter.getDefaultImageMetadata(imgType, null);6162// Observe the result with insufficient data.63numThumbnails = jpgWriter.getNumThumbnailsSupported(null,64null, null, null);65if (numThumbnails != -1) {66reportException("Incorrect number of thumbnails returned.");67}6869// Observe the result with valid image type.70numThumbnails = jpgWriter.getNumThumbnailsSupported(imgType,71null, null, null);72if (numThumbnails != Integer.MAX_VALUE) {73reportException("Incorrect number of thumbnails returned.");74}7576// Observe the result with valid image metadata.77numThumbnails = jpgWriter.getNumThumbnailsSupported(null,78null, null, imgMetadata);79if (numThumbnails != Integer.MAX_VALUE) {80reportException("Incorrect number of thumbnails returned.");81}8283// Observe the result with valid image type and metadata.84numThumbnails = jpgWriter.getNumThumbnailsSupported(imgType,85null, null, imgMetadata);86if (numThumbnails != Integer.MAX_VALUE) {87reportException("Incorrect number of thumbnails returned.");88}89} finally {90// Dispose the writer91jpgWriter.dispose();92}93}94}9596private static void reportException(String message) {97// Report a runtime exception with the required message.98throw new RuntimeException("Test Failed. " + message);99}100}101102103