Path: blob/master/test/jdk/javax/imageio/stream/NullStreamCheckTest.java
41152 views
/*1* Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/**24* @test25* @bug 804428926* @summary Test verifies that when some of the read() and write() methods27* are not able to get stream from createImageInputStream() and28* createImageOutputStream() are we doing null check for stream29* and throwing IOException as per specification.30* @run main NullStreamCheckTest31*/3233import java.awt.image.BufferedImage;34import java.io.File;35import java.io.IOException;36import java.io.InputStream;37import java.io.OutputStream;38import java.net.MalformedURLException;39import java.net.URL;40import javax.imageio.ImageIO;41import javax.imageio.spi.IIORegistry;42import javax.imageio.spi.ImageInputStreamSpi;43import javax.imageio.spi.ImageOutputStreamSpi;4445public class NullStreamCheckTest {4647// get ImageIORegistry default instance.48private static final IIORegistry localRegistry = IIORegistry.49getDefaultInstance();50// stream variables needed for input and output.51static LocalOutputStream outputStream = new LocalOutputStream();52static LocalInputStream inputStream = new LocalInputStream();5354static final int width = 50, height = 50;5556// input and output BufferedImage needed while read and write.57static BufferedImage inputImage = new BufferedImage(width, height,58BufferedImage.TYPE_INT_ARGB);5960/* if we catch expected IOException message return61* false otherwise return true.62*/63private static boolean verifyOutputExceptionMessage(IOException ex) {64String message = ex.getMessage();65return (!message.equals("Can't create an ImageOutputStream!"));66}6768/* if we catch expected IOException message return69* false otherwise return true.70*/71private static boolean verifyInputExceptionMessage(IOException ex) {72String message = ex.getMessage();73return (!message.equals("Can't create an ImageInputStream!"));74}7576private static void verifyFileWrite() throws IOException {77File outputTestFile = File.createTempFile("outputTestFile", ".png");78try {79ImageIO.write(inputImage, "png", outputTestFile);80} catch (IOException ex) {81if (verifyOutputExceptionMessage(ex))82throw ex;83} finally {84outputTestFile.delete();85}86}8788private static void verifyStreamWrite() throws IOException {89try {90ImageIO.write(inputImage, "png", outputStream);91} catch (IOException ex) {92if (verifyOutputExceptionMessage(ex))93throw ex;94} finally {95try {96outputStream.close();97} catch (IOException ex) {98throw ex;99}100}101}102103private static void verifyFileRead() throws IOException {104File inputTestFile = File.createTempFile("inputTestFile", ".png");105try {106ImageIO.read(inputTestFile);107} catch (IOException ex) {108if (verifyInputExceptionMessage(ex))109throw ex;110} finally {111inputTestFile.delete();112}113}114115private static void verifyStreamRead() throws IOException {116try {117ImageIO.read(inputStream);118} catch (IOException ex) {119if (verifyInputExceptionMessage(ex))120throw ex;121} finally {122try {123inputStream.close();124} catch (IOException ex) {125throw ex;126}127}128}129130private static void verifyUrlRead() throws IOException {131URL url;132File inputTestUrlFile = File.createTempFile("inputTestFile", ".png");133try {134try {135url = inputTestUrlFile.toURI().toURL();136} catch (MalformedURLException ex) {137throw ex;138}139140try {141ImageIO.read(url);142} catch (IOException ex) {143if (verifyInputExceptionMessage(ex))144throw ex;145}146} finally {147inputTestUrlFile.delete();148}149}150151public static void main(String[] args) throws IOException,152MalformedURLException {153154/* deregister ImageOutputStreamSpi so that we creatImageOutputStream155* returns null while writing.156*/157localRegistry.deregisterAll(ImageOutputStreamSpi.class);158/* verify possible ImageIO.write() scenario's for null stream output159* from createImageOutputStream() API in ImageIO class.160*/161verifyFileWrite();162verifyStreamWrite();163164/* deregister ImageInputStreamSpi so that we creatImageInputStream165* returns null while reading.166*/167localRegistry.deregisterAll(ImageInputStreamSpi.class);168/* verify possible ImageIO.read() scenario's for null stream output169* from createImageInputStream API in ImageIO class.170*/171verifyFileRead();172verifyStreamRead();173verifyUrlRead();174}175176static class LocalOutputStream extends OutputStream {177178@Override179public void write(int i) throws IOException {180}181}182183static class LocalInputStream extends InputStream {184185@Override186public int read() throws IOException {187return 0;188}189}190}191192193