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 some of the read() and write() methods
28 *          are not able to get stream from createImageInputStream() and
29 *          createImageOutputStream() are we doing null check for stream
30 *          and throwing IOException as per specification.
31 * @run     main NullStreamCheckTest
32 */
33
34import java.awt.image.BufferedImage;
35import java.io.File;
36import java.io.IOException;
37import java.io.InputStream;
38import java.io.OutputStream;
39import java.net.MalformedURLException;
40import java.net.URL;
41import javax.imageio.ImageIO;
42import javax.imageio.spi.IIORegistry;
43import javax.imageio.spi.ImageInputStreamSpi;
44import javax.imageio.spi.ImageOutputStreamSpi;
45
46public class NullStreamCheckTest {
47
48    // get ImageIORegistry default instance.
49    private static final IIORegistry localRegistry = IIORegistry.
50            getDefaultInstance();
51    // stream variables needed for input and output.
52    static LocalOutputStream outputStream = new LocalOutputStream();
53    static LocalInputStream inputStream = new LocalInputStream();
54
55    static final int width = 50, height = 50;
56
57    // input and output BufferedImage needed while read and write.
58    static BufferedImage inputImage = new BufferedImage(width, height,
59            BufferedImage.TYPE_INT_ARGB);
60
61    // creates test file needed for read and write in local directory.
62    private static File createTestFile(String name) throws IOException {
63        String sep = System.getProperty("file.separator");
64        String dir = System.getProperty("test.src", ".");
65        String filePath = dir+sep;
66        File directory = new File(filePath);
67        File tmpTestFile = File.createTempFile(name, ".png", directory);
68        directory.delete();
69        return tmpTestFile;
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");
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");
117        try {
118            ImageIO.read(inputTestFile);
119        } catch (IOException ex) {
120            if (verifyInputExceptionMessage(ex))
121                throw ex;
122        } finally {
123            inputTestFile.delete();
124        }
125    }
126
127    private static void verifyStreamRead() throws IOException {
128        try {
129            ImageIO.read(inputStream);
130        } catch (IOException ex) {
131            if (verifyInputExceptionMessage(ex))
132                throw ex;
133        } finally {
134            try {
135                inputStream.close();
136            } catch (IOException ex) {
137                throw ex;
138            }
139        }
140    }
141
142    private static void verifyUrlRead() throws IOException {
143        URL url;
144        File inputTestUrlFile = createTestFile("inputTestFile");
145        try {
146            try {
147                url = inputTestUrlFile.toURI().toURL();
148            } catch (MalformedURLException ex) {
149                throw ex;
150            }
151
152            try {
153                ImageIO.read(url);
154            } catch (IOException ex) {
155                if (verifyInputExceptionMessage(ex))
156                    throw ex;
157            }
158        } finally {
159            inputTestUrlFile.delete();
160        }
161    }
162
163    public static void main(String[] args) throws IOException,
164                                                  MalformedURLException {
165
166        /* deregister ImageOutputStreamSpi so that we creatImageOutputStream
167         * returns null while writing.
168         */
169        localRegistry.deregisterAll(ImageOutputStreamSpi.class);
170        /* verify possible ImageIO.write() scenario's for null stream output
171         * from createImageOutputStream() API in ImageIO class.
172         */
173        verifyFileWrite();
174        verifyStreamWrite();
175
176        /* deregister ImageInputStreamSpi so that we creatImageInputStream
177         * returns null while reading.
178         */
179        localRegistry.deregisterAll(ImageInputStreamSpi.class);
180        /* verify possible ImageIO.read() scenario's for null stream output
181         * from createImageInputStream API in ImageIO class.
182         */
183        verifyFileRead();
184        verifyStreamRead();
185        verifyUrlRead();
186    }
187
188    static class LocalOutputStream extends OutputStream {
189
190        @Override
191        public void write(int i) throws IOException {
192        }
193    }
194
195    static class LocalInputStream extends InputStream {
196
197        @Override
198        public int read() throws IOException {
199            return 0;
200        }
201    }
202}
203