Path: blob/master/test/jdk/javax/imageio/spi/RegisterPluginTwiceTest.java
51315 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 4836432 803774326* @summary This test attempts to register two instances of one ImageReaderSPI.27* Expected behavior is that only one instance of ImageReaderSPI will28* be registered.29*/3031import java.io.IOException;32import java.util.Iterator;33import java.util.Locale;3435import javax.imageio.ImageReader;36import javax.imageio.spi.IIORegistry;37import javax.imageio.spi.ServiceRegistry;38import javax.imageio.stream.ImageInputStream;3940public class RegisterPluginTwiceTest {4142public RegisterPluginTwiceTest() throws Exception {43BMPImageReaderSPI BMPSpi = new BMPImageReaderSPI();44BMPImageReaderSPI BMPSpi1 = new BMPImageReaderSPI();4546IIORegistry regis = IIORegistry.getDefaultInstance();47boolean res148= regis.registerServiceProvider(BMPSpi,49javax.imageio.spi.ImageReaderSpi.class);50boolean res251= regis.registerServiceProvider(BMPSpi1,52javax.imageio.spi.ImageReaderSpi.class);5354if(!res1 || res2) {55throw new RuntimeException("Bad returned values for registerServiceProvider");56}57Iterator it = regis.getServiceProviders(Class.forName("javax.imageio.spi.ImageReaderSpi"), true);58int count = 0;59while (it.hasNext()) {60Object o = it.next();61if(o instanceof BMPImageReaderSPI) {62count++;63System.out.println("Found next BMPImageReaderSPI, count = " +count);64}65}66if(count > 1) {67throw new RuntimeException("Too many instances of the BMPImageReaderSPI was registered!");68}69}7071public static void main(String args[]) throws Exception{72RegisterPluginTwiceTest fnio = new RegisterPluginTwiceTest();73}747576/**77Not a perfect implementation of SPI. This is just a dummy implementation78which denotes some arbitrary reader class. The intention is to check how this79is getting registered in the registry. Hence some of the values in this class80may be inappropriate..81*/82public static class BMPImageReaderSPI extends javax.imageio.spi.ImageReaderSpi{8384private static final String vendorName = "Javasoft";8586private static final String version = "2.0";8788private static final String[] names = { "bmp" };8990private static final String[] suffixes = { "bmp" };9192private static final String[] MIMETypes = { "image/x-bmp"};9394private static final String readerClassName =95"com.sun.imageio.plugins.png.PNGImageReader";9697private static final String[] writerSpiNames = {98"com.sun.imageio.plugins.png.PNGImageWriterSpi"99};100101public BMPImageReaderSPI() {102super(vendorName,103version,104names,105suffixes,106MIMETypes,107readerClassName,108STANDARD_INPUT_TYPE,109writerSpiNames,110false,111null, null,112null, null,113true,114"BMP Native Metadata",115"com.sun.imageio.plugins.png.PNGMetadataFormat",116null, null117);118}119120public String getDescription(Locale locale) {121return "Standard BMP image reader";122}123124public boolean canDecodeInput(Object input) throws IOException {125if (!(input instanceof ImageInputStream)) {126return false;127}128129ImageInputStream stream = (ImageInputStream)input;130byte[] b = new byte[8];131stream.mark();132stream.readFully(b);133stream.reset();134135return (b[0] == (byte)137 &&136b[1] == (byte)80 &&137b[2] == (byte)78 &&138b[3] == (byte)71 &&139b[4] == (byte)13 &&140b[5] == (byte)10 &&141b[6] == (byte)26 &&142b[7] == (byte)10);143}144145public ImageReader createReaderInstance(Object extension) {146//return new PNGImageReader(this);147return null;148}149public void onRegistration(ServiceRegistry sr, Class<?> category) {150//System.out.println("Registered "+category);151super.onRegistration(sr, category);152}153154public void onDeregistration(ServiceRegistry sr, Class<?> category) {155//System.out.println("De-Registered "+category);156//super.onRegistration(sr, category);157}158}159}160161162