Path: blob/master/test/jdk/javax/imageio/plugins/gif/WriteMetadataTest.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 628788026* @summary Test verifies that default metadata for stream and image returned by27* GIFImageWriter can be modified by the tree representation28*/2930import java.awt.image.BufferedImage;3132import javax.imageio.ImageIO;33import javax.imageio.ImageTypeSpecifier;34import javax.imageio.ImageWriteParam;35import javax.imageio.ImageWriter;36import javax.imageio.metadata.IIOInvalidTreeException;37import javax.imageio.metadata.IIOMetadata;38import javax.imageio.metadata.IIOMetadataNode;3940public class WriteMetadataTest {41private static String format = "GIF";4243public static void main(String[] args) {44ImageWriter w = ImageIO.getImageWritersByFormatName(format).next();45if (w == null) {46throw new RuntimeException("No available writers for format " + format);47}48ImageWriteParam p = w.getDefaultWriteParam();4950ImageTypeSpecifier t =51ImageTypeSpecifier.createFromBufferedImageType(BufferedImage.TYPE_INT_RGB);5253IIOMetadata m = w.getDefaultImageMetadata(t, p);54System.out.println("Default image metadata is " + m);55testWritableMetadata(m);5657IIOMetadata sm = w.getDefaultStreamMetadata(p);58System.out.println("Default stream metadata is " + sm);59testWritableMetadata(sm);60}6162public static void testWritableMetadata(IIOMetadata m) {63String nativeFormatName =64m.getNativeMetadataFormatName();65System.out.println("Format: " + nativeFormatName);66IIOMetadataNode root = (IIOMetadataNode)m.getAsTree(nativeFormatName);67if (m.isReadOnly()) {68throw new RuntimeException("Metadata is read only!");69}70try {71m.setFromTree(nativeFormatName, root);72} catch (IIOInvalidTreeException e) {73e.printStackTrace();74throw new RuntimeException("Test failed!", e);75} catch (IllegalStateException e) {76throw new RuntimeException("Test failed!", e);77}78System.out.println("Test passed.\n\n");79}80}818283