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 60 // creates test file needed for read and write in local directory. 61 private static File createTestFile(String name) throws IOException { 62 String sep = System.getProperty("file.separator"); 63 String dir = System.getProperty("test.src", "."); 64 String filePath = dir+sep; 65 File directory = new File(filePath); 66 File tmpTestFile = File.createTempFile(name, ".png", directory); 67 directory.delete(); 68 return tmpTestFile; 69 } 70 71 /* if we catch expected IOException message return 72 * false otherwise return true. 73 */ 74 private static boolean verifyOutputExceptionMessage(IOException ex) { 75 String message = ex.getMessage(); 76 return (!message.equals("Can't create an ImageOutputStream!")); 77 } 78 79 /* if we catch expected IOException message return 80 * false otherwise return true. 81 */ 82 private static boolean verifyInputExceptionMessage(IOException ex) { 83 String message = ex.getMessage(); 84 return (!message.equals("Can't create an ImageInputStream!")); 85 } 86 87 private static void verifyFileWrite() throws IOException { 88 File outputTestFile = createTestFile("outputTestFile"); 89 try { 90 ImageIO.write(inputImage, "png", outputTestFile); 91 } catch (IOException ex) { 92 if (verifyOutputExceptionMessage(ex)) 93 throw ex; 94 } finally { 95 outputTestFile.delete(); 96 } 97 } 98 99 private static void verifyStreamWrite() throws IOException { 100 try { 101 ImageIO.write(inputImage, "png", outputStream); 102 } catch (IOException ex) { 103 if (verifyOutputExceptionMessage(ex)) 104 throw ex; 105 } finally { 106 try { 107 outputStream.close(); 108 } catch (IOException ex) { 109 throw ex; 110 } 111 } 112 } 113 114 private static void verifyFileRead() throws IOException { 115 File inputTestFile = createTestFile("inputTestFile"); 116 try { 117 ImageIO.read(inputTestFile); 118 } catch (IOException ex) { 119 if (verifyInputExceptionMessage(ex)) 120 throw ex; 121 } finally { 122 inputTestFile.delete(); 123 } 124 } 125 126 private static void verifyStreamRead() throws IOException { 127 try { 128 ImageIO.read(inputStream); 129 } catch (IOException ex) { 130 if (verifyInputExceptionMessage(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 ImageIO.read(url); 153 } catch (IOException ex) { 154 if (verifyInputExceptionMessage(ex)) 155 throw ex; 156 } 157 } finally { 158 inputTestUrlFile.delete(); 159 } 160 } 161 162 public static void main(String[] args) throws IOException, 163 MalformedURLException { 164 165 /* deregister ImageOutputStreamSpi so that we creatImageOutputStream 166 * returns null while writing. 167 */ 168 localRegistry.deregisterAll(ImageOutputStreamSpi.class); 169 /* verify possible ImageIO.write() scenario's for null stream output 170 * from createImageOutputStream() API in ImageIO class. 171 */ 172 verifyFileWrite(); 173 verifyStreamWrite(); 174 175 /* deregister ImageInputStreamSpi so that we creatImageInputStream 176 * returns null while reading. 177 */ 178 localRegistry.deregisterAll(ImageInputStreamSpi.class); 179 /* verify possible ImageIO.read() scenario's for null stream output 180 * from createImageInputStream API in ImageIO class. 181 */ 182 verifyFileRead(); 183 verifyStreamRead(); 184 verifyUrlRead(); 185 } 186 187 static class LocalOutputStream extends OutputStream { 188 189 @Override 190 public void write(int i) throws IOException { 191 } 192 } 193 194 static class LocalInputStream extends InputStream { 195 196 @Override 197 public int read() throws IOException { 198 return 0; 199 } 200 } 201 }