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 4954545
  27  * @summary This test verifies whether the ImageWriter implementations throw
  28  *          IllegalArgumentException when a null image is passed to the write
  29  *          method. This is tested for all the image writers available with the
  30  *          IIORegistry.
  31  */
  32 
  33 import java.io.FileOutputStream;
  34 import java.util.Iterator;
  35 
  36 import javax.imageio.ImageIO;
  37 import javax.imageio.ImageWriter;
  38 import javax.imageio.spi.IIORegistry;
  39 import javax.imageio.spi.ImageWriterSpi;
  40 import javax.imageio.stream.ImageOutputStream;
  41 
  42 public class WriteNullImageTest {
  43 
  44     public WriteNullImageTest() {
  45         boolean testFailed = false;
  46         String failMsg = "FAIL: IllegalArgumentException is not thrown by the " +
  47             "ImageWriter when the image passed to the write() method is " +
  48             "null, for the image formats: ";
  49 
  50         try {
  51             IIORegistry reg = IIORegistry.getDefaultInstance();
  52             ImageWriter writer = null;
  53             Iterator writerSpiIter = reg.getServiceProviders(ImageWriterSpi.class, true);
  54 
  55             while (writerSpiIter.hasNext()) {
  56                 ImageWriterSpi writerSpi = (ImageWriterSpi) writerSpiIter.next();
  57                 writer = writerSpi.createWriterInstance();
  58                 String names[] = writerSpi.getFormatNames();
  59 
  60                 FileOutputStream fos = new FileOutputStream("temp");
  61                 ImageOutputStream ios = ImageIO.createImageOutputStream(fos);
  62                 writer.setOutput(ios);
  63 
  64                 try {
  65                     writer.write(null, null, null);
  66                 } catch (IllegalArgumentException iae) {
  67                     System.out.println("PASS: Expected exception is thrown when null img is passed " +
  68                                        "to the write method, for the image format: " + names[0]);
  69                     System.out.println("\n");
  70                 } catch (Exception e) {
  71                     testFailed = true;
  72                     failMsg = failMsg + names[0] + ", ";
  73                 }
  74             }
  75 
  76         } catch (Exception e) {
  77             testFailed = true;
  78             throw new RuntimeException("Test Failed. Exception thrown: " + e.toString());
  79         }
  80         if (testFailed) {
  81             failMsg = failMsg.substring(0, failMsg.lastIndexOf(","));
  82             throw new RuntimeException(failMsg);
  83         }
  84     }
  85 
  86     public static void main(String args[]) {
  87         WriteNullImageTest test = new WriteNullImageTest();
  88     }
  89 }