Path: blob/master/test/jdk/javax/imageio/plugins/gif/DisableCompressionTest.java
41154 views
/*1* Copyright (c) 2005, 2017, 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 629468326* @summary Test verifies that GIF ImageWriteParam behaves according to spec27*/2829import javax.imageio.ImageIO;30import javax.imageio.ImageWriteParam;31import javax.imageio.ImageWriter;3233public class DisableCompressionTest {3435public static void main(String[] args) {36testFormat("GIF");37}3839protected static void testFormat(String format) {40ImageWriter writer = ImageIO.getImageWritersByFormatName(format).next();41if (writer == null) {42throw new RuntimeException("No writer for " + format);43}4445ImageWriteParam param = writer.getDefaultWriteParam();46int[] supported_modes = new int[] {47ImageWriteParam.MODE_COPY_FROM_METADATA,48ImageWriteParam.MODE_DEFAULT,49ImageWriteParam.MODE_EXPLICIT };5051for (int mode : supported_modes) {52String mode_name = getModeName(mode);53System.out.println("Test mode " + mode_name + "...");54// we know that GIF image writer supports compression55// and supports any compression mode form supportd_modes56// If exception would be thrown here then test failed.57param.setCompressionMode(mode);5859// now we are trying to disable compression.60// This operation is not supported because GIF image writer61// does not provide uncompressed output.62// The expected behaviour is that UnsupportedOperationException63// will be thrown here and current compression mode will not be64// changed.65boolean gotException = false;66try {67param.setCompressionMode(ImageWriteParam.MODE_DISABLED);68} catch (UnsupportedOperationException e) {69gotException = true;70} catch (Throwable e) {71throw new RuntimeException("Test failed due to unexpected exception", e);72}7374if (!gotException) {75throw new RuntimeException("Test failed.");76}7778if (param.getCompressionMode() != mode) {79throw new RuntimeException("Param state was changed.");80}81System.out.println("Test passed.");82}83}8485private static String getModeName(int mode) {86switch(mode) {87case ImageWriteParam.MODE_COPY_FROM_METADATA:88return "MODE_COPY_FROM_METADATA";89case ImageWriteParam.MODE_DEFAULT:90return "MODE_DEFAULT";91case ImageWriteParam.MODE_DISABLED:92return "MODE_DISABLED";93case ImageWriteParam.MODE_EXPLICIT:94return "MODE_EXPLICIT";95default:96throw new IllegalArgumentException("Unknown mode: " + mode);97}98}99}100101102