1 /*
   2  * Copyright (c) 2014, 2018, 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 java.base/jdk.internal.misc
  31  *          java.management
  32  *          jdk.jartool/sun.tools.jar
  33  * @run driver ClassPathAttr
  34  */
  35 
  36 import jdk.test.lib.process.OutputAnalyzer;
  37 import java.io.File;
  38 import java.nio.file.Files;
  39 import java.nio.file.FileAlreadyExistsException;
  40 import java.nio.file.StandardCopyOption;
  41 import java.nio.file.Paths;
  42 
  43 
  44 public class ClassPathAttr {
  45 
  46   public static void main(String[] args) throws Exception {
  47     buildCpAttr("cpattr1", "cpattr1.mf", "CpAttr1", "CpAttr1");
  48     buildCpAttr("cpattr1_long", "cpattr1_long.mf", "CpAttr1", "CpAttr1");
  49     buildCpAttr("cpattr2", "cpattr2.mf", "CpAttr2", "CpAttr2");
  50     buildCpAttr("cpattr3", "cpattr3.mf", "CpAttr3", "CpAttr2", "CpAttr3");
  51     buildCpAttr("cpattr4", "cpattr4.mf", "CpAttr4",
  52         "CpAttr2", "CpAttr3", "CpAttr4", "CpAttr5");
  53     buildCpAttr("cpattr5_123456789_223456789_323456789_423456789_523456789_623456789", "cpattr5_extra_long.mf", "CpAttr5", "CpAttr5");
  54 
  55     for (int i=1; i<=2; i++) {
  56       String jar1 = TestCommon.getTestJar("cpattr1.jar");
  57       String jar4 = TestCommon.getTestJar("cpattr4.jar");
  58       if (i == 2) {
  59         // Test case #2 -- same as #1, except we use cpattr1_long.jar, which has a super-long
  60         // Class-Path: attribute.
  61         jar1 = TestCommon.getTestJar("cpattr1_long.jar");
  62       }
  63       String cp = jar1 + File.pathSeparator + jar4;
  64 
  65       TestCommon.testDump(cp, TestCommon.list("CpAttr1",
  66                                                           "CpAttr2",
  67                                                           "CpAttr3",
  68                                                           "CpAttr4",
  69                                                           "CpAttr5"));
  70 
  71       TestCommon.run(
  72           "-cp", cp,
  73           "CpAttr1")
  74         .assertNormalExit();
  75 
  76       // Logging test for class+path.
  77       TestCommon.run(
  78           "-Xlog:class+path",
  79           "-cp", cp,
  80           "CpAttr1")
  81         .assertNormalExit(output -> {
  82             output.shouldMatch("checking shared classpath entry: .*cpattr2.jar");
  83             output.shouldMatch("checking shared classpath entry: .*cpattr3.jar");
  84           });
  85 
  86       //  Make sure aliased TraceClassPaths still works
  87       TestCommon.run(
  88           "-XX:+TraceClassPaths",
  89           "-cp", cp,
  90           "CpAttr1")
  91         .assertNormalExit(output -> {
  92             output.shouldMatch("checking shared classpath entry: .*cpattr2.jar");
  93             output.shouldMatch("checking shared classpath entry: .*cpattr3.jar");
  94           });
  95     }
  96   }
  97 
  98   private static void buildCpAttr(String jarName, String manifest, String enclosingClassName, String ...testClassNames) throws Exception {
  99     String jarClassesDir = System.getProperty("test.classes") + File.separator + jarName + "_classes";
 100     try { Files.createDirectory(Paths.get(jarClassesDir)); } catch (FileAlreadyExistsException e) { }
 101 
 102     JarBuilder.compile(jarClassesDir, System.getProperty("test.src") + File.separator +
 103         "test-classes" + File.separator + enclosingClassName + ".java");
 104     JarBuilder.buildWithManifest(jarName, manifest, jarClassesDir, testClassNames);
 105   }
 106 }