1 /*
   2  * Copyright (c) 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  * @test
  26  * @bug 8201528
  27  * @summary The test will create JAR file(s) with the manifest file
  28  *          that customized package versioning information (different info for
  29  *          same package if multiple jars). Then verify package versioning info
  30  * @library /test/lib
  31  * @modules jdk.compiler
  32  * @run main PackageFromManifest setup test
  33  * @run main PackageFromManifest runJar test1.jar
  34  * @run main PackageFromManifest runJar test1.jar test2.jar foo.Foo1
  35  * @run main PackageFromManifest runJar test1.jar test2.jar foo.Foo2
  36  * @run main/othervm PackageFromManifest runUrlLoader test1.jar
  37  * @run main/othervm PackageFromManifest runUrlLoader test1.jar test2.jar foo.Foo1
  38  * @run main/othervm PackageFromManifest runUrlLoader test1.jar test2.jar foo.Foo2
  39  */
  40 
  41 import jdk.test.lib.compiler.CompilerUtils;
  42 import jdk.test.lib.process.ProcessTools;
  43 import jdk.test.lib.util.FileUtils;
  44 
  45 import java.io.File;
  46 import java.io.IOException;
  47 import java.io.InputStream;
  48 import java.net.MalformedURLException;
  49 import java.net.URL;
  50 import java.net.URLClassLoader;
  51 import java.nio.file.Files;
  52 import java.nio.file.Path;
  53 import java.nio.file.Paths;
  54 import java.util.ArrayList;
  55 import java.util.Arrays;
  56 import java.util.List;
  57 import java.util.jar.Manifest;
  58 import java.util.stream.Collectors;
  59 import java.util.stream.Stream;
  60 
  61 /**
  62  * This test accept at least two input parameters, first one is run type like
  63  * 'setup', 'runJar', 'runTest', 'runUrlLoader', the rest parameters are options
  64  * to each run type. 'setup' run type should be placed at first since it will
  65  * create necessary jars for the rest tests. 'runTest' will be called in test
  66  * logic only, it should not be used in @run
  67  *
  68  * #1 test will do setup only to generate required jars before other tests run
  69  * PackageFromManifest setup test
  70  *
  71  * #2 test will run against single jar file to verify package versioning
  72  * PackageFromManifest runJar test1.jar
  73  *
  74  * #4 test will run against two jar files, load class foo.Foo1 first, then
  75  * verify package versioning
  76  * PackageFromManifest runJar test1.jar test2.jar foo.Foo1
  77  *
  78  * #5 test will run against two jar files, load class foo.Foo2 first, then
  79  * verify package versioning
  80  * PackageFromManifest runJar test1.jar test2.jar foo.Foo2
  81  *
  82  * #3 test will use URLCLassLoader to load single jar file, then verify
  83  * package versioning
  84  * PackageFromManifest runUrlLoader test1.jar
  85  *
  86  * #6 test will use URLCLassLoader to load two jars, load class foo.Foo1 first,
  87  * then verify package versioning
  88  * PackageFromManifest runUrlLoader test1.jar test2.jar foo.Foo1
  89  *
  90  * #7 test will use URLCLassLoader to load two jars, load class foo.Foo2 first,
  91  * then verify package versioning
  92  * PackageFromManifest runUrlLoader test1.jar test2.jar foo.Foo2
  93  */
  94 public class PackageFromManifest {
  95 
  96     private static final String PACKAGE_NAME = "foo";
  97     private static final String TEST_JAR_FILE1 = "test1.jar";
  98     private static final String TEST_JAR_FILE2 = "test2.jar";
  99     private static final String TEST_SUFFIX1 = "1";
 100     private static final String TEST_SUFFIX2 = "2";
 101     private static final String TEST_CLASS_PREFIX = "Foo";
 102     private static final String TEST_CLASS_NAME1 =
 103             TEST_CLASS_PREFIX + TEST_SUFFIX1;
 104     private static final String TEST_CLASS_NAME2 =
 105             TEST_CLASS_PREFIX + TEST_SUFFIX2;
 106     private static final String MANIFEST_FILE = "test.mf";
 107     private static final String SPEC_TITLE = "testSpecTitle";
 108     private static final String SPEC_VENDOR = "testSpecVendor";
 109     private static final String IMPL_TITLE = "testImplTitle";
 110     private static final String IMPL_VENDOR = "testImplVendor";
 111     private static final Path WORKING_PATH = Paths.get(".");
 112 
 113     public static void main(String[] args) throws Exception {
 114         if (args != null && args.length > 1) {
 115             String runType = args[0];
 116             String[] options = Arrays.copyOfRange(args, 1, args.length);
 117             switch (runType) {
 118                 case "setup":
 119                     setup();
 120                     break;
 121                 case "runTest":
 122                     runTest(options);
 123                     break;
 124                 case "runJar":
 125                     runJar(options);
 126                     break;
 127                 case "runUrlLoader":
 128                     testUrlLoader(options);
 129                     break;
 130                 default:
 131                     throw new RuntimeException("Invalid run type : " + runType);
 132             }
 133         } else {
 134             throw new RuntimeException("Invalid input arguments");
 135         }
 136     }
 137 
 138     private static void createTestClass(String name) throws IOException {
 139         List<String> content = new ArrayList<>();
 140         content.add("package " + PACKAGE_NAME + ";");
 141         content.add("public class " + name + " {");
 142         content.add("}");
 143 
 144         Path javaFile = WORKING_PATH.resolve(name + ".java");
 145 
 146         Files.write(javaFile, content);
 147 
 148         CompilerUtils.compile(WORKING_PATH, WORKING_PATH);
 149 
 150         // clean up created java file
 151         Files.delete(javaFile);
 152     }
 153 
 154     private static void createManifest(String suffix) throws IOException {
 155         List<String> content = new ArrayList<>();
 156         content.add("Manifest-version: 1.1");
 157         content.add("Name: " + PACKAGE_NAME + "/");
 158         content.add("Specification-Title: " + SPEC_TITLE + suffix);
 159         content.add("Specification-Version: " + suffix);
 160         content.add("Specification-Vendor: " + SPEC_VENDOR + suffix);
 161         content.add("Implementation-Title: " + IMPL_TITLE + suffix);
 162         content.add("Implementation-Version: " + suffix);
 163         content.add("Implementation-Vendor: " + IMPL_VENDOR + suffix);
 164 
 165         Files.write(WORKING_PATH.resolve(MANIFEST_FILE), content);
 166     }
 167 
 168     private static void buildJar(String jarFileName, boolean isIncludeSelf)
 169             throws IOException {
 170         try (InputStream is = Files.newInputStream(Paths.get(MANIFEST_FILE))) {
 171             if (isIncludeSelf) {
 172                 Path selfPath = WORKING_PATH
 173                         .resolve("PackageFromManifest.class");
 174                 if (!Files.exists(selfPath)) {
 175                     Files.copy(Paths.get(System.getProperty("test.classes"))
 176                             .resolve("PackageFromManifest.class"), selfPath);
 177                 }
 178                 JarUtils.createJarFile(Paths.get(jarFileName), new Manifest(is),
 179                         WORKING_PATH, selfPath,
 180                         WORKING_PATH.resolve(PACKAGE_NAME));
 181             } else {
 182                 JarUtils.createJarFile(Paths.get(jarFileName), new Manifest(is),
 183                         WORKING_PATH, WORKING_PATH.resolve(PACKAGE_NAME));
 184             }
 185         }
 186 
 187         // clean up build files
 188         FileUtils.deleteFileTreeWithRetry(WORKING_PATH.resolve(PACKAGE_NAME));
 189         Files.delete(WORKING_PATH.resolve(MANIFEST_FILE));
 190     }
 191 
 192     private static void runJar(String[] options) throws Exception {
 193         String[] cmds;
 194         String classPath = Stream.of(options).takeWhile(s -> s.endsWith(".jar"))
 195                 .collect(Collectors.joining(File.pathSeparator));
 196         if (options.length == 1) {
 197             cmds = new String[] { "-cp", classPath, "PackageFromManifest",
 198                     "runTest", "single" };
 199         } else {
 200             cmds = new String[] { "-cp", classPath, "PackageFromManifest",
 201                     "runTest", options[options.length - 1] };
 202         }
 203 
 204         ProcessTools.executeTestJava(cmds).outputTo(System.out)
 205                 .errorTo(System.err).shouldHaveExitValue(0);
 206     }
 207 
 208     private static void runTest(String[] options)
 209             throws ClassNotFoundException {
 210         String option = options[0];
 211         if (option.equalsIgnoreCase("single")) {
 212             runTest(Class.forName(PACKAGE_NAME + "." + TEST_CLASS_NAME1)
 213                     .getPackage(), TEST_SUFFIX1);
 214         } else {
 215             // Load one specified class first
 216             System.out.println("Load " + Class.forName(option) + " first");
 217 
 218             String suffix = option.endsWith(TEST_SUFFIX1) ?
 219                     TEST_SUFFIX1 :
 220                     TEST_SUFFIX2;
 221 
 222             runTest(Class.forName(PACKAGE_NAME + "." + TEST_CLASS_NAME1)
 223                     .getPackage(), suffix);
 224             runTest(Class.forName(PACKAGE_NAME + "." + TEST_CLASS_NAME2)
 225                     .getPackage(), suffix);
 226         }
 227     }
 228 
 229     private static void runTest(Package testPackage, String suffix) {
 230         checkValue("Package Name", PACKAGE_NAME, testPackage.getName());
 231         checkValue("Spec Title", SPEC_TITLE + suffix,
 232                 testPackage.getSpecificationTitle());
 233         checkValue("Spec Vendor", SPEC_VENDOR + suffix,
 234                 testPackage.getSpecificationVendor());
 235         checkValue("Spec Version", suffix,
 236                 testPackage.getSpecificationVersion());
 237         checkValue("Impl Title", IMPL_TITLE + suffix,
 238                 testPackage.getImplementationTitle());
 239         checkValue("Impl Vendor", IMPL_VENDOR + suffix,
 240                 testPackage.getImplementationVendor());
 241         checkValue("Impl Version", suffix,
 242                 testPackage.getImplementationVersion());
 243     }
 244 
 245     private static void checkValue(String name, String expect, String actual) {
 246         if (!expect.equals(actual)) {
 247             throw new RuntimeException(
 248                     "Failed, unexpected value for " + name + ", expect: "
 249                             + expect + ", actual: " + actual);
 250         } else {
 251             System.out.println(name + " : " + actual);
 252         }
 253     }
 254 
 255     private static void setup() throws IOException {
 256         if (!Files.exists(WORKING_PATH.resolve(TEST_JAR_FILE1))) {
 257             createTestClass(TEST_CLASS_NAME1);
 258             createManifest(TEST_SUFFIX1);
 259             buildJar(TEST_JAR_FILE1, true);
 260         }
 261 
 262         if (!Files.exists(WORKING_PATH.resolve(TEST_JAR_FILE2))) {
 263             createTestClass(TEST_CLASS_NAME2);
 264             createManifest(TEST_SUFFIX2);
 265             buildJar(TEST_JAR_FILE2, false);
 266         }
 267     }
 268 
 269     private static void testUrlLoader(String[] options)
 270             throws ClassNotFoundException {
 271         URLClassLoader cl = new URLClassLoader(
 272                 Stream.of(options).takeWhile(s -> s.endsWith(".jar")).map(s -> {
 273                     try {
 274                         return WORKING_PATH.resolve(s).toUri().toURL();
 275                     } catch (MalformedURLException e) {
 276                         return null;
 277                     }
 278                 }).toArray(URL[]::new));
 279         if (options.length == 1) {
 280             runTest(Class
 281                     .forName(PACKAGE_NAME + "." + TEST_CLASS_NAME1, true, cl)
 282                     .getPackage(), TEST_SUFFIX1);
 283         } else {
 284             // Load one specified class first
 285             System.out.println("Load " + Class
 286                     .forName(options[options.length - 1], true, cl) + " first");
 287 
 288             String suffix = options[options.length - 1].endsWith(TEST_SUFFIX1) ?
 289                     TEST_SUFFIX1 :
 290                     TEST_SUFFIX2;
 291 
 292             runTest(Class
 293                     .forName(PACKAGE_NAME + "." + TEST_CLASS_NAME1, true, cl)
 294                     .getPackage(), suffix);
 295             runTest(Class
 296                     .forName(PACKAGE_NAME + "." + TEST_CLASS_NAME2, true, cl)
 297                     .getPackage(), suffix);
 298         }
 299     }
 300 }