test/jdk/javax/imageio/ImageReaderGetDestination.java
changeset 47216 71c04702a3d5
parent 45351 f7f615a3aaff
equal deleted inserted replaced
47215:4ebc2e2fb97c 47216:71c04702a3d5
       
     1 /*
       
     2  * Copyright (c) 2001, 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 4449211
       
    27  * @summary Checks that ImageReader.getDestination throws correct exceptions
       
    28  */
       
    29 
       
    30 import java.awt.image.BufferedImage;
       
    31 import java.io.IOException;
       
    32 import java.util.Iterator;
       
    33 import java.util.Vector;
       
    34 
       
    35 import javax.imageio.IIOException;
       
    36 import javax.imageio.ImageReadParam;
       
    37 import javax.imageio.ImageReader;
       
    38 import javax.imageio.ImageTypeSpecifier;
       
    39 import javax.imageio.metadata.IIOMetadata;
       
    40 import javax.imageio.spi.ImageReaderSpi;
       
    41 
       
    42 public class ImageReaderGetDestination {
       
    43 
       
    44     public static void main(String argv[]) {
       
    45         Vector imageTypes = new Vector();
       
    46         boolean gotIAE1 = false;
       
    47         boolean gotIAE2 = false;
       
    48         boolean gotIAE3 = false;
       
    49         boolean gotIAE4 = false;
       
    50 
       
    51         try {
       
    52             DummyImageReaderImpl.getDestination(null, null, 5, 10);
       
    53         } catch (IllegalArgumentException iae) {
       
    54             gotIAE1 = true;
       
    55         } catch (Throwable ee) {
       
    56             System.out.println("Unexpected exception 1:");
       
    57             ee.printStackTrace();
       
    58         }
       
    59         if (!gotIAE1) {
       
    60             throw new RuntimeException("Failed to get IAE #1!");
       
    61         }
       
    62 
       
    63         try {
       
    64             DummyImageReaderImpl.getDestination(null, imageTypes.iterator(),
       
    65                                                 5, 10);
       
    66         } catch (IllegalArgumentException iae) {
       
    67             gotIAE2 = true;
       
    68         } catch (Throwable ee) {
       
    69             System.out.println("Unexpected exception 2:");
       
    70             ee.printStackTrace();
       
    71         }
       
    72         if (!gotIAE2) {
       
    73             throw new RuntimeException("Failed to get IAE #2!");
       
    74         }
       
    75 
       
    76         imageTypes.add("abc");
       
    77         try {
       
    78             DummyImageReaderImpl.getDestination(null, imageTypes.iterator(),
       
    79                                                 5, 10);
       
    80         } catch (IllegalArgumentException iae) {
       
    81             gotIAE3 = true;
       
    82         } catch (Throwable ee) {
       
    83             System.out.println("Unexpected exception 3:");
       
    84             ee.printStackTrace();
       
    85         }
       
    86         if (!gotIAE3) {
       
    87             throw new RuntimeException("Failed to get IAE #3!");
       
    88         }
       
    89 
       
    90         imageTypes.clear();
       
    91         ImageTypeSpecifier its = ImageTypeSpecifier.createFromBufferedImageType
       
    92             (BufferedImage.TYPE_INT_RGB);
       
    93         imageTypes.add(its);
       
    94         try {
       
    95             DummyImageReaderImpl.getDestination(null,
       
    96                                                 imageTypes.iterator(),
       
    97                                                 Integer.MAX_VALUE,
       
    98                                                 Integer.MAX_VALUE);
       
    99         } catch (IllegalArgumentException iae) {
       
   100             gotIAE4 = true;
       
   101         } catch (Throwable ee) {
       
   102             System.out.println("Unexpected exception 4: ");
       
   103             ee.printStackTrace();
       
   104         }
       
   105         if (!gotIAE4) {
       
   106             throw new RuntimeException("Failed to get IAE #4!");
       
   107         }
       
   108     }
       
   109 
       
   110     public static class DummyImageReaderImpl extends ImageReader {
       
   111         public DummyImageReaderImpl(ImageReaderSpi originatingProvider) {
       
   112             super(originatingProvider);
       
   113         }
       
   114         public static BufferedImage getDestination(ImageReadParam param,
       
   115                                                    Iterator imageTypes,
       
   116                                                    int width,
       
   117                                                    int height)
       
   118           throws IIOException {
       
   119             return ImageReader.getDestination(param, imageTypes, width, height);
       
   120         }
       
   121         public int getNumImages(boolean allowSearch) throws IOException {return 1;}
       
   122         public int getWidth(int imageIndex) throws IOException {return 1;}
       
   123         public int getHeight(int imageIndex) throws IOException {return 1;}
       
   124         public Iterator getImageTypes(int imageIndex)
       
   125           throws IOException {return null;}
       
   126         public IIOMetadata getStreamMetadata() throws IOException {return null;}
       
   127         public IIOMetadata getImageMetadata(int imageIndex)
       
   128           throws IOException {return null;}
       
   129         public BufferedImage read(int imageIndex, ImageReadParam param)
       
   130           throws IOException {return null;}
       
   131     }
       
   132 }