test/jdk/javax/imageio/spi/SpiTest.java
changeset 47216 71c04702a3d5
parent 45351 f7f615a3aaff
equal deleted inserted replaced
47215:4ebc2e2fb97c 47216:71c04702a3d5
       
     1 /*
       
     2  * Copyright (c) 2001, 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 4395376
       
    27  * @summary Performs various sanity checks on Spi class constructors and get
       
    28  *          methods
       
    29  */
       
    30 
       
    31 import java.util.Iterator;
       
    32 import java.util.Locale;
       
    33 
       
    34 import javax.imageio.ImageReader;
       
    35 import javax.imageio.ImageTypeSpecifier;
       
    36 import javax.imageio.ImageWriter;
       
    37 import javax.imageio.spi.IIORegistry;
       
    38 import javax.imageio.spi.IIOServiceProvider;
       
    39 import javax.imageio.spi.ImageReaderSpi;
       
    40 import javax.imageio.spi.ImageReaderWriterSpi;
       
    41 import javax.imageio.spi.ImageWriterSpi;
       
    42 import javax.imageio.spi.ServiceRegistry;
       
    43 
       
    44 public class SpiTest {
       
    45 
       
    46     String vendorName = null;
       
    47     String version = null;
       
    48     String[] names = null;
       
    49     String[] suffixes = null;
       
    50     String[] MIMETypes = null;
       
    51     String readerClassName = null;
       
    52     String writerClassName = null;
       
    53     Class[] inputTypes = null;
       
    54     Class[] outputTypes = null;
       
    55     String[] writerSpiNames = null;
       
    56     String[] readerSpiNames = null;
       
    57     String nativeStreamMetadataFormatName = null;
       
    58     String nativeStreamMetadataFormatClassName = null;
       
    59     String[] extraStreamMetadataFormatNames = null;
       
    60     String[] extraStreamMetadataFormatClassNames = null;
       
    61     String nativeImageMetadataFormatName = null;
       
    62     String nativeImageMetadataFormatClassName = null;
       
    63     String[] extraImageMetadataFormatNames = null;
       
    64     String[] extraImageMetadataFormatClassNames = null;
       
    65 
       
    66     private void error(String message) {
       
    67         // System.out.println("Error: " + message);
       
    68         throw new RuntimeException(message);
       
    69     }
       
    70 
       
    71     private void testSpi(IIOServiceProvider spi) {
       
    72         if (spi.getVendorName() == null) {
       
    73             error(spi + " getVendorName == null!");
       
    74         }
       
    75         if (spi.getVersion() == null) {
       
    76             error(spi + " getVersion == null!");
       
    77         }
       
    78     }
       
    79 
       
    80     private void testSpi(ImageReaderWriterSpi spi) {
       
    81         testSpi((IIOServiceProvider)spi);
       
    82         if (spi.getFormatNames() == null) {
       
    83             error("spi.getFormatNames == null!");
       
    84         }
       
    85         String[] suffixes = spi.getFileSuffixes();
       
    86         if (suffixes != null && suffixes.length == 0) {
       
    87             error("suffixes.length == 0!");
       
    88         }
       
    89         String[] MIMETypes = spi.getMIMETypes();
       
    90         if (MIMETypes != null && MIMETypes.length == 0) {
       
    91             error("MIMETypes.length == 0!");
       
    92         }
       
    93         if (spi.getPluginClassName() == null) {
       
    94             error("spi.getPluginClassName == null!");
       
    95         }
       
    96         String[] extraStreamMetadataFormatNames =
       
    97             spi.getExtraStreamMetadataFormatNames();
       
    98         if (extraStreamMetadataFormatNames != null &&
       
    99             extraStreamMetadataFormatNames.length == 0) {
       
   100             error("extraStreamMetadataFormatNames.length == 0!");
       
   101         }
       
   102         String[] extraImageMetadataFormatNames =
       
   103             spi.getExtraImageMetadataFormatNames();
       
   104         if (extraImageMetadataFormatNames != null &&
       
   105             extraImageMetadataFormatNames.length == 0) {
       
   106             error("extraImageMetadataFormatNames.length == 0!");
       
   107         }
       
   108     }
       
   109 
       
   110     public void testSpi(ImageReaderSpi spi) {
       
   111         testSpi((ImageReaderWriterSpi)spi);
       
   112         Class[] inputTypes = spi.getInputTypes();
       
   113         if (inputTypes == null) {
       
   114             error("inputTypes == null!");
       
   115         }
       
   116         if (inputTypes.length == 0) {
       
   117             error("inputTypes.length == 0!");
       
   118         }
       
   119         String[] writerSpiNames = spi.getImageWriterSpiNames();
       
   120         if (writerSpiNames != null && writerSpiNames.length == 0) {
       
   121             error("writerSpiNames.length == 0!");
       
   122         }
       
   123     }
       
   124 
       
   125     public void testSpi(ImageWriterSpi spi) {
       
   126         testSpi((ImageReaderWriterSpi)spi);
       
   127         Class[] outputTypes = spi.getOutputTypes();
       
   128         if (outputTypes == null) {
       
   129             error("outputTypes == null!");
       
   130         }
       
   131         if (outputTypes.length == 0) {
       
   132             error("outputTypes.length == 0!");
       
   133         }
       
   134         String[] readerSpiNames = spi.getImageReaderSpiNames();
       
   135         if (readerSpiNames != null && readerSpiNames.length == 0) {
       
   136             error("readerSpiNames.length == 0!");
       
   137         }
       
   138     }
       
   139 
       
   140     private void resetConstructorArguments() {
       
   141         vendorName = null;
       
   142         version = null;
       
   143         names = null;
       
   144         suffixes = null;
       
   145         MIMETypes = null;
       
   146         readerClassName = null;
       
   147         inputTypes = null;
       
   148         outputTypes = null;
       
   149         writerSpiNames = null;
       
   150         readerSpiNames = null;
       
   151         nativeStreamMetadataFormatName = null;
       
   152         nativeStreamMetadataFormatClassName = null;
       
   153         extraStreamMetadataFormatNames = null;
       
   154         extraStreamMetadataFormatClassNames = null;
       
   155         nativeImageMetadataFormatName = null;
       
   156         nativeImageMetadataFormatClassName = null;
       
   157         extraImageMetadataFormatNames = null;
       
   158         extraImageMetadataFormatClassNames = null;
       
   159     }
       
   160 
       
   161     private ImageReaderSpi constructImageReaderSpi() {
       
   162         return new ImageReaderSpi(vendorName,
       
   163                                   version,
       
   164                                   names,
       
   165                                   suffixes,
       
   166                                   MIMETypes,
       
   167                                   readerClassName,
       
   168                                   inputTypes,
       
   169                                   writerSpiNames,
       
   170                                   false,
       
   171                                   nativeStreamMetadataFormatName,
       
   172                                   nativeStreamMetadataFormatClassName,
       
   173                                   extraStreamMetadataFormatNames,
       
   174                                   extraStreamMetadataFormatClassNames,
       
   175                                   false,
       
   176                                   nativeImageMetadataFormatName,
       
   177                                   nativeImageMetadataFormatClassName,
       
   178                                   extraImageMetadataFormatNames,
       
   179                                   extraImageMetadataFormatClassNames) {
       
   180 
       
   181                 public String getDescription(Locale locale) {
       
   182                     return null;
       
   183                 }
       
   184 
       
   185                 public boolean canDecodeInput(Object source) {
       
   186                     return false;
       
   187                 }
       
   188 
       
   189                 public ImageReader createReaderInstance(Object extension) {
       
   190                     return null;
       
   191                 }
       
   192          };
       
   193     }
       
   194 
       
   195     private ImageWriterSpi constructImageWriterSpi() {
       
   196         return new ImageWriterSpi(vendorName,
       
   197                                   version,
       
   198                                   names,
       
   199                                   suffixes,
       
   200                                   MIMETypes,
       
   201                                   writerClassName,
       
   202                                   outputTypes,
       
   203                                   readerSpiNames,
       
   204                                   false,
       
   205                                   nativeStreamMetadataFormatName,
       
   206                                   nativeStreamMetadataFormatClassName,
       
   207                                   extraStreamMetadataFormatNames,
       
   208                                   extraStreamMetadataFormatClassNames,
       
   209                                   false,
       
   210                                   nativeImageMetadataFormatName,
       
   211                                   nativeImageMetadataFormatClassName,
       
   212                                   extraImageMetadataFormatNames,
       
   213                                   extraImageMetadataFormatClassNames) {
       
   214 
       
   215                 public String getDescription(Locale locale) {
       
   216                     return null;
       
   217                 }
       
   218 
       
   219                 public boolean canEncodeImage(ImageTypeSpecifier type) {
       
   220                     return false;
       
   221                 }
       
   222 
       
   223                 public ImageWriter createWriterInstance(Object extension) {
       
   224                     return null;
       
   225                 }
       
   226          };
       
   227     }
       
   228 
       
   229     private void checkImageReaderSpiConstructor(boolean shouldFail) {
       
   230         boolean gotIAE = false;
       
   231         try {
       
   232             constructImageReaderSpi();
       
   233         } catch (Exception e) {
       
   234             if (!(e instanceof IllegalArgumentException)) {
       
   235                 error("Got exception " + e);
       
   236             } else {
       
   237                 gotIAE = true;
       
   238             }
       
   239         }
       
   240         if (gotIAE != shouldFail) {
       
   241             if (gotIAE) {
       
   242                 error("ImageReaderSpi constructor threw an IAE!");
       
   243             } else {
       
   244                 error("ImageReaderSpi constructor didn't throw an IAE!");
       
   245             }
       
   246         }
       
   247     }
       
   248 
       
   249     private void checkImageWriterSpiConstructor(boolean shouldFail) {
       
   250         boolean gotIAE = false;
       
   251         try {
       
   252             constructImageWriterSpi();
       
   253         } catch (Exception e) {
       
   254             if (!(e instanceof IllegalArgumentException)) {
       
   255                 error("Got exception " + e);
       
   256             } else {
       
   257                 gotIAE = true;
       
   258             }
       
   259         }
       
   260         if (gotIAE != shouldFail) {
       
   261             if (gotIAE) {
       
   262                 error("ImageWriterSpi constructor threw an IAE!");
       
   263             } else {
       
   264                 error("ImageWriterSpi constructor didn't throw an IAE!");
       
   265             }
       
   266         }
       
   267     }
       
   268 
       
   269     public void testImageReaderSpiConstructor() {
       
   270         resetConstructorArguments();
       
   271 
       
   272         checkImageReaderSpiConstructor(true);
       
   273         vendorName = "My Vendor";
       
   274         checkImageReaderSpiConstructor(true);
       
   275         version = "My Version";
       
   276         checkImageReaderSpiConstructor(true);
       
   277         names = new String[0];
       
   278         checkImageReaderSpiConstructor(true);
       
   279         names = new String[1];
       
   280         names[0] = "My Format Name";
       
   281         checkImageReaderSpiConstructor(true);
       
   282         readerClassName = "com.mycompany.Reader";
       
   283         checkImageReaderSpiConstructor(true);
       
   284         inputTypes = new Class[0];
       
   285         checkImageReaderSpiConstructor(true);
       
   286         inputTypes = new Class[1];
       
   287         inputTypes[0] = Object.class;
       
   288         // Now it should work
       
   289         checkImageReaderSpiConstructor(false);
       
   290 
       
   291         // Test normalization of zero-length arrays
       
   292         suffixes = new String[0];
       
   293         MIMETypes = new String[0];
       
   294         writerSpiNames = new String[0];
       
   295         extraStreamMetadataFormatNames = new String[0];
       
   296         extraImageMetadataFormatNames = new String[0];
       
   297 
       
   298         ImageReaderSpi spi = constructImageReaderSpi();
       
   299         if (spi.getFileSuffixes() != null) {
       
   300             error("Failed to normalize suffixes!");
       
   301         }
       
   302         if (spi.getMIMETypes() != null) {
       
   303             error("Failed to normalize MIMETypes!");
       
   304         }
       
   305         if (spi.getImageWriterSpiNames() != null) {
       
   306             error("Failed to normalize writerSpiNames!");
       
   307         }
       
   308         if (spi.getExtraStreamMetadataFormatNames() != null) {
       
   309             error("Failed to normalize extraStreamMetadataFormatNames!");
       
   310         }
       
   311         if (spi.getExtraImageMetadataFormatNames() != null) {
       
   312             error("Failed to normalize extraImageMetadataFormatNames!");
       
   313         }
       
   314     }
       
   315 
       
   316     public void testImageWriterSpiConstructor() {
       
   317         resetConstructorArguments();
       
   318 
       
   319         checkImageWriterSpiConstructor(true);
       
   320         vendorName = "My Vendor";
       
   321         checkImageWriterSpiConstructor(true);
       
   322         version = "My Version";
       
   323         checkImageWriterSpiConstructor(true);
       
   324         names = new String[0];
       
   325         checkImageWriterSpiConstructor(true);
       
   326         names = new String[1];
       
   327         names[0] = "My Format Name";
       
   328         checkImageWriterSpiConstructor(true);
       
   329         writerClassName = "com.mycompany.Writer";
       
   330         checkImageWriterSpiConstructor(true);
       
   331         outputTypes = new Class[0];
       
   332         checkImageWriterSpiConstructor(true);
       
   333         outputTypes = new Class[1];
       
   334         outputTypes[0] = Object.class;
       
   335         // Now it should work
       
   336         checkImageWriterSpiConstructor(false);
       
   337 
       
   338         // Test normalization of zero-length arrays
       
   339         suffixes = new String[0];
       
   340         MIMETypes = new String[0];
       
   341         readerSpiNames = new String[0];
       
   342         extraStreamMetadataFormatNames = new String[0];
       
   343         extraStreamMetadataFormatClassNames = new String[0];
       
   344         extraImageMetadataFormatNames = new String[0];
       
   345         extraImageMetadataFormatClassNames = new String[0];
       
   346 
       
   347         ImageWriterSpi spi = constructImageWriterSpi();
       
   348         if (spi.getFileSuffixes() != null) {
       
   349             error("Failed to normalize suffixes!");
       
   350         }
       
   351         if (spi.getMIMETypes() != null) {
       
   352             error("Failed to normalize MIMETypes!");
       
   353         }
       
   354         if (spi.getImageReaderSpiNames() != null) {
       
   355             error("Failed to normalize readerSpiNames!");
       
   356         }
       
   357         if (spi.getExtraStreamMetadataFormatNames() != null) {
       
   358             error("Failed to normalize extraStreamMetadataFormatNames!");
       
   359         }
       
   360         if (spi.getExtraImageMetadataFormatNames() != null) {
       
   361             error("Failed to normalize extraImageMetadataFormatNames!");
       
   362         }
       
   363     }
       
   364 
       
   365     public SpiTest() {
       
   366         testImageReaderSpiConstructor();
       
   367         testImageWriterSpiConstructor();
       
   368 
       
   369         ServiceRegistry registry = IIORegistry.getDefaultInstance();
       
   370         Iterator readers = registry.getServiceProviders(ImageReaderSpi.class,
       
   371                                                         false);
       
   372         while (readers.hasNext()) {
       
   373             ImageReaderSpi rspi = (ImageReaderSpi)readers.next();
       
   374             System.out.println("*** Testing " + rspi.getClass().getName());
       
   375             testSpi(rspi);
       
   376         }
       
   377 
       
   378         Iterator writers = registry.getServiceProviders(ImageWriterSpi.class,
       
   379                                                         false);
       
   380         while (writers.hasNext()) {
       
   381             ImageWriterSpi wspi = (ImageWriterSpi)writers.next();
       
   382             System.out.println("*** Testing " + wspi.getClass().getName());
       
   383             testSpi(wspi);
       
   384         }
       
   385     }
       
   386 
       
   387     public static void main(String[] args) {
       
   388         new SpiTest();
       
   389     }
       
   390 }