jdk/test/java/util/ServiceLoader/modules/Basic.java
author alanb
Fri, 07 Apr 2017 08:05:54 +0000
changeset 44545 83b611b88ac8
parent 44115 bb4e971bf5d4
permissions -rw-r--r--
8177530: Module system implementation refresh (4/2017) Reviewed-by: mchung, alanb Contributed-by: alan.bateman@oracle.com, mandy.chung@oracle.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
     1
/*
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
     2
 * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
     4
 *
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
     7
 * published by the Free Software Foundation.
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
     8
 *
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    13
 * accompanied this code).
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    14
 *
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    18
 *
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    21
 * questions.
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    22
 */
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    23
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    24
/**
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    25
 * @test
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    26
 * @library modules
44115
bb4e971bf5d4 8176195: Fix misc module dependencies in jdk_core tests
xiaofeya
parents: 43712
diff changeset
    27
 * @modules java.scripting
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    28
 * @build bananascript/*
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    29
 * @compile src/pearscript/org/pear/PearScriptEngineFactory.java
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    30
 *          src/pearscript/org/pear/PearScript.java
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    31
 * @run testng/othervm Basic
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    32
 * @summary Basic test for ServiceLoader with a provider deployed as a module.
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    33
 */
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    34
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    35
import java.lang.module.Configuration;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    36
import java.lang.module.ModuleFinder;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    37
import java.nio.file.Files;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    38
import java.nio.file.Path;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    39
import java.nio.file.Paths;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    40
import java.nio.file.StandardCopyOption;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    41
import java.util.*;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    42
import java.util.ServiceLoader.Provider;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    43
import java.util.stream.Collectors;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    44
import javax.script.ScriptEngineFactory;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    45
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    46
import org.testng.annotations.Test;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    47
import org.testng.annotations.BeforeTest;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    48
import static org.testng.Assert.*;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    49
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    50
/**
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    51
 * Basic test for ServiceLoader. The test make use of two service providers:
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    52
 * 1. BananaScriptEngine - a ScriptEngineFactory deployed as a module on the
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    53
 *    module path. It implementations a singleton via the public static
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    54
 *    provider method.
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    55
 * 2. PearScriptEngine - a ScriptEngineFactory deployed on the class path
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    56
 *    with a service configuration file.
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    57
 */
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    58
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    59
public class Basic {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    60
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    61
    // Copy the services configuration file for "pearscript" into place.
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    62
    @BeforeTest
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    63
    public void setup() throws Exception {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    64
        Path src = Paths.get(System.getProperty("test.src", ""));
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    65
        Path classes = Paths.get(System.getProperty("test.classes", ""));
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    66
        String st = ScriptEngineFactory.class.getName();
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    67
        Path config = Paths.get("META-INF", "services", st);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    68
        Path source = src.resolve("src").resolve("pearscript").resolve(config);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    69
        Path target = classes.resolve(config);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    70
        Files.createDirectories(target.getParent());
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    71
        Files.copy(source, target, StandardCopyOption.REPLACE_EXISTING);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    72
    }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    73
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    74
    /**
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    75
     * Basic test of iterator() to ensure that providers located as modules
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    76
     * and on the class path are found.
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    77
     */
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    78
    @Test
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    79
    public void testIterator() {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    80
        ServiceLoader<ScriptEngineFactory> loader
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    81
            = ServiceLoader.load(ScriptEngineFactory.class);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    82
        Set<String> names = collectAll(loader)
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    83
                .stream()
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    84
                .map(ScriptEngineFactory::getEngineName)
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    85
                .collect(Collectors.toSet());
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    86
        assertTrue(names.contains("BananaScriptEngine"));
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    87
        assertTrue(names.contains("PearScriptEngine"));
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    88
    }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    89
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    90
    /**
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    91
     * Basic test of iterator() to test iteration order. Providers deployed
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    92
     * as named modules should be found before providers deployed on the class
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    93
     * path.
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    94
     */
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    95
    @Test
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    96
    public void testIteratorOrder() {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    97
        ServiceLoader<ScriptEngineFactory> loader
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    98
            = ServiceLoader.load(ScriptEngineFactory.class);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    99
        boolean foundUnnamed = false;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   100
        for (ScriptEngineFactory factory : collectAll(loader)) {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   101
            if (factory.getClass().getModule().isNamed()) {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   102
                if (foundUnnamed) {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   103
                    assertTrue(false, "Named module element after unnamed");
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   104
                }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   105
            } else {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   106
                foundUnnamed = true;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   107
            }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   108
        }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   109
    }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   110
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   111
    /**
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   112
     * Basic test of Provider::type
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   113
     */
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   114
    @Test
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   115
    public void testProviderType() {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   116
        Set<String> types = ServiceLoader.load(ScriptEngineFactory.class)
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   117
                .stream()
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   118
                .map(Provider::type)
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   119
                .map(Class::getName)
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   120
                .collect(Collectors.toSet());
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   121
        assertTrue(types.contains("org.banana.BananaScriptEngineFactory"));
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   122
        assertTrue(types.contains("org.pear.PearScriptEngineFactory"));
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   123
    }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   124
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   125
    /**
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   126
     * Basic test of Provider::get
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   127
     */
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   128
    @Test
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   129
    public void testProviderGet() {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   130
        Set<String> names = ServiceLoader.load(ScriptEngineFactory.class)
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   131
                .stream()
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   132
                .map(Provider::get)
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   133
                .map(ScriptEngineFactory::getEngineName)
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   134
                .collect(Collectors.toSet());
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   135
        assertTrue(names.contains("BananaScriptEngine"));
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   136
        assertTrue(names.contains("PearScriptEngine"));
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   137
    }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   138
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   139
    /**
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   140
     * Basic test of the public static provider method. BananaScriptEngine
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   141
     * defines a provider method that returns the same instance.
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   142
     */
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   143
    @Test
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   144
    public void testSingleton() {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   145
        Optional<Provider<ScriptEngineFactory>> oprovider
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   146
            = ServiceLoader.load(ScriptEngineFactory.class)
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   147
                .stream()
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   148
                .filter(p -> p.type().getName().equals("org.banana.BananaScriptEngineFactory"))
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   149
                .findFirst();
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   150
        assertTrue(oprovider.isPresent());
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   151
        Provider<ScriptEngineFactory> provider = oprovider.get();
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   152
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   153
        // invoke Provider::get twice
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   154
        ScriptEngineFactory factory1 = provider.get();
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   155
        ScriptEngineFactory factory2 = provider.get();
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   156
        assertTrue(factory1 == factory2);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   157
    }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   158
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   159
    /**
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   160
     * Basic test of stream() to ensure that elements for providers in named
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   161
     * modules come before elements for providers in unnamed modules.
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   162
     */
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   163
    @Test
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   164
    public void testStreamOrder() {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   165
        List<Class<?>> types = ServiceLoader.load(ScriptEngineFactory.class)
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   166
                .stream()
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   167
                .map(Provider::type)
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   168
                .collect(Collectors.toList());
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   169
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   170
        boolean foundUnnamed = false;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   171
        for (Class<?> factoryClass : types) {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   172
            if (factoryClass.getModule().isNamed()) {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   173
                if (foundUnnamed) {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   174
                    assertTrue(false, "Named module element after unnamed");
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   175
                }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   176
            } else {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   177
                foundUnnamed = true;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   178
            }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   179
        }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   180
    }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   181
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   182
    /**
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   183
     * Basic test of ServiceLoader.findFirst()
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   184
     */
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   185
    @Test
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   186
    public void testFindFirst() {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   187
        Optional<ScriptEngineFactory> ofactory
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   188
            = ServiceLoader.load(ScriptEngineFactory.class).findFirst();
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   189
        assertTrue(ofactory.isPresent());
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   190
        ScriptEngineFactory factory = ofactory.get();
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   191
        assertTrue(factory.getClass().getModule().isNamed());
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   192
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   193
        class S { }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   194
        assertFalse(ServiceLoader.load(S.class).findFirst().isPresent());
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   195
    }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   196
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   197
    /**
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   198
     * Basic test ServiceLoader.load specifying the platform class loader.
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   199
     * The providers on the module path and class path should not be located.
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   200
     */
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   201
    @Test
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   202
    public void testWithPlatformClassLoader() {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   203
        ClassLoader pcl = ClassLoader.getPlatformClassLoader();
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   204
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   205
        // iterator
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   206
        ServiceLoader<ScriptEngineFactory> loader
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   207
                = ServiceLoader.load(ScriptEngineFactory.class, pcl);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   208
        Set<String> names = collectAll(loader)
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   209
                .stream()
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   210
                .map(ScriptEngineFactory::getEngineName)
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   211
                .collect(Collectors.toSet());
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   212
        assertFalse(names.contains("BananaScriptEngine"));
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   213
        assertFalse(names.contains("PearScriptEngine"));
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   214
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   215
        // stream
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   216
        names = ServiceLoader.load(ScriptEngineFactory.class, pcl)
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   217
                .stream()
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   218
                .map(Provider::get)
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   219
                .map(ScriptEngineFactory::getEngineName)
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   220
                .collect(Collectors.toSet());
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   221
        assertFalse(names.contains("BananaScriptEngine"));
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   222
        assertFalse(names.contains("PearScriptEngine"));
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   223
    }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   224
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   225
    /**
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   226
     * Basic test of ServiceLoader.load, using the class loader for
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   227
     * a module in a custom layer as the context.
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   228
     */
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   229
    @Test
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   230
    public void testWithCustomLayer1() {
44545
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents: 44115
diff changeset
   231
        ModuleLayer layer = createCustomLayer("bananascript");
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   232
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   233
        ClassLoader loader = layer.findLoader("bananascript");
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   234
        List<ScriptEngineFactory> providers
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   235
            = collectAll(ServiceLoader.load(ScriptEngineFactory.class, loader));
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   236
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   237
        // should have at least 2 x bananascript + pearscript
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   238
        assertTrue(providers.size() >= 3);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   239
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   240
        // first element should be the provider in the custom layer
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   241
        ScriptEngineFactory factory = providers.get(0);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   242
        assertTrue(factory.getClass().getClassLoader() == loader);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   243
        assertTrue(factory.getClass().getModule().getLayer() == layer);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   244
        assertTrue(factory.getEngineName().equals("BananaScriptEngine"));
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   245
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   246
        // remainder should be the boot layer
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   247
        providers.remove(0);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   248
        Set<String> names = providers.stream()
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   249
                .map(ScriptEngineFactory::getEngineName)
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   250
                .collect(Collectors.toSet());
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   251
        assertTrue(names.contains("BananaScriptEngine"));
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   252
        assertTrue(names.contains("PearScriptEngine"));
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   253
    }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   254
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   255
    /**
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   256
     * Basic test of ServiceLoader.load using a custom Layer as the context.
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   257
     */
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   258
    @Test
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   259
    public void testWithCustomLayer2() {
44545
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents: 44115
diff changeset
   260
        ModuleLayer layer = createCustomLayer("bananascript");
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   261
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   262
        List<ScriptEngineFactory> factories
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   263
            = collectAll(ServiceLoader.load(layer, ScriptEngineFactory.class));
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   264
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   265
        // should have at least 2 x bananascript
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   266
        assertTrue(factories.size() >= 2);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   267
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   268
        // first element should be the provider in the custom layer
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   269
        ScriptEngineFactory factory = factories.get(0);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   270
        assertTrue(factory.getClass().getModule().getLayer() == layer);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   271
        assertTrue(factory.getEngineName().equals("BananaScriptEngine"));
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   272
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   273
        // remainder should be the boot layer
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   274
        factories.remove(0);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   275
        Set<String> names = factories.stream()
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   276
                .map(ScriptEngineFactory::getEngineName)
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   277
                .collect(Collectors.toSet());
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   278
        assertTrue(names.contains("BananaScriptEngine"));
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   279
        assertFalse(names.contains("PearScriptEngine"));
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   280
    }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   281
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   282
    /**
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   283
     * Basic test of ServiceLoader.load with a tree of layers.
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   284
     *
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   285
     * Test scenario:
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   286
     * - boot layer contains "bananascript", maybe other script engines
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   287
     * - layer1, with boot layer as parent, contains "bananascript"
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   288
     * - layer2, with boot layer as parent, contains "bananascript"
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   289
     * - layer3, with layer1 ad layer as parents, contains "bananascript"
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   290
     *
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   291
     * ServiceLoader should locate all 4 script engine factories in DFS order.
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   292
     */
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   293
    @Test
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   294
    public void testWithCustomLayer3() {
44545
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents: 44115
diff changeset
   295
        ModuleLayer bootLayer = ModuleLayer.boot();
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   296
        Configuration cf0 = bootLayer.configuration();
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   297
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   298
        // boot layer should contain "bananascript"
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   299
        List<ScriptEngineFactory> factories
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   300
            = collectAll(ServiceLoader.load(bootLayer, ScriptEngineFactory.class));
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   301
        int countInBootLayer = factories.size();
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   302
        assertTrue(countInBootLayer >= 1);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   303
        assertTrue(factories.stream()
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   304
                .map(p -> p.getEngineName())
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   305
                .filter("BananaScriptEngine"::equals)
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   306
                .findAny()
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   307
                .isPresent());
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   308
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   309
        ClassLoader scl = ClassLoader.getSystemClassLoader();
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   310
        Path dir = Paths.get(System.getProperty("test.classes", "."), "modules");
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   311
        ModuleFinder finder = ModuleFinder.of(dir);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   312
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   313
        // layer1
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42338
diff changeset
   314
        Configuration cf1 = cf0.resolveAndBind(finder, ModuleFinder.of(), Set.of());
44545
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents: 44115
diff changeset
   315
        ModuleLayer layer1 = bootLayer.defineModulesWithOneLoader(cf1, scl);
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   316
        assertTrue(layer1.modules().size() == 1);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   317
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   318
        // layer2
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42338
diff changeset
   319
        Configuration cf2 = cf0.resolveAndBind(finder, ModuleFinder.of(), Set.of());
44545
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents: 44115
diff changeset
   320
        ModuleLayer layer2 = bootLayer.defineModulesWithOneLoader(cf2, scl);
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   321
        assertTrue(layer2.modules().size() == 1);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   322
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   323
        // layer3 with layer1 and layer2 as parents
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42338
diff changeset
   324
        Configuration cf3 = Configuration.resolveAndBind(finder,
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   325
                List.of(cf1, cf2),
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   326
                ModuleFinder.of(),
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   327
                Set.of());
44545
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents: 44115
diff changeset
   328
        ModuleLayer layer3
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents: 44115
diff changeset
   329
            = ModuleLayer.defineModulesWithOneLoader(cf3, List.of(layer1, layer2), scl).layer();
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   330
        assertTrue(layer3.modules().size() == 1);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   331
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   332
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   333
        // class loaders
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   334
        ClassLoader loader1 = layer1.findLoader("bananascript");
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   335
        ClassLoader loader2 = layer2.findLoader("bananascript");
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   336
        ClassLoader loader3 = layer3.findLoader("bananascript");
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   337
        assertTrue(loader1 != loader2);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   338
        assertTrue(loader1 != loader3);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   339
        assertTrue(loader2 != loader3);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   340
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   341
        // load all factories with layer3 as the context
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   342
        factories = collectAll(ServiceLoader.load(layer3, ScriptEngineFactory.class));
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   343
        int count = factories.size();
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   344
        assertTrue(count == countInBootLayer + 3);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   345
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   346
        // the ordering should be layer3, layer1, boot layer, layer2
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   347
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   348
        ScriptEngineFactory factory = factories.get(0);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   349
        assertTrue(factory.getClass().getModule().getLayer() == layer3);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   350
        assertTrue(factory.getClass().getClassLoader() == loader3);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   351
        assertTrue(factory.getEngineName().equals("BananaScriptEngine"));
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   352
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   353
        factory = factories.get(1);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   354
        assertTrue(factory.getClass().getModule().getLayer() == layer1);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   355
        assertTrue(factory.getClass().getClassLoader() == loader1);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   356
        assertTrue(factory.getEngineName().equals("BananaScriptEngine"));
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   357
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   358
        // boot layer "bananascript" and maybe other factories
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   359
        int last = count -1;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   360
        boolean found = false;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   361
        for (int i=2; i<last; i++) {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   362
            factory = factories.get(i);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   363
            assertTrue(factory.getClass().getModule().getLayer() == bootLayer);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   364
            if (factory.getEngineName().equals("BananaScriptEngine")) {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   365
                assertFalse(found);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   366
                found = true;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   367
            }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   368
        }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   369
        assertTrue(found);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   370
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   371
        factory = factories.get(last);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   372
        assertTrue(factory.getClass().getModule().getLayer() == layer2);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   373
        assertTrue(factory.getClass().getClassLoader() == loader2);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   374
        assertTrue(factory.getEngineName().equals("BananaScriptEngine"));
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   375
    }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   376
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   377
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   378
    // -- nulls --
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   379
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   380
    @Test(expectedExceptions = { NullPointerException.class })
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   381
    public void testLoadNull1() {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   382
        ServiceLoader.load(null);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   383
    }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   384
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   385
    @Test(expectedExceptions = { NullPointerException.class })
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   386
    public void testLoadNull2() {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   387
        ServiceLoader.load((Class<?>) null, ClassLoader.getSystemClassLoader());
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   388
    }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   389
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   390
    @Test(expectedExceptions = { NullPointerException.class })
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   391
    public void testLoadNull3() {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   392
        class S { }
44545
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents: 44115
diff changeset
   393
        ServiceLoader.load((ModuleLayer) null, S.class);
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   394
    }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   395
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   396
    @Test(expectedExceptions = { NullPointerException.class })
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   397
    public void testLoadNull4() {
44545
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents: 44115
diff changeset
   398
        ServiceLoader.load(ModuleLayer.empty(), null);
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   399
    }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   400
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   401
    @Test(expectedExceptions = { NullPointerException.class })
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   402
    public void testLoadNull5() {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   403
        ServiceLoader.loadInstalled(null);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   404
    }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   405
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   406
    /**
44545
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents: 44115
diff changeset
   407
     * Create a custom layer by resolving the given module names. The modules
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   408
     * are located in the {@code ${test.classes}/modules} directory.
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   409
     */
44545
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents: 44115
diff changeset
   410
    private ModuleLayer createCustomLayer(String... modules) {
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   411
        Path dir = Paths.get(System.getProperty("test.classes", "."), "modules");
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   412
        ModuleFinder finder = ModuleFinder.of(dir);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   413
        Set<String> roots = new HashSet<>();
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   414
        Collections.addAll(roots, modules);
44545
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents: 44115
diff changeset
   415
        ModuleLayer bootLayer = ModuleLayer.boot();
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   416
        Configuration parent = bootLayer.configuration();
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42338
diff changeset
   417
        Configuration cf = parent.resolve(finder, ModuleFinder.of(), roots);
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   418
        ClassLoader scl = ClassLoader.getSystemClassLoader();
44545
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents: 44115
diff changeset
   419
        ModuleLayer layer = bootLayer.defineModulesWithOneLoader(cf, scl);
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   420
        assertTrue(layer.modules().size() == 1);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   421
        return layer;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   422
    }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   423
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   424
    private <E> List<E> collectAll(ServiceLoader<E> loader) {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   425
        List<E> list = new ArrayList<>();
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   426
        Iterator<E> iterator = loader.iterator();
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   427
        while (iterator.hasNext()) {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   428
            list.add(iterator.next());
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   429
        }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   430
        return list;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   431
    }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   432
}
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   433