Path: blob/master/test/jdk/javax/imageio/spi/ServiceRegistryRestriction.java
41152 views
/*1* Copyright (c) 2015, 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 806874926* @run main ServiceRegistryRestriction27* @summary Tests ServiceRegistry's restriction on handling28* only standard Image I/O service types.29*/3031import java.util.*;32import java.util.function.Consumer;33import javax.imageio.spi.*;3435public class ServiceRegistryRestriction {36static class DummyTestSpi {37}3839ClassLoader cl = ServiceRegistryRestriction.class.getClassLoader();4041<T> void construct(Class<T> clazz) {42List<Class<?>> list = Arrays.<Class<?>>asList(clazz);43ServiceRegistry sr = new ServiceRegistry(list.iterator());44}4546<T> void lookup(Class<T> clazz) {47Iterator<T> i = ServiceRegistry.lookupProviders(clazz);48}4950<T> void lookupCL(Class<T> clazz) {51Iterator<T> i = ServiceRegistry.lookupProviders(clazz, cl);52}5354<T> void doOneTest(String label, Class<T> clazz, boolean expectFail, Consumer<Class<T>> op) {55System.out.printf("testing %s with %s...", label, clazz.getName());56try {57op.accept(clazz);58if (expectFail) {59throw new AssertionError("fail, operation succeeded unexpectedly");60} else {61System.out.println("success");62}63} catch (IllegalArgumentException iae) {64if (expectFail) {65System.out.println("success, got expected IAE");66} else {67throw new AssertionError("fail, unexpected exception", iae);68}69}70}7172void doTests(Class<?> clazz, boolean expectFail) {73doOneTest("constructor", clazz, expectFail, this::construct);74doOneTest("lookup", clazz, expectFail, this::lookup);75doOneTest("lookupCL", clazz, expectFail, this::lookupCL);76}7778void run() {79doTests(ImageInputStreamSpi.class, false);80doTests(ImageOutputStreamSpi.class, false);81doTests(ImageReaderSpi.class, false);82doTests(ImageTranscoderSpi.class, false);83doTests(ImageWriterSpi.class, false);84doTests(DummyTestSpi.class, true);85}8687public static void main(String[] args) {88new ServiceRegistryRestriction().run();89}90}919293