1/*
2 * Copyright (c) 2003, 2017, 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 4924512
27 * @summary Test that wbmp image reader detects incorrect image format
28 */
29
30import java.awt.Color;
31import java.awt.Graphics;
32import java.awt.image.BufferedImage;
33import java.io.ByteArrayInputStream;
34import java.io.ByteArrayOutputStream;
35
36import javax.imageio.IIOException;
37import javax.imageio.ImageIO;
38import javax.imageio.ImageReader;
39import javax.imageio.stream.ImageInputStream;
40
41public class ValidWbmpTest {
42
43    public static void main(String[] args) {
44        try {
45            String[] formats = { "JPEG", "PNG", "BMP" };
46
47            BufferedImage img = new BufferedImage(100, 100,
48                                                  BufferedImage.TYPE_BYTE_GRAY);
49            Graphics g = img.createGraphics();
50            g.setColor(Color.white);
51            g.fillRect(0,0,100,100);
52            g.setColor(Color.black);
53            g.fillRect(10,10,80,80);
54
55            ImageReader ir = (ImageReader)ImageIO.getImageReadersByFormatName("WBMP").next();
56            if (ir==null) {
57                throw new RuntimeException("No readers for WBMP format!");
58            }
59            for(int i=0; i<formats.length; i++) {
60                System.out.println("Test " + formats[i] + " stream...");
61                boolean passed = false;
62                ByteArrayOutputStream baos = new ByteArrayOutputStream();
63                ImageIO.write(img, formats[i], baos);
64                baos.close();
65                ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
66
67                ImageInputStream iis = null;
68                iis = ImageIO.createImageInputStream(bais);
69                ir.setInput(iis);
70                try {
71                    BufferedImage res = ir.read(0);
72                } catch (IIOException e) {
73                    StackTraceElement[] stack = e.getStackTrace();
74                    if (ir.getClass().getName().equals(stack[0].getClassName())
75                        && "readHeader".equals(stack[0].getMethodName()))
76                    {
77                        passed = true;
78                    }
79                } catch (Throwable t) {
80                    t.printStackTrace();
81                }
82
83                if (!passed) {
84                    throw new RuntimeException("Test failed!");
85                }
86            }
87        } catch (Exception e) {
88            e.printStackTrace();
89            throw new RuntimeException("Unexpected exception. Test failed.");
90        }
91
92    }
93}
94