test/hotspot/jtreg/runtime/cds/appcds/jigsaw/modulepath/MainModuleOnly.java
branchdatagramsocketimpl-branch
changeset 58678 9cf78a70fa4f
parent 54927 1512d88b24c6
child 58679 9c3209ff7550
equal deleted inserted replaced
58677:13588c901957 58678:9cf78a70fa4f
       
     1 /*
       
     2  * Copyright (c) 2018, 2019, Oracle and/or its affiliates. All rights reserved.
       
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
       
     4  *
       
     5  * This code is free software; you can redistribute it and/or modify it
       
     6  * under the terms of the GNU General Public License version 2 only, as
       
     7  * published by the Free Software Foundation.
       
     8  *
       
     9  * This code is distributed in the hope that it will be useful, but WITHOUT
       
    10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
       
    11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
       
    12  * version 2 for more details (a copy is included in the LICENSE file that
       
    13  * accompanied this code).
       
    14  *
       
    15  * You should have received a copy of the GNU General Public License version
       
    16  * 2 along with this work; if not, write to the Free Software Foundation,
       
    17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
       
    18  *
       
    19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
       
    20  * or visit www.oracle.com if you need additional information or have any
       
    21  * questions.
       
    22  *
       
    23  */
       
    24 
       
    25 /**
       
    26  * @test
       
    27  * @requires vm.cds
       
    28  * @library /test/lib /test/hotspot/jtreg/runtime/cds/appcds
       
    29  * @modules jdk.jartool/sun.tools.jar
       
    30  * @run main/othervm MainModuleOnly
       
    31  * @summary Test some scenarios with a main modular jar specified in the --module-path and -cp options in the command line.
       
    32  */
       
    33 
       
    34 import java.io.File;
       
    35 import java.nio.file.Files;
       
    36 import java.nio.file.Path;
       
    37 import java.nio.file.Paths;
       
    38 import java.util.Arrays;
       
    39 
       
    40 import jdk.test.lib.cds.CDSTestUtils.Result;
       
    41 import jdk.test.lib.process.OutputAnalyzer;
       
    42 import jdk.test.lib.Platform;
       
    43 
       
    44 public class MainModuleOnly {
       
    45 
       
    46     private static final Path USER_DIR = Paths.get(System.getProperty("user.dir"));
       
    47 
       
    48     private static final String TEST_SRC = System.getProperty("test.src");
       
    49 
       
    50     private static final Path SRC_DIR = Paths.get(TEST_SRC, "src");
       
    51     private static final Path MODS_DIR = Paths.get("mods");
       
    52 
       
    53     // the module name of the test module
       
    54     private static final String TEST_MODULE1 = "com.simple";
       
    55 
       
    56     // the module main class
       
    57     private static final String MAIN_CLASS = "com.simple.Main";
       
    58 
       
    59     private static Path moduleDir = null;
       
    60     private static Path moduleDir2 = null;
       
    61     private static Path destJar = null;
       
    62 
       
    63     private static final String jarFileError = "A jar file is not the one used while building the shared archive file:";
       
    64 
       
    65     public static void buildTestModule() throws Exception {
       
    66 
       
    67         // javac -d mods/$TESTMODULE --module-path MOD_DIR src/$TESTMODULE/**
       
    68         JarBuilder.compileModule(SRC_DIR.resolve(TEST_MODULE1),
       
    69                                  MODS_DIR.resolve(TEST_MODULE1),
       
    70                                  MODS_DIR.toString());
       
    71 
       
    72 
       
    73         moduleDir = Files.createTempDirectory(USER_DIR, "mlib");
       
    74         moduleDir2 = Files.createTempDirectory(USER_DIR, "mlib2");
       
    75 
       
    76         Path srcJar = moduleDir.resolve(TEST_MODULE1 + ".jar");
       
    77         destJar = moduleDir2.resolve(TEST_MODULE1 + ".jar");
       
    78         String classes = MODS_DIR.resolve(TEST_MODULE1).toString();
       
    79         JarBuilder.createModularJar(srcJar.toString(), classes, MAIN_CLASS);
       
    80         Files.copy(srcJar, destJar);
       
    81 
       
    82     }
       
    83 
       
    84     public static void main(String... args) throws Exception {
       
    85         // compile the modules and create the modular jar files
       
    86         buildTestModule();
       
    87         String appClasses[] = {MAIN_CLASS};
       
    88         // create an archive with both -cp and --module-path in the command line.
       
    89         // Only the class in the modular jar in the --module-path will be archived;
       
    90         // the class in the modular jar in the -cp won't be archived.
       
    91         OutputAnalyzer output = TestCommon.createArchive(
       
    92                                         destJar.toString(), appClasses,
       
    93                                         "--module-path", moduleDir.toString(),
       
    94                                         "-m", TEST_MODULE1);
       
    95         TestCommon.checkDump(output);
       
    96 
       
    97         // run with the archive using the same command line as in dump time.
       
    98         // The main class should be loaded from the archive.
       
    99         TestCommon.run("-Xlog:class+load=trace",
       
   100                        "-cp", destJar.toString(),
       
   101                        "--module-path", moduleDir.toString(),
       
   102                        "-m", TEST_MODULE1)
       
   103             .assertNormalExit("[class,load] com.simple.Main source: shared objects file");
       
   104 
       
   105         // run with the archive with the main class name inserted before the -m.
       
   106         // The main class name will be picked up before the module name. So the
       
   107         // main class should be loaded from the jar in the -cp.
       
   108         TestCommon.run("-Xlog:class+load=trace",
       
   109                        "-cp", destJar.toString(),
       
   110                        "--module-path", moduleDir.toString(),
       
   111                        MAIN_CLASS, "-m", TEST_MODULE1)
       
   112             .assertNormalExit(out ->
       
   113                 out.shouldMatch(".class.load. com.simple.Main source:.*com.simple.jar"));
       
   114 
       
   115         // run with the archive with exploded module. Since during dump time, we
       
   116         // only archive classes from the modular jar in the --module-path, the
       
   117         // main class should be loaded from the exploded module directory.
       
   118         TestCommon.run("-Xlog:class+load=trace",
       
   119                        "-cp", destJar.toString(),
       
   120                        "--module-path", MODS_DIR.toString(),
       
   121                        "-m", TEST_MODULE1 + "/" + MAIN_CLASS)
       
   122             .assertNormalExit(out -> {
       
   123                 out.shouldMatch(".class.load. com.simple.Main source:.*com.simple")
       
   124                    .shouldContain(MODS_DIR.toString());
       
   125             });
       
   126 
       
   127         // run with the archive with the --upgrade-module-path option.
       
   128         // CDS will be disabled with this options and the main class will be
       
   129         // loaded from the modular jar.
       
   130         TestCommon.run("-Xlog:class+load=trace",
       
   131                        "-cp", destJar.toString(),
       
   132                        "--upgrade-module-path", moduleDir.toString(),
       
   133                        "--module-path", moduleDir.toString(),
       
   134                        "-m", TEST_MODULE1)
       
   135             .assertSilentlyDisabledCDS(out -> {
       
   136                 out.shouldHaveExitValue(0)
       
   137                    .shouldMatch("CDS is disabled when the.*option is specified")
       
   138                    .shouldMatch(".class.load. com.simple.Main source:.*com.simple.jar");
       
   139             });
       
   140         // run with the archive with the --limit-modules option.
       
   141         // CDS will be disabled with this options and the main class will be
       
   142         // loaded from the modular jar.
       
   143         TestCommon.run("-Xlog:class+load=trace",
       
   144                        "-cp", destJar.toString(),
       
   145                        "--limit-modules", "java.base," + TEST_MODULE1,
       
   146                        "--module-path", moduleDir.toString(),
       
   147                        "-m", TEST_MODULE1)
       
   148             .assertSilentlyDisabledCDS(out -> {
       
   149                 out.shouldHaveExitValue(0)
       
   150                    .shouldMatch("CDS is disabled when the.*option is specified")
       
   151                    .shouldMatch(".class.load. com.simple.Main source:.*com.simple.jar");
       
   152             });
       
   153         // run with the archive with the --patch-module option.
       
   154         // CDS will be disabled with this options and the main class will be
       
   155         // loaded from the modular jar.
       
   156         TestCommon.run("-Xlog:class+load=trace",
       
   157                        "-cp", destJar.toString(),
       
   158                        "--patch-module", TEST_MODULE1 + "=" + MODS_DIR.toString(),
       
   159                        "--module-path", moduleDir.toString(),
       
   160                        "-m", TEST_MODULE1)
       
   161             .assertSilentlyDisabledCDS(out -> {
       
   162                 out.shouldHaveExitValue(0)
       
   163                    .shouldMatch("CDS is disabled when the.*option is specified")
       
   164                    .shouldMatch(".class.load. com.simple.Main source:.*com.simple.jar");
       
   165             });
       
   166         // modify the timestamp of the jar file
       
   167         (new File(destJar.toString())).setLastModified(System.currentTimeMillis() + 2000);
       
   168         // run with the archive and the jar with modified timestamp.
       
   169         // It should fail due to timestamp of the jar doesn't match the one
       
   170         // used during dump time.
       
   171         Result res = TestCommon.run("-cp", destJar.toString(),
       
   172                        "-Xlog:cds",
       
   173                        "--module-path", moduleDir.toString(),
       
   174                        "-m", TEST_MODULE1);
       
   175         res.assertAbnormalExit(jarFileError);
       
   176         // create an archive with a non-empty directory in the --module-path.
       
   177         // The dumping process will exit with an error due to non-empty directory
       
   178         // in the --module-path.
       
   179         String mainModule = TEST_MODULE1;
       
   180         if (TestCommon.isDynamicArchive()) {
       
   181             mainModule += "/" + MAIN_CLASS;
       
   182         }
       
   183         output = TestCommon.createArchive(destJar.toString(), appClasses,
       
   184                                           "--module-path", MODS_DIR.toString(),
       
   185                                           "-m", mainModule);
       
   186         output.shouldHaveExitValue(1)
       
   187               .shouldMatch("Error: non-empty directory.*com.simple");
       
   188 
       
   189         // test module path with very long length
       
   190         //
       
   191         // This test can't be run on the windows platform due to an existing
       
   192         // issue in ClassLoader::get_canonical_path() (JDK-8190737).
       
   193         if (Platform.isWindows()) {
       
   194             System.out.println("Long module path test cannot be tested on the Windows platform.");
       
   195             return;
       
   196         }
       
   197         Path longDir = USER_DIR;
       
   198         int pathLen = longDir.toString().length();
       
   199         int PATH_LEN = 2034;
       
   200         int MAX_DIR_LEN = 250;
       
   201         while (pathLen < PATH_LEN) {
       
   202             int remaining = PATH_LEN - pathLen;
       
   203             int subPathLen = remaining > MAX_DIR_LEN ? MAX_DIR_LEN : remaining;
       
   204             char[] chars = new char[subPathLen];
       
   205             Arrays.fill(chars, 'x');
       
   206             String subPath = new String(chars);
       
   207             longDir = Paths.get(longDir.toString(), subPath);
       
   208             pathLen = longDir.toString().length();
       
   209         }
       
   210         File longDirFile = new File(longDir.toString());
       
   211         try {
       
   212             longDirFile.mkdirs();
       
   213         } catch (Exception e) {
       
   214             throw e;
       
   215         }
       
   216         Path longDirJar = longDir.resolve(TEST_MODULE1 + ".jar");
       
   217         // IOException results from the Files.copy() call on platform
       
   218         // such as MacOS X. Test can't be proceeded further with the
       
   219         // exception.
       
   220         try {
       
   221             Files.copy(destJar, longDirJar);
       
   222         } catch (java.io.IOException ioe) {
       
   223             System.out.println("Caught IOException from Files.copy(). Cannot continue.");
       
   224             return;
       
   225         }
       
   226         output = TestCommon.createArchive(destJar.toString(), appClasses,
       
   227                                           "-Xlog:exceptions=trace",
       
   228                                           "--module-path", longDirJar.toString(),
       
   229                                           "-m", TEST_MODULE1);
       
   230         if (output.getExitValue() != 0) {
       
   231             output.shouldMatch("os::stat error.*CDS dump aborted");
       
   232         }
       
   233     }
       
   234 }