1 /* 2 * Copyright (c) 2012, 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 6901992 27 * @summary InvalidJarIndexException due to bug in sun.misc.JarIndex.merge() 28 * @modules java.base/jdk.internal.util.jar 29 * @compile -XDignore.symbol.file JarIndexMergeTest.java 30 * @run main JarIndexMergeTest 31 * @author Diego Belfer 32 */ 33 34 import java.io.File; 35 import java.io.FileNotFoundException; 36 import java.io.FileOutputStream; 37 import java.io.IOException; 38 import java.util.LinkedList; 39 import java.util.jar.JarEntry; 40 import java.util.jar.JarOutputStream; 41 // implementation specific API 42 import jdk.internal.util.jar.JarIndex; 43 44 public class JarIndexMergeTest { 45 static final String slash = File.separator; 46 static final String testClassesDir = System.getProperty("test.classes", "."); 47 static final File tmpFolder = new File(testClassesDir); 48 main(String[] args)49 public static void main(String[] args) throws Exception { 50 File jar1 = buildJar1(); 51 File jar2 = buildJar2(); 52 53 JarIndex jarIndex1 = new JarIndex(new String[] { jar1.getAbsolutePath() }); 54 JarIndex jarIndex2 = new JarIndex(new String[] { jar2.getAbsolutePath() }); 55 56 jarIndex1.merge(jarIndex2, null); 57 58 assertFileResolved(jarIndex2, "com/test1/resource1.file", 59 jar1.getAbsolutePath()); 60 assertFileResolved(jarIndex2, "com/test2/resource2.file", 61 jar2.getAbsolutePath()); 62 } 63 assertFileResolved(JarIndex jarIndex2, String file, String jarName)64 static void assertFileResolved(JarIndex jarIndex2, String file, 65 String jarName) { 66 @SuppressWarnings("unchecked") 67 LinkedList<String> jarLists = (LinkedList<String>)jarIndex2.get(file); 68 if (jarLists == null || jarLists.size() == 0 || 69 !jarName.equals(jarLists.get(0))) { 70 throw new RuntimeException( 71 "Unexpected result: the merged index must resolve file: " 72 + file); 73 } 74 } 75 buildJar1()76 private static File buildJar1() throws FileNotFoundException, IOException { 77 JarBuilder jar1Builder = new JarBuilder(tmpFolder, "jar1-merge.jar"); 78 jar1Builder.addResourceFile("com/test1/resource1.file", "resource1"); 79 return jar1Builder.build(); 80 } 81 buildJar2()82 private static File buildJar2() throws FileNotFoundException, IOException { 83 JarBuilder jar2Builder = new JarBuilder(tmpFolder, "jar2-merge.jar"); 84 jar2Builder.addResourceFile("com/test2/resource2.file", "resource2"); 85 return jar2Builder.build(); 86 } 87 88 /* 89 * Helper class for building jar files 90 */ 91 public static class JarBuilder { 92 private JarOutputStream os; 93 private File jarFile; 94 JarBuilder(File tmpFolder, String jarName)95 public JarBuilder(File tmpFolder, String jarName) 96 throws FileNotFoundException, IOException 97 { 98 this.jarFile = new File(tmpFolder, jarName); 99 this.os = new JarOutputStream(new FileOutputStream(jarFile)); 100 } 101 addResourceFile(String pathFromRoot, String content)102 public void addResourceFile(String pathFromRoot, String content) 103 throws IOException 104 { 105 JarEntry entry = new JarEntry(pathFromRoot); 106 os.putNextEntry(entry); 107 os.write(content.getBytes("ASCII")); 108 os.closeEntry(); 109 } 110 build()111 public File build() throws IOException { 112 os.close(); 113 return jarFile; 114 } 115 } 116 } 117 118