test/hotspot/jtreg/runtime/appcds/dynamicArchive/MainModuleOnly.java
changeset 54927 1512d88b24c6
equal deleted inserted replaced
54926:d4e7ccaf1445 54927:1512d88b24c6
       
     1 /*
       
     2  * Copyright (c) 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/appcds
       
    29  * @modules jdk.compiler
       
    30  *          jdk.jartool/sun.tools.jar
       
    31  *          jdk.jlink
       
    32  * @run driver MainModuleOnly
       
    33  * @summary Test some scenarios with a main modular jar specified in the --module-path and -cp options in the command line.
       
    34  */
       
    35 
       
    36 import java.io.File;
       
    37 import java.nio.file.Files;
       
    38 import java.nio.file.Path;
       
    39 import java.nio.file.Paths;
       
    40 import java.util.Arrays;
       
    41 
       
    42 import jdk.test.lib.process.OutputAnalyzer;
       
    43 import jdk.test.lib.Platform;
       
    44 
       
    45 public class MainModuleOnly extends DynamicArchiveTestBase {
       
    46 
       
    47     private static final Path USER_DIR = Paths.get(System.getProperty("user.dir"));
       
    48 
       
    49     private static final String FS = File.separator;
       
    50     private static final String TEST_SRC = System.getProperty("test.src") +
       
    51         FS + ".." + FS + "jigsaw" + FS + "modulepath";
       
    52 
       
    53     private static final Path SRC_DIR = Paths.get(TEST_SRC, "src");
       
    54     private static final Path MODS_DIR = Paths.get("mods");
       
    55 
       
    56     // the module name of the test module
       
    57     private static final String TEST_MODULE1 = "com.simple";
       
    58 
       
    59     // the module main class
       
    60     private static final String MAIN_CLASS = "com.simple.Main";
       
    61 
       
    62     private static Path moduleDir = null;
       
    63     private static Path moduleDir2 = null;
       
    64     private static Path destJar = null;
       
    65 
       
    66     public static void buildTestModule() throws Exception {
       
    67 
       
    68         // javac -d mods/$TESTMODULE --module-path MOD_DIR src/$TESTMODULE/**
       
    69         JarBuilder.compileModule(SRC_DIR.resolve(TEST_MODULE1),
       
    70                                  MODS_DIR.resolve(TEST_MODULE1),
       
    71                                  MODS_DIR.toString());
       
    72 
       
    73 
       
    74         moduleDir = Files.createTempDirectory(USER_DIR, "mlib");
       
    75         moduleDir2 = Files.createTempDirectory(USER_DIR, "mlib2");
       
    76 
       
    77         Path srcJar = moduleDir.resolve(TEST_MODULE1 + ".jar");
       
    78         destJar = moduleDir2.resolve(TEST_MODULE1 + ".jar");
       
    79         String classes = MODS_DIR.resolve(TEST_MODULE1).toString();
       
    80         JarBuilder.createModularJar(srcJar.toString(), classes, MAIN_CLASS);
       
    81         Files.copy(srcJar, destJar);
       
    82 
       
    83     }
       
    84 
       
    85     static void testDefaultBase() throws Exception {
       
    86         String topArchiveName = getNewArchiveName("top");
       
    87         doTest(topArchiveName);
       
    88     }
       
    89 
       
    90     public static void main(String... args) throws Exception {
       
    91         runTest(MainModuleOnly::testDefaultBase);
       
    92     }
       
    93 
       
    94     public static void doTest(String topArchiveName) throws Exception {
       
    95         // compile the modules and create the modular jar files
       
    96         buildTestModule();
       
    97         // create an archive with both -cp and --module-path in the command line.
       
    98         // Only the class in the modular jar in the --module-path will be archived;
       
    99         // the class in the modular jar in the -cp won't be archived.
       
   100         dump2(null, topArchiveName,
       
   101               "-Xlog:cds+dynamic=debug,cds=debug",
       
   102               "-cp", destJar.toString(),
       
   103               "--module-path", moduleDir.toString(),
       
   104               "-m", TEST_MODULE1)
       
   105               .assertNormalExit(output -> {
       
   106                       output.shouldContain("Buffer-space to target-space delta")
       
   107                             .shouldContain("Written dynamic archive 0x");
       
   108                   });
       
   109 
       
   110         // run with the archive using the same command line as in dump time.
       
   111         // The main class should be loaded from the archive.
       
   112         run2(null, topArchiveName,
       
   113              "-Xlog:cds+dynamic=debug,cds=debug,class+load=trace",
       
   114              "-cp", destJar.toString(),
       
   115              "--module-path", moduleDir.toString(),
       
   116              "-m", TEST_MODULE1)
       
   117             .assertNormalExit(output -> {
       
   118                     output.shouldContain("[class,load] com.simple.Main source: shared objects file")
       
   119                           .shouldHaveExitValue(0);
       
   120                 });
       
   121 
       
   122         // run with the archive with the main class name inserted before the -m.
       
   123         // The main class name will be picked up before the module name. So the
       
   124         // main class should be loaded from the jar in the -cp.
       
   125         run2(null, topArchiveName,
       
   126              "-Xlog:cds+dynamic=debug,cds=debug,class+load=trace",
       
   127              "-cp", destJar.toString(),
       
   128              "--module-path", moduleDir.toString(),
       
   129              MAIN_CLASS, "-m", TEST_MODULE1)
       
   130             .assertNormalExit(out ->
       
   131                 out.shouldMatch(".class.load. com.simple.Main source:.*com.simple.jar"));
       
   132 
       
   133         // run with the archive with exploded module. Since during dump time, we
       
   134         // only archive classes from the modular jar in the --module-path, the
       
   135         // main class should be loaded from the exploded module directory.
       
   136         run2(null, topArchiveName,
       
   137              "-Xlog:cds+dynamic=debug,cds=debug,class+load=trace",
       
   138              "-cp", destJar.toString(),
       
   139              "--module-path", MODS_DIR.toString(),
       
   140              "-m", TEST_MODULE1 + "/" + MAIN_CLASS)
       
   141             .assertNormalExit(out -> {
       
   142                 out.shouldMatch(".class.load. com.simple.Main source:.*com.simple")
       
   143                    .shouldContain(MODS_DIR.toString());
       
   144             });
       
   145 
       
   146         // run with the archive with the --upgrade-module-path option.
       
   147         // CDS will be disabled with this options and the main class will be
       
   148         // loaded from the modular jar.
       
   149         run2(null, topArchiveName,
       
   150              "-Xlog:cds+dynamic=debug,cds=debug,class+load=trace",
       
   151              "-cp", destJar.toString(),
       
   152              "--upgrade-module-path", moduleDir.toString(),
       
   153              "--module-path", moduleDir.toString(),
       
   154              "-m", TEST_MODULE1)
       
   155             .assertSilentlyDisabledCDS(out -> {
       
   156                 out.shouldHaveExitValue(0)
       
   157                    .shouldMatch("CDS is disabled when the.*option is specified")
       
   158                    .shouldMatch(".class.load. com.simple.Main source:.*com.simple.jar");
       
   159             });
       
   160         // run with the archive with the --limit-modules option.
       
   161         // CDS will be disabled with this options and the main class will be
       
   162         // loaded from the modular jar.
       
   163         run2(null, topArchiveName,
       
   164              "-Xlog:cds+dynamic=debug,cds=debug,class+load=trace",
       
   165              "-cp", destJar.toString(),
       
   166              "--limit-modules", "java.base," + TEST_MODULE1,
       
   167              "--module-path", moduleDir.toString(),
       
   168              "-m", TEST_MODULE1)
       
   169             .assertSilentlyDisabledCDS(out -> {
       
   170                 out.shouldHaveExitValue(0)
       
   171                    .shouldMatch("CDS is disabled when the.*option is specified")
       
   172                    .shouldMatch(".class.load. com.simple.Main source:.*com.simple.jar");
       
   173             });
       
   174         // run with the archive with the --patch-module option.
       
   175         // CDS will be disabled with this options and the main class will be
       
   176         // loaded from the modular jar.
       
   177         run2(null, topArchiveName,
       
   178              "-Xlog:cds+dynamic=debug,cds=debug,class+load=trace",
       
   179              "-cp", destJar.toString(),
       
   180              "--patch-module", TEST_MODULE1 + "=" + MODS_DIR.toString(),
       
   181              "--module-path", moduleDir.toString(),
       
   182              "-m", TEST_MODULE1)
       
   183             .assertSilentlyDisabledCDS(out -> {
       
   184                 out.shouldHaveExitValue(0)
       
   185                    .shouldMatch("CDS is disabled when the.*option is specified")
       
   186                    .shouldMatch(".class.load. com.simple.Main source:.*com.simple.jar");
       
   187             });
       
   188         // modify the timestamp of the jar file
       
   189         (new File(destJar.toString())).setLastModified(System.currentTimeMillis() + 2000);
       
   190         // run with the archive and the jar with modified timestamp.
       
   191         // It should fail due to timestamp of the jar doesn't match the one
       
   192         // used during dump time.
       
   193         run2(null, topArchiveName,
       
   194              "-Xlog:cds+dynamic=debug,cds=debug,class+load=trace",
       
   195              "-cp", destJar.toString(),
       
   196              "--module-path", moduleDir.toString(),
       
   197              "-m", TEST_MODULE1)
       
   198             .assertAbnormalExit(
       
   199                 "A jar file is not the one used while building the shared archive file:");
       
   200         // create an archive with a non-empty directory in the --module-path.
       
   201         // The dumping process will exit with an error due to non-empty directory
       
   202         // in the --module-path.
       
   203         dump2(null, topArchiveName,
       
   204               "-Xlog:cds+dynamic=debug,cds=debug",
       
   205               "-cp", destJar.toString(),
       
   206               "--module-path", MODS_DIR.toString(),
       
   207               "-m", TEST_MODULE1 + "/" + MAIN_CLASS)
       
   208             .assertAbnormalExit(output -> {
       
   209                 output.shouldMatch("Error: non-empty directory.*com.simple");
       
   210                 });
       
   211 
       
   212         // test module path with very long length
       
   213         //
       
   214         // This test can't be run on the windows platform due to an existing
       
   215         // issue in ClassLoader::get_canonical_path() (JDK-8190737).
       
   216         if (Platform.isWindows()) {
       
   217             System.out.println("Long module path test cannot be tested on the Windows platform.");
       
   218             return;
       
   219         }
       
   220         Path longDir = USER_DIR;
       
   221         int pathLen = longDir.toString().length();
       
   222         int PATH_LEN = 2034;
       
   223         int MAX_DIR_LEN = 250;
       
   224         while (pathLen < PATH_LEN) {
       
   225             int remaining = PATH_LEN - pathLen;
       
   226             int subPathLen = remaining > MAX_DIR_LEN ? MAX_DIR_LEN : remaining;
       
   227             char[] chars = new char[subPathLen];
       
   228             Arrays.fill(chars, 'x');
       
   229             String subPath = new String(chars);
       
   230             longDir = Paths.get(longDir.toString(), subPath);
       
   231             pathLen = longDir.toString().length();
       
   232         }
       
   233         File longDirFile = new File(longDir.toString());
       
   234         try {
       
   235             longDirFile.mkdirs();
       
   236         } catch (Exception e) {
       
   237             throw e;
       
   238         }
       
   239         Path longDirJar = longDir.resolve(TEST_MODULE1 + ".jar");
       
   240         // IOException results from the Files.copy() call on platform
       
   241         // such as MacOS X. Test can't be proceeded further with the
       
   242         // exception.
       
   243         try {
       
   244             Files.copy(destJar, longDirJar);
       
   245         } catch (java.io.IOException ioe) {
       
   246             System.out.println("Caught IOException from Files.copy(). Cannot continue.");
       
   247             return;
       
   248         }
       
   249         dump2(null, topArchiveName,
       
   250               "-Xlog:cds+dynamic=debug,cds=debug",
       
   251               "-cp", destJar.toString(),
       
   252               "-Xlog:exceptions=trace",
       
   253               "--module-path", longDirJar.toString(),
       
   254               "-m", TEST_MODULE1)
       
   255             .ifAbnormalExit(output -> {
       
   256                 output.shouldMatch("os::stat error.*CDS dump aborted");
       
   257                 });
       
   258     }
       
   259 }