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  *          does read() returns null and write() returns false.
  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) throws IOException {
  64         String sep = System.getProperty("file.separator");
  65         String dir = System.getProperty("test.src", ".");
  66         String filePath = dir+sep;
  67         File directory = new File(filePath);
  68         File tmpTestFile = File.createTempFile(name, ".png", directory);
  69         directory.delete();
  70         return tmpTestFile;
  71     }
  72 
  73     private static void verifyFileWrite() throws IOException {
  74         File outputTestFile = createTestFile("outputTestFile");
  75         Boolean verify;
  76         try {
  77             verify = ImageIO.write(inputImage, "png", outputTestFile);
  78             if (verify != false) {
  79                 throw new RuntimeException("ImageIO.write() is not following"
  80                         + " the specification");
  81             }
  82         } catch (IOException ex) {
  83             throw ex;
  84         } finally {
  85             outputTestFile.delete();
  86         }
  87     }
  88 
  89     private static void verifyStreamWrite() throws IOException {
  90         Boolean verify;
  91         try {
  92             verify = ImageIO.write(inputImage, "png", outputStream);
  93             if (verify != false) {
  94                 throw new RuntimeException("ImageIO.write() is not following"
  95                         + " the specification");
  96             }
  97         } catch (IOException ex) {
  98             throw ex;
  99         } finally {
 100             try {
 101                 outputStream.close();
 102             } catch (IOException ex) {
 103                 throw ex;
 104             }
 105         }
 106     }
 107 
 108     private static void verifyFileRead() throws IOException {
 109         File inputTestFile = createTestFile("inputTestFile");
 110         try {
 111             outputImage = ImageIO.read(inputTestFile);
 112             if (outputImage != null) {
 113                 throw new RuntimeException("ImageIO.read() is not following"
 114                         + " the specification");
 115             }
 116         } catch (IOException ex) {
 117             throw ex;
 118         } finally {
 119             inputTestFile.delete();
 120         }
 121     }
 122 
 123     private static void verifyStreamRead() throws IOException {
 124         try {
 125             outputImage = ImageIO.read(inputStream);
 126             if (outputImage != null) {
 127                 throw new RuntimeException("ImageIO.read() is not following"
 128                         + " the specification");
 129             }
 130         } catch (IOException ex) {
 131             throw ex;
 132         } finally {
 133             try {
 134                 inputStream.close();
 135             } catch (IOException ex) {
 136                 throw ex;
 137             }
 138         }
 139     }
 140 
 141     private static void verifyUrlRead() throws IOException {
 142         URL url;
 143         File inputTestUrlFile = createTestFile("inputTestFile");
 144         try {
 145             try {
 146                 url = inputTestUrlFile.toURI().toURL();
 147             } catch (MalformedURLException ex) {
 148                 throw ex;
 149             }
 150 
 151             try {
 152                 outputImage = ImageIO.read(url);
 153                 if (outputImage != null) {
 154                     throw new RuntimeException("ImageIO.read() is not"
 155                             + " following the specification");
 156                 }
 157             } catch (IOException ex) {
 158                 throw ex;
 159             }
 160         } finally {
 161             inputTestUrlFile.delete();
 162         }
 163     }
 164 
 165     public static void main(String[] args) throws IOException,
 166                                                   MalformedURLException {
 167 
 168         /* deregister ImageOutputStreamSpi so that we creatImageOutputStream
 169          * returns null while writing.
 170          */
 171         localRegistry.deregisterAll(ImageOutputStreamSpi.class);
 172         /* verify possible ImageIO.write() scenario's for null stream output
 173          * from createImageOutputStream() API in ImageIO class.
 174          */
 175         verifyFileWrite();
 176         verifyStreamWrite();
 177 
 178         /* deregister ImageInputStreamSpi so that we creatImageInputStream
 179          * returns null while reading.
 180          */
 181         localRegistry.deregisterAll(ImageInputStreamSpi.class);
 182         /* verify possible ImageIO.read() scenario's for null stream output
 183          * from createImageInputStream API in ImageIO class.
 184          */
 185         verifyFileRead();
 186         verifyStreamRead();
 187         verifyUrlRead();
 188     }
 189 
 190     static class LocalOutputStream extends OutputStream {
 191 
 192         @Override
 193         public void write(int i) throws IOException {
 194         }
 195     }
 196 
 197     static class LocalInputStream extends InputStream {
 198 
 199         @Override
 200         public int read() throws IOException {
 201             return 0;
 202         }
 203     }
 204 }