jdk/test/java/util/ServiceLoader/modules/ServicesTest.java
author skovalev
Thu, 08 Sep 2016 09:59:54 -0700
changeset 40797 4618571397a5
parent 40261 86a49ba76f52
permissions -rw-r--r--
8165583: Fix module dependencies for jdk/java/util/* tests Reviewed-by: alanb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     1
/*
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     2
 * Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     4
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     7
 * published by the Free Software Foundation.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     8
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    13
 * accompanied this code).
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    14
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    18
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    21
 * questions.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    22
 */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    23
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    24
import java.io.File;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    25
import java.lang.module.Configuration;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    26
import java.lang.module.ModuleFinder;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    27
import java.lang.reflect.Layer;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    28
import java.nio.file.Files;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    29
import java.nio.file.Path;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    30
import java.nio.file.Paths;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    31
import java.util.ServiceLoader;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    32
import java.util.Set;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    33
import javax.script.ScriptEngineFactory;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    34
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    35
import static jdk.testlibrary.ProcessTools.executeTestJava;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    36
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    37
import org.testng.annotations.BeforeTest;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    38
import org.testng.annotations.Test;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    39
import static org.testng.Assert.*;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    40
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    41
/**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    42
 * @test
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    43
 * @library /lib/testlibrary
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    44
 * @modules java.scripting
40797
4618571397a5 8165583: Fix module dependencies for jdk/java/util/* tests
skovalev
parents: 40261
diff changeset
    45
 *          jdk.compiler
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    46
 * @build ServicesTest CompilerUtils jdk.testlibrary.*
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    47
 * @run testng ServicesTest
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    48
 * @summary Tests ServiceLoader to locate service providers on the module path
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    49
 *          and class path. Also tests ServiceLoader with a custom Layer.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    50
 */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    51
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    52
@Test
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    53
public class ServicesTest {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    54
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    55
    private static final String TEST_SRC = System.getProperty("test.src");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    56
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    57
    private static final Path SRC_DIR = Paths.get(TEST_SRC, "src");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    58
    private static final Path CLASSES_DIR = Paths.get("classes");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    59
    private static final Path MODS_DIR = Paths.get("mods");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    60
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    61
    // modules to compile to the module path
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    62
    private static final String MODULES[] = { "test", "bananascript" };
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    63
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    64
    // directories of classes to compile to the class path
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    65
    private static final String CLASSES[] = { "pearscript" };
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    66
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    67
    // resources to copy to the class path
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    68
    private static final String RESOURCES[] = {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    69
        "pearscript/META-INF/services/javax.script.ScriptEngineFactory"
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    70
    };
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    71
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    72
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    73
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    74
     * Compiles all modules and classes used by the test
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    75
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    76
    @BeforeTest
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    77
    public void setup() throws Exception {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    78
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    79
        // modules
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    80
        for (String mn : MODULES ) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    81
            Path src = SRC_DIR.resolve(mn);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    82
            Path mods = MODS_DIR.resolve(mn);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    83
            assertTrue(CompilerUtils.compile(src, mods));
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    84
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    85
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    86
        // classes
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    87
        for (String dir : CLASSES) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    88
            Path src = SRC_DIR.resolve(dir);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    89
            assertTrue(CompilerUtils.compile(src, CLASSES_DIR));
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    90
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    91
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    92
        // copy resources
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    93
        for (String rn : RESOURCES) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    94
            Path file = Paths.get(rn.replace('/', File.separatorChar));
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    95
            Path source = SRC_DIR.resolve(file);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    96
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    97
            // drop directory to get a target of classes/META-INF/...
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    98
            Path target = CLASSES_DIR.resolve(file.subpath(1, file.getNameCount()));
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    99
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   100
            Files.createDirectories(target.getParent());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   101
            Files.copy(source, target);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   102
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   103
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   104
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   105
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   106
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   107
    /**
40261
86a49ba76f52 8136930: Simplify use of module-system options by custom launchers
mchung
parents: 38457
diff changeset
   108
     * Run test with --module-path.
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   109
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   110
     * BananaScriptEngine should be found.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   111
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   112
    public void runWithModulePath() throws Exception {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   113
        int exitValue
40261
86a49ba76f52 8136930: Simplify use of module-system options by custom launchers
mchung
parents: 38457
diff changeset
   114
            = executeTestJava("--module-path", MODS_DIR.toString(),
40797
4618571397a5 8165583: Fix module dependencies for jdk/java/util/* tests
skovalev
parents: 40261
diff changeset
   115
                              "--add-modules", "bananascript",
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   116
                              "-m", "test/test.Main",
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   117
                              "BananaScriptEngine")
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   118
                .outputTo(System.out)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   119
                .errorTo(System.out)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   120
                .getExitValue();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   121
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   122
        assertTrue(exitValue == 0);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   123
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   124
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   125
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   126
    /**
40261
86a49ba76f52 8136930: Simplify use of module-system options by custom launchers
mchung
parents: 38457
diff changeset
   127
     * Run test with --module-path and -classpath.
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   128
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   129
     * Both BananaScriptEngine and PearScriptEngine should be found
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   130
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   131
    public void runWithModulePathAndClassPath() throws Exception {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   132
        int exitValue
40261
86a49ba76f52 8136930: Simplify use of module-system options by custom launchers
mchung
parents: 38457
diff changeset
   133
            = executeTestJava("--module-path", MODS_DIR.toString(),
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   134
                              "-cp", CLASSES_DIR.toString(),
40797
4618571397a5 8165583: Fix module dependencies for jdk/java/util/* tests
skovalev
parents: 40261
diff changeset
   135
                              "--add-modules", "bananascript",
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   136
                              "-m", "test/test.Main",
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   137
                              "BananaScriptEngine", "PearScriptEngine")
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   138
                .outputTo(System.out)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   139
                .errorTo(System.out)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   140
                .getExitValue();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   141
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   142
        assertTrue(exitValue == 0);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   143
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   144
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   145
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   146
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   147
     * Exercise ServiceLoader.load(Layer, Class).
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   148
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   149
    public void testWithCustomLayer() throws Exception {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   150
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   151
        ServiceLoader<ScriptEngineFactory> sl;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   152
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   153
        // BananaScriptEngine should not be in the boot Layer
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   154
        sl = ServiceLoader.load(Layer.boot(), ScriptEngineFactory.class);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   155
        assertTrue(find("BananaScriptEngine", sl) == null);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   156
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   157
        // create a custom Layer
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   158
        ModuleFinder finder = ModuleFinder.of(MODS_DIR);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   159
        Layer bootLayer = Layer.boot();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   160
        Configuration parent = bootLayer.configuration();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   161
        Configuration cf
38457
3d019217e322 8152650: ModuleFinder.compose should accept varargs
alanb
parents: 36511
diff changeset
   162
            = parent.resolveRequiresAndUses(finder, ModuleFinder.of(), Set.of());
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   163
        ClassLoader scl = ClassLoader.getSystemClassLoader();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   164
        Layer layer = bootLayer.defineModulesWithOneLoader(cf, scl);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   165
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   166
        assertTrue(layer.findModule("bananascript").isPresent());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   167
        ClassLoader loader = layer.findLoader("bananascript");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   168
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   169
        sl = ServiceLoader.load(layer, ScriptEngineFactory.class);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   170
        ScriptEngineFactory factory = find("BananaScriptEngine", sl);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   171
        assertTrue(factory != null);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   172
        assertEquals(factory.getClass().getModule().getName(), "bananascript");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   173
        assertTrue(factory.getClass().getClassLoader() == loader);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   174
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   175
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   176
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   177
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   178
     * Find the given scripting engine (by name) via the ScriptEngineFactory
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   179
     * that ServiceLoader has found.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   180
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   181
    static ScriptEngineFactory find(String name,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   182
                                    ServiceLoader<ScriptEngineFactory> sl) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   183
        for (ScriptEngineFactory factory : sl) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   184
            if (factory.getEngineName().equals(name))
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   185
                return factory;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   186
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   187
        return null;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   188
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   189
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   190
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   191
}
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   192