jdk/test/java/util/ServiceLoader/Basic.java
author martin
Tue, 15 Sep 2015 21:56:04 -0700
changeset 32649 2ee9017c7597
parent 14191 01f4a54604a6
permissions -rw-r--r--
8136583: Core libraries should use blessed modifier order Summary: Run blessed-modifier-order script (see bug) Reviewed-by: psandoz, chegar, alanb, plevart
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
14191
01f4a54604a6 7198496: (sl) ServiceLoader.load(Class, null) behavior differs from spec
psandoz
parents: 5506
diff changeset
     2
 * Copyright (c) 2005, 2012, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
//
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
public class Basic {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
    private static PrintStream out = System.err;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
    private static <T> Set<T> setOf(Iterable<T> it) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
        Set<T> s = new HashSet<T>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
        for (T t : it)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
            s.add(t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
        return s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
    private static <T> void checkEquals(Set<T> s1, Set<T> s2, boolean eq) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
        if (s1.equals(s2) != eq)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
            throw new RuntimeException(String.format("%b %s : %s",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
                                                     eq, s1, s2));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 14191
diff changeset
    47
    abstract static class TestLoader {
14191
01f4a54604a6 7198496: (sl) ServiceLoader.load(Class, null) behavior differs from spec
psandoz
parents: 5506
diff changeset
    48
        String name;
01f4a54604a6 7198496: (sl) ServiceLoader.load(Class, null) behavior differs from spec
psandoz
parents: 5506
diff changeset
    49
01f4a54604a6 7198496: (sl) ServiceLoader.load(Class, null) behavior differs from spec
psandoz
parents: 5506
diff changeset
    50
        TestLoader(String name) { this.name = name; }
01f4a54604a6 7198496: (sl) ServiceLoader.load(Class, null) behavior differs from spec
psandoz
parents: 5506
diff changeset
    51
01f4a54604a6 7198496: (sl) ServiceLoader.load(Class, null) behavior differs from spec
psandoz
parents: 5506
diff changeset
    52
        abstract ServiceLoader<FooService> load();
01f4a54604a6 7198496: (sl) ServiceLoader.load(Class, null) behavior differs from spec
psandoz
parents: 5506
diff changeset
    53
    }
01f4a54604a6 7198496: (sl) ServiceLoader.load(Class, null) behavior differs from spec
psandoz
parents: 5506
diff changeset
    54
01f4a54604a6 7198496: (sl) ServiceLoader.load(Class, null) behavior differs from spec
psandoz
parents: 5506
diff changeset
    55
    static TestLoader tcclLoader = new TestLoader("Thread context class loader") {
01f4a54604a6 7198496: (sl) ServiceLoader.load(Class, null) behavior differs from spec
psandoz
parents: 5506
diff changeset
    56
        ServiceLoader<FooService> load() {
01f4a54604a6 7198496: (sl) ServiceLoader.load(Class, null) behavior differs from spec
psandoz
parents: 5506
diff changeset
    57
            return ServiceLoader.load(FooService.class);
01f4a54604a6 7198496: (sl) ServiceLoader.load(Class, null) behavior differs from spec
psandoz
parents: 5506
diff changeset
    58
        }
01f4a54604a6 7198496: (sl) ServiceLoader.load(Class, null) behavior differs from spec
psandoz
parents: 5506
diff changeset
    59
    };
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
14191
01f4a54604a6 7198496: (sl) ServiceLoader.load(Class, null) behavior differs from spec
psandoz
parents: 5506
diff changeset
    61
    static TestLoader systemClLoader = new TestLoader("System class loader") {
01f4a54604a6 7198496: (sl) ServiceLoader.load(Class, null) behavior differs from spec
psandoz
parents: 5506
diff changeset
    62
        ServiceLoader<FooService> load() {
01f4a54604a6 7198496: (sl) ServiceLoader.load(Class, null) behavior differs from spec
psandoz
parents: 5506
diff changeset
    63
            return ServiceLoader.load(FooService.class, ClassLoader.getSystemClassLoader());
01f4a54604a6 7198496: (sl) ServiceLoader.load(Class, null) behavior differs from spec
psandoz
parents: 5506
diff changeset
    64
        }
01f4a54604a6 7198496: (sl) ServiceLoader.load(Class, null) behavior differs from spec
psandoz
parents: 5506
diff changeset
    65
    };
01f4a54604a6 7198496: (sl) ServiceLoader.load(Class, null) behavior differs from spec
psandoz
parents: 5506
diff changeset
    66
01f4a54604a6 7198496: (sl) ServiceLoader.load(Class, null) behavior differs from spec
psandoz
parents: 5506
diff changeset
    67
    static TestLoader nullClLoader = new TestLoader("null (defer to system class loader)") {
01f4a54604a6 7198496: (sl) ServiceLoader.load(Class, null) behavior differs from spec
psandoz
parents: 5506
diff changeset
    68
        ServiceLoader<FooService> load() {
01f4a54604a6 7198496: (sl) ServiceLoader.load(Class, null) behavior differs from spec
psandoz
parents: 5506
diff changeset
    69
            return ServiceLoader.load(FooService.class, null);
01f4a54604a6 7198496: (sl) ServiceLoader.load(Class, null) behavior differs from spec
psandoz
parents: 5506
diff changeset
    70
        }
01f4a54604a6 7198496: (sl) ServiceLoader.load(Class, null) behavior differs from spec
psandoz
parents: 5506
diff changeset
    71
    };
01f4a54604a6 7198496: (sl) ServiceLoader.load(Class, null) behavior differs from spec
psandoz
parents: 5506
diff changeset
    72
01f4a54604a6 7198496: (sl) ServiceLoader.load(Class, null) behavior differs from spec
psandoz
parents: 5506
diff changeset
    73
    public static void main(String[] args) {
01f4a54604a6 7198496: (sl) ServiceLoader.load(Class, null) behavior differs from spec
psandoz
parents: 5506
diff changeset
    74
        for (TestLoader tl : Arrays.asList(tcclLoader, systemClLoader, nullClLoader)) {
01f4a54604a6 7198496: (sl) ServiceLoader.load(Class, null) behavior differs from spec
psandoz
parents: 5506
diff changeset
    75
            test(tl);
01f4a54604a6 7198496: (sl) ServiceLoader.load(Class, null) behavior differs from spec
psandoz
parents: 5506
diff changeset
    76
        }
01f4a54604a6 7198496: (sl) ServiceLoader.load(Class, null) behavior differs from spec
psandoz
parents: 5506
diff changeset
    77
    }
01f4a54604a6 7198496: (sl) ServiceLoader.load(Class, null) behavior differs from spec
psandoz
parents: 5506
diff changeset
    78
01f4a54604a6 7198496: (sl) ServiceLoader.load(Class, null) behavior differs from spec
psandoz
parents: 5506
diff changeset
    79
    static void test(TestLoader tl) {
01f4a54604a6 7198496: (sl) ServiceLoader.load(Class, null) behavior differs from spec
psandoz
parents: 5506
diff changeset
    80
        ServiceLoader<FooService> sl = tl.load();
01f4a54604a6 7198496: (sl) ServiceLoader.load(Class, null) behavior differs from spec
psandoz
parents: 5506
diff changeset
    81
        out.format("%s: %s%n", tl.name, sl);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        // Providers are cached
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        Set<FooService> ps = setOf(sl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        checkEquals(ps, setOf(sl), true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        // The cache can be flushed and reloaded
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        sl.reload();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        checkEquals(ps, setOf(sl), false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
}