Path: blob/master/test/jdk/javax/imageio/spi/PluginSpiTest.java
41149 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 627511226* @summary Test verifies that imageReaders for base image formats return27* meaningful name of service provider interface (SPI) for base image28* formats29* @modules java.desktop/com.sun.imageio.plugins.gif30* java.desktop/com.sun.imageio.plugins.png31* java.desktop/com.sun.imageio.plugins.jpeg32* java.desktop/com.sun.imageio.plugins.bmp33* java.desktop/com.sun.imageio.plugins.wbmp34*/3536import javax.imageio.ImageIO;37import javax.imageio.ImageReader;38import javax.imageio.ImageWriter;39import javax.imageio.spi.ImageReaderSpi;4041public class PluginSpiTest {4243public static void main(String[] args) {44String format[] = { "GIF", "PNG", "JPEG", "BMP", "WBMP" };45for (int i = 0; i < format.length; i++) {46System.out.println("\nFormat " + format[i]);47testFormat(format[i]);48}49}5051public static void testFormat(String format) {52ImageReader reader =53ImageIO.getImageReadersByFormatName(format).next();54if (reader == null) {55throw new RuntimeException("Failed to get reader for " + format);56}5758ImageReaderSpi readerSpi = reader.getOriginatingProvider();59System.out.println(format + " Reader SPI: " + readerSpi);6061String writerSpiNames[] = readerSpi.getImageWriterSpiNames();62if (writerSpiNames == null || writerSpiNames.length == 0) {63throw new RuntimeException("Failed to get writer spi names for " +64format);65}6667System.out.println("Available writer spi names:");68for (int i = 0; i < writerSpiNames.length; i++) {69System.out.println(writerSpiNames[i]);70try {71Class spiClass = Class.forName(writerSpiNames[i]);72if (spiClass == null) {73throw new RuntimeException("Failed to get spi class " +74writerSpiNames[i]);75}76System.out.println("Got class " + spiClass.getName());7778Object spiObject = spiClass.newInstance();79if (spiObject == null) {80throw new RuntimeException("Failed to instantiate spi " +81writerSpiNames[i]);82}83System.out.println("Got instance " + spiObject);84} catch (Throwable e) {85throw new RuntimeException("Failed to test spi " +86writerSpiNames[i]);87}88}8990ImageWriter writer = ImageIO.getImageWriter(reader);91if (writer == null) {92throw new RuntimeException("Failed to get writer for " + format);93}94}95}969798