Path: blob/master/test/jdk/javax/imageio/plugins/jpeg/WritingInterruptionTest.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 WritingInterruptionTest29*/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.ImageWriter;40import javax.imageio.ImageWriteParam;41import javax.imageio.event.IIOWriteProgressListener;42import javax.imageio.stream.ImageOutputStream;434445public class WritingInterruptionTest implements IIOWriteProgressListener {4647static File pwd = new File(".");48static BufferedImage img;4950public static void main(String[] args) {51img = createTestImage();5253System.out.println("Test abort()....");54WritingInterruptionTest t = new WritingInterruptionTest(new AbortAction());55t.doTest();5657System.out.println("Test reset()....");58t = new WritingInterruptionTest(new ResetAction());59t.doTest();6061System.out.println("Test dispose()....");62t = new WritingInterruptionTest(new DisposeAction());63t.doTest();64}6566protected abstract static class Action implements Runnable {67protected ImageWriter target;6869public void setTarget(ImageWriter target) {70this.target = target;71}7273public abstract void run();74}7576protected static class DisposeAction extends Action {77public void run() {78try {79target.dispose();80} catch (IllegalStateException e) {81System.out.println("Test PASSED: IllegalStateException was thrown.");82} catch (Throwable e) {83throw new RuntimeException("Test FAILED.", e);84}85}86}8788protected static class AbortAction extends Action {89public void run() {90try {91target.abort();92} catch (IllegalStateException e) {93System.out.println("Test PASSED: IllegalStateException was thrown.");94} catch (Throwable e) {95throw new RuntimeException("Test FAILED.", e);96}97}98}99100protected static class ResetAction extends Action {101public void run() {102try {103target.reset();104} catch (IllegalStateException e) {105System.out.println("Test PASSED: IllegalStateException was thrown.");106} catch (Throwable e) {107throw new RuntimeException("Test FAILED.", e);108}109}110}111112113Action action;114ImageOutputStream ios;115ImageWriter w;116117protected WritingInterruptionTest(Action action) {118this.action = action;119120w = ImageIO.getImageWritersByFormatName("JPEG").next();121122this.action.setTarget(w);123}124125public void doTest() {126try {127w.addIIOWriteProgressListener(this);128File f = File.createTempFile("writer_", ".jpg", pwd);129ImageOutputStream ios = ImageIO.createImageOutputStream(f);130w.setOutput(ios);131Thread.sleep(70);132w.write(img);133Thread.sleep(70);134} catch (Exception e) {135/*136* we do expect that concurrent attempt to dispose this137* instance of image writer will be blocked. So, this image138* should be writen sucessfuly. Otherwise, something went wrong139* and we need to report test failure.140*/141throw new RuntimeException("Test FAILED", e);142} finally {143/*144* it would happen that concurrent invocation of dispose() method145* will be successful. Due to race condition it seems to be possible146* that dispose operation will be performed after than write() operation147* leaveled thread lock. In this case all subsequent calls for writer148* methods should results in IllegalStateException. So, we treat149* IllegalStateException as success. Everything else means test failure.150*/151try {152w.reset();153} catch (IllegalStateException e) {154System.out.println("Expected exception was caught: " + e);155} catch(Exception e) {156throw new RuntimeException("Test FAILED.", e);157}158}159System.out.println("Test PASSED.");160}161162// listener medthods163public void imageStarted(ImageWriter source,164int imageIndex) {} ;165166public void imageProgress(ImageWriter source,167float percentageDone)168{169if (20f < percentageDone && percentageDone < 80f ) {170Thread t = new Thread(action);171t.start();172}173};174175public void imageComplete(ImageWriter source) {};176177public void thumbnailStarted(ImageWriter source,178int imageIndex,179int thumbnailIndex) {};180181public void thumbnailProgress(ImageWriter source,182float percentageDone) {};183184public void thumbnailComplete(ImageWriter source) {};185186public void writeAborted(ImageWriter source) {};187188189private static BufferedImage createTestImage() {190int w = 1024;191int h = 768;192193BufferedImage img = new194BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);195Graphics2D g = img.createGraphics();196Color[] colors = { Color.red, Color.green, Color.blue };197float[] dist = {0.0f, 0.5f, 1.0f };198Point2D center = new Point2D.Float(0.5f * w, 0.5f * h);199200RadialGradientPaint p =201new RadialGradientPaint(center, 0.5f * w, dist, colors);202g.setPaint(p);203g.fillRect(0, 0, w, h);204g.dispose();205206return img;207}208}209210211