Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/misc/JarIndex/JarIndexMergeForClassLoaderTest.java
38838 views
/*1* Copyright (c) 2012, 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 690199226* @summary InvalidJarIndexException due to bug in sun.misc.JarIndex.merge()27* Test URLClassLoader usage of the merge method when using indexes28* @author Diego Belfer29*/30import java.io.BufferedReader;31import java.io.File;32import java.io.FileNotFoundException;33import java.io.FileOutputStream;34import java.io.IOException;35import java.io.InputStream;36import java.io.InputStreamReader;37import java.net.URL;38import java.net.URLClassLoader;39import java.util.jar.JarEntry;40import java.util.jar.JarOutputStream;4142public class JarIndexMergeForClassLoaderTest {43static final String slash = File.separator;44static final String testClassesDir = System.getProperty("test.classes", ".");45static final String jar;46static final boolean debug = true;47static final File tmpFolder = new File(testClassesDir);4849static {50String javaHome = System.getProperty("java.home");51if (javaHome.endsWith("jre")) {52int index = javaHome.lastIndexOf(slash);53if (index != -1)54javaHome = javaHome.substring(0, index);55}5657jar = javaHome + slash + "bin" + slash + "jar";58}5960public static void main(String[] args) throws Exception {61// Create the jars file62File jar1 = buildJar1();63File jar2 = buildJar2();64File jar3 = buildJar3();6566// Index jar files in two levels: jar1 -> jar2 -> jar367createIndex(jar2.getName(), jar3.getName());68createIndex(jar1.getName(), jar2.getName());6970// Get root jar of the URLClassLoader71URL url = jar1.toURI().toURL();7273URLClassLoader classLoader = new URLClassLoader(new URL[] { url });7475assertResource(classLoader, "com/jar1/resource.file", "jar1");76assertResource(classLoader, "com/test/resource1.file", "resource1");77assertResource(classLoader, "com/jar2/resource.file", "jar2");78assertResource(classLoader, "com/test/resource2.file", "resource2");79assertResource(classLoader, "com/test/resource3.file", "resource3");8081/*82* The following two asserts failed before the fix of the bug 690199283*/84// Check that an existing file is found using the merged index85assertResource(classLoader, "com/missing/jar3/resource.file", "jar3");86// Check that a non existent file in directory which does not contain87// any file is not found and it does not throw InvalidJarIndexException88assertResource(classLoader, "com/missing/nofile", null);89}9091private static File buildJar3() throws FileNotFoundException, IOException {92JarBuilder jar3Builder = new JarBuilder(tmpFolder, "jar3.jar");93jar3Builder.addResourceFile("com/test/resource3.file", "resource3");94jar3Builder.addResourceFile("com/missing/jar3/resource.file", "jar3");95return jar3Builder.build();96}9798private static File buildJar2() throws FileNotFoundException, IOException {99JarBuilder jar2Builder = new JarBuilder(tmpFolder, "jar2.jar");100jar2Builder.addResourceFile("com/jar2/resource.file", "jar2");101jar2Builder.addResourceFile("com/test/resource2.file", "resource2");102return jar2Builder.build();103}104105private static File buildJar1() throws FileNotFoundException, IOException {106JarBuilder jar1Builder = new JarBuilder(tmpFolder, "jar1.jar");107jar1Builder.addResourceFile("com/jar1/resource.file", "jar1");108jar1Builder.addResourceFile("com/test/resource1.file", "resource1");109return jar1Builder.build();110}111112/* create the index */113static void createIndex(String parentJar, String childJar) {114// ProcessBuilder is used so that the current directory can be set115// to the directory that directly contains the jars.116debug("Running jar to create the index for: " + parentJar + " and "117+ childJar);118ProcessBuilder pb = new ProcessBuilder(jar, "-i", parentJar, childJar);119120pb.directory(tmpFolder);121// pd.inheritIO();122try {123Process p = pb.start();124if (p.waitFor() != 0)125throw new RuntimeException("jar indexing failed");126127if (debug && p != null) {128debugStream(p.getInputStream());129debugStream(p.getErrorStream());130}131} catch (InterruptedException | IOException x) {132throw new RuntimeException(x);133}134}135136private static void debugStream(InputStream is) throws IOException {137try (BufferedReader reader = new BufferedReader(new InputStreamReader(is))) {138String line;139while ((line = reader.readLine()) != null) {140debug(line);141}142}143}144145private static void assertResource(URLClassLoader classLoader, String file,146String expectedContent) throws IOException {147InputStream fileStream = classLoader.getResourceAsStream(file);148149if (fileStream == null && expectedContent == null) {150return;151}152if (fileStream == null && expectedContent != null) {153throw new RuntimeException(154buildMessage(file, expectedContent, null));155}156try {157String actualContent = readAsString(fileStream);158159if (fileStream != null && expectedContent == null) {160throw new RuntimeException(buildMessage(file, null,161actualContent));162}163if (!expectedContent.equals(actualContent)) {164throw new RuntimeException(buildMessage(file, expectedContent,165actualContent));166}167} finally {168fileStream.close();169}170}171172private static String buildMessage(String file, String expectedContent,173String actualContent) {174return "Expected: " + expectedContent + " for: " + file + " was: "175+ actualContent;176}177178private static String readAsString(InputStream fileStream)179throws IOException {180byte[] buffer = new byte[1024];181int count, len = 0;182while ((count = fileStream.read(buffer, len, buffer.length-len)) != -1)183len += count;184return new String(buffer, 0, len, "ASCII");185}186187static void debug(Object message) {188if (debug)189System.out.println(message);190}191192/*193* Helper class for building jar files194*/195public static class JarBuilder {196private JarOutputStream os;197private File jarFile;198199public JarBuilder(File tmpFolder, String jarName)200throws FileNotFoundException, IOException201{202this.jarFile = new File(tmpFolder, jarName);203this.os = new JarOutputStream(new FileOutputStream(jarFile));204}205206public void addResourceFile(String pathFromRoot, String content)207throws IOException208{209JarEntry entry = new JarEntry(pathFromRoot);210os.putNextEntry(entry);211os.write(content.getBytes("ASCII"));212os.closeEntry();213}214215public File build() throws IOException {216os.close();217return jarFile;218}219}220}221222223224