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.util.Iterator;
  27 import java.nio.file.Files;
  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/manual CanWriteSequence
  44  */
  45 public final class CanWriteSequence {
  46 
  47     public static void main(final String[] args) throws Exception {
  48         final IIORegistry registry = IIORegistry.getDefaultInstance();
  49         final Iterator<ImageWriterSpi> iter =
  50                 registry.getServiceProviders(ImageWriterSpi.class,
  51                         provider -> true, true);
  52         // Validates all supported ImageWriters
  53         while (iter.hasNext()) {
  54             final ImageWriter writer = iter.next().createWriterInstance();
  55             System.out.println("ImageWriter = " + writer);
  56             test(writer);
  57         }
  58         System.out.println("Test passed");
  59     }
  60 
  61     private static void test(final ImageWriter writer) throws Exception {
  62         String sep = System.getProperty("file.separator");
  63         String dir = System.getProperty("test.src", ".");
  64         String filePath = dir+sep;
  65         File directory = new File(filePath);
  66         final File file = File.createTempFile("temp", ".img", directory);
  67         directory.delete();
  68         final FileOutputStream fos = new FileOutputStream(file);
  69         final ImageOutputStream ios = ImageIO.createImageOutputStream(fos);
  70         writer.setOutput(ios);
  71         final IIOMetadata data = writer.getDefaultStreamMetadata(null);
  72 
  73         if (writer.canWriteSequence()) {
  74             writer.prepareWriteSequence(data);
  75         } else {
  76             try {
  77                 writer.prepareWriteSequence(data);
  78                 Files.delete(file.toPath());
  79                 throw new RuntimeException(
  80                         "UnsupportedOperationException was not thrown");
  81             } catch (final UnsupportedOperationException ignored) {
  82                 // expected
  83             }
  84         }
  85         writer.dispose();
  86         fos.close();
  87         Files.delete(file.toPath());
  88     }
  89 }
  90