Path: blob/master/test/jdk/javax/imageio/WriteAbortTest.java
41144 views
/*1* Copyright (c) 2016, 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 816493126* @summary Test verifies that if we call ImageWriter.abort() in27* IIOWriteProgressListener.imageStarted() or28* IIOWriteProgressListener.imageProgress() are we29* calling IIOWriteProgressListener.readAborted() for all readers.30* @run main WriteAbortTest31*/32import java.awt.image.BufferedImage;33import java.io.File;34import javax.imageio.ImageIO;35import javax.imageio.stream.ImageInputStream;36import java.awt.Color;37import java.awt.Graphics2D;38import java.nio.file.Files;39import javax.imageio.ImageWriter;40import javax.imageio.event.IIOWriteProgressListener;41import javax.imageio.stream.ImageOutputStream;4243public class WriteAbortTest implements IIOWriteProgressListener {4445ImageWriter writer = null;46ImageOutputStream ios = null;47BufferedImage bimg = null;48File file;49boolean startAbort = false;50boolean startAborted = false;51boolean progressAbort = false;52boolean progressAborted = false;53Color srccolor = Color.red;54int width = 100;55int heght = 100;5657public WriteAbortTest(String format) throws Exception {58try {59System.out.println("Test for format " + format);60bimg = new BufferedImage(width, heght,61BufferedImage.TYPE_INT_RGB);6263Graphics2D g = bimg.createGraphics();64g.setColor(srccolor);65g.fillRect(0, 0, width, heght);66g.dispose();6768file = File.createTempFile("src_", "." + format, new File("."));69ImageInputStream ios = ImageIO.createImageOutputStream(file);7071ImageWriter writer =72ImageIO.getImageWritersByFormatName(format).next();7374writer.setOutput(ios);75writer.addIIOWriteProgressListener(this);7677// Abort writing in IIOWriteProgressListener.imageStarted().78startAbort = true;79writer.write(bimg);80startAbort = false;8182// Abort writing in IIOWriteProgressListener.imageProgress().83progressAbort = true;84writer.write(bimg);85progressAbort = false;8687ios.close();88/*89* All abort requests from imageStarted,imageProgress90* from IIOWriteProgressListener should be reached91* otherwise throw RuntimeException.92*/93if (!(startAborted94&& progressAborted)) {95throw new RuntimeException("All IIOWriteProgressListener abort"96+ " requests are not processed for format "97+ format);98}99} finally {100Files.delete(file.toPath());101}102}103104/*105* Abstract methods that we need to implement from106* IIOWriteProgressListener, and relevant for this test case.107*/108@Override109public void imageStarted(ImageWriter source, int imageIndex) {110System.out.println("imageStarted called");111if (startAbort) {112source.abort();113}114}115116@Override117public void imageProgress(ImageWriter source, float percentageDone) {118System.out.println("imageProgress called");119if (progressAbort) {120source.abort();121}122}123124@Override125public void writeAborted(ImageWriter source) {126System.out.println("writeAborted called");127// Verify IIOWriteProgressListener.imageStarted() abort request.128if (startAbort) {129System.out.println("imageStarted aborted ");130startAborted = true;131}132133// Verify IIOWriteProgressListener.imageProgress() abort request.134if (progressAbort) {135System.out.println("imageProgress aborted ");136progressAborted = true;137}138}139140public static void main(String args[]) throws Exception {141final String[] formats = {"bmp", "png", "gif", "jpg", "tif"};142for (String format : formats) {143new WriteAbortTest(format);144}145}146147/*148* Remaining abstract methods that we need to implement from149* IIOWriteProgressListener, but not relevant for this test case.150*/151@Override152public void imageComplete(ImageWriter source) {153}154155@Override156public void thumbnailStarted(ImageWriter source, int imageIndex,157int thumbnailIndex) {158}159160@Override161public void thumbnailProgress(ImageWriter source, float percentageDone) {162}163164@Override165public void thumbnailComplete(ImageWriter source) {166}167}168169170171