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 6275112
  27  * @summary Test verifies that imageReaders for base image formats return
  28  *          meaningful name of service provider interface (SPI) for base image
  29  *          formats
  30  * @modules java.desktop/com.sun.imageio.plugins.gif
  31  *          java.desktop/com.sun.imageio.plugins.png
  32  *          java.desktop/com.sun.imageio.plugins.jpeg
  33  *          java.desktop/com.sun.imageio.plugins.bmp
  34  *          java.desktop/com.sun.imageio.plugins.wbmp
  35  */
  36 
  37 import javax.imageio.ImageIO;
  38 import javax.imageio.ImageReader;
  39 import javax.imageio.ImageWriter;
  40 import javax.imageio.spi.ImageReaderSpi;
  41 
  42 public class PluginSpiTest {
  43 
  44     public static void main(String[] args) {
  45         String format[] = { "GIF", "PNG", "JPEG", "BMP", "WBMP" };
  46         for (int i = 0; i < format.length; i++) {
  47             System.out.println("\nFormat " + format[i]);
  48             testFormat(format[i]);
  49         }
  50     }
  51 
  52     public static void testFormat(String format) {
  53         ImageReader reader =
  54             ImageIO.getImageReadersByFormatName(format).next();
  55         if (reader == null) {
  56             throw new RuntimeException("Failed to get reader for " + format);
  57         }
  58 
  59         ImageReaderSpi readerSpi = reader.getOriginatingProvider();
  60         System.out.println(format + " Reader SPI: " + readerSpi);
  61 
  62         String writerSpiNames[] = readerSpi.getImageWriterSpiNames();
  63         if (writerSpiNames == null || writerSpiNames.length == 0) {
  64             throw new RuntimeException("Failed to get writer spi names for " +
  65                                        format);
  66         }
  67 
  68         System.out.println("Available writer spi names:");
  69         for (int i = 0; i < writerSpiNames.length; i++) {
  70             System.out.println(writerSpiNames[i]);
  71             try {
  72                 Class spiClass = Class.forName(writerSpiNames[i]);
  73                 if (spiClass == null) {
  74                     throw new RuntimeException("Failed to get spi class " +
  75                                                writerSpiNames[i]);
  76                 }
  77                 System.out.println("Got class " + spiClass.getName());
  78 
  79                 Object spiObject = spiClass.newInstance();
  80                 if (spiObject == null) {
  81                     throw new RuntimeException("Failed to instantiate spi " +
  82                                                writerSpiNames[i]);
  83                 }
  84                 System.out.println("Got instance " + spiObject);
  85             } catch (Throwable e) {
  86                 throw new RuntimeException("Failed to test spi " +
  87                                            writerSpiNames[i]);
  88             }
  89         }
  90 
  91         ImageWriter writer = ImageIO.getImageWriter(reader);
  92         if (writer == null) {
  93             throw new RuntimeException("Failed to get writer for " + format);
  94         }
  95     }
  96 }