1 /*
   2  * Copyright (c) 2016, 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     8044289
  27  * @summary Test verifies that when createImageInputStream() or
  28  *          createImageOutputStream() returns null while read or write,
  29  *          are we doing null check properly.
  30  * @run     main NullStreamCheckTest
  31  */
  32 
  33 import java.awt.image.BufferedImage;
  34 import java.io.File;
  35 import java.io.IOException;
  36 import java.io.InputStream;
  37 import java.io.OutputStream;
  38 import java.net.MalformedURLException;
  39 import java.net.URL;
  40 import javax.imageio.ImageIO;
  41 import javax.imageio.spi.IIORegistry;
  42 import javax.imageio.spi.ImageInputStreamSpi;
  43 import javax.imageio.spi.ImageOutputStreamSpi;
  44 
  45 public class NullStreamCheckTest {
  46 
  47     // get ImageIORegistry default instance.
  48     private static final IIORegistry localRegistry = IIORegistry.
  49             getDefaultInstance();
  50     // stream variables needed for input and output.
  51     static LocalOutputStream outputStream = new LocalOutputStream();
  52     static LocalInputStream inputStream = new LocalInputStream();
  53 
  54     static final int width = 50, height = 50;
  55 
  56     // input and output BufferedImage needed while read and write.
  57     static BufferedImage inputImage = new BufferedImage(width, height,
  58             BufferedImage.TYPE_INT_ARGB);
  59     static BufferedImage outputImage = new BufferedImage(width, height,
  60             BufferedImage.TYPE_INT_ARGB);
  61 
  62     // creates test file needed for read and write in local directory.
  63     private static File createTestFile(String name) {
  64         String fileName = name;
  65         String sep = System.getProperty("file.separator");
  66         String dir = System.getProperty("test.src", ".");
  67         String filePath = dir+sep+fileName;
  68         File testFile = new File(filePath);
  69         return testFile;
  70     }
  71 
  72     /* if we catch expected IOException message return
  73      * false otherwise return true.
  74      */
  75     private static boolean verifyOutputExceptionMessage(IOException ex) {
  76         String message = ex.getMessage();
  77         return (!message.equals("Can't create an ImageOutputStream!"));
  78     }
  79 
  80     /* if we catch expected IOException message return
  81      * false otherwise return true.
  82      */
  83     private static boolean verifyInputExceptionMessage(IOException ex) {
  84         String message = ex.getMessage();
  85         return (!message.equals("Can't create an ImageInputStream!"));
  86     }
  87 
  88     private static void verifyFileWrite() throws IOException {
  89         File outputTestFile = createTestFile("outputTestFile.png");
  90         try {
  91             ImageIO.write(inputImage, "png", outputTestFile);
  92         } catch (IOException ex) {
  93             if (verifyOutputExceptionMessage(ex))
  94                 throw ex;
  95         } finally {
  96             outputTestFile.delete();
  97         }
  98     }
  99 
 100     private static void verifyStreamWrite() throws IOException {
 101         try {
 102             ImageIO.write(inputImage, "png", outputStream);
 103         } catch (IOException ex) {
 104             if (verifyOutputExceptionMessage(ex))
 105                 throw ex;
 106         } finally {
 107             try {
 108                 outputStream.close();
 109             } catch (IOException ex) {
 110                 throw ex;
 111             }
 112         }
 113     }
 114 
 115     private static void verifyFileRead() throws IOException {
 116         File inputTestFile = createTestFile("inputTestFile.png");
 117         try {
 118             outputImage = ImageIO.read(inputTestFile);
 119         } catch (IOException ex) {
 120             if (verifyInputExceptionMessage(ex))
 121                 throw ex;
 122         }
 123     }
 124 
 125     private static void verifyStreamRead() throws IOException {
 126         try {
 127             ImageIO.read(inputStream);
 128         } catch (IOException ex) {
 129             if (verifyInputExceptionMessage(ex))
 130                 throw ex;
 131         } finally {
 132             try {
 133                 inputStream.close();
 134             } catch (IOException ex) {
 135                 throw ex;
 136             }
 137         }
 138     }
 139 
 140     private static void verifyUrlRead() throws IOException {
 141         URL url = null;
 142         File inputTestFile = createTestFile("inputTestFile.png");
 143         try {
 144             url = inputTestFile.toURI().toURL();
 145         } catch (MalformedURLException ex) {
 146             throw ex;
 147         }
 148         try {
 149             ImageIO.read(url);
 150         } catch (IOException ex) {
 151             if (verifyInputExceptionMessage(ex))
 152                 throw ex;
 153         }
 154     }
 155 
 156     public static void main(String[] args) throws IOException,
 157                                                   MalformedURLException {
 158 
 159         /* deregister ImageOutputStreamSpi so that we creatImageOutputStream
 160          * returns null while writing.
 161          */
 162         localRegistry.deregisterAll(ImageOutputStreamSpi.class);
 163         /* verify possible ImageIO.write() scenraio's for null stream output
 164          * from createImageOutputStream() API in ImageIO class.
 165          */
 166         verifyFileWrite();
 167         verifyStreamWrite();
 168 
 169         /* deregister ImageInputStreamSpi so that we creatImageInputStream
 170          * returns null while reading.
 171          */
 172         localRegistry.deregisterAll(ImageInputStreamSpi.class);
 173         /* verify possible ImageIO.read() scenraio's for null stream output
 174          * from createImageInputStream API in ImageIO class.
 175          */
 176         verifyFileRead();
 177         verifyStreamRead();
 178         verifyUrlRead();
 179     }
 180 
 181     static class LocalOutputStream extends OutputStream {
 182 
 183         @Override
 184         public void write(int i) throws IOException {
 185         }
 186     }
 187 
 188     static class LocalInputStream extends InputStream {
 189 
 190         @Override
 191         public int read() throws IOException {
 192             return 0;
 193         }
 194     }
 195 }