test/hotspot/jtreg/runtime/appcds/AppendClasspath.java
branchJDK-8200758-branch
changeset 57588 dac8f245de8e
parent 57587 16c4975e9e09
parent 57586 f459f98aa30d
child 57591 6805e0ef7453
equal deleted inserted replaced
57587:16c4975e9e09 57588:dac8f245de8e
     1 /*
       
     2  * Copyright (c) 2014, 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  * @summary At run time, it is OK to append new elements to the classpath that was used at dump time.
       
    28  * @requires vm.cds
       
    29  * @library /test/lib
       
    30  * @modules java.base/jdk.internal.misc
       
    31  *          java.management
       
    32  *          jdk.jartool/sun.tools.jar
       
    33  * @compile test-classes/Hello.java
       
    34  * @compile test-classes/HelloMore.java
       
    35  * @run driver AppendClasspath
       
    36  */
       
    37 
       
    38 import java.io.File;
       
    39 import java.nio.file.Files;
       
    40 import java.nio.file.Paths;
       
    41 import java.nio.file.StandardCopyOption;
       
    42 import jdk.test.lib.process.OutputAnalyzer;
       
    43 
       
    44 public class AppendClasspath {
       
    45 
       
    46   public static void main(String[] args) throws Exception {
       
    47     String appJar = JarBuilder.getOrCreateHelloJar();
       
    48     String appJar2 = JarBuilder.build("AppendClasspath_HelloMore", "HelloMore");
       
    49 
       
    50     // Dump an archive with a specified JAR file in -classpath
       
    51     TestCommon.testDump(appJar, TestCommon.list("Hello"));
       
    52 
       
    53     // PASS: 1) runtime with classpath containing the one used in dump time
       
    54     TestCommon.run(
       
    55         "-cp", appJar + File.pathSeparator + appJar2,
       
    56         "HelloMore")
       
    57       .assertNormalExit();
       
    58 
       
    59     // PASS: 2) runtime has an non-existing jar in the -cp
       
    60     String classDir = System.getProperty("test.classes");
       
    61     String newFile = "non-exist.jar";
       
    62     String nonExistPath = classDir + File.separator + newFile;
       
    63     String classPath = appJar + File.pathSeparator + nonExistPath;
       
    64     File nonExistJar = new File(classDir, newFile);
       
    65     if (nonExistJar.exists()) {
       
    66         nonExistJar.delete();
       
    67     }
       
    68     TestCommon.run(
       
    69         "-cp", classPath,
       
    70         "-Xlog:class+path=trace",
       
    71         "Hello")
       
    72       .assertNormalExit();
       
    73 
       
    74     final String errorMessage1 = "Unable to use shared archive";
       
    75     final String errorMessage2 = "shared class paths mismatch";
       
    76     // FAIL: 1) runtime with classpath different from the one used in dump time
       
    77     // (runtime has an extra jar file prepended to the class path)
       
    78     TestCommon.run(
       
    79         "-Xlog:cds",
       
    80         "-cp", appJar2 + File.pathSeparator + appJar,
       
    81         "HelloMore")
       
    82         .assertAbnormalExit(errorMessage1, errorMessage2);
       
    83 
       
    84     // FAIL: 2) runtime with classpath part of the one used in dump time
       
    85     TestCommon.testDump(appJar + File.pathSeparator + appJar2,
       
    86                                       TestCommon.list("Hello"));
       
    87     TestCommon.run(
       
    88         "-Xlog:cds",
       
    89         "-cp", appJar2,
       
    90         "Hello")
       
    91         .assertAbnormalExit(errorMessage1, errorMessage2);
       
    92 
       
    93     // FAIL: 3) runtime with same set of jar files in the classpath but
       
    94     // with different order
       
    95     TestCommon.run(
       
    96         "-Xlog:cds",
       
    97         "-cp", appJar2 + File.pathSeparator + appJar,
       
    98         "HelloMore")
       
    99         .assertAbnormalExit(errorMessage1, errorMessage2);
       
   100 
       
   101     // FAIL: 4) non-existing jar during dump time but jar exists during runtime
       
   102     TestCommon.testDump(classPath, TestCommon.list("Hello"));
       
   103 
       
   104     Files.copy(Paths.get(classDir, "hello.jar"),
       
   105         Paths.get(classDir, newFile),
       
   106         StandardCopyOption.REPLACE_EXISTING);
       
   107 
       
   108     TestCommon.run(
       
   109         "-cp", classPath,
       
   110         "-Xlog:class+path=trace",
       
   111         "Hello")
       
   112         .assertAbnormalExit(errorMessage1, errorMessage2);
       
   113 
       
   114     }
       
   115 }