1 /*
   2  * Copyright (c) 2005, 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 /*
  25  * @test
  26  * @bug 6294683
  27  * @summary Test verifies that GIF ImageWriteParam behaves according to spec
  28  */
  29 
  30 import javax.imageio.ImageIO;
  31 import javax.imageio.ImageWriteParam;
  32 import javax.imageio.ImageWriter;
  33 
  34 public class DisableCompressionTest {
  35 
  36     public static void main(String[] args) {
  37         testFormat("GIF");
  38     }
  39 
  40     protected static void testFormat(String format) {
  41         ImageWriter writer = ImageIO.getImageWritersByFormatName(format).next();
  42         if (writer == null) {
  43             throw new RuntimeException("No writer for " + format);
  44         }
  45 
  46         ImageWriteParam param = writer.getDefaultWriteParam();
  47         int[] supported_modes = new int[] {
  48             ImageWriteParam.MODE_COPY_FROM_METADATA,
  49                     ImageWriteParam.MODE_DEFAULT,
  50                     ImageWriteParam.MODE_EXPLICIT };
  51 
  52         for (int mode : supported_modes) {
  53             String mode_name = getModeName(mode);
  54             System.out.println("Test mode " + mode_name + "...");
  55             // we know that GIF image writer supports compression
  56             // and supports any compression mode form supportd_modes
  57             // If exception would be thrown here then test failed.
  58             param.setCompressionMode(mode);
  59 
  60             // now we are trying to disable compression.
  61             // This operation is not supported because GIF image writer
  62             // does not provide uncompressed output.
  63             // The expected behaviour is that UnsupportedOperationException
  64             // will be thrown here and current compression mode will not be
  65             // changed.
  66             boolean gotException = false;
  67             try {
  68                 param.setCompressionMode(ImageWriteParam.MODE_DISABLED);
  69             } catch (UnsupportedOperationException e) {
  70                 gotException = true;
  71             } catch (Throwable e) {
  72                 throw new RuntimeException("Test failed due to unexpected exception", e);
  73             }
  74 
  75             if (!gotException) {
  76                 throw new RuntimeException("Test failed.");
  77             }
  78 
  79             if (param.getCompressionMode() != mode) {
  80                 throw new RuntimeException("Param state was changed.");
  81             }
  82             System.out.println("Test passed.");
  83         }
  84     }
  85 
  86     private static String getModeName(int mode) {
  87         switch(mode) {
  88             case ImageWriteParam.MODE_COPY_FROM_METADATA:
  89                 return "MODE_COPY_FROM_METADATA";
  90             case ImageWriteParam.MODE_DEFAULT:
  91                 return "MODE_DEFAULT";
  92             case ImageWriteParam.MODE_DISABLED:
  93                 return "MODE_DISABLED";
  94             case ImageWriteParam.MODE_EXPLICIT:
  95                 return "MODE_EXPLICIT";
  96             default:
  97                 throw new IllegalArgumentException("Unknown mode: " + mode);
  98         }
  99     }
 100 }