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  * @summary Handling of non-existent classpath elements during dump time and run time
  28  * @requires vm.cds
  29  * @library /test/lib
  30  * @modules jdk.jartool/sun.tools.jar
  31  * @compile test-classes/Hello.java
  32  * @compile test-classes/HelloMore.java
  33  * @run driver NonExistClasspath
  34  */
  35 
  36 import java.io.File;
  37 import java.nio.file.Files;
  38 import java.nio.file.Paths;
  39 import java.nio.file.StandardCopyOption;
  40 import jdk.test.lib.process.OutputAnalyzer;
  41 
  42 public class NonExistClasspath {
  43     public static void main(String[] args) throws Exception {
  44         String appJar = JarBuilder.getOrCreateHelloJar();
  45         doTest(appJar, false);
  46         doTest(appJar, true);
  47     }
  48 
  49     static void doTest(String appJar, boolean bootcp) throws Exception {
  50         String classDir = System.getProperty("test.classes");
  51         String newFile = "non-exist.jar";
  52         String nonExistPath = classDir + File.separator + newFile;
  53         final String errorMessage1 = "Unable to use shared archive";
  54         final String errorMessage2 = "shared class paths mismatch";
  55         final String errorMessage3 = (bootcp ? "BOOT" : "APP") + " classpath mismatch";
  56 
  57         (new File(nonExistPath)).delete();
  58 
  59         String classPath = nonExistPath + File.pathSeparator + appJar;
  60         TestCommon.testDump("foobar", TestCommon.list("Hello"), make_args(bootcp, classPath));
  61 
  62         // The nonExistPath doesn't exist yet, so we should be able to run without problem
  63         TestCommon.run(make_args(bootcp,
  64                                  classPath, 
  65                                  "-Xlog:class+path=trace",
  66                                  "Hello"))
  67             .assertNormalExit();
  68 
  69         // Replace nonExistPath with another non-existent file in the CP, it should still work
  70         TestCommon.run(make_args(bootcp,
  71                                  nonExistPath + ".duh"  + File.pathSeparator + appJar,
  72                                  "-Xlog:class+path=trace",
  73                                  "Hello"))
  74             .assertNormalExit();
  75 
  76         // Add a few more non-existent files in the CP, it should still work
  77         TestCommon.run(make_args(bootcp,
  78                                  nonExistPath + ".duh"  + File.pathSeparator +
  79                                  nonExistPath + ".daa"  + File.pathSeparator +
  80                                  nonExistPath + ".boo"  + File.pathSeparator +
  81                                  appJar,
  82                                  "-Xlog:class+path=trace",
  83                                  "Hello"))
  84             .assertNormalExit();
  85 
  86         // Or, remove all non-existent paths from the CP, it should still work
  87         TestCommon.run(make_args(bootcp,
  88                                  appJar,
  89                                  "-Xlog:class+path=trace",
  90                                  "Hello"))
  91             .assertNormalExit();
  92 
  93         // Now make nonExistPath exist. CDS will fail to load.
  94         Files.copy(Paths.get(classDir, "hello.jar"),
  95                    Paths.get(classDir, newFile),
  96                    StandardCopyOption.REPLACE_EXISTING);
  97 
  98         TestCommon.run(make_args(bootcp,
  99                                  classPath,
 100                                  "-Xlog:class+path=trace",
 101                                  "Hello"))
 102             .assertAbnormalExit(errorMessage1, errorMessage2, errorMessage3);
 103     }
 104 
 105     static String[] make_args(boolean bootcp, String cp, String... suffix) {
 106         String args[];
 107         if (bootcp) {
 108             args = TestCommon.concat("-Xbootclasspath/a:" + cp);
 109         } else {
 110             args = TestCommon.concat("-cp", cp);
 111         }
 112 
 113         return TestCommon.concat(args, suffix);
 114     }
 115 }