Path: blob/master/test/jdk/javax/imageio/plugins/jpeg/ReadingInterruptionTest.java
41152 views
/*1* Copyright (c) 2007, 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 6557086 654724126* @summary Test verifies that invocation of reset/abort/dispose methods from27* another thread does not cause crash in jpeg library.28* @run main ReadingInterruptionTest29*/3031import java.awt.Color;32import java.awt.Graphics2D;33import java.awt.RadialGradientPaint;34import java.awt.geom.Point2D;35import java.awt.image.BufferedImage;36import java.io.File;37import java.io.IOException;38import javax.imageio.ImageIO;39import javax.imageio.ImageReader;40import javax.imageio.ImageReadParam;41import javax.imageio.event.IIOReadProgressListener;42import javax.imageio.stream.ImageInputStream;434445public class ReadingInterruptionTest implements IIOReadProgressListener {4647public static void main(String[] args) {48createTestFile();4950System.out.println("Test abort()....");51ReadingInterruptionTest t = new ReadingInterruptionTest(new AbortAction());52t.doTest();5354System.out.println("Test reset()....");55t = new ReadingInterruptionTest(new ResetAction());56t.doTest();5758System.out.println("Test dispose()....");59t = new ReadingInterruptionTest(new DisposeAction());60t.doTest();61}6263protected abstract static class Action implements Runnable {64protected ImageReader target;6566public void setTarget(ImageReader target) {67this.target = target;68}6970public abstract void run();71}7273protected static class DisposeAction extends Action {74public void run() {75try {76target.dispose();77} catch (IllegalStateException e) {78System.out.println("Test PASSED: IllegalStateException was thrown.");79} catch (Throwable e) {80throw new RuntimeException("Test FAILED.", e);81}82}83}8485protected static class AbortAction extends Action {86public void run() {87try {88target.abort();89} catch (IllegalStateException e) {90System.out.println("Test PASSED: IllegalStateException was thrown.");91} catch (Throwable e) {92throw new RuntimeException("Test FAILED.", e);93}94}95}9697protected static class ResetAction extends Action {98public void run() {99try {100target.reset();101} catch (IllegalStateException e) {102System.out.println("Test PASSED: IllegalStateException was thrown.");103} catch (Throwable e) {104throw new RuntimeException("Test FAILED.", e);105}106}107}108109static File file = new File("IMGP1001.JPG");110111Action action;112ImageInputStream iis;113ImageReader reader;114115protected ReadingInterruptionTest(Action action) {116this.action = action;117118reader = ImageIO.getImageReadersByFormatName("JPEG").next();119120this.action.setTarget(reader);121}122123public void doTest() {124try {125reader.addIIOReadProgressListener(this);126iis = ImageIO.createImageInputStream(file);127reader.setInput(iis);128ImageReadParam p = reader.getDefaultReadParam();129Thread.sleep(70);130BufferedImage res = reader.read(0, p);131Thread.sleep(70);132} catch (Exception e) {133/*134* we do expect that concurrent attempt to dispose this135* instance of image reader will be blocked. So, this image136* should be read sucessfuly. Otherwise, something went wrong137* and we need to report test failure.138*/139throw new RuntimeException("Test FAILED", e);140} finally {141/*142* it would happen that concurrent invocation of dispose() method143* will be successful. Due to race condition it seems to be possible144* that dispose operation will be performed after than read() operation145* leaveled thread lock. In this case all subsequent calls for reader146* methods should results in IllegalStateException. So, we treat147* IllegalStateException as success. Everything else means test failure.148*/149try {150reader.reset();151} catch (IllegalStateException e) {152System.out.println("Expected exception was caught: " + e);153} catch(Exception e) {154throw new RuntimeException("Test FAILED.", e);155}156}157System.out.println("Test PASSED.");158}159160// listener medthods161public void imageStarted(ImageReader source,162int imageIndex) {} ;163164public void imageProgress(ImageReader source,165float percentageDone)166{167if (20f < percentageDone && percentageDone < 80f) {168Thread t = new Thread(action);169t.start();170}171};172173public void imageComplete(ImageReader source) {};174175176public void sequenceStarted(ImageReader source,177int minIndex) {};178179public void sequenceComplete(ImageReader source) {};180181public void thumbnailStarted(ImageReader source,182int imageIndex,183int thumbnailIndex) {};184185public void thumbnailProgress(ImageReader source,186float percentageDone) {};187188public void thumbnailComplete(ImageReader source) {};189190public void readAborted(ImageReader source) {};191192private static void createTestFile() {193int w = 1280;194int h = 1024;195196BufferedImage img = new197BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);198Graphics2D g = img.createGraphics();199Color[] colors = { Color.red, Color.green, Color.blue };200float[] dist = {0.0f, 0.5f, 1.0f };201Point2D center = new Point2D.Float(0.5f * w, 0.5f * h);202203RadialGradientPaint p =204new RadialGradientPaint(center, 0.5f * w, dist, colors);205g.setPaint(p);206g.fillRect(0, 0, w, h);207g.dispose();208209try {210System.out.println("Create test image " + file.getAbsolutePath());211boolean b = ImageIO.write(img, "JPEG", file);212if (!b) {213throw new RuntimeException("Failed to create test image.");214}215} catch (IOException e) {216throw new RuntimeException("Test failed", e);217}218}219}220221222