Path: blob/master/test/jdk/javax/imageio/plugins/shared/CanWriteSequence.java
41153 views
/*1* Copyright (c) 2015, 2020, 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*/2223import java.io.File;24import java.io.FileOutputStream;25import java.nio.file.Files;26import java.util.Iterator;2728import javax.imageio.ImageIO;29import javax.imageio.ImageWriter;30import javax.imageio.metadata.IIOMetadata;31import javax.imageio.spi.IIORegistry;32import javax.imageio.spi.ImageWriterSpi;33import javax.imageio.stream.ImageOutputStream;3435/**36* @test37* @bug 4958064 818334938* @summary Test verifies that when we try to forcefully run39* prepareWriteSequence() where it is not supported40* will ImageIO throws an UnsupportedOperationException41* or not.42* @run main CanWriteSequence43*/44public final class CanWriteSequence {4546private static File file;47private static FileOutputStream fos;48private static ImageOutputStream ios;4950public static void main(final String[] args) throws Exception {51final IIORegistry registry = IIORegistry.getDefaultInstance();52final Iterator<ImageWriterSpi> iter =53registry.getServiceProviders(ImageWriterSpi.class,54provider -> true, true);55// Validates all supported ImageWriters56while (iter.hasNext()) {57final ImageWriter writer = iter.next().createWriterInstance();58System.out.println("ImageWriter = " + writer);59test(writer);60}61System.out.println("Test passed");62}6364private static void test(final ImageWriter writer) throws Exception {65try {66file = File.createTempFile("temp", ".img");67fos = new FileOutputStream(file);68ios = ImageIO.createImageOutputStream(fos);69writer.setOutput(ios);70final IIOMetadata data = writer.getDefaultStreamMetadata(null);7172if (writer.canWriteSequence()) {73writer.prepareWriteSequence(data);74} else {75try {76writer.prepareWriteSequence(data);77throw new RuntimeException(78"UnsupportedOperationException was not thrown");79} catch (final UnsupportedOperationException ignored) {80// expected81}82}83} finally {84writer.dispose();85if (file != null) {86if (ios != null) {87ios.close();88}89if (fos != null) {90fos.close();91}92Files.delete(file.toPath());93}94}95}96}9798