1/*
2 * Copyright (c) 2015, 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 8068749
27 * @run main ServiceRegistryRestriction
28 * @summary Tests ServiceRegistry's restriction on handling
29 *          only standard Image I/O service types.
30 */
31
32import java.util.*;
33import java.util.function.Consumer;
34import javax.imageio.spi.*;
35
36public class ServiceRegistryRestriction {
37    static class DummyTestSpi {
38    }
39
40    ClassLoader cl = ServiceRegistryRestriction.class.getClassLoader();
41
42    <T> void construct(Class<T> clazz) {
43        List<Class<?>> list = Arrays.<Class<?>>asList(clazz);
44        ServiceRegistry sr = new ServiceRegistry(list.iterator());
45    }
46
47    <T> void lookup(Class<T> clazz) {
48        Iterator<T> i = ServiceRegistry.lookupProviders(clazz);
49    }
50
51    <T> void lookupCL(Class<T> clazz) {
52        Iterator<T> i = ServiceRegistry.lookupProviders(clazz, cl);
53    }
54
55    <T> void doOneTest(String label, Class<T> clazz, boolean expectFail, Consumer<Class<T>> op) {
56        System.out.printf("testing %s with %s...", label, clazz.getName());
57        try {
58            op.accept(clazz);
59            if (expectFail) {
60                throw new AssertionError("fail, operation succeeded unexpectedly");
61            } else {
62                System.out.println("success");
63            }
64        } catch (IllegalArgumentException iae) {
65            if (expectFail) {
66                System.out.println("success, got expected IAE");
67            } else {
68                throw new AssertionError("fail, unexpected exception", iae);
69            }
70        }
71    }
72
73    void doTests(Class<?> clazz, boolean expectFail) {
74        doOneTest("constructor", clazz, expectFail, this::construct);
75        doOneTest("lookup", clazz, expectFail, this::lookup);
76        doOneTest("lookupCL", clazz, expectFail, this::lookupCL);
77    }
78
79    void run() {
80        doTests(ImageInputStreamSpi.class, false);
81        doTests(ImageOutputStreamSpi.class, false);
82        doTests(ImageReaderSpi.class, false);
83        doTests(ImageTranscoderSpi.class, false);
84        doTests(ImageWriterSpi.class, false);
85        doTests(DummyTestSpi.class, true);
86    }
87
88    public static void main(String[] args) {
89        new ServiceRegistryRestriction().run();
90    }
91}
92