0N/A/*
2362N/A * Copyright (c) 2007, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/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.
0N/A */
0N/A
0N/A/**
0N/A * @test
0N/A * @bug 6557086 6547241
0N/A * @summary Test verifies that invocation of reset/abort/dispose methods from
0N/A * another thread does not cause crash in jpeg library.
0N/A * @run main ReadingInterruptionTest
0N/A */
0N/A
0N/Aimport java.awt.Color;
0N/Aimport java.awt.Graphics2D;
0N/Aimport java.awt.RadialGradientPaint;
0N/Aimport java.awt.geom.Point2D;
0N/Aimport java.awt.image.BufferedImage;
0N/Aimport java.io.File;
0N/Aimport java.io.IOException;
0N/Aimport javax.imageio.ImageIO;
0N/Aimport javax.imageio.ImageReader;
0N/Aimport javax.imageio.ImageReadParam;
0N/Aimport javax.imageio.event.IIOReadProgressListener;
0N/Aimport javax.imageio.stream.ImageInputStream;
0N/A
0N/A
0N/Apublic class ReadingInterruptionTest implements IIOReadProgressListener {
0N/A
0N/A public static void main(String[] args) {
0N/A createTestFile();
0N/A
0N/A System.out.println("Test abort()....");
0N/A ReadingInterruptionTest t = new ReadingInterruptionTest(new AbortAction());
0N/A t.doTest();
0N/A
0N/A System.out.println("Test reset()....");
0N/A t = new ReadingInterruptionTest(new ResetAction());
0N/A t.doTest();
0N/A
0N/A System.out.println("Test dispose()....");
0N/A t = new ReadingInterruptionTest(new DisposeAction());
0N/A t.doTest();
0N/A }
0N/A
0N/A protected abstract static class Action implements Runnable {
0N/A protected ImageReader target;
0N/A
0N/A public void setTarget(ImageReader target) {
0N/A this.target = target;
0N/A }
0N/A
0N/A public abstract void run();
0N/A }
0N/A
0N/A protected static class DisposeAction extends Action {
0N/A public void run() {
0N/A try {
0N/A target.dispose();
0N/A } catch (IllegalStateException e) {
0N/A System.out.println("Test PASSED: IllegalStateException was thrown.");
0N/A } catch (Throwable e) {
0N/A throw new RuntimeException("Test FAILED.", e);
0N/A }
0N/A }
0N/A }
0N/A
0N/A protected static class AbortAction extends Action {
0N/A public void run() {
0N/A try {
0N/A target.abort();
0N/A } catch (IllegalStateException e) {
0N/A System.out.println("Test PASSED: IllegalStateException was thrown.");
0N/A } catch (Throwable e) {
0N/A throw new RuntimeException("Test FAILED.", e);
0N/A }
0N/A }
0N/A }
0N/A
0N/A protected static class ResetAction extends Action {
0N/A public void run() {
0N/A try {
0N/A target.reset();
0N/A } catch (IllegalStateException e) {
0N/A System.out.println("Test PASSED: IllegalStateException was thrown.");
0N/A } catch (Throwable e) {
0N/A throw new RuntimeException("Test FAILED.", e);
0N/A }
0N/A }
0N/A }
0N/A
0N/A static File file = new File("IMGP1001.JPG");
0N/A
0N/A Action action;
0N/A ImageInputStream iis;
0N/A ImageReader reader;
0N/A
0N/A protected ReadingInterruptionTest(Action action) {
0N/A this.action = action;
0N/A
0N/A reader = ImageIO.getImageReadersByFormatName("JPEG").next();
0N/A
0N/A this.action.setTarget(reader);
0N/A }
0N/A
0N/A public void doTest() {
0N/A try {
0N/A reader.addIIOReadProgressListener(this);
0N/A iis = ImageIO.createImageInputStream(file);
0N/A reader.setInput(iis);
0N/A ImageReadParam p = reader.getDefaultReadParam();
0N/A Thread.sleep(70);
0N/A BufferedImage res = reader.read(0, p);
0N/A Thread.sleep(70);
0N/A } catch (Exception e) {
0N/A /*
0N/A * we do expect that concurrent attempt to dispose this
0N/A * instance of image reader will be blocked. So, this image
0N/A * should be read sucessfuly. Otherwise, something went wrong
0N/A * and we need to report test failure.
0N/A */
0N/A throw new RuntimeException("Test FAILED", e);
0N/A } finally {
0N/A /*
0N/A * it would happen that concurrent invocation of dispose() method
0N/A * will be successful. Due to race condition it seems to be possible
0N/A * that dispose operation will be performed after than read() operation
0N/A * leaveled thread lock. In this case all subsequent calls for reader
0N/A * methods should results in IllegalStateException. So, we treat
0N/A * IllegalStateException as success. Everything else means test failure.
0N/A */
0N/A try {
0N/A reader.reset();
0N/A } catch (IllegalStateException e) {
0N/A System.out.println("Expected exception was caught: " + e);
0N/A } catch(Exception e) {
0N/A throw new RuntimeException("Test FAILED.", e);
0N/A }
0N/A }
0N/A System.out.println("Test PASSED.");
0N/A }
0N/A
0N/A // listener medthods
0N/A public void imageStarted(ImageReader source,
0N/A int imageIndex) {} ;
0N/A
0N/A public void imageProgress(ImageReader source,
0N/A float percentageDone)
0N/A {
0N/A if (20f < percentageDone && percentageDone < 80f) {
0N/A Thread t = new Thread(action);
0N/A t.start();
0N/A }
0N/A };
0N/A
0N/A public void imageComplete(ImageReader source) {};
0N/A
0N/A
0N/A public void sequenceStarted(ImageReader source,
0N/A int minIndex) {};
0N/A
0N/A public void sequenceComplete(ImageReader source) {};
0N/A
0N/A public void thumbnailStarted(ImageReader source,
0N/A int imageIndex,
0N/A int thumbnailIndex) {};
0N/A
0N/A public void thumbnailProgress(ImageReader source,
0N/A float percentageDone) {};
0N/A
0N/A public void thumbnailComplete(ImageReader source) {};
0N/A
0N/A public void readAborted(ImageReader source) {};
0N/A
0N/A private static void createTestFile() {
0N/A int w = 1280;
0N/A int h = 1024;
0N/A
0N/A BufferedImage img = new
0N/A BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
0N/A Graphics2D g = img.createGraphics();
0N/A Color[] colors = { Color.red, Color.green, Color.blue };
0N/A float[] dist = {0.0f, 0.5f, 1.0f };
0N/A Point2D center = new Point2D.Float(0.5f * w, 0.5f * h);
0N/A
0N/A RadialGradientPaint p =
0N/A new RadialGradientPaint(center, 0.5f * w, dist, colors);
0N/A g.setPaint(p);
0N/A g.fillRect(0, 0, w, h);
0N/A g.dispose();
0N/A
0N/A try {
0N/A System.out.println("Create test image " + file.getAbsolutePath());
0N/A boolean b = ImageIO.write(img, "JPEG", file);
0N/A if (!b) {
0N/A throw new RuntimeException("Failed to create test image.");
0N/A }
0N/A } catch (IOException e) {
0N/A throw new RuntimeException("Test failed", e);
0N/A }
0N/A }
0N/A}