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 6319418
  27  * @summary Test verifies that GIF stream metadata could be merged with tree
  28  *          representation for all supported formats
  29  */
  30 
  31 import javax.imageio.ImageIO;
  32 import javax.imageio.ImageWriteParam;
  33 import javax.imageio.ImageWriter;
  34 import javax.imageio.metadata.IIOInvalidTreeException;
  35 import javax.imageio.metadata.IIOMetadata;
  36 
  37 import org.w3c.dom.Node;
  38 
  39 public class StreamMetadataTest {
  40     protected static final String format = "GIF";
  41 
  42     ImageWriter writer = null;
  43     IIOMetadata streamData = null;
  44     ImageWriteParam wparam = null;
  45     boolean doMerge = true;
  46 
  47     public StreamMetadataTest() {
  48         writer = ImageIO.getImageWritersByFormatName(format).next();
  49         wparam = writer.getDefaultWriteParam();
  50         streamData = writer.getDefaultStreamMetadata(wparam);
  51     }
  52 
  53     public void doTest() throws IIOInvalidTreeException {
  54         if (streamData == null) {
  55             throw new RuntimeException("No stream metadata available");
  56         }
  57 
  58         String[] formatNames = streamData.getMetadataFormatNames();
  59         for(String fname : formatNames) {
  60             System.out.println("Format name: " + fname);
  61             Node root = streamData.getAsTree(fname);
  62             if (streamData.isReadOnly()) {
  63                 throw new RuntimeException("Stream metadata is readonly!");
  64             }
  65             streamData.reset();
  66             streamData.mergeTree(fname, root);
  67         }
  68     }
  69 
  70     public static void main(String args[]) {
  71         StreamMetadataTest test = new StreamMetadataTest();
  72         try {
  73             test.doTest();
  74         } catch (Exception e) {
  75             throw new RuntimeException("Test failed.", e);
  76         }
  77     }
  78 }