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 Class-Path: attribute in MANIFEST file
  28  * @requires vm.cds
  29  * @library /test/lib
  30  * @modules jdk.jartool/sun.tools.jar
  31  * @run driver ClassPathAttr
  32  */
  33 
  34 import jdk.test.lib.process.OutputAnalyzer;
  35 import java.io.File;
  36 import java.nio.file.Files;
  37 import java.nio.file.FileAlreadyExistsException;
  38 import java.nio.file.StandardCopyOption;
  39 import java.nio.file.Paths;
  40 
  41 
  42 public class ClassPathAttr {
  43 
  44   public static void main(String[] args) throws Exception {
  45     testNormalOps();
  46     testNonExistentJars();
  47   }
  48 
  49   static void testNormalOps() throws Exception {
  50     buildCpAttr("cpattr1", "cpattr1.mf", "CpAttr1", "CpAttr1");
  51     buildCpAttr("cpattr1_long", "cpattr1_long.mf", "CpAttr1", "CpAttr1");
  52     buildCpAttr("cpattr2", "cpattr2.mf", "CpAttr2", "CpAttr2");
  53     buildCpAttr("cpattr3", "cpattr3.mf", "CpAttr3", "CpAttr2", "CpAttr3");
  54     buildCpAttr("cpattr4", "cpattr4.mf", "CpAttr4",
  55         "CpAttr2", "CpAttr3", "CpAttr4", "CpAttr5");
  56     buildCpAttr("cpattr5_123456789_223456789_323456789_423456789_523456789_623456789", "cpattr5_extra_long.mf", "CpAttr5", "CpAttr5");
  57 
  58     for (int i=1; i<=2; i++) {
  59       String jar1 = TestCommon.getTestJar("cpattr1.jar");
  60       String jar4 = TestCommon.getTestJar("cpattr4.jar");
  61       if (i == 2) {
  62         // Test case #2 -- same as #1, except we use cpattr1_long.jar, which has a super-long
  63         // Class-Path: attribute.
  64         jar1 = TestCommon.getTestJar("cpattr1_long.jar");
  65       }
  66       String cp = jar1 + File.pathSeparator + jar4;
  67 
  68       TestCommon.testDump(cp, TestCommon.list("CpAttr1",
  69                                                           "CpAttr2",
  70                                                           "CpAttr3",
  71                                                           "CpAttr4",
  72                                                           "CpAttr5"));
  73 
  74       TestCommon.run(
  75           "-cp", cp,
  76           "CpAttr1")
  77         .assertNormalExit();
  78 
  79       // Logging test for class+path.
  80       TestCommon.run(
  81           "-Xlog:class+path",
  82           "-cp", cp,
  83           "CpAttr1")
  84         .assertNormalExit(output -> {
  85             output.shouldMatch("checking shared classpath entry: .*cpattr2.jar");
  86             output.shouldMatch("checking shared classpath entry: .*cpattr3.jar");
  87           });
  88 
  89       //  Make sure aliased TraceClassPaths still works
  90       TestCommon.run(
  91           "-XX:+TraceClassPaths",
  92           "-cp", cp,
  93           "CpAttr1")
  94         .assertNormalExit(output -> {
  95             output.shouldMatch("checking shared classpath entry: .*cpattr2.jar");
  96             output.shouldMatch("checking shared classpath entry: .*cpattr3.jar");
  97           });
  98     }
  99   }
 100 
 101   static void testNonExistentJars() throws Exception {
 102     buildCpAttr("cpattr6", "cpattr6.mf", "CpAttr6", "CpAttr6");
 103 
 104     String cp = TestCommon.getTestJar("cpattr6.jar");
 105     String nonExistPath = System.getProperty("test.classes") + File.separator + "cpattrX.jar";
 106     (new File(nonExistPath)).delete();
 107 
 108     TestCommon.testDump(cp, TestCommon.list("CpAttr6"),
 109         "-Xlog:class+path");
 110 
 111     TestCommon.run(
 112         "-Xlog:class+path",
 113         "-cp", cp,
 114         "CpAttr6")
 115       .assertNormalExit(output -> {
 116           output.shouldMatch("should be non-existent: .*cpattrX.jar");
 117         });
 118 
 119     // Now make nonExistPath exist. CDS will fail to load.
 120     Files.copy(Paths.get(cp), Paths.get(nonExistPath),
 121                StandardCopyOption.REPLACE_EXISTING);
 122 
 123     TestCommon.run(
 124         "-Xlog:class+path",
 125         "-cp", cp,
 126         "CpAttr6")
 127       .assertAbnormalExit(output -> {
 128           output.shouldMatch("should be non-existent: .*cpattrX.jar");
 129           output.shouldMatch("file must not exist: .*cpattrX.jar");
 130         });
 131   }
 132 
 133   private static void buildCpAttr(String jarName, String manifest, String enclosingClassName, String ...testClassNames) throws Exception {
 134     String jarClassesDir = System.getProperty("test.classes") + File.separator + jarName + "_classes";
 135     try { Files.createDirectory(Paths.get(jarClassesDir)); } catch (FileAlreadyExistsException e) { }
 136 
 137     JarBuilder.compile(jarClassesDir, System.getProperty("test.src") + File.separator +
 138         "test-classes" + File.separator + enclosingClassName + ".java");
 139     JarBuilder.buildWithManifest(jarName, manifest, jarClassesDir, testClassNames);
 140   }
 141 }