1 /* 2 * Copyright (c) 2009, 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 5101862 27 * @summary Test verifies that SPI of WBMP image reader 28 * does not claims to be able to decode QT movies, 29 * tga images, or ico files. 30 * @run main CanDecodeTest 31 */ 32 33 import java.io.ByteArrayInputStream; 34 import java.io.File; 35 import java.io.FileOutputStream; 36 import java.io.IOException; 37 import java.io.InputStream; 38 import java.util.Arrays; 39 import java.util.Vector; 40 import javax.imageio.ImageIO; 41 import javax.imageio.ImageReader; 42 import javax.imageio.spi.ImageReaderSpi; 43 import javax.imageio.stream.ImageInputStream; 44 45 public class CanDecodeTest { 46 main(String[] args)47 public static void main(String[] args) throws IOException { 48 ImageReader r = 49 ImageIO.getImageReadersByFormatName("WBMP").next(); 50 ImageReaderSpi spi = r.getOriginatingProvider(); 51 52 Vector<TestCase> tests = getTestCases(); 53 for (TestCase t : tests) { 54 t.doTest(spi); 55 } 56 System.out.println("Test passed."); 57 } 58 getTestCases()59 private static Vector<TestCase> getTestCases() { 60 Vector<TestCase> v = new Vector<TestCase>(4); 61 v.add(new TestCase("wbmp", new byte[]{(byte) 0x00, (byte) 0x00, 62 (byte) 0x60, (byte) 0x14}, 244, true)); 63 v.add(new TestCase("mov", new byte[]{(byte) 0x00, (byte) 0x00, 64 (byte) 0x07, (byte) 0xb5, (byte) 0x6d}, 82397, false)); 65 v.add(new TestCase("tga", new byte[]{(byte) 0x00, (byte) 0x00, 66 (byte) 0x0a, (byte) 0x00}, 39693, false)); 67 v.add(new TestCase("ico", new byte[]{(byte) 0x00, (byte) 0x00, 68 (byte) 0x01, (byte) 0x00}, 1078, false)); 69 return v; 70 } 71 72 private static class TestCase { 73 74 private String title; 75 private byte[] header; 76 private int dataLength; 77 private boolean canDecode; 78 TestCase(String title, byte[] header, int dataLength, boolean canDecode)79 public TestCase(String title, byte[] header, 80 int dataLength, boolean canDecode) { 81 this.title = title; 82 this.dataLength = dataLength; 83 this.header = header.clone(); 84 this.canDecode = canDecode; 85 86 } 87 doTest(ImageReaderSpi spi)88 public void doTest(ImageReaderSpi spi) throws IOException { 89 System.out.println("Test for " + title + 90 (canDecode ? " (can decode)" : " (can't decode)")); 91 System.out.print("As a stream..."); 92 ImageInputStream iis = 93 ImageIO.createImageInputStream(getDataStream()); 94 95 if (spi.canDecodeInput(iis) != canDecode) { 96 throw new RuntimeException("Test failed: wrong decideion " + 97 "for stream data"); 98 } 99 System.out.println("OK"); 100 101 System.out.print("As a file..."); 102 iis = ImageIO.createImageInputStream(getDataFile()); 103 if (spi.canDecodeInput(iis) != canDecode) { 104 throw new RuntimeException("Test failed: wrong decideion " + 105 "for file data"); 106 } 107 System.out.println("OK"); 108 } 109 getData()110 private byte[] getData() { 111 byte[] data = new byte[dataLength]; 112 Arrays.fill(data, (byte) 0); 113 System.arraycopy(header, 0, data, 0, header.length); 114 115 return data; 116 } getDataStream()117 public InputStream getDataStream() { 118 return new ByteArrayInputStream(getData()); 119 } 120 getDataFile()121 public File getDataFile() throws IOException { 122 File f = File.createTempFile("wbmp_", "." + title, new File(".")); 123 FileOutputStream fos = new FileOutputStream(f); 124 fos.write(getData()); 125 fos.flush(); 126 fos.close(); 127 128 return f; 129 } 130 } 131 } 132