Path: blob/master/test/jdk/javax/imageio/plugins/wbmp/CanDecodeTest.java
41153 views
/*1* Copyright (c) 2009, 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 510186226* @summary Test verifies that SPI of WBMP image reader27* does not claims to be able to decode QT movies,28* tga images, or ico files.29* @run main CanDecodeTest30*/3132import java.io.ByteArrayInputStream;33import java.io.File;34import java.io.FileOutputStream;35import java.io.IOException;36import java.io.InputStream;37import java.util.Arrays;38import java.util.Vector;39import javax.imageio.ImageIO;40import javax.imageio.ImageReader;41import javax.imageio.spi.ImageReaderSpi;42import javax.imageio.stream.ImageInputStream;4344public class CanDecodeTest {4546public static void main(String[] args) throws IOException {47ImageReader r =48ImageIO.getImageReadersByFormatName("WBMP").next();49ImageReaderSpi spi = r.getOriginatingProvider();5051Vector<TestCase> tests = getTestCases();52for (TestCase t : tests) {53t.doTest(spi);54}55System.out.println("Test passed.");56}5758private static Vector<TestCase> getTestCases() {59Vector<TestCase> v = new Vector<TestCase>(4);60v.add(new TestCase("wbmp", new byte[]{(byte) 0x00, (byte) 0x00,61(byte) 0x60, (byte) 0x14}, 244, true));62v.add(new TestCase("mov", new byte[]{(byte) 0x00, (byte) 0x00,63(byte) 0x07, (byte) 0xb5, (byte) 0x6d}, 82397, false));64v.add(new TestCase("tga", new byte[]{(byte) 0x00, (byte) 0x00,65(byte) 0x0a, (byte) 0x00}, 39693, false));66v.add(new TestCase("ico", new byte[]{(byte) 0x00, (byte) 0x00,67(byte) 0x01, (byte) 0x00}, 1078, false));68return v;69}7071private static class TestCase {7273private String title;74private byte[] header;75private int dataLength;76private boolean canDecode;7778public TestCase(String title, byte[] header,79int dataLength, boolean canDecode) {80this.title = title;81this.dataLength = dataLength;82this.header = header.clone();83this.canDecode = canDecode;8485}8687public void doTest(ImageReaderSpi spi) throws IOException {88System.out.println("Test for " + title +89(canDecode ? " (can decode)" : " (can't decode)"));90System.out.print("As a stream...");91ImageInputStream iis =92ImageIO.createImageInputStream(getDataStream());9394if (spi.canDecodeInput(iis) != canDecode) {95throw new RuntimeException("Test failed: wrong decideion " +96"for stream data");97}98System.out.println("OK");99100System.out.print("As a file...");101iis = ImageIO.createImageInputStream(getDataFile());102if (spi.canDecodeInput(iis) != canDecode) {103throw new RuntimeException("Test failed: wrong decideion " +104"for file data");105}106System.out.println("OK");107}108109private byte[] getData() {110byte[] data = new byte[dataLength];111Arrays.fill(data, (byte) 0);112System.arraycopy(header, 0, data, 0, header.length);113114return data;115}116public InputStream getDataStream() {117return new ByteArrayInputStream(getData());118}119120public File getDataFile() throws IOException {121File f = File.createTempFile("wbmp_", "." + title, new File("."));122FileOutputStream fos = new FileOutputStream(f);123fos.write(getData());124fos.flush();125fos.close();126127return f;128}129}130}131132133