1 /*
   2  * Copyright (c) 2003, 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 4836432 8037743
  27  * @summary This test attempts to register two instances of one ImageReaderSPI.
  28  *          Expected behavior is that only one instance of ImageReaderSPI will
  29  *          be registered.
  30  */
  31 
  32 import java.io.IOException;
  33 import java.util.Iterator;
  34 import java.util.Locale;
  35 
  36 import javax.imageio.ImageReader;
  37 import javax.imageio.spi.IIORegistry;
  38 import javax.imageio.spi.ServiceRegistry;
  39 import javax.imageio.stream.ImageInputStream;
  40 
  41 public class RegisterPluginTwiceTest {
  42 
  43     public RegisterPluginTwiceTest() throws Exception {
  44         BMPImageReaderSPI BMPSpi = new BMPImageReaderSPI();
  45         BMPImageReaderSPI BMPSpi1 = new BMPImageReaderSPI();
  46 
  47         IIORegistry regis = IIORegistry.getDefaultInstance();
  48         boolean res1
  49             = regis.registerServiceProvider(BMPSpi,
  50                                             javax.imageio.spi.ImageReaderSpi.class);
  51         boolean res2
  52             = regis.registerServiceProvider(BMPSpi1,
  53                                             javax.imageio.spi.ImageReaderSpi.class);
  54 
  55         if(!res1 || res2) {
  56             throw new RuntimeException("Bad returned values for registerServiceProvider");
  57         }
  58         Iterator it = regis.getServiceProviders(Class.forName("javax.imageio.spi.ImageReaderSpi"), true);
  59         int count = 0;
  60         while (it.hasNext()) {
  61             Object o = it.next();
  62             if(o instanceof BMPImageReaderSPI) {
  63                 count++;
  64                 System.out.println("Found next BMPImageReaderSPI, count = " +count);
  65             }
  66         }
  67         if(count > 1) {
  68             throw new RuntimeException("Too many instances of the BMPImageReaderSPI was registered!");
  69         }
  70     }
  71 
  72     public static void main(String args[]) throws Exception{
  73         RegisterPluginTwiceTest fnio = new RegisterPluginTwiceTest();
  74     }
  75 
  76 
  77  /**
  78   Not a perfect implementation of SPI. This is just a dummy implementation
  79   which denotes some arbitrary reader class. The intention is to check how this
  80   is getting registered in the registry. Hence some of the values in this class
  81   may be inappropriate..
  82  */
  83     public static class BMPImageReaderSPI extends javax.imageio.spi.ImageReaderSpi{
  84 
  85         private static final String vendorName = "Javasoft";
  86 
  87         private static final String version = "2.0";
  88 
  89         private static final String[] names = { "bmp" };
  90 
  91         private static final String[] suffixes = { "bmp" };
  92 
  93         private static final String[] MIMETypes = { "image/x-bmp"};
  94 
  95         private static final String readerClassName =
  96         "com.sun.imageio.plugins.png.PNGImageReader";
  97 
  98         private static final String[] writerSpiNames = {
  99             "com.sun.imageio.plugins.png.PNGImageWriterSpi"
 100         };
 101 
 102         public BMPImageReaderSPI() {
 103             super(vendorName,
 104                   version,
 105                   names,
 106                   suffixes,
 107                   MIMETypes,
 108                   readerClassName,
 109                   STANDARD_INPUT_TYPE,
 110                   writerSpiNames,
 111                   false,
 112                   null, null,
 113                   null, null,
 114                   true,
 115                   "BMP Native Metadata",
 116                   "com.sun.imageio.plugins.png.PNGMetadataFormat",
 117                   null, null
 118                   );
 119         }
 120 
 121         public String getDescription(Locale locale) {
 122             return "Standard BMP image reader";
 123         }
 124 
 125         public boolean canDecodeInput(Object input) throws IOException {
 126             if (!(input instanceof ImageInputStream)) {
 127                 return false;
 128             }
 129 
 130             ImageInputStream stream = (ImageInputStream)input;
 131             byte[] b = new byte[8];
 132             stream.mark();
 133             stream.readFully(b);
 134             stream.reset();
 135 
 136             return (b[0] == (byte)137 &&
 137                     b[1] == (byte)80 &&
 138                     b[2] == (byte)78 &&
 139                     b[3] == (byte)71 &&
 140                     b[4] == (byte)13 &&
 141                     b[5] == (byte)10 &&
 142                     b[6] == (byte)26 &&
 143                     b[7] == (byte)10);
 144         }
 145 
 146         public ImageReader createReaderInstance(Object extension) {
 147             //return new PNGImageReader(this);
 148             return null;
 149         }
 150         public void onRegistration(ServiceRegistry sr, Class<?> category) {
 151             //System.out.println("Registered "+category);
 152             super.onRegistration(sr, category);
 153         }
 154 
 155         public void onDeregistration(ServiceRegistry sr, Class<?> category) {
 156             //System.out.println("De-Registered "+category);
 157             //super.onRegistration(sr, category);
 158         }
 159     }
 160 }