Path: blob/master/test/jdk/javax/imageio/plugins/bmp/BmpDefaultImageMetadataTest.java
41153 views
/*1* Copyright (c) 2003, 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 489551226* @summary Test that default image metadata for BMP image writer is not null27*/2829import java.awt.Color;30import java.awt.Graphics;31import java.awt.image.BufferedImage;32import java.util.Iterator;3334import javax.imageio.ImageIO;35import javax.imageio.ImageTypeSpecifier;36import javax.imageio.ImageWriteParam;37import javax.imageio.ImageWriter;38import javax.imageio.metadata.IIOMetadata;3940import org.w3c.dom.NamedNodeMap;41import org.w3c.dom.Node;4243public class BmpDefaultImageMetadataTest {44ImageWriter writer = null;45IIOMetadata imageData = null;46ImageWriteParam writeParam = null;47BufferedImage bimg = null;4849public BmpDefaultImageMetadataTest(String format) {50try {51bimg = new BufferedImage(200, 200, bimg.TYPE_INT_RGB);52Graphics gg = bimg.getGraphics();53gg.setColor(Color.red);54gg.fillRect(50, 50, 100, 100);5556Iterator it = ImageIO.getImageWritersByFormatName(format);57if (it.hasNext()) {58writer = (ImageWriter) it.next();59}60if (writer == null) {61throw new RuntimeException("No writer available for the given format."62+ " Test failed.");63}64writeParam = writer.getDefaultWriteParam();6566System.out.println("Testing Image Metadata for "+format+"\n");67imageData = writer.getDefaultImageMetadata(new ImageTypeSpecifier(bimg), writeParam);68if (imageData == null) {69System.out.println("return value is null. No default image metadata is associated with "+format+" writer");70throw new RuntimeException("Default image metadata is null."71+ " Test failed.");72}73int j = 0;74String imageDataNames[] = null;75if(imageData != null) {76System.out.println("Is standard metadata format supported (Image) ? "+77imageData.isStandardMetadataFormatSupported() );78imageDataNames = imageData.getMetadataFormatNames();79System.out.println("\nAll supported Metadata Format Names\n");80if(imageDataNames!=null){81for(j=0; j<imageDataNames.length; j++) {82System.out.println("FORMAT NAME: "+imageDataNames[j]);83if (imageDataNames[j].equals(imageData.getNativeMetadataFormatName())) {84System.out.println("This is a Native Metadata format\n");85} else {86System.out.println("\n");87}88System.out.println("");89System.out.println("IIOImageMetadata DOM tree for "+imageDataNames[j]);90System.out.println("");91Node imageNode = imageData.getAsTree(imageDataNames[j]);92displayMetadata(imageNode);93System.out.println("\n\n");94}95}96}97}catch(Exception e){98e.printStackTrace();99throw new RuntimeException("Exception was thrown."100+ " Test failed.");101}102}103104public void displayMetadata(Node root) {105displayMetadata(root, 0);106}107108void indent(int level) {109for (int i = 0; i < level; i++) {110System.out.print(" ");111}112}113114void displayMetadata(Node node, int level) {115indent(level); // emit open tag116System.out.print("<" + node.getNodeName());117NamedNodeMap map = node.getAttributes();118if (map != null) { // print attribute values119int length = map.getLength();120for (int i = 0; i < length; i++) {121Node attr = map.item(i);122System.out.print(" " + attr.getNodeName() +123"=\"" + attr.getNodeValue() + "\"");124}125}126Node child = node.getFirstChild();127128if (node.getNodeValue() != null && !node.getNodeValue().equals("") ) {129System.out.println(">");130indent(level);131System.out.println(node.getNodeValue());132indent(level); // emit close tag133System.out.println("</" + node.getNodeName() + ">");134} else if (child != null) {135System.out.println(">"); // close current tag136while (child != null) { // emit child tags recursively137displayMetadata(child, level + 1);138child = child.getNextSibling();139}140indent(level); // emit close tag141System.out.println("</" + node.getNodeName() + ">");142} else {143System.out.println("/>");144}145}146147public static void main(String args[]) {148BmpDefaultImageMetadataTest test = new BmpDefaultImageMetadataTest("bmp");149}150}151152153