1 /*
   2  * Copyright (c) 2015, 2017, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 import java.io.File;
  25 import java.io.FileOutputStream;
  26 import java.nio.file.Files;
  27 import java.util.Iterator;
  28 
  29 import javax.imageio.ImageIO;
  30 import javax.imageio.ImageWriter;
  31 import javax.imageio.metadata.IIOMetadata;
  32 import javax.imageio.spi.IIORegistry;
  33 import javax.imageio.spi.ImageWriterSpi;
  34 import javax.imageio.stream.ImageOutputStream;
  35 
  36 /**
  37  * @test
  38  * @bug     4958064 8183349
  39  * @summary Test verifies that when we try to forcefully run
  40  *          prepareWriteSequence() where it is not supported
  41  *          will ImageIO throws an UnsupportedOperationException
  42  *          or not.
  43  * @run     main CanWriteSequence
  44  */
  45 public final class CanWriteSequence {
  46 
  47     private static File file;
  48     private static FileOutputStream fos;
  49 
  50     public static void main(final String[] args) throws Exception {
  51         final IIORegistry registry = IIORegistry.getDefaultInstance();
  52         final Iterator<ImageWriterSpi> iter =
  53                 registry.getServiceProviders(ImageWriterSpi.class,
  54                         provider -> true, true);
  55         // Validates all supported ImageWriters
  56         while (iter.hasNext()) {
  57             final ImageWriter writer = iter.next().createWriterInstance();
  58             System.out.println("ImageWriter = " + writer);
  59             test(writer);
  60         }
  61         System.out.println("Test passed");
  62     }
  63 
  64     private static void test(final ImageWriter writer) throws Exception {
  65         try {
  66             file = File.createTempFile("temp", ".img");
  67             fos = new FileOutputStream(file);
  68             final ImageOutputStream ios = ImageIO.createImageOutputStream(fos);
  69             writer.setOutput(ios);
  70             final IIOMetadata data = writer.getDefaultStreamMetadata(null);
  71 
  72             if (writer.canWriteSequence()) {
  73                 writer.prepareWriteSequence(data);
  74             } else {
  75                 try {
  76                     writer.prepareWriteSequence(data);
  77                     throw new RuntimeException(
  78                             "UnsupportedOperationException was not thrown");
  79                 } catch (final UnsupportedOperationException ignored) {
  80                 // expected
  81                 }
  82             }
  83         } finally {
  84             writer.dispose();
  85             if (file != null) {
  86                 if (fos != null) {
  87                     fos.close();
  88                 }
  89                 Files.delete(file.toPath());
  90             }
  91         }
  92     }
  93 }
  94