Path: blob/master/test/jdk/java/lang/Package/PackageFromManifest.java
41149 views
/*1* Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 820152826* @summary The test will create JAR file(s) with the manifest file27* that customized package versioning information (different info for28* same package if multiple jars). Then verify package versioning info29* @library /test/lib30* @modules jdk.compiler31* @run main PackageFromManifest setup test32* @run main PackageFromManifest runJar test1.jar33* @run main PackageFromManifest runJar test1.jar test2.jar foo.Foo134* @run main PackageFromManifest runJar test1.jar test2.jar foo.Foo235* @run main/othervm PackageFromManifest runUrlLoader test1.jar36* @run main/othervm PackageFromManifest runUrlLoader test1.jar test2.jar foo.Foo137* @run main/othervm PackageFromManifest runUrlLoader test1.jar test2.jar foo.Foo238*/3940import jdk.test.lib.compiler.CompilerUtils;41import jdk.test.lib.process.ProcessTools;42import jdk.test.lib.util.FileUtils;43import jdk.test.lib.util.JarUtils;4445import java.io.File;46import java.io.IOException;47import java.io.InputStream;48import java.net.MalformedURLException;49import java.net.URL;50import java.net.URLClassLoader;51import java.nio.file.Files;52import java.nio.file.Path;53import java.nio.file.Paths;54import java.util.ArrayList;55import java.util.Arrays;56import java.util.List;57import java.util.jar.Manifest;58import java.util.stream.Collectors;59import java.util.stream.Stream;6061/**62* This test accept at least two input parameters, first one is run type like63* 'setup', 'runJar', 'runTest', 'runUrlLoader', the rest parameters are options64* to each run type. 'setup' run type should be placed at first since it will65* create necessary jars for the rest tests. 'runTest' will be called in test66* logic only, it should not be used in @run67*68* #1 test will do setup only to generate required jars before other tests run69* PackageFromManifest setup test70*71* #2 test will run against single jar file to verify package versioning72* PackageFromManifest runJar test1.jar73*74* #4 test will run against two jar files, load class foo.Foo1 first, then75* verify package versioning76* PackageFromManifest runJar test1.jar test2.jar foo.Foo177*78* #5 test will run against two jar files, load class foo.Foo2 first, then79* verify package versioning80* PackageFromManifest runJar test1.jar test2.jar foo.Foo281*82* #3 test will use URLCLassLoader to load single jar file, then verify83* package versioning84* PackageFromManifest runUrlLoader test1.jar85*86* #6 test will use URLCLassLoader to load two jars, load class foo.Foo1 first,87* then verify package versioning88* PackageFromManifest runUrlLoader test1.jar test2.jar foo.Foo189*90* #7 test will use URLCLassLoader to load two jars, load class foo.Foo2 first,91* then verify package versioning92* PackageFromManifest runUrlLoader test1.jar test2.jar foo.Foo293*/94public class PackageFromManifest {9596private static final String PACKAGE_NAME = "foo";97private static final String TEST_JAR_FILE1 = "test1.jar";98private static final String TEST_JAR_FILE2 = "test2.jar";99private static final String TEST_SUFFIX1 = "1";100private static final String TEST_SUFFIX2 = "2";101private static final String TEST_CLASS_PREFIX = "Foo";102private static final String TEST_CLASS_NAME1 =103TEST_CLASS_PREFIX + TEST_SUFFIX1;104private static final String TEST_CLASS_NAME2 =105TEST_CLASS_PREFIX + TEST_SUFFIX2;106private static final String MANIFEST_FILE = "test.mf";107private static final String SPEC_TITLE = "testSpecTitle";108private static final String SPEC_VENDOR = "testSpecVendor";109private static final String IMPL_TITLE = "testImplTitle";110private static final String IMPL_VENDOR = "testImplVendor";111private static final Path WORKING_PATH = Paths.get(".");112113public static void main(String[] args) throws Exception {114if (args != null && args.length > 1) {115String runType = args[0];116String[] options = Arrays.copyOfRange(args, 1, args.length);117switch (runType) {118case "setup":119setup();120break;121case "runTest":122runTest(options);123break;124case "runJar":125runJar(options);126break;127case "runUrlLoader":128testUrlLoader(options);129break;130default:131throw new RuntimeException("Invalid run type : " + runType);132}133} else {134throw new RuntimeException("Invalid input arguments");135}136}137138private static void createTestClass(String name) throws IOException {139List<String> content = new ArrayList<>();140content.add("package " + PACKAGE_NAME + ";");141content.add("public class " + name + " {");142content.add("}");143144Path javaFile = WORKING_PATH.resolve(name + ".java");145146Files.write(javaFile, content);147148CompilerUtils.compile(WORKING_PATH, WORKING_PATH);149150// clean up created java file151Files.delete(javaFile);152}153154private static void createManifest(String suffix) throws IOException {155List<String> content = new ArrayList<>();156content.add("Manifest-version: 1.1");157content.add("Name: " + PACKAGE_NAME + "/");158content.add("Specification-Title: " + SPEC_TITLE + suffix);159content.add("Specification-Version: " + suffix);160content.add("Specification-Vendor: " + SPEC_VENDOR + suffix);161content.add("Implementation-Title: " + IMPL_TITLE + suffix);162content.add("Implementation-Version: " + suffix);163content.add("Implementation-Vendor: " + IMPL_VENDOR + suffix);164165Files.write(WORKING_PATH.resolve(MANIFEST_FILE), content);166}167168private static void buildJar(String jarFileName, boolean isIncludeSelf)169throws IOException {170try (InputStream is = Files.newInputStream(Paths.get(MANIFEST_FILE))) {171if (isIncludeSelf) {172Path selfPath = WORKING_PATH173.resolve("PackageFromManifest.class");174if (!Files.exists(selfPath)) {175Files.copy(Paths.get(System.getProperty("test.classes"))176.resolve("PackageFromManifest.class"), selfPath);177}178JarUtils.createJarFile(Paths.get(jarFileName), new Manifest(is),179WORKING_PATH, selfPath,180WORKING_PATH.resolve(PACKAGE_NAME));181} else {182JarUtils.createJarFile(Paths.get(jarFileName), new Manifest(is),183WORKING_PATH, WORKING_PATH.resolve(PACKAGE_NAME));184}185}186187// clean up build files188FileUtils.deleteFileTreeWithRetry(WORKING_PATH.resolve(PACKAGE_NAME));189Files.delete(WORKING_PATH.resolve(MANIFEST_FILE));190}191192private static void runJar(String[] options) throws Exception {193String[] cmds;194String classPath = Stream.of(options).takeWhile(s -> s.endsWith(".jar"))195.collect(Collectors.joining(File.pathSeparator));196if (options.length == 1) {197cmds = new String[] { "-cp", classPath, "PackageFromManifest",198"runTest", "single" };199} else {200cmds = new String[] { "-cp", classPath, "PackageFromManifest",201"runTest", options[options.length - 1] };202}203204ProcessTools.executeTestJava(cmds).outputTo(System.out)205.errorTo(System.err).shouldHaveExitValue(0);206}207208private static void runTest(String[] options)209throws ClassNotFoundException {210String option = options[0];211if (option.equalsIgnoreCase("single")) {212runTest(Class.forName(PACKAGE_NAME + "." + TEST_CLASS_NAME1)213.getPackage(), TEST_SUFFIX1);214} else {215// Load one specified class first216System.out.println("Load " + Class.forName(option) + " first");217218String suffix = option.endsWith(TEST_SUFFIX1) ?219TEST_SUFFIX1 :220TEST_SUFFIX2;221222runTest(Class.forName(PACKAGE_NAME + "." + TEST_CLASS_NAME1)223.getPackage(), suffix);224runTest(Class.forName(PACKAGE_NAME + "." + TEST_CLASS_NAME2)225.getPackage(), suffix);226}227}228229private static void runTest(Package testPackage, String suffix) {230checkValue("Package Name", PACKAGE_NAME, testPackage.getName());231checkValue("Spec Title", SPEC_TITLE + suffix,232testPackage.getSpecificationTitle());233checkValue("Spec Vendor", SPEC_VENDOR + suffix,234testPackage.getSpecificationVendor());235checkValue("Spec Version", suffix,236testPackage.getSpecificationVersion());237checkValue("Impl Title", IMPL_TITLE + suffix,238testPackage.getImplementationTitle());239checkValue("Impl Vendor", IMPL_VENDOR + suffix,240testPackage.getImplementationVendor());241checkValue("Impl Version", suffix,242testPackage.getImplementationVersion());243}244245private static void checkValue(String name, String expect, String actual) {246if (!expect.equals(actual)) {247throw new RuntimeException(248"Failed, unexpected value for " + name + ", expect: "249+ expect + ", actual: " + actual);250} else {251System.out.println(name + " : " + actual);252}253}254255private static void setup() throws IOException {256if (!Files.exists(WORKING_PATH.resolve(TEST_JAR_FILE1))) {257createTestClass(TEST_CLASS_NAME1);258createManifest(TEST_SUFFIX1);259buildJar(TEST_JAR_FILE1, true);260}261262if (!Files.exists(WORKING_PATH.resolve(TEST_JAR_FILE2))) {263createTestClass(TEST_CLASS_NAME2);264createManifest(TEST_SUFFIX2);265buildJar(TEST_JAR_FILE2, false);266}267}268269private static void testUrlLoader(String[] options)270throws ClassNotFoundException {271URLClassLoader cl = new URLClassLoader(272Stream.of(options).takeWhile(s -> s.endsWith(".jar")).map(s -> {273try {274return WORKING_PATH.resolve(s).toUri().toURL();275} catch (MalformedURLException e) {276return null;277}278}).toArray(URL[]::new));279if (options.length == 1) {280runTest(Class281.forName(PACKAGE_NAME + "." + TEST_CLASS_NAME1, true, cl)282.getPackage(), TEST_SUFFIX1);283} else {284// Load one specified class first285System.out.println("Load " + Class286.forName(options[options.length - 1], true, cl) + " first");287288String suffix = options[options.length - 1].endsWith(TEST_SUFFIX1) ?289TEST_SUFFIX1 :290TEST_SUFFIX2;291292runTest(Class293.forName(PACKAGE_NAME + "." + TEST_CLASS_NAME1, true, cl)294.getPackage(), suffix);295runTest(Class296.forName(PACKAGE_NAME + "." + TEST_CLASS_NAME2, true, cl)297.getPackage(), suffix);298}299}300}301302303