Path: blob/master/test/jdk/sun/misc/JarIndex/JarIndexMergeForClassLoaderTest.java
41152 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 {50jar = System.getProperty("java.home") + slash + "bin" + slash + "jar";51}5253public static void main(String[] args) throws Exception {54// Create the jars file55File jar1 = buildJar1();56File jar2 = buildJar2();57File jar3 = buildJar3();5859// Index jar files in two levels: jar1 -> jar2 -> jar360createIndex(jar2.getName(), jar3.getName());61createIndex(jar1.getName(), jar2.getName());6263// Get root jar of the URLClassLoader64URL url = jar1.toURI().toURL();6566URLClassLoader classLoader = new URLClassLoader(new URL[] { url });6768assertResource(classLoader, "com/jar1/resource.file", "jar1");69assertResource(classLoader, "com/test/resource1.file", "resource1");70assertResource(classLoader, "com/jar2/resource.file", "jar2");71assertResource(classLoader, "com/test/resource2.file", "resource2");72assertResource(classLoader, "com/test/resource3.file", "resource3");7374/*75* The following two asserts failed before the fix of the bug 690199276*/77// Check that an existing file is found using the merged index78assertResource(classLoader, "com/missing/jar3/resource.file", "jar3");79// Check that a non existent file in directory which does not contain80// any file is not found and it does not throw InvalidJarIndexException81assertResource(classLoader, "com/missing/nofile", null);82}8384private static File buildJar3() throws FileNotFoundException, IOException {85JarBuilder jar3Builder = new JarBuilder(tmpFolder, "jar3.jar");86jar3Builder.addResourceFile("com/test/resource3.file", "resource3");87jar3Builder.addResourceFile("com/missing/jar3/resource.file", "jar3");88return jar3Builder.build();89}9091private static File buildJar2() throws FileNotFoundException, IOException {92JarBuilder jar2Builder = new JarBuilder(tmpFolder, "jar2.jar");93jar2Builder.addResourceFile("com/jar2/resource.file", "jar2");94jar2Builder.addResourceFile("com/test/resource2.file", "resource2");95return jar2Builder.build();96}9798private static File buildJar1() throws FileNotFoundException, IOException {99JarBuilder jar1Builder = new JarBuilder(tmpFolder, "jar1.jar");100jar1Builder.addResourceFile("com/jar1/resource.file", "jar1");101jar1Builder.addResourceFile("com/test/resource1.file", "resource1");102return jar1Builder.build();103}104105/* create the index */106static void createIndex(String parentJar, String childJar) {107// ProcessBuilder is used so that the current directory can be set108// to the directory that directly contains the jars.109debug("Running jar to create the index for: " + parentJar + " and "110+ childJar);111ProcessBuilder pb = new ProcessBuilder(jar, "-i", parentJar, childJar);112113pb.directory(tmpFolder);114// pd.inheritIO();115try {116Process p = pb.start();117if (p.waitFor() != 0)118throw new RuntimeException("jar indexing failed");119120if (debug && p != null) {121debugStream(p.getInputStream());122debugStream(p.getErrorStream());123}124} catch (InterruptedException | IOException x) {125throw new RuntimeException(x);126}127}128129private static void debugStream(InputStream is) throws IOException {130try (BufferedReader reader = new BufferedReader(new InputStreamReader(is))) {131String line;132while ((line = reader.readLine()) != null) {133debug(line);134}135}136}137138private static void assertResource(URLClassLoader classLoader, String file,139String expectedContent) throws IOException {140InputStream fileStream = classLoader.getResourceAsStream(file);141142if (fileStream == null && expectedContent == null) {143return;144}145if (fileStream == null && expectedContent != null) {146throw new RuntimeException(147buildMessage(file, expectedContent, null));148}149try {150String actualContent = readAsString(fileStream);151152if (fileStream != null && expectedContent == null) {153throw new RuntimeException(buildMessage(file, null,154actualContent));155}156if (!expectedContent.equals(actualContent)) {157throw new RuntimeException(buildMessage(file, expectedContent,158actualContent));159}160} finally {161fileStream.close();162}163}164165private static String buildMessage(String file, String expectedContent,166String actualContent) {167return "Expected: " + expectedContent + " for: " + file + " was: "168+ actualContent;169}170171private static String readAsString(InputStream fileStream)172throws IOException {173byte[] buffer = new byte[1024];174int count, len = 0;175while ((count = fileStream.read(buffer, len, buffer.length-len)) != -1)176len += count;177return new String(buffer, 0, len, "ASCII");178}179180static void debug(Object message) {181if (debug)182System.out.println(message);183}184185/*186* Helper class for building jar files187*/188public static class JarBuilder {189private JarOutputStream os;190private File jarFile;191192public JarBuilder(File tmpFolder, String jarName)193throws FileNotFoundException, IOException194{195this.jarFile = new File(tmpFolder, jarName);196this.os = new JarOutputStream(new FileOutputStream(jarFile));197}198199public void addResourceFile(String pathFromRoot, String content)200throws IOException201{202JarEntry entry = new JarEntry(pathFromRoot);203os.putNextEntry(entry);204os.write(content.getBytes("ASCII"));205os.closeEntry();206}207208public File build() throws IOException {209os.close();210return jarFile;211}212}213}214215216217