1290N/A/*
2362N/A * Copyright (c) 2009, Oracle and/or its affiliates. All rights reserved.
1290N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
1290N/A *
1290N/A * This code is free software; you can redistribute it and/or modify it
1290N/A * under the terms of the GNU General Public License version 2 only, as
1290N/A * published by the Free Software Foundation.
1290N/A *
1290N/A * This code is distributed in the hope that it will be useful, but WITHOUT
1290N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1290N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
1290N/A * version 2 for more details (a copy is included in the LICENSE file that
1290N/A * accompanied this code).
1290N/A *
1290N/A * You should have received a copy of the GNU General Public License version
1290N/A * 2 along with this work; if not, write to the Free Software Foundation,
1290N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
1290N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
1290N/A */
1290N/A
1290N/A/**
1290N/A * @test
1290N/A * @bug 5101862
1290N/A * @summary Test verifies that SPI of WBMP image reader
1290N/A * does not claims to be able to decode QT movies,
1290N/A * tga images, or ico files.
1290N/A * @run main CanDecodeTest
1290N/A */
1290N/A
1290N/Aimport java.io.ByteArrayInputStream;
1290N/Aimport java.io.File;
1290N/Aimport java.io.FileOutputStream;
1290N/Aimport java.io.IOException;
1290N/Aimport java.io.InputStream;
1290N/Aimport java.util.Arrays;
1290N/Aimport java.util.Vector;
1290N/Aimport javax.imageio.ImageIO;
1290N/Aimport javax.imageio.ImageReader;
1290N/Aimport javax.imageio.spi.ImageReaderSpi;
1290N/Aimport javax.imageio.stream.ImageInputStream;
1290N/A
1290N/Apublic class CanDecodeTest {
1290N/A
1290N/A public static void main(String[] args) throws IOException {
1290N/A ImageReader r =
1290N/A ImageIO.getImageReadersByFormatName("WBMP").next();
1290N/A ImageReaderSpi spi = r.getOriginatingProvider();
1290N/A
1290N/A Vector<TestCase> tests = getTestCases();
1290N/A for (TestCase t : tests) {
1290N/A t.doTest(spi);
1290N/A }
1290N/A System.out.println("Test passed.");
1290N/A }
1290N/A
1290N/A private static Vector<TestCase> getTestCases() {
1290N/A Vector<TestCase> v = new Vector<TestCase>(4);
1290N/A v.add(new TestCase("wbmp", new byte[]{(byte) 0x00, (byte) 0x00,
1290N/A (byte) 0x60, (byte) 0x14}, 244, true));
1290N/A v.add(new TestCase("mov", new byte[]{(byte) 0x00, (byte) 0x00,
1290N/A (byte) 0x07, (byte) 0xb5, (byte) 0x6d}, 82397, false));
1290N/A v.add(new TestCase("tga", new byte[]{(byte) 0x00, (byte) 0x00,
1290N/A (byte) 0x0a, (byte) 0x00}, 39693, false));
1290N/A v.add(new TestCase("ico", new byte[]{(byte) 0x00, (byte) 0x00,
1290N/A (byte) 0x01, (byte) 0x00}, 1078, false));
1290N/A return v;
1290N/A }
1290N/A
1290N/A private static class TestCase {
1290N/A
1290N/A private String title;
1290N/A private byte[] header;
1290N/A private int dataLength;
1290N/A private boolean canDecode;
1290N/A
1290N/A public TestCase(String title, byte[] header,
1290N/A int dataLength, boolean canDecode) {
1290N/A this.title = title;
1290N/A this.dataLength = dataLength;
1290N/A this.header = header.clone();
1290N/A this.canDecode = canDecode;
1290N/A
1290N/A }
1290N/A
1290N/A public void doTest(ImageReaderSpi spi) throws IOException {
1290N/A System.out.println("Test for " + title +
1290N/A (canDecode ? " (can decode)" : " (can't decode)"));
1290N/A System.out.print("As a stream...");
1290N/A ImageInputStream iis =
1290N/A ImageIO.createImageInputStream(getDataStream());
1290N/A
1290N/A if (spi.canDecodeInput(iis) != canDecode) {
1290N/A throw new RuntimeException("Test failed: wrong decideion " +
1290N/A "for stream data");
1290N/A }
1290N/A System.out.println("OK");
1290N/A
1290N/A System.out.print("As a file...");
1290N/A iis = ImageIO.createImageInputStream(getDataFile());
1290N/A if (spi.canDecodeInput(iis) != canDecode) {
1290N/A throw new RuntimeException("Test failed: wrong decideion " +
1290N/A "for file data");
1290N/A }
1290N/A System.out.println("OK");
1290N/A }
1290N/A
1290N/A private byte[] getData() {
1290N/A byte[] data = new byte[dataLength];
1290N/A Arrays.fill(data, (byte) 0);
1290N/A System.arraycopy(header, 0, data, 0, header.length);
1290N/A
1290N/A return data;
1290N/A }
1290N/A public InputStream getDataStream() {
1290N/A return new ByteArrayInputStream(getData());
1290N/A }
1290N/A
1290N/A public File getDataFile() throws IOException {
1290N/A File f = File.createTempFile("wbmp_", "." + title, new File("."));
1290N/A FileOutputStream fos = new FileOutputStream(f);
1290N/A fos.write(getData());
1290N/A fos.flush();
1290N/A fos.close();
1290N/A
1290N/A return f;
1290N/A }
1290N/A }
1290N/A}