Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/javax/imageio/stream/NullStreamCheckTest.java
38840 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// creates test file needed for read and write in local directory.61private static File createTestFile(String name) throws IOException {62String sep = System.getProperty("file.separator");63String dir = System.getProperty("test.src", ".");64String filePath = dir+sep;65File directory = new File(filePath);66File tmpTestFile = File.createTempFile(name, ".png", directory);67directory.delete();68return tmpTestFile;69}7071/* if we catch expected IOException message return72* false otherwise return true.73*/74private static boolean verifyOutputExceptionMessage(IOException ex) {75String message = ex.getMessage();76return (!message.equals("Can't create an ImageOutputStream!"));77}7879/* if we catch expected IOException message return80* false otherwise return true.81*/82private static boolean verifyInputExceptionMessage(IOException ex) {83String message = ex.getMessage();84return (!message.equals("Can't create an ImageInputStream!"));85}8687private static void verifyFileWrite() throws IOException {88File outputTestFile = createTestFile("outputTestFile");89try {90ImageIO.write(inputImage, "png", outputTestFile);91} catch (IOException ex) {92if (verifyOutputExceptionMessage(ex))93throw ex;94} finally {95outputTestFile.delete();96}97}9899private static void verifyStreamWrite() throws IOException {100try {101ImageIO.write(inputImage, "png", outputStream);102} catch (IOException ex) {103if (verifyOutputExceptionMessage(ex))104throw ex;105} finally {106try {107outputStream.close();108} catch (IOException ex) {109throw ex;110}111}112}113114private static void verifyFileRead() throws IOException {115File inputTestFile = createTestFile("inputTestFile");116try {117ImageIO.read(inputTestFile);118} catch (IOException ex) {119if (verifyInputExceptionMessage(ex))120throw ex;121} finally {122inputTestFile.delete();123}124}125126private static void verifyStreamRead() throws IOException {127try {128ImageIO.read(inputStream);129} catch (IOException ex) {130if (verifyInputExceptionMessage(ex))131throw ex;132} finally {133try {134inputStream.close();135} catch (IOException ex) {136throw ex;137}138}139}140141private static void verifyUrlRead() throws IOException {142URL url;143File inputTestUrlFile = createTestFile("inputTestFile");144try {145try {146url = inputTestUrlFile.toURI().toURL();147} catch (MalformedURLException ex) {148throw ex;149}150151try {152ImageIO.read(url);153} catch (IOException ex) {154if (verifyInputExceptionMessage(ex))155throw ex;156}157} finally {158inputTestUrlFile.delete();159}160}161162public static void main(String[] args) throws IOException,163MalformedURLException {164165/* deregister ImageOutputStreamSpi so that we creatImageOutputStream166* returns null while writing.167*/168localRegistry.deregisterAll(ImageOutputStreamSpi.class);169/* verify possible ImageIO.write() scenario's for null stream output170* from createImageOutputStream() API in ImageIO class.171*/172verifyFileWrite();173verifyStreamWrite();174175/* deregister ImageInputStreamSpi so that we creatImageInputStream176* returns null while reading.177*/178localRegistry.deregisterAll(ImageInputStreamSpi.class);179/* verify possible ImageIO.read() scenario's for null stream output180* from createImageInputStream API in ImageIO class.181*/182verifyFileRead();183verifyStreamRead();184verifyUrlRead();185}186187static class LocalOutputStream extends OutputStream {188189@Override190public void write(int i) throws IOException {191}192}193194static class LocalInputStream extends InputStream {195196@Override197public int read() throws IOException {198return 0;199}200}201}202203204