Path: blob/master/test/jdk/javax/imageio/spi/DeregisterAllSpiTest.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 483584126* @summary This test verifies that we will able to register new SPI after27* deregistration all previously registered SPIs by using28* deregisterAll() method29*/3031import java.io.IOException;32import java.util.Locale;3334import javax.imageio.ImageIO;35import javax.imageio.ImageReader;36import javax.imageio.spi.IIORegistry;37import javax.imageio.spi.ImageReaderSpi;38import javax.imageio.spi.ServiceRegistry;39import javax.imageio.stream.ImageInputStream;4041public class DeregisterAllSpiTest {4243public DeregisterAllSpiTest() throws Exception {44ImageReaderSpi BMPSpi = new BMPImageReaderSPI();45IIORegistry.getDefaultInstance().registerServiceProvider(BMPSpi);4647System.out.println("Reader Format Names available in the registry");48String formatNames[] = ImageIO.getReaderFormatNames();49if( formatNames == null || formatNames.length <= 0) {50throw new RuntimeException("No registered ImageReaders!");51}52for (int x=0; x < formatNames.length; x++) {53System.out.println("format "+formatNames[x]);54}5556IIORegistry.getDefaultInstance().deregisterAll();5758System.out.println("\nReader Format Names after deregistering all SPIs");59formatNames = ImageIO.getReaderFormatNames();60if(formatNames.length == 0) {61System.out.println("No readers available\n");62} else {63throw new RuntimeException("Some providers was not deregistered!");64}6566IIORegistry.getDefaultInstance().registerServiceProvider(BMPSpi);67System.out.println("Reader Format Names after re-register of BMP Plugin");68formatNames = ImageIO.getReaderFormatNames();69if(formatNames.length == 0) {70throw new RuntimeException("Unable to register new SPI after deregisterAll()!");71}72}737475public static void main(String args[]) throws Exception{76DeregisterAllSpiTest regis = new DeregisterAllSpiTest();77}787980public static class BMPImageReaderSPI extends javax.imageio.spi.ImageReaderSpi{8182private static final String vendorName = "Javasoft";8384private static final String version = "2.0";8586private static final String[] names = { "bmp" };8788private static final String[] suffixes = { "bmp" };8990private static final String[] MIMETypes = { "image/x-bmp"};9192private static final String readerClassName =93"com.sun.imageio.plugins.png.PNGImageReader";9495private static final String[] writerSpiNames = {96"com.sun.imageio.plugins.png.PNGImageWriterSpi"97};9899public BMPImageReaderSPI() {100super(vendorName,101version,102names,103suffixes,104MIMETypes,105readerClassName,106STANDARD_INPUT_TYPE,107writerSpiNames,108false,109null, null,110null, null,111true,112"BMP Native Metadata",113"com.sun.imageio.plugins.png.PNGMetadataFormat",114null, null115);116}117118public String getDescription(Locale locale) {119return "Standard BMP image reader";120}121122public boolean canDecodeInput(Object input) throws IOException {123if (!(input instanceof ImageInputStream)) {124return false;125}126127ImageInputStream stream = (ImageInputStream)input;128byte[] b = new byte[8];129stream.mark();130stream.readFully(b);131stream.reset();132133return (b[0] == (byte)137 &&134b[1] == (byte)80 &&135b[2] == (byte)78 &&136b[3] == (byte)71 &&137b[4] == (byte)13 &&138b[5] == (byte)10 &&139b[6] == (byte)26 &&140b[7] == (byte)10);141}142143public ImageReader createReaderInstance(Object extension) {144//return new PNGImageReader(this);145return null;146}147public void onRegistration(ServiceRegistry sr, Class<?> category) {148System.out.println("\nfrom OnRegistration: BMP plugin Registered\n");149super.onRegistration(sr, category);150}151152public void onDeregistration(ServiceRegistry sr, Class<?> category) {153System.out.println("\nfrom OnDeregistration: BMP plugin De-Registered\n");154//super.onRegistration(sr, category);155}156}157}158159160