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 6287880
27 * @summary Test verifies that default metadata for stream and image returned by
28 *          GIFImageWriter can be modified by the tree representation
29 */
30
31import java.awt.image.BufferedImage;
32
33import javax.imageio.ImageIO;
34import javax.imageio.ImageTypeSpecifier;
35import javax.imageio.ImageWriteParam;
36import javax.imageio.ImageWriter;
37import javax.imageio.metadata.IIOInvalidTreeException;
38import javax.imageio.metadata.IIOMetadata;
39import javax.imageio.metadata.IIOMetadataNode;
40
41public class WriteMetadataTest {
42    private static String format = "GIF";
43
44    public static void main(String[] args) {
45        ImageWriter w = ImageIO.getImageWritersByFormatName(format).next();
46        if (w == null) {
47            throw new RuntimeException("No available writers for format " + format);
48        }
49        ImageWriteParam p = w.getDefaultWriteParam();
50
51        ImageTypeSpecifier t =
52                ImageTypeSpecifier.createFromBufferedImageType(BufferedImage.TYPE_INT_RGB);
53
54        IIOMetadata m = w.getDefaultImageMetadata(t, p);
55        System.out.println("Default image metadata is " + m);
56        testWritableMetadata(m);
57
58        IIOMetadata sm = w.getDefaultStreamMetadata(p);
59        System.out.println("Default stream metadata is " + sm);
60        testWritableMetadata(sm);
61    }
62
63    public static void testWritableMetadata(IIOMetadata m) {
64        String nativeFormatName =
65                m.getNativeMetadataFormatName();
66        System.out.println("Format: " + nativeFormatName);
67        IIOMetadataNode root = (IIOMetadataNode)m.getAsTree(nativeFormatName);
68        if (m.isReadOnly()) {
69            throw new RuntimeException("Metadata is read only!");
70        }
71        try {
72            m.setFromTree(nativeFormatName, root);
73        } catch (IIOInvalidTreeException e) {
74            e.printStackTrace();
75            throw new RuntimeException("Test failed!", e);
76        } catch (IllegalStateException e) {
77            throw new RuntimeException("Test failed!", e);
78        }
79        System.out.println("Test passed.\n\n");
80    }
81}
82