Path: blob/master/test/jdk/javax/imageio/stream/ShortStreamTest.java
41152 views
/*1* Copyright (c) 2015, 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 807495426* @summary Test verifies that an IOException is triggered if input stream27* does not contain enough data to read a multi-byte type.28*29* @run main ShortStreamTest30*/3132import javax.imageio.ImageIO;33import javax.imageio.stream.ImageInputStream;34import java.io.ByteArrayInputStream;35import java.io.IOException;3637public class ShortStreamTest {38public static void main(String[] args) throws IOException {39TestCase[] tests = createTests();4041for (TestCase t : tests) {42t.test();43}44}4546private static abstract class TestCase {47abstract void testRead(ImageInputStream iis) throws IOException;4849public void test() {50boolean gotException = false;5152ImageInputStream iis = createShortStream();5354try {55testRead(iis);56} catch (IOException e) {57e.printStackTrace(System.out);58gotException = true;59}6061if (!gotException) {62throw new RuntimeException("Test failed.");63}64System.out.println("Test PASSED");65}66}676869private static ImageInputStream createShortStream() {70try {71byte[] integerTestArray = new byte[] { 80 };72ByteArrayInputStream bais = new ByteArrayInputStream(integerTestArray);7374return ImageIO.createImageInputStream(bais);75} catch (IOException e) {76return null;77}78}7980private static TestCase[] createTests() {81return new TestCase[]{82new TestCase() {83@Override84void testRead(ImageInputStream iis) throws IOException {85iis.readInt();86}87},88new TestCase() {89@Override90void testRead(ImageInputStream iis) throws IOException {91iis.readShort();92}93},94new TestCase() {95@Override96void testRead(ImageInputStream iis) throws IOException {97iis.readDouble();98}99},100new TestCase() {101@Override102void testRead(ImageInputStream iis) throws IOException {103iis.readFloat();104}105},106new TestCase() {107@Override108void testRead(ImageInputStream iis) throws IOException {109iis.readLong();110}111},112new TestCase() {113@Override114void testRead(ImageInputStream iis) throws IOException {115iis.readUnsignedInt();116}117},118new TestCase() {119@Override120void testRead(ImageInputStream iis) throws IOException {121iis.readUnsignedShort();122}123}124};125}126}127128129