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