test/jdk/jdk/modules/scenarios/automaticmodules/RunWithAutomaticModules.java
author shurailine
Fri, 16 Mar 2018 03:15:35 -0700
changeset 49258 be0ec3cc0c2a
parent 47216 71c04702a3d5
child 51675 b487c1e914d0
permissions -rw-r--r--
8199616: Fix @module declarations in tier1 tests Reviewed-by: mchung, alanb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     1
/*
45393
de4e1efc8eec 8181335: remove packageless CompilerUtils
iignatyev
parents: 40261
diff changeset
     2
 * Copyright (c) 2015, 2017, Oracle and/or its affiliates. All rights reserved.
36511
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
/**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    25
 * @test
45393
de4e1efc8eec 8181335: remove packageless CompilerUtils
iignatyev
parents: 40261
diff changeset
    26
 * @library /lib/testlibrary /test/lib
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    27
 * @modules jdk.compiler
49258
be0ec3cc0c2a 8199616: Fix @module declarations in tier1 tests
shurailine
parents: 47216
diff changeset
    28
 *          java.scripting
be0ec3cc0c2a 8199616: Fix @module declarations in tier1 tests
shurailine
parents: 47216
diff changeset
    29
 *          jdk.zipfs
45393
de4e1efc8eec 8181335: remove packageless CompilerUtils
iignatyev
parents: 40261
diff changeset
    30
 * @build RunWithAutomaticModules jdk.test.lib.compiler.CompilerUtils JarUtils
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    31
 *        jdk.testlibrary.ProcessTools
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    32
 * @run testng RunWithAutomaticModules
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    33
 * @summary Runs tests that make use of automatic modules
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    34
 */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    35
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    36
import java.nio.file.Files;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    37
import java.nio.file.Path;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    38
import java.nio.file.Paths;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    39
45393
de4e1efc8eec 8181335: remove packageless CompilerUtils
iignatyev
parents: 40261
diff changeset
    40
import jdk.test.lib.compiler.CompilerUtils;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    41
import static jdk.testlibrary.ProcessTools.*;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    42
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    43
import org.testng.annotations.Test;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    44
import static org.testng.Assert.*;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    45
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    46
@Test
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    47
public class RunWithAutomaticModules {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    48
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    49
    private static final String TEST_SRC = System.getProperty("test.src");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    50
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    51
    private static final Path SRC_DIR = Paths.get(TEST_SRC, "src");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    52
    private static final Path CLASSES_DIR = Paths.get("classes");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    53
    private static final Path MODS_DIR = Paths.get("mods");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    54
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    55
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    56
     * Basic test that consists of 3 modules:
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    57
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    58
     * basictest - the test itself
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    59
     * httpserver - a JAR file (automatic module) with a dummy HTTP server
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    60
     * logging - a JAR file (automatic module) with a dummy logging library
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    61
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    62
     * The test runs the dummy HTTP server and checks that has the expected
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    63
     * reads and exported packages.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    64
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    65
     * The HTTP server uses the logging library.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    66
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    67
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    68
    public void testBasic() throws Exception {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    69
        boolean compiled;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    70
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    71
        Path loggingSrc = SRC_DIR.resolve("logging");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    72
        Path loggingClasses = CLASSES_DIR.resolve("logging");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    73
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    74
        Path httpServerSrc = SRC_DIR.resolve("httpserver");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    75
        Path httpServerClasses = CLASSES_DIR.resolve("httpserver");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    76
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    77
        String testModule = "basictest";
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    78
        String mainClass = "test.Main";
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    79
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    80
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    81
        // compile + create mods/logging-1.0.jar
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    82
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    83
        compiled = CompilerUtils.compile(loggingSrc, loggingClasses);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    84
        assertTrue(compiled);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    85
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    86
        JarUtils.createJarFile(MODS_DIR.resolve("logging-1.0.jar"),
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    87
                               loggingClasses);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    88
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    89
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    90
        // compile + create mods/httpserver-9.0.0.jar
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    91
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    92
        compiled = CompilerUtils.compile(httpServerSrc,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    93
                httpServerClasses,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    94
                "-cp", loggingClasses.toString());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    95
        assertTrue(compiled);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    96
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    97
        JarUtils.createJarFile(MODS_DIR.resolve("httpserver-9.0.0.jar"),
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    98
                httpServerClasses);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    99
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   100
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   101
        // compile basictest to mods/basictest
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   102
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   103
        compiled = CompilerUtils
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   104
            .compile(SRC_DIR.resolve(testModule),
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   105
                    MODS_DIR.resolve(testModule),
40261
86a49ba76f52 8136930: Simplify use of module-system options by custom launchers
mchung
parents: 36511
diff changeset
   106
                    "--module-path", MODS_DIR.toString());
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   107
        assertTrue(compiled);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   108
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   109
40261
86a49ba76f52 8136930: Simplify use of module-system options by custom launchers
mchung
parents: 36511
diff changeset
   110
        // launch the test. Need --add-mdoules because nothing explicitly depends on logging
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   111
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   112
        int exitValue
40261
86a49ba76f52 8136930: Simplify use of module-system options by custom launchers
mchung
parents: 36511
diff changeset
   113
            = executeTestJava("--module-path", MODS_DIR.toString(),
86a49ba76f52 8136930: Simplify use of module-system options by custom launchers
mchung
parents: 36511
diff changeset
   114
                              "--add-modules", "logging",
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   115
                              "-m", testModule + "/" + mainClass)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   116
                .outputTo(System.out)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   117
                .errorTo(System.out)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   118
                .getExitValue();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   119
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   120
        assertTrue(exitValue == 0);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   121
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   122
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
     * Test using a JAR file with a service provider as an automatic module.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   127
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   128
     * The consists of 2 modules:
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   129
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   130
     * sptest - the test itself
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   131
     * bananascript - a JAR file (automatic module) with a dummy ScriptEngineFactory
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   132
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   133
     * The test uses ServiceLoader to locate and load ScriptEngineFactory
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   134
     * implementations. It checks that bananascript is located.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   135
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   136
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   137
    public void testServiceProvider() throws Exception {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   138
        boolean compiled;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   139
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   140
        Path providerSrc = SRC_DIR.resolve("bananascript");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   141
        Path providerClasses = CLASSES_DIR.resolve("bananascript");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   142
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   143
        String testModule = "sptest";
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   144
        String mainClass = "test.Main";
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
        // create mods/bananascript-0.9.jar
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   148
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   149
        compiled = CompilerUtils.compile(providerSrc, providerClasses);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   150
        assertTrue(compiled);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   151
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   152
        String config = "META-INF/services/javax.script.ScriptEngineFactory";
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   153
        Path services = providerClasses.resolve(config).getParent();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   154
        Files.createDirectories(services);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   155
        Files.copy(providerSrc.resolve(config), providerClasses.resolve(config));
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   156
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   157
        JarUtils.createJarFile(MODS_DIR.resolve("bananascript-0.9.jar"), providerClasses);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   158
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   159
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   160
        // compile sptest to mods/sptest
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   161
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   162
        compiled = CompilerUtils
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   163
                .compile(SRC_DIR.resolve(testModule),
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   164
                        MODS_DIR.resolve(testModule),
40261
86a49ba76f52 8136930: Simplify use of module-system options by custom launchers
mchung
parents: 36511
diff changeset
   165
                        "--module-path", MODS_DIR.toString());
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   166
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   167
        assertTrue(compiled);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   168
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   169
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   170
        // launch the test
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   171
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   172
        int exitValue
40261
86a49ba76f52 8136930: Simplify use of module-system options by custom launchers
mchung
parents: 36511
diff changeset
   173
            = executeTestJava("--module-path", MODS_DIR.toString(),
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   174
                              "-m", testModule + "/" + mainClass)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   175
                .outputTo(System.out)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   176
                .errorTo(System.out)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   177
                .getExitValue();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   178
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   179
        assertTrue(exitValue == 0);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   180
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   181
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   182
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   183
}