Path: blob/master/test/jdk/java/lang/ClassLoader/GetSystemPackage.java
41149 views
/*1* Copyright (c) 2014, 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 806013026* @library /test/lib27* @build package2.Class2 GetSystemPackage28* @summary Test if getSystemPackage() return consistent values for cases29* where a manifest is provided or not and ensure only jars on30* bootclasspath gets resolved via Package.getSystemPackage31* @run main GetSystemPackage32*/3334import java.io.File;35import java.io.FileInputStream;36import java.io.FileNotFoundException;37import java.io.FileOutputStream;38import java.io.IOException;39import java.util.jar.Attributes;40import java.util.jar.JarEntry;41import java.util.jar.JarOutputStream;42import java.util.jar.Manifest;43import jdk.test.lib.process.ProcessTools;4445public class GetSystemPackage {4647static final String testClassesDir = System.getProperty("test.classes", ".");48static final File tmpFolder = new File(testClassesDir);49static final String manifestTitle = "Special JAR";5051public static void main(String ... args) throws Exception {52if (args.length == 0) {53buildJarsAndInitiateSystemPackageTest();54return;55}56switch (args[0]) {57case "system-manifest":58verifyPackage(true, true);59break;60case "system-no-manifest":61verifyPackage(false, true);62break;63case "non-system-manifest":64verifyPackage(true, false);65break;66case "non-system-no-manifest":67default:68verifyPackage(false, false);69break;70}71}7273private static void buildJarsAndInitiateSystemPackageTest()74throws Exception75{76Manifest m = new Manifest();77// not setting MANIFEST_VERSION prevents META-INF/MANIFEST.MF from78// getting written79m.getMainAttributes().put(Attributes.Name.MANIFEST_VERSION, "1.0");80m.getMainAttributes().put(Attributes.Name.SPECIFICATION_TITLE,81manifestTitle);8283buildJar("manifest.jar", m);84buildJar("no-manifest.jar", null);8586runSubProcess("System package with manifest improperly resolved.",87"-Xbootclasspath/a:" + testClassesDir + "/manifest.jar",88"GetSystemPackage", "system-manifest");8990runSubProcess("System package from directory improperly resolved.",91"-Xbootclasspath/a:" + testClassesDir, "GetSystemPackage",92"system-no-manifest");9394runSubProcess("System package with no manifest improperly resolved",95"-Xbootclasspath/a:" + testClassesDir + "/no-manifest.jar",96"GetSystemPackage", "system-no-manifest");9798runSubProcess("Classpath package with manifest improperly resolved",99"-cp", testClassesDir + "/manifest.jar", "GetSystemPackage",100"non-system-manifest");101102runSubProcess("Classpath package with no manifest improperly resolved",103"-cp", testClassesDir + "/no-manifest.jar", "GetSystemPackage",104"non-system-no-manifest");105106}107108private static void buildJar(String name, Manifest man) throws Exception {109JarBuilder jar = new JarBuilder(tmpFolder, name, man);110jar.addClassFile("package2/Class2.class",111testClassesDir + "/package2/Class2.class");112jar.addClassFile("GetSystemPackage.class",113testClassesDir + "/GetSystemPackage.class");114jar.build();115}116117private static void runSubProcess(String messageOnError, String ... args)118throws Exception119{120ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(args);121int res = pb.directory(tmpFolder).inheritIO().start().waitFor();122if (res != 0) {123throw new RuntimeException(messageOnError);124}125}126127private static void verifyPackage(boolean hasManifest,128boolean isSystemPackage)129throws Exception130{131Class<?> c = Class.forName("package2.Class2");132Package pkg = c.getPackage();133if (pkg == null || pkg != Package.getPackage("package2") ||134!"package2".equals(pkg.getName())) {135fail("package2 not found via Package.getPackage()");136}137138String specificationTitle = pkg.getSpecificationTitle();139if (!"package2".equals(pkg.getName())) {140fail("Invalid package for Class2");141}142143if (hasManifest && (specificationTitle == null144|| !manifestTitle.equals(specificationTitle))) {145fail("Invalid manifest for package " + pkg.getName());146}147if (!hasManifest && specificationTitle != null) {148fail("Invalid manifest for package " + pkg.getName() + ": was " +149specificationTitle + " expected: null");150}151152ClassLoader ld = c.getClassLoader();153Package systemPkg = ld != null ? null : Package.getPackage("package2");154155if (findPackage("java.lang") == null) {156fail("java.lang not found via Package.getPackages()");157}158Package foundPackage = findPackage("package2");159if (isSystemPackage) {160if (systemPkg == null) {161fail("System package could not be found via getSystemPackage");162}163if (foundPackage != systemPkg || systemPkg != pkg) {164fail("Inconsistent package found via Package.getPackages()");165}166} else {167if (systemPkg != null) {168fail("Non-system package could be found via getSystemPackage");169}170if (foundPackage == null) {171fail("Non-system package not found via Package.getPackages()");172}173}174}175176private static Package findPackage(String name) {177Package[] packages = Package.getPackages();178for (Package p : packages) {179if (p.getName().equals(name)) {180return p;181}182}183return null;184}185186private static void fail(String message) {187throw new RuntimeException(message);188}189}190191/*192* Helper class for building jar files193*/194class JarBuilder {195196private JarOutputStream os;197198public JarBuilder(File tmpFolder, String jarName, Manifest manifest)199throws FileNotFoundException, IOException200{201File jarFile = new File(tmpFolder, jarName);202if (manifest != null) {203this.os = new JarOutputStream(new FileOutputStream(jarFile),204manifest);205} else {206this.os = new JarOutputStream(new FileOutputStream(jarFile));207}208}209210public void addClassFile(String pathFromRoot, String file)211throws IOException212{213byte[] buf = new byte[1024];214try (FileInputStream in = new FileInputStream(file)) {215JarEntry entry = new JarEntry(pathFromRoot);216os.putNextEntry(entry);217int len;218while ((len = in.read(buf)) > 0) {219os.write(buf, 0, len);220}221os.closeEntry();222}223}224225public void build() throws IOException {226os.close();227}228}229230231